Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include const int SIZE = 3 ; void initialize ( char board [ SIZE ] [ SIZE ] , int record [ 2

#include
#include
#include
const int SIZE =3;
void initialize(char board[SIZE][SIZE], int record[2][10]){
for (int i =0; i < SIZE; ++i){
for (int j =0; j < SIZE; ++j){
board[i][j]='';
}
}
for (int i =0; i <2; ++i){
for (int j =0; j <10; ++j){
record[i][j]=0;
}
}
}
void drawBoard(char board[SIZE][SIZE]){
system("cls"); // You might need to change this for other platforms
std::cout <<"-------------
";
for (int i =0; i < SIZE; i++){
std::cout <<"|";
for (int j =0; j < SIZE; j++){
std::cout << board[i][j]<<"|";
}
std::cout <<"
-------------
";
}
}
bool checkWin(char board[SIZE][SIZE], char player){
for (int i =0; i < SIZE; i++){
if ((board[i][0]== player && board[i][1]== player && board[i][2]== player)||
(board[0][i]== player && board[1][i]== player && board[2][i]== player)){
return true;
}
}
if ((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 true;
}
return false;
}
void computerTurn(char board[SIZE][SIZE]){
int row, col;
do {
row = rand()% SIZE;
col = rand()% SIZE;
} while (board[row][col]!='');
board[row][col]='O';
}
int main(){
char board[SIZE][SIZE];
int record[2][10];
int userWins =0;
int computerWins =0;
int gamesToPlay;
std::cout << "Enter the number of games you want to play: ";
std::cin >> gamesToPlay;
srand(static_cast(time(0)));
for (int game =1; game <= gamesToPlay; ++game){
initialize(board, record);
std::cout << "Welcome to Tic-Tac-Toe (Game "<< game <<")!
";
for (int turn =0; turn < SIZE * SIZE; ++turn){
drawBoard(board);
if (turn %2==0){
int row, col;
std::cout << "Your turn (X), enter row (1-3) and column (1-3): ";
std::cin >> row >> col;
--row;
--col;
if (row <0|| row >= SIZE || col <0|| col >= SIZE || board[row][col]!=''){
std::cout << "Invalid move. Try again.
";
--turn; // Repeat the same turn
continue;
}
board[row][col]='X';
if (checkWin(board,'X')){
drawBoard(board);
std::cout << "You win!
";
++userWins;
break;
}
} else {
std::cout << "Computer's turn (O):
";
computerTurn(board);
if (checkWin(board,'O')){
drawBoard(board);
std::cout << "Computer wins!
";
++computerWins;
break;
}
}
}
// End of the game
drawBoard(board);
if (!checkWin(board,'X') && !checkWin(board,'O')){
std::cout << "It's a draw!
";
}
}
std::cout << "Overall Winner: "<<((userWins > computerWins)? "You" : "Computer")<<" with "
<<((userWins > computerWins)? userWins : computerWins)<<" wins.
";
std::cout << "Number of user wins: "<< userWins <<"
";
std::cout << "Number of computer wins: "<< computerWins <<"
";
return 0;
}
what is wrong with my code? it doesnot shows the board properly .please review it

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

Database Administration The Complete Guide To Dba Practices And Procedures

Authors: Craig S. Mullins

2nd Edition

0321822943, 978-0321822949

More Books

Students also viewed these Databases questions

Question

(g) In what sub-space of the data space must the point y then lie?

Answered: 1 week ago