Question

In: Computer Science

Need C++ code for following with a document describing the code as well: Write a program...

Need C++ code for following with a document describing the code as well:

Write a program to implement the game of Tic Tac Toe Four.

The game is played on a 4 × 4 chessboard, within 2 players. The player who is playing "X" always goes first. Players alternate placing Xs and Os on the board until either one player has four in a row, horizontally, vertically, or diagonally; or all 16 squares are filled(which is a draw).

The program should be able to:

1) keep receiving the input from two players in turn.

E.g. receive both row index and column index from keyboard input

2) print the current chessboard pattern for every turn. Below is an example of chessboard printing, with ‘-’ for empty squares. You can use any other pattern you like to visualize the chessboard.

- - - -

- - - -

- - - -

- - - -

game start

- - - -

- - x -

- o - -

- - - -

during game

- - - -

- x x -

- o - -

- - o -

during game

o o o o

- x x -

- o - x

x - o x

“o” wins

3) determine if any player wins the game, or the game comes to a draw. Print out the final game result.

E.g. “Player1 wins” or “Draw”

Please submit all your source codes together with a document describing your code and ideas

Solutions

Expert Solution

Code

#include<iostream>
using namespace std;

const int ROWS=4;
const int COLS=4;


bool checkForWinner(char board[][COLS], char player);
bool isBoardFull(char board[][COLS]);
void fillBoard(char [][4]);
bool gameOver(char board[][4]);
void getChoice(char board[][4],bool);
void showBoard(char board[][4]);
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[][4])
{
   for(int i=0;i<ROWS;i++)
       for(int j=0;j<COLS;j++)
           board[i][j]='-';
}

void showBoard(char board[][4])
{
   cout<<" 1 2 3 4"<<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[][4])
{
   bool win=false;
   for(int i=0;i<4;i++)
   {
       if(board[i][0]=='X' && board[i][1]=='X' && board[i][2]=='X' && board[i][3]=='X')
       {
           cout<<"\nPlayer X Wins!"<<endl;
           win=true;
           return win;
       }
       if(board[0][i]=='X' &&board[1][i]=='X' && board[2][i]=='X' && board[3][i]=='X')
       {
           cout<<"\nPlayer X Wins!"<<endl;
           win=true;
           return win;
       }
   }
   if(board[0][0]=='X' && board[1][1]=='X' && board[2][2]=='X' && board[3][3]=='X')
   {
       cout<<"\nPlayer X Wins!"<<endl;
       win=true;
       return win;
   }
   if(board[0][3]=='X' && board[1][2]=='X' && board[3][0]=='X' && board[2][1]=='X')
   {
       cout<<"\nPlayer X Wins!"<<endl;
       win=true;
       return win;
   }

   for(int i=0;i<4;i++)
   {
       if(board[i][0]=='O' && board[i][1]=='O' && board[i][2]=='O' && board[i][3]=='O')
       {
           cout<<"\nPlayer X Wins!"<<endl;
           win=true;
           return win;
       }
       if(board[0][i]=='O' &&board[1][i]=='O' && board[2][i]=='O' && board[3][i]=='O')
       {
           cout<<"\nPlayer O Wins!"<<endl;
           win=true;
           return win;
       }
   }
   if(board[0][0]=='O' && board[1][1]=='O' && board[2][2]=='O' && board[3][3]=='O')
   {
       cout<<"\nPlayer O Wins!"<<endl;
       win=true;
       return win;
   }
   if(board[0][3]=='O' && board[1][2]=='O' && board[3][0]=='O' && board[2][1]=='O')
   {
       cout<<"\nPlayer O 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[][4],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 "<<symbol<<", Enter Row: ";
           cin>>row;
           if(row>=1 && row<=4)
               break;
       }
       while(true)
       {
           cout<<"Player "<<symbol<<", Enter Column: ";
           cin>>col;
           if(col>=1 && col<=4)
               break;
       }
       if(board[row-1][col-1]=='-')
       {
           board[row-1][col-1]=symbol;
           break;
       }
       else
           cout<<"That postion is already taken! Try Again."<<endl;
   }
  
}

outputs

game 1

game 2

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


Related Solutions

write pseudocode for the following problems not c code Pseudocode only Write a C program to...
write pseudocode for the following problems not c code Pseudocode only Write a C program to print all natural numbers from 1 to n. - using while loop Write a C program to print all natural numbers in reverse (from n to 1). - using while loop Write a C program to print all alphabets from a to z. - using while loop Write a C program to print all even numbers between 1 to 100. - using while loop...
I need assistance translating a custom C++ program to MIPS. My C++ code is the following:...
I need assistance translating a custom C++ program to MIPS. My C++ code is the following: I have made numerous attempts on my own to no avail, any assistance is appreciated. Also, template code for this solution is provided below: #include int moveRobots(int *, int *, int, int ); int getNew(int, int); int main() { int x[4], y[4], i, j, myX = 25, myY = 25, move, status = 1; // initialize positions of four robots x[0] = 0; y[0]...
Write c code program for the following Write a function, circle, which takes the radius of...
Write c code program for the following Write a function, circle, which takes the radius of a circle from the main function and assign the area of the circle to the variable, area, and the perimeter of the circle to the variable, perimeter. Hint: The function should have three variables as input. Since the contents of the variables are to be modified by a function, it is necessary to use pointers. Please type out the full usable program. Thank you.
I need to write a program in C with the following specification * ​​​​​​​Line one of...
I need to write a program in C with the following specification * ​​​​​​​Line one of the standard input will have the total number of people which must not be greater than 10 * Each of the subsequent lines of input will be the first name of a person and their number (ex. "Adam 85") one space between name and number * Each name must be a maximum of 14 characters * Put the first names into an array called...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
1. Write pseudocode for the following program. You do not have to write actual C++ code....
1. Write pseudocode for the following program. You do not have to write actual C++ code. Assume you have a text file, that has been encrypted by adding 10 to the ASCII value of each character in the message. Design a decryption program that would reverse this process, and display the original message to the console.to the new array. 2.Write the code for the specified program. Use proper style and naming. Test and upload your code as a CPP file....
Write a C++ die roller program. We need you to write a program that will roll...
Write a C++ die roller program. We need you to write a program that will roll dice for them, but not just six-sided dice. Your program needs to be able to take an input in for form nDx or ndx where the d is the letter d or D. n may or not be present. If it is, it represents the number of dice. If not, assume it is a 1. x may or may not be present. If it...
C++ good documentation as well as explanations would be great. I need the code to be...
C++ good documentation as well as explanations would be great. I need the code to be written without (#include if possible. Please don't just copy and paste someone else's answer. In this HW assignment we will simulate a car wash and calculate the arrival time, departure time and wait time for each car that came in for a car wash. We will use the following assumptions about our car wash: Each car takes 3 minutes from start of the car...
This problem needs to be solved with source code. I need a C++ program that will...
This problem needs to be solved with source code. I need a C++ program that will help me solve this question. I need it in C++, please. Writing with comments so it maybe cleared. 1.2. We received the following ciphertext which was encoded with a shift cipher: xultpaajcxitltlxaarpjhtiwtgxktghidhipxciwtvgtpilpit ghlxiwiwtxgqadds. 1. Perform an attack against the cipher based on a letter frequency count: How many letters do you have to identify through a frequency count to recover the key? What is...
I need to complete this C++ program. The instructions are in the comments inside the code...
I need to complete this C++ program. The instructions are in the comments inside the code below: ------------------------------------------------------------------------- Original string is: this is a secret! Encypted string is: uijt!jt!b!tfdsfu" Decrypted string is: this is a secret! //Encoding program //Pre-_____? //other necessary stuff here int main() { //create a string to encrypt using a char array cout<< "Original string is: "<<string<<endl; encrypt(string); cout<< "Encrypted string is: "<<string<<endl; decrypt(string); cout<<"Decrypted string is: "<<string<<endl; return 0; } void encrypt(char e[]) { //Write implementation...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT