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

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

  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe {     private final int BOARDSIZE = 3; // size of the board     private enum Status { WIN, DRAW, CONTINUE }; // game states     private char[][] board; // board representation     private boolean firstPlayer; // whether it's player 1's move     private boolean gameOver; // whether...

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

  • Please I need your help I have the code below but I need to add one...

    Please I need your help I have the code below but I need to add one thing, after player choose "No" to not play the game again, Add a HELP button or page that displays your name, the rules of the game and the month/day/year when you finished the implementation. import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; import java.util.Random; public class TicTacToe extends JFrame implements ChangeListener, ActionListener { private JSlider slider; private JButton oButton, xButton; private Board...

  • This is my code for a TicTacToe game. However, I don't know how to write JUnit...

    This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe {    private char currentPlayer;    private char[][] board;    private char winner;       /**    * Default constructor    * Initializes the board to be 3 by 3 and...

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

  • Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner...

    Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...

  • I need help with the following and written in c++ thank you!: 1) replace the 2D...

    I need help with the following and written in c++ thank you!: 1) replace the 2D arrays with vectors 2) add a constructor to the Sudoku class that reads the initial configuration from a file 3) adds a function to the Sudoku class that writes the final Sudoku grid to a file or to the standard output device, cout. Sudoku.h #pragma once /* notes sudoku() default constructor precondition : none postcondition: grid is initialized to 0 sudoku(g[][9]) 1-parameter constructor precondition...

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
ADVERTISEMENT