Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

What are the two main functions of food in an animals body?

Answered: 1 week ago

Question

How did qualitative research methods emerge in psychology?

Answered: 1 week ago