Question

I wrote program that have to block all moves for player 1, for game TicTacTOE AI,...

I wrote program that have to block all moves for player 1, for game TicTacTOE AI, but it is not working. Can someone check what I have not eliminated?

#include

using namespace std;

const int ROWS = 3;

const int COLS = 3;

int counter = 0;

void aITurn(char b[][COLS]);

void fillBoard(char board[ROWS][COLS]);

void getChoice(char b[][COLS], bool playerToggle);

bool gameOver(char b[][COLS]);

void showBoard(char board[ROWS][COLS]);

int main()

{

   ///main is complete, nothing to do here

   char board[ROWS][COLS];

   bool playerToggle = false;

   fillBoard(board);

   showBoard(board);

   while (!gameOver(board))

   {

   getChoice(board, playerToggle);

   showBoard(board);

   playerToggle = !playerToggle;

   }

   return 0;

}

void fillBoard(char board[ROWS][COLS])

{

   for(int i = 0; i < ROWS; i++)

   {

   for(int j = 0; j < COLS; j++)

   {

   board[i][j] = '*';

   }

   }

}

void showBoard(char board[ROWS][COLS])

{

cout << " 1 2 3" << endl;

   for(int i = 0; i < ROWS; i++)

   {

   cout << i + 1 << " ";

   for(int j = 0; j < COLS; j++)

   {

   cout << board[i][j] << " ";

   }

   cout << endl;

   }

}

void getChoice(char b[][COLS], bool playerToggle){

string player;

char c;

int row, col;

if (playerToggle == false)

{ player = "1";

c = 'X';

do

{

do

{

cout << "Player" << player << "," << " Row: " << endl;

cin >> row;

  

}

while (row < 1 || row > 3);

  

do {

cout << "Player" << player << "," << " Column: " << endl;

cin >> col;

} while (col < 1 || col > 3);

row--;

col--;

} while (b[row][col] == 'X' || b[row][col] == 'O');

b[row][col] = c;

}

else { player = "AI";

aITurn(b);

   }

}

bool gameOver(char b[][COLS])

{

   bool gameOver = false;

   if(((b[0][0]=='X' && b[0][1] == 'X' && b[0][2] =='X') ||

(b[1][0] == 'X' && b[1][1] == 'X' && b[1][2] == 'X') ||

(b[2][0] == 'X' && b[2][1] == 'X' && b[2][2] == 'X') ||

(b[0][0] == 'X' && b[1][0] == 'X' && b[2][0] == 'X') ||

(b[0][1] == 'X' && b[1][1] == 'X' && b[2][1] == 'X') ||

(b[0][2] == 'X' && b[1][2] == 'X' && b[2][2] == 'X') ||

(b[0][0] == 'X' && b[1][1] == 'X' && b[2][2] == 'X') ||

(b[0][2] == 'X' && b[1][1] == 'X' && b[2][0] == 'X') ))

   /// X Wins

   {

cout << "Player 1 wins!" << endl;

   gameOver = true;

   }

else if(((b[0][0]=='O' && b[0][1] == 'O' && b[0][2] == 'O') ||

(b[1][0] == 'O' && b[1][1] == 'O' && b[1][2] == 'O') ||

(b[2][0] == 'O' && b[2][1] == 'O' && b[2][2] == 'O') ||

(b[0][0] == 'O' && b[1][0] == 'O' && b[2][0] == 'O') ||

(b[0][1] == 'O' && b[1][1] == 'O' && b[2][1] == 'O') ||

(b[0][2] == 'O' && b[1][2] == 'O' && b[2][2] == 'O') ||

(b[0][0] == 'O' && b[1][1] == 'O' && b[2][2] == 'O') ||

(b[0][2] == 'O' && b[1][1] == 'O' && b[2][0] == 'O') ))

   /// O Wins

   {

   cout << " AI wins." << endl;

   gameOver = true;

   }

   else

   {

   for (int row = 0; row < 3; row++)

   {

   for (int ccl = 0; ccl < 3; ccl++)

   {

   if (b[row][ccl] == '*')

   {

   return gameOver;

   }

   }

   }

   cout << "Tie!" << endl;// Tie

   gameOver = true;

   }

   return gameOver;

}

void aITurn(char b[][COLS])

{

   cout << "AI's turn..." << endl;

  

   if(counter == 0)

   {

   //first turn checking to see if player1 got center

   if(b[1][1] != 'X')

   {

   b[1][1] = 'O';

   }

   else

   {

   b[0][0] ='O';

   }

   }

// following turns to be checked

if(counter >= 1)

{

  

   for(int i = 0; i < ROWS; i++){

   for(int j = 0; j < COLS; j++){

   if((b[i][j] == 'X' || b[i][j] == 'O') && (b[i+1][j] == b[i][j]) && (b[i+2][j] != 'X' && b[i+2][j] != 'O')){

b[i+2][j] = 'O'; return;

}

   }

   }

   for(int i = 0; i < ROWS; i++){

   for(int j = 0; j < COLS; j++){

   if((b[i][j] == 'X' || b[i][j] == 'O') && (b[i+1][j] == b[i][j]) && (b[i-1][j] != 'X' && b[i-1][j] != 'O')){

b[i-1][j] = 'O'; return;

}

   }

   }

   for(int i = 0; i < ROWS; i++){

   for(int j = 0; j < COLS; j++){

if((b[i][j] == 'X' || b[i][j] == 'O') && (b[i+2][j] == b[i][j]) && (b[i+1][j] != 'X' && b[i+1][j] != 'O')){

b[i+1][j] = 'O'; return;}

   }

   }

  

   for(int i = 0; i < ROWS; i++){

   for(int j = 0; j < COLS; j++){

   if((b[i][j] == 'X' || b[i][j] == 'O') && (b[i][j+1] == b[i][j]) && (b[i][j+2] != 'X' && b[i][j+2] != 'O')){

   b[i][j+2] = 'O'; return;

   }}

   }

   for(int i = 0; i < ROWS; i++){

   for(int j = 0; j < COLS; j++){

   if((b[i][j] == 'X' || b[i][j] == 'O') && (b[i][j+1] == b[i][j]) && (b[i][j-1] != 'X' && b[i][j-1] != 'O')){

   b[i][j-1] = 'O'; return;

   }

   }}

   for(int i = 0; i < ROWS; i++){

   for(int j = 0; j < COLS; j++){

   if((b[i][j] == 'X' || b[i][j] == 'O') && (b[i][j+2] == b[i][j]) && (b[i][j+1] != 'X' && b[i][j+1] != 'O')){

  

   b[i][j+1] = 'O'; return;

   }

   if((b[0][0] == b[1][1]) && (b[2][2] != 'X' && b[2][2] != 'O')){

  

   b[2][2] = 'O'; return;

   }

   if((b[2][0] == b[1][1]) && (b[0][2] != 'X' && b[0][2] != 'O')){

   b[0][2] = 'O'; return;

   }

   if((b[0][2] == b[1][1]) && (b[2][0] != 'X' && b[2][0] != 'O')){

   b[2][0] = 'O'; return;

  

   }

  

   if((b[2][2] == b[1][1]) && (b[0][0] != 'X' && b[0][0] != 'O')){

   b[0][0] = 'O'; return;

   }

   if((b[0][0] == b[2][2]) && (b[1][1] != 'X' && b[1][1] != 'O')){

b[1][1] = 'O'; return;

   }

   if((b[0][2] == b[2][0]) && (b[1][1] != 'X' && b[1][1] != 'O')){

   b[1][1] = 'O'; return;

   }

   }}

if((b[0][0] == b[0][1]) && (b[0][1] != 'X' && b[0][1] != 'O')){

b[0][1] = 'O'; return;}

   }

  

counter += 1;// checking of the n-th round ends

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

First of all the Win condition check is really messy.

Also, the counter increment is not at the actual place.

So here I have implemented the whole program >>

Here is the code >>


#include<bits/stdc++.h>
using namespace std;
  
#define AI 1
#define HUMAN 2
  
#define SIDE 3 //This is the length of the board
#define AIMOVE 'O' //Computer will move with O
#define HUMANMOVE 'X' //user will move with X
  
// Check status of the board
void checkBoard(char boardIMPL[][SIDE])
{
printf("\n\n");
  
printf("\t\t\t %c | %c | %c \n", boardIMPL[0][0],
boardIMPL[0][1], boardIMPL[0][2]);
printf("\t\t\t--------------\n");
printf("\t\t\t %c | %c | %c \n", boardIMPL[1][0],
boardIMPL[1][1], boardIMPL[1][2]);
printf("\t\t\t--------------\n");
printf("\t\t\t %c | %c | %c \n\n", boardIMPL[2][0],
boardIMPL[2][1], boardIMPL[2][2]);

return;
}
void instruction()
{
printf("\t\t\t Tic Tac Toe\n\n");
printf("please choose the cell number:"
" and play\n\n");
  
printf("\t\t\t 1 | 2 | 3 \n");
printf("\t\t\t-------------\n");
printf("\t\t\t 4 | 5 | 6 \n");
printf("\t\t\t-------------\n");
printf("\t\t\t 7 | 8 | 9 \n\n");
  
printf("-\t-\t-\t-\t-\t-\t-\t-\t-\n\n");
  
return;
}
  
void Initialization(char boardIMPL[][SIDE], int turns[])
{

srand(time(NULL));
  
// Initially the boardIMPL is empty
for (int i=0; i<SIDE; i++)
{
for (int j=0; j<SIDE; j++)
boardIMPL[i][j] = ' ';
}
for (int i=0; i<SIDE*SIDE; i++)
turns[i] = i;
  
random_shuffle(turns, turns + SIDE*SIDE);
  
return;
}
  

void Winner(int player) //declare the winner
{
if (player == AI)
printf("AI won\n");
else
printf("HUMAN won\n");
return;
}

bool rowCutCheck(char boardIMPL[][SIDE])
{
for (int i=0; i<SIDE; i++)
{
if (boardIMPL[i][0] == boardIMPL[i][1] &&
boardIMPL[i][1] == boardIMPL[i][2] &&
boardIMPL[i][0] != ' ')
return (true);
}
return(false);
}
  
bool columnCutCheck(char boardIMPL[][SIDE])
{
for (int i=0; i<SIDE; i++)
{
if (boardIMPL[0][i] == boardIMPL[1][i] &&
boardIMPL[1][i] == boardIMPL[2][i] &&
boardIMPL[0][i] != ' ')
return (true);
}
return(false);
}
  

bool diagonalCutCheck(char boardIMPL[][SIDE])
{
if (boardIMPL[0][0] == boardIMPL[1][1] &&
boardIMPL[1][1] == boardIMPL[2][2] &&
boardIMPL[0][0] != ' ')
return(true);
  
if (boardIMPL[0][2] == boardIMPL[1][1] &&
boardIMPL[1][1] == boardIMPL[2][0] &&
boardIMPL[0][2] != ' ')
return(true);
  
return(false);
}
//This function checks the board condition
bool ifGameOver(char boardIMPL[][SIDE])
{
return(rowCutCheck(boardIMPL) || columnCutCheck(boardIMPL)
|| diagonalCutCheck(boardIMPL) );
}
  
// playing the game
void playTicTacToe(int player)
{
//baord initilization
char boardIMPL[SIDE][SIDE];
  
int turns[SIDE*SIDE];
  
Initialization(boardIMPL, turns);
instruction();
  
int moveIndex = 0, x, y;
  
while (ifGameOver(boardIMPL) == false &&
moveIndex != SIDE*SIDE)
{
if (player == AI)
{
x = turns[moveIndex] / SIDE;
y = turns[moveIndex] % SIDE;
boardIMPL[x][y] = AIMOVE;
printf("AI put %c in %d\n",
AIMOVE, turns[moveIndex]+1);
checkBoard(boardIMPL);
moveIndex ++;
player = HUMAN;
}
  
else if (player == HUMAN)
{
x = turns[moveIndex] / SIDE;
y = turns[moveIndex] % SIDE;
boardIMPL[x][y] = HUMANMOVE;
printf ("HUMAN put %c in %d\n",
HUMANMOVE, turns[moveIndex]+1);
checkBoard(boardIMPL);
moveIndex ++;
player = AI;
}
}
  
// game drawn condition
if (ifGameOver(boardIMPL) == false &&
moveIndex == SIDE * SIDE)
printf("It's a draw\n");
else
{
//toggle the user
if (player == AI)
player = HUMAN;
else if (player == HUMAN)
player = AI;
  
//print the winner of the game
Winner(player);
}
return;
}
  
// Test method
int main()
{
//Ai starts the game
playTicTacToe(AI);
  
return (0);
}

Here is the code snippet>>

Here is the code live output>>

<<<DO comment for any queries>>>>#>#>#>#

Add a comment
Know the answer?
Add Answer to:
I wrote program that have to block all moves for player 1, for game TicTacTOE AI,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<i...

    I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...

  • This is an advanced version of the game tic tac toe, where the player has to...

    This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...

  • In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user....

    In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. Use Inheritance to create a derived class from your Lab #9 Tic-Tac-Toe class. You can use ASCII art to generate and display the 3x3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The...

  • This program is in C++ which reserves flight seats. It uses a file imported to get...

    This program is in C++ which reserves flight seats. It uses a file imported to get the seat chart called "chartIn.txt" which looks like this 1 A B C D E F 2 A B C D E F It continues until row 10. Sorry unable to provide the chartIn.txt file. I am having issues with function displaySeats, it displays then but when it comes to 10 the row looks like this 1 0 A B C D E ,...

  • I've created a functioning program but I can't figure out how to implement class structure into...

    I've created a functioning program but I can't figure out how to implement class structure into it. I'm currently studying classes and I'm wondering if you could help me recreate my program with classes so I can have a better understanding of how classes work. Thank you. #pragma once #include <iostream> using namespace std;    bool DeclareResults(char ch, char gameboard[][3]);    bool FilledBoard(char gameboard[][3]);    void printGameBoard(char gameboard[][3]); #include "fnt.h"; #include <iostream> using namespace std; int main() {    //declare...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

  • how to randomly generate moves until the whole board is filled?. If the board size is...

    how to randomly generate moves until the whole board is filled?. If the board size is c*r, you can only call the random function rand() c*r times (excluding the random function rand() that have been used in the provided code). Below is the code I need to convert. It is manual right now (user inputs numbers until whole board filled). I also have a piece of algorithmn below..but am unsure of how I can use it in the code: //Algorithm...

  • Hello, I am trying to write this program and have received a "Segmentation Fault" error but...

    Hello, I am trying to write this program and have received a "Segmentation Fault" error but cannot seem to figure out where. I haven't been able to find it and have been looking for quite a while. The goal is to basically create a version of Conway's Game of Life. I am able to generate a table if the input values for rows and columns are equal, but if they are not I receive a segmentation fault and cannot continue...

  • Can I get help with adding the following to this code please? 1. When buying multiple...

    Can I get help with adding the following to this code please? 1. When buying multiple tickets, if one of the seats selected is already taken, give a message like "Sorry, one or more of the seats selected is already taken." and prompt the user to select the seats all over. (Or if possible to make it suggest a location where the seats are together that the user can select instead) 2. Print the total cost after all of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT