Question

Extra Credit Opportunity You may work on this if you have fully completed the previous Tic Tac Toe exercise in Zybooks Replac

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)<<" ";

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

{

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

}

cout<<endl;

}

}

bool gameOver(char board[][3])

{

bool win=false;

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

{

if(board[i][0]=='X' &&board[i][1]=='X' && board[i][2]=='X')

{

cout<<"\nPlayer 1 Wins!"<<endl;

win=true;

return win;

}

if(board[0][i]=='X' &&board[1][i]=='X' && board[2][i]=='X')

{

cout<<"\nPlayer 1 Wins!"<<endl;

win=true;

return win;

}

}

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

{

cout<<"\nPlayer 1 Wins!"<<endl;

win=true;

return win;

}

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

{

cout<<"\nPlayer 1 Wins!"<<endl;

win=true;

return win;

}

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

{

if(board[i][0]=='O' &&board[i][1]=='O' && board[i][2]=='O')

{

cout<<"Player 2 Wins!"<<endl;

win=true;

return win;

}

if(board[0][i]=='O' &&board[1][i]=='O' && board[2][i]=='O')

{

cout<<"Player 2 Wins!"<<endl;

win=true;

return win;

}

}

if(board[0][0]=='O' && board[1][1]=='O' && board[2][2]=='O')

{

cout<<"Player 2 Wins!"<<endl;

win=true;

return win;

}

if(board[0][2]=='O' && board[1][1]=='O' && board[2][0]=='O')

{

cout<<"Player 2 Wins!"<<endl;

win=true;

return win;

}

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

{

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

{

if(board[i][j]=='*')

return win;

}

}

cout<<"Tie!"<<endl;

return true;

}

void getChoice(char board[][3],bool playerToggle)

{

char symbol;

int player;

int row,col;

if(playerToggle)

{

symbol='O';

player=2;

}

else

{

symbol='X';

player=1;

}

while(true)

{

while(true)

{

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

cin>>row;

if(row>=1 && row<=3)

break;

}

while(true)

{

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

cin>>col;

if(col>=1 && col<=3)

break;

}

if(board[row-1][col-1]=='*')

{

board[row-1][col-1]=symbol;

break;

}

else

cout<<endl;

}

}

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

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)<<" ";

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

{

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

}

cout<<endl;

}

}

bool gameOver(char board[][3])

{

bool win=false;

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

{

if(board[i][0]=='X' &&board[i][1]=='X' && board[i][2]=='X')

{

cout<<"\nPlayer 1 Wins!"<<endl;

win=true;

return win;

}

if(board[0][i]=='X' &&board[1][i]=='X' && board[2][i]=='X')

{

cout<<"\nPlayer 1 Wins!"<<endl;

win=true;

return win;

}

}

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

{

cout<<"\nPlayer 1 Wins!"<<endl;

win=true;

return win;

}

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

{

cout<<"\nPlayer 1 Wins!"<<endl;

win=true;

return win;

}

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

{

if(board[i][0]=='O' &&board[i][1]=='O' && board[i][2]=='O')

{

cout<<"Computer Wins!"<<endl;

win=true;

return win;

}

if(board[0][i]=='O' &&board[1][i]=='O' && board[2][i]=='O')

{

cout<<"Computer Wins!"<<endl;

win=true;

return win;

}

}

if(board[0][0]=='O' && board[1][1]=='O' && board[2][2]=='O')

{

cout<<"Computer Wins!"<<endl;

win=true;

return win;

}

if(board[0][2]=='O' && board[1][1]=='O' && board[2][0]=='O')

{

cout<<"Computer Wins!"<<endl;

win=true;

return win;

}

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

{

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

{

if(board[i][j]=='*')

return win;

}

}

cout<<"Tie!"<<endl;

return true;

}

void find_winning_position (char noughtorcross, int &row, int &col,char board[][3])

{

// check if specified character (nought or cross) can win along any row

int i, j, chcount, emptycol, emptyrow;

// Loop through rows

for (i = 0; i < 3; i ++)

{

// Chcount -number of either O or X along the row

chcount = 0;

emptycol = -1;

// Loop through columns

for (j = 0; j < 3; j ++)

{

// If the specified player is occupying the cell, add to chcount

if (board[i][j] == noughtorcross)

chcount ++;

// If specified cell is blank mark the column as empty

else if (board[i][j] == '*')

emptycol = j;

}

// If there are two of the specified player in the row and there is an

// empty cell, then the empty cell is the winning position

if (chcount == 2 && emptycol != -1)

{

row = i;

col = emptycol;

return;

}

}

// The same logic as above applies for columns

// check if specified character can win along any column

for (j = 0; j < 3; j ++)

{

chcount = 0;

emptyrow = -1;

for (i = 0; i < 3; i ++)

{

if (board[i][j] == noughtorcross)

chcount ++;

else if (board[i][j] == '*')

emptyrow = i;

}

if (chcount == 2 && emptyrow != -1)

{

row = emptyrow;

col = j;

return;

}

}

// Check if specified character can win along diagonal top-left to bottom-right

chcount = 0;

emptyrow = -1;

emptycol = -1;

// Loop through the diagonal

for (i = 0; i < 3; i ++)

{

if (board[i][i] == noughtorcross)

chcount ++;

else if (board[i][i] == '*')

emptyrow = emptycol = i;

}

if (chcount == 2 && emptyrow != -1 && emptycol != -1)

{

row = emptyrow;

col = emptycol;

return;

}

// Check if specified character can win along diagonal top-right to bottom-left

chcount = 0;

emptyrow = -1;

emptycol = -1;

// Loop through the diagonal

for (i = 0; i < 3; i ++)

{

if (board[i][2-i] == noughtorcross)

chcount ++;

else if (board[i][2-i] == '*')

{

emptyrow = i;

emptycol = 2 - i;

}

}

if (chcount == 2 && emptyrow != -1 && emptycol != -1)

{

row = emptyrow;

col = emptycol;

return;

}

}

void getChoice(char board[][3],bool playerToggle)

{

char symbol;

int player;

int row,col;

if(playerToggle)

{

cout<<"Computer's Turn"<<endl;

char ai_char='O';

char player_char='X';

player=2;

// Check if AI can win in this move

int row=-1, col=-1;

// See if AI can win in this move

find_winning_position ('O', row, col,board);

// if AI can win, play the winning move

if (row != -1 && col != -1)

{

board[row][col] = ai_char;

return;

}

row = col = -1;

// see if player can win next move and block

// if possible

find_winning_position (player_char, row, col,board);

if (row != -1 && col != -1)

{

board[row][col] = ai_char;

return;

}

// check if the middle can be played

if (board[1][1] == ' ')

{

board[1][1] = ai_char;

return;

}

//Corner positions

int corners[4][2] = {{0, 0}, {0, 2}, {2, 0}, {2, 2}};

// If any one or more corners are blank

if (board[0][0] == '*' || board[0][2] == '*' || board[2][0] == '*' || board[2][2] == '*')

{

// randomly select corner

while (1)

{

int corner = rand () % 4;

row = corners[corner][0];

col = corners[corner][1];

if (board[row][col] == '*')

{

board[row][col] = ai_char;

return;

}

}

}

// Edge positions

int edges[4][2] = {{0, 1}, {1, 0}, {1, 2}, {2, 1}};

// if any one or more edges are blank

if (board[0][1] == '*' || board[1][0] == '*' || board[1][2] == '*' || board[2][1] == '*')

{

// randomly select edge

while (1)

{

int edge = rand () % 4;

row = edges[edge][0];

col = edges[edge][1];

if (board[row][col] == '*')

{

board[row][col] = ai_char;

return;

}

}

}

}

else

{

symbol='X';

player=1;

while(true)

{

while(true)

{

cout<<"Player 1 Row: ";

cin>>row;

if(row>=1 && row<=3)

break;

}

while(true)

{

cout<<"Player 1 Column: ";

cin>>col;

if(col>=1 && col<=3)

break;

}

if(board[row-1][col-1]=='*')

{

board[row-1][col-1]=symbol;

break;

}

else

cout<<endl;

}

}

}

output

1 2 3 Player 1 Row: 2 Player 1 Column: 2 1 2 3 2X Computers Turn 1 2 3 10 2X Player 1 Row: 3 Player 1 Column: 3 1 2 3 10 2X1 2 3 Player 1 Row: 1 Player 1 Column: 3 1 2 3 1 X Computers Turn 1 2 3 1 X 3 0 Player 1 Row: 2 Player 1 Column: 2 1 2 3 1 XWINDOWSsystem32)cmd.exe 2X 3 00 Player 1 Row: 3 layer 1 Column: 2 1 2 3 1 X 2X 3ΟΧΟ Computers Turn 1 2 3 2X Player 1 Row:2 PIf you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<i...
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 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 =...

  • 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 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...

  • 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;...

  • 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...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • There is a problem with thecode. I get the error messages " 'board' was not declared...

    There is a problem with thecode. I get the error messages " 'board' was not declared in this scope", \src\TicTacToe.cpp|7|error: expected unqualified-id before ')' token| \src\TicTacToe.cpp|100|error: expected declaration before '}' token| Here is the code #ifndef TICTACTOE_H #define TICTACTOE_H #include class TicTacToe { private: char board [3][3]; public: TicTacToe () {} void printBoard(); void computerTurn(char ch); bool playerTurn (char ch); char winVerify(); }; ************************************ TicTacToe.cpp #endif // TICTACTOE_H #include "TicTacToe.h" #include #include using namespace std; TicTacToe() { for(int i =...

  • The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[...

    The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[SIZE];    int player, choice, win, count;    char mark;    count = 0; // number of boxes marked till now    initBoard(board);       // start the game    player = 1; // default player    mark = 'X'; // default mark    do {        displayBoard(board);        cout << "Player " << player << "(" << mark...

  • 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...

  • I need help solving this in c++ using visual Studio. For this assignment, you will be filling in missing pieces of code within a program, follow the comments in the code. These comments will describe...

    I need help solving this in c++ using visual Studio. For this assignment, you will be filling in missing pieces of code within a program, follow the comments in the code. These comments will describe the missing portion. As you write in your code, be sure to use appropriate comments to describe your work. After you have finished, test the code by compiling it and running the program, then turn in your finished source code. // TicTacToe.cpp: Follow along with...

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