online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
/****************************************************************************** Online C Compiler. Code, Compile, Run and Debug C program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> typedef struct { char board[3][3]; char current_player; } TicTacToeBoard; // Function Prototype to display the Tic Tac Toe board void display_board (TicTacToeBoard * board); // Function Prototype to initialize the Tic Tac Toe board with empty cells void initialize_board (TicTacToeBoard * board); // Function Prototype to make a move on the Tic Tac Toe board int make_move (TicTacToeBoard * board, int row, int col); // Function Prototype to check if a player has won int check_win (TicTacToeBoard * board, char player); // Function Prototype to check if the game is a draw int check_draw (TicTacToeBoard * board); // Function Prototype to save the game state to a file void save_board_to_file (TicTacToeBoard * board, char *file_path); // Function Prototype to load the game state from a file void load_board_from_file (TicTacToeBoard * board, char *file_path); int main () { //Initialize char *file_path = "tic_tac_toe_board.txt"; TicTacToeBoard board; char player1[50], player2[50]; //Player Name input printf ("Enter Player 1 name: "); scanf ("%s", player1); printf ("Enter Player 2 name: "); scanf ("%s", player2); //Load Previous Game char choice; printf ("Do you want to continue the previous game? (y/n): "); scanf (" %c", &choice); if (choice == 'y' || choice == 'Y') { load_board_from_file (&board, file_path); printf ("Loaded the previous game board:\n"); display_board (&board); } int row, col; while (1) { // Your code here // 1. Initialize Board void initialize_board (TicTacToeBoard * board); // 2. Ask for move (row, col) int make_move (TicTacToeBoard * board, int row, int col); // 3. Check if valid move using make_move int make_move (TicTacToeBoard * board, int row, int col); // Provided: use similar method for the rest. int move_result = make_move (&board, row, col); if (move_result == 0) { printf ("Cell is already occupied or invalid input. Try again.\n"); continue; } // 4. Display board void display_board (TicTacToeBoard * board); // 5. Save board after every move. save_board_to_file (&board, file_path); // 6. Check for a win. int player_won = check_win (&board, 'X'); int opponent_won = check_win (&board, 'O'); int is_draw = check_draw (&board); // Construct if statements based on the returned values from // check_win and check_draw } } // Function to display the Tic Tac Toe board void display_board (TicTacToeBoard * board) { printf("\n"); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { printf(" %c ", board->board[i][j]); if (j != 2) { printf("|"); } } printf("\n"); if (i != 2) { printf("-----------\n"); } } printf("\n"); } // Function to make a move on the Tic Tac Toe board int make_move (TicTacToeBoard * board, int row, int col) { if (row < 0 || row > 2 || col < 0 || col > 2) { printf("Invalid input. Try again.\n"); return 0; } if (board -> board[row][col] == 'X' || board -> board[row][col] == 'O') { printf("That cell is already taken. Try again.\n"); return 0; } board->board[row][col] = 'X'; // assuming player X is making the move return 1; } // Function to initialize the Tic Tac Toe board with empty cells void initialize_board (TicTacToeBoard * board) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board->board[i][j] = ' '; } } } // Function to check if a player has won int check_win (TicTacToeBoard * board, char player) { for (int i = 0; i < 3; i++) { if (board->board[i][0] == player && board->board[i][1] == player && board->board[i][2] == player) { return 1; } } for (int i = 0; i < 3; i++) { if (board->board[0][i] == player && board->board[1][i] == player && board->board[2][i] == player) { return 1; } } if (board->board[0][0] == player && board->board[1][1] == player && board->board[2][2] == player) { return 1; } if (board->board[0][2] == player && board->board[1][1] == player && board->board[2][0] == player) { return 1; } return 0; } // Function to check if the game is a draw int check_draw (TicTacToeBoard * board) { int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { if (board->board[i][j] == ' ') { return 0; } } } return 1; } // Function to save the game state to a file void save_board_to_file (TicTacToeBoard * board, char *file_path) { FILE *fp; fp = fopen(file_path, "wb"); if (fp == NULL) { printf("Error opening file.\n"); return; } fprintf(board, sizeof(TicTacToeBoard), 1, fp); fclose(fp); } // Function to load the game state from a file void load_board_from_file (TicTacToeBoard * board, char *file_path) { FILE *fp; fp = fopen(file_path, "rb"); if (fp == NULL) { printf("Error opening file.\n"); return; } fscanf(board, sizeof(TicTacToeBoard), 1, fp); fclose(fp); }
X

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue