Question: I have a .C program that can play TicTacToe (human vs computer) the program is working , what I need help is with adding a

I have a .C program that can play TicTacToe (human vs computer) the program is working , what I need help is with adding a do/while loop to the program or some kind of function that ask the player if it wanna play again? yes or no, if yes repeat the game if no terminate the program.

#include #define _CRT_SECURE_NO_WARNINGS 12 13 14 15 16 #if defined linux #define IS_LINU\" v:shapes=\"Picture_x0020_112\">

Full Code, please modify to adjust or leave steps to fallow: #include #include #define _CRT_SECURE_NO_WARNINGS

#if defined __linux__ #define IS_LINUX 1 #elif defined _WIN32 #define IS_LINUX 0 #endif

#define PLAYER -1 #define BLANK 0 #define AI 1

int optimalX, optimalY; char playAgain; int arr[3][3] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} };

void pboard(int board[3][3]) { if (IS_LINUX)

system(\"clear\"); // for Unix else system(\"cls\"); // for Windows for (int i = 0; i { for (int j = 0; j { printf(\" \"); if (board[i][j] == AI && IS_LINUX) { printf(\"\"); } else if (board[i][j] == PLAYER && IS_LINUX) { printf(\"\"); } printf(\"%c \", board[i][j] == BLANK ? (3 * i) + j + 1 + '0' : (board[i][j] == PLAYER ? 'o' : 'x')); if (IS_LINUX) printf(\"\"); if (j != 2) printf(\"|\"); } if (i != 2) printf(\" ---+---+--- \"); } printf(\" \"); }

int game_board(int board[3][3]) { if ( (board[0][0] == PLAYER && board[0][1] == PLAYER && board[0][2] == PLAYER) || (board[1][0] == PLAYER && board[1][1] == PLAYER && board[1][2] == PLAYER) || (board[2][0] == PLAYER && board[2][1] == PLAYER && board[2][2] == PLAYER) || (board[0][0] == PLAYER && board[1][0] == PLAYER && board[2][0] == PLAYER) || (board[0][1] == PLAYER && board[1][1] == PLAYER && board[2][1] == PLAYER) || (board[0][2] == PLAYER && board[1][2] == PLAYER && board[2][2] == PLAYER) || (board[0][0] == PLAYER && board[1][1] == PLAYER && board[2][2] == PLAYER) || (board[0][2] == PLAYER && board[1][1] == PLAYER && board[2][0] == PLAYER)) return PLAYER; else if ( (board[0][0] == AI && board[0][1] == AI && board[0][2] == AI) || (board[1][0] == AI && board[1][1] == AI && board[1][2] == AI) || (board[2][0] == AI && board[2][1] == AI && board[2][2] == AI) || (board[0][0] == AI && board[1][0] == AI && board[2][0] == AI) || (board[0][1] == AI && board[1][1] == AI && board[2][1] == AI) || (board[0][2] == AI && board[1][2] == AI && board[2][2] == AI) || (board[0][0] == AI && board[1][1] == AI && board[2][2] == AI) || (board[0][2] == AI && board[1][1] == AI && board[2][0] == AI)) return AI; else return BLANK; }

int minimax(int board[3][3], int player) { if (game_board(board) != BLANK) { return game_board(board) * player; // -1 * -1 || 1 * 1 }

int localX = -1; int localY = -1; int score = -2;

for (int x = 0; x { for (int y = 0; y { if (board[x][y] == BLANK) { board[x][y] = player; int moveScore = -minimax(board, player == PLAYER ? AI : PLAYER); if (moveScore > score) { score = moveScore; localX = x; localY = y; } board[x][y] = BLANK; } } } optimalX = localX; optimalY = localY; if (optimalX == -1 && optimalY == -1) { return 0; // No move - it's a draw } return score; }

void obtainMove(int board[3][3]) { int position = 0; printf(\"Enter the number of the field on the board: \"); scanf(\"%d\", &position);

if (position >= 1 && position => { int x = (position - 1) / 3; int y = (position - 1) % 3; if (board[x][y] == BLANK) { board[x][y] = PLAYER; } else { pboard(board); if (IS_LINUX) printf(\"\"); printf(\"This field is already taken!\"); if (IS_LINUX) printf(\"\"); printf(\" \"); obtainMove(board); } } else { pboard(board); printf(\"Invalid value! Please restart the program\"); printf(\" \"); exit(0); } }

int main(void) { int end = 0; pboard(arr); while (!end) { obtainMove(arr); pboard(arr);

if (game_board(arr) == PLAYER) { printf(\"You won! \"); //this is impossible to achieve, but it's here just in case return 0; }

printf(\"Processing... \"); minimax(arr, AI); arr[optimalX][optimalY] = AI; pboard(arr);

if (game_board(arr) == AI) { printf(\"You lost... \"); printf(\"Thank you for playing. Developed by Vigio Garcia \");

}

end = 1; for (int i = 0; i for (int j = 0; j if (arr[i][j] == BLANK) end = 0; } printf(\"Draw. \"); printf(\"Thank you for playing. Developed by Vigio Garcia \");

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!