Question: C++ Please!! The following program is the base for the game: tic-tac-toe. The main function and program structure is given, dont modify them. Complete three
C++ Please!!
The following program is the base for the game: tic-tac-toe. The main function and program structure is given, dont modify them. Complete three functions as required. Compile and run this program, and then you can play the game. Have fun!
#include
const int DIM=3; char chessboard[DIM][DIM]; //initChessBoard void initChessBoard(char cb[][DIM])
{
//set all the elements of the ChessBoard to blanks
//Complete this part in the following:
}
//printChessBoard
void printChessBoard(char cb[][DIM])
{
//print all the elements of the chessBoard with each row in one line
//Complete this part in the following:
}
//putChequer
bool putChequer(char cb[][DIM], int i, int j, char x)
{
/* if i and j are not out of bound(that is, i and j are in the range of 0 and DIM-1) and cb[i][j] is not occupied(that is, the value of cb[i][j] is blank), set cb[i][j] to be the value of x and return true. Otherwise, return false.*/
// Complete this part in the following:
}
//judge the state of the game. The player has put x in the position (row, col). // If all the elements in this row are x, x wins // If all the elements in this column are x, x wins. // If x is in the main diagonal, and if all the elements in the main diagonal are x, x wins. // If x is in the opposite diagonal, and if all the elements in the opposite diagonal are x, x wins.
bool state(char cb[][DIM], int row, int col, char x)
{
/* We declare four variables count1, count2, count3, count4 to represent the occurrence number of x in row number row, column number col, in the main diagonal, and in the opposite diagonal. If after calculation, count1, count2, count3 or count4 equals to DIM, return true(that is, x wins). */
int count1=0, count2=0, count3=0, count4=0;
for(int i=0; i { // Complete this part in the following: // if the element in position (row, i) is x, count1 is increased by 1. // if the element in position (i, col) is x, count2 is increased by 1. /* if x is in the main diagonal, and the element in position (i, i) is x, count3 is increased by 1.*/ /* if x is in the opposite diagonal, and the element in position (i, DIM-1-I) is x, count4 is increased by 1.*/ } return (count1==DIM || count2==DIM || count3==DIM || count4==DIM); } int main() { int row, col; int blanks=DIM*DIM; initChessBoard(chessboard); printChessBoard(chessboard); char cur='O'; cout<<"Input the position(row col), (-1 -1) for exit; It is the turn of "< cin>>row>>col; while(row!=-1 && col!=-1) { if(!putChequer(chessboard, row, col, cur)) { cout<<"Invalid move"< printChessBoard(chessboard); } else { --blanks; printChessBoard(chessboard); if(state(chessboard, row, col, cur)) { cout<< cur << " Wins"< return 0; } }; if(blanks==0) { cout<< "Ties"< return 0; } if(cur=='X') cur= 'O'; else cur='X'; } cout<<"Input the position(row col), (-1 -1) for exit; It is the ture of "< cin>>row>>col; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
