Please follow the data 1) For Line 23 In the IF - Condition yo.pdf

35 views 25 slides Apr 09, 2023
Slide 1
Slide 1 of 25
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25

About This Presentation

Please follow the data :
1) For Line 23 :
In the IF - Condition you are just checking the cells of the grid at positions 0 and 1. But I
suppose you would like to check if both the cells are having the character value \'x\'. So to
achieve this you need to replace the code with
if(grid[0] == g...


Slide Content

Please follow the data :
1) For Line 23 :
In the IF - Condition you are just checking the cells of the grid at positions 0 and 1. But I
suppose you would like to check if both the cells are having the character value \'x\'. So to
achieve this you need to replace the code with
if(grid[0] == grid[1] == \'x\')
2) For Line 29 :
The error is that the function has been define before the error line and the loop has not been
closed so pplace a closing brackets for the loop before the line starting with bool tie()
3) For Line 37 :
The above change should also resolve this as the scope of the main function isnt predefined.
4) For Line 69 :
Remove the excess bracket at the end of the code.
Even after replacing these I suppose your code would not work as the input has not been defined
from where and how thw user can get the data to the code. So to avoid this I am placing my
code.
Please follow the code and comments for descripiton :
CODE :
#include
using namespace std;
int checkWin( char[]); // rewuired variables
void grid( char[]);
int main()
{
   char cell[10] = {\'o\',\'1\',\'2\',\'3\',\'4\',\'5\',\'6\',\'7\',\'8\',\'9\'}; // placing the cell names
   int player = 1,i,choice; // initialisations
   char symbol;
   do // looping to get the player choices
   {
       grid(cell);
       if(player%2==1) { // checking for the player
           player=1;
       } else {
           player=2;
       }

       // player 2 is the computer
       if(player==2) // if is a player 2
       {
           cout << \"Player \" << player<> choice;
           symbol=\'X\';
           if (choice == 1 && cell[1] == \'1\') {
               cell[1] = symbol;
           }
           else if (choice == 2 && cell[2] == \'2\') {
               cell[2] = symbol;
           }
           else if (choice == 3 && cell[3] == \'3\') {
               cell[3] = symbol;
           }
           else if (choice == 4 && cell[4] == \'4\') {
               cell[4] = symbol;
           }
           else if (choice == 5 && cell[5] == \'5\') {
               cell[5] = symbol;
           }
           else if (choice == 6 && cell[6] == \'6\') {
               cell[6] = symbol;
           }
           else if (choice == 7 && cell[7] == \'7\') {
               cell[7] = symbol;
           }
           else if (choice == 8 && cell[8] == \'8\') {
               cell[8] = symbol;
           }
           else if (choice == 9 && cell[9] == \'9\') {
               cell[9] = symbol;
           } else {
               cout<<\"Invalid move..!!!\"<< endl;
               player--;
               exit(0);

           }
           i=checkWin(cell);
           player++;
       }
   } while(i==-1);
   grid(cell);
   if(i==1) {
       cout<<\"Congratulations! Player \"<<--player<<\" won the Game..!!\";
   } else {
       cout<<\" OOPS..!!!! The Game has been tie..!!\";
   }
   exit(0);
   return 0;
}
void grid(char cell[]) // printing the grid everytime the user has placed the symbol
{
   system(\"cls\");
   cout << \" Tic Tac Toe Game \";
   cout << \"Player 1 (X) - Player 2 (O)\" << endl << endl;
   cout << endl;
   cout << \" | | \" << endl;
   cout << \" \" << cell[1] << \" | \" << cell[2] << \" | \" << cell[3] << endl;
   cout << \"_____|_____|_____\" << endl;
   cout << \" | | \" << endl;
   cout << \" \" << cell[4] << \" | \" << cell[5] << \" | \" << cell[6] << endl;
   cout << \"_____|_____|_____\" << endl;
   cout << \" | | \" << endl;
   cout << \" \" << cell[7] << \" | \" << cell[8] << \" | \" << cell[9] << endl;
   cout << \" | | \" << endl << endl;
}

int checkWin(char cell[])// checking for the win condition
{
   if (cell[1] == cell[2] && cell[2] == cell[3]) {
       return 1;
   }

   else if (cell[4] == cell[5] && cell[5] == cell[6]) {
       return 1;
   }
   else if (cell[7] == cell[8] && cell[8] == cell[9]) {
       return 1;
   }
   else if (cell[1] == cell[4] && cell[4] == cell[7]) {
       return 1;
   }
   else if (cell[2] == cell[5] && cell[5] == cell[8]) {
       return 1;
   }
   else if (cell[3] == cell[6] && cell[6] == cell[9]) {
       return 1;
   }
   else if (cell[1] == cell[5] && cell[5] == cell[9]) {
       return 1;
   }
   else if (cell[3] == cell[5] && cell[5] == cell[7]) {
       return 1;
   }
   else if (cell[1] != \'1\' && cell[2] != \'2\' && cell[3] != \'3\'
           && cell[4] != \'4\' && cell[5] != \'5\' && cell[6] != \'6\'
           && cell[7] != \'7\' && cell[8] != \'8\' && cell[9] != \'9\') {
               return 0;
           }
   else {
       return -1;
   }
}
OUTPUT :
1) For a Tie Game Case :
   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   

1 | 2 | 3
_____|_____|_____
| |   
4 | 5 | 6
_____|_____|_____
| |   
7 | 8 | 9
| |   
Player 1, Please enter the grid number to place the symbol :
7

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
1 | 2 | 3
_____|_____|_____
| |   
4 | 5 | 6
_____|_____|_____
| |   
X | 8 | 9
| |   
Player 2
Press Enter for the Computer Turn..!!

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | 2 | 3
_____|_____|_____
| |   
4 | 5 | 6
_____|_____|_____
| |   

X | 8 | 9
| |   
   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | 2 | 3
_____|_____|_____
| |   
4 | 5 | 6
_____|_____|_____
| |   
X | 8 | 9
| |   
Player 1, Please enter the grid number to place the symbol : 5

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | 2 | 3
_____|_____|_____
| |   
4 | X | 6
_____|_____|_____
| |   
X | 8 | 9
| |   
Player 2
Press Enter for the Computer Turn..!!

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | 2 | O

_____|_____|_____
| |   
4 | X | 6
_____|_____|_____
| |   
X | 8 | 9
| |   
   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | 2 | O
_____|_____|_____
| |   
4 | X | 6
_____|_____|_____
| |   
X | 8 | 9
| |   
Player 1, Please enter the grid number to place the symbol : 2

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | X | O
_____|_____|_____
| |   
4 | X | 6
_____|_____|_____
| |   
X | 8 | 9
| |   
Player 2
Press Enter for the Computer Turn..!!

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | X | O
_____|_____|_____
| |   
4 | X | O
_____|_____|_____
| |   
X | 8 | 9
| |   
   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | X | O
_____|_____|_____
| |   
4 | X | O
_____|_____|_____
| |   
X | 8 | 9
| |   
Player 1, Please enter the grid number to place the symbol : 4

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | X | O
_____|_____|_____
| |   
X | X | O
_____|_____|_____
| |   

X | 8 | 9
| |   
Player 2
Press Enter for the Computer Turn..!!

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | X | O
_____|_____|_____
| |   
X | X | O
_____|_____|_____
| |   
X | O | 9
| |   
   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | X | O
_____|_____|_____
| |   
X | X | O
_____|_____|_____
| |   
X | O | 9
| |   
Player 1, Please enter the grid number to place the symbol : 9

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | X | O

_____|_____|_____
| |   
X | X | O
_____|_____|_____
| |   
X | O | X
| |   
OOPS..!!!!
The Game has been tie..!!
2) For Win Case :

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
1 | 2 | 3
_____|_____|_____
| |   
4 | 5 | 6
_____|_____|_____
| |   
7 | 8 | 9
| |   
Player 1, Please enter the grid number to place the symbol : 1

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
X | 2 | 3
_____|_____|_____
| |   
4 | 5 | 6
_____|_____|_____
| |   
7 | 8 | 9

| |   
Player 2
Press Enter for the Computer Turn..!!

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
X | 2 | 3
_____|_____|_____
| |   
4 | 5 | 6
_____|_____|_____
| |   
O | 8 | 9
| |   
   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
X | 2 | 3
_____|_____|_____
| |   
4 | 5 | 6
_____|_____|_____
| |   
O | 8 | 9
| |   
Player 1, Please enter the grid number to place the symbol : 2

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
X | X | 3
_____|_____|_____

| |   
4 | 5 | 6
_____|_____|_____
| |   
O | 8 | 9
| |   
Player 2
Press Enter for the Computer Turn..!!

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
X | X | 3
_____|_____|_____
| |   
4 | O | 6
_____|_____|_____
| |   
O | 8 | 9
| |   
   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
X | X | 3
_____|_____|_____
| |   
4 | O | 6
_____|_____|_____
| |   
O | 8 | 9
| |   
Player 1, Please enter the grid number to place the symbol : 3

   Tic Tac Toe Game

Player 1 (X) - Player 2 (O)

| |   
X | X | X
_____|_____|_____
| |   
4 | O | 6
_____|_____|_____
| |   
O | 8 | 9
| |   
Congratulations!
Player 1 won the Game..!!
Hope this is helpful.

Solution

Please follow the data :
1) For Line 23 :
In the IF - Condition you are just checking the cells of the grid at positions 0 and 1. But I
suppose you would like to check if both the cells are having the character value \'x\'. So to
achieve this you need to replace the code with
if(grid[0] == grid[1] == \'x\')
2) For Line 29 :
The error is that the function has been define before the error line and the loop has not been
closed so pplace a closing brackets for the loop before the line starting with bool tie()
3) For Line 37 :
The above change should also resolve this as the scope of the main function isnt predefined.
4) For Line 69 :
Remove the excess bracket at the end of the code.
Even after replacing these I suppose your code would not work as the input has not been defined
from where and how thw user can get the data to the code. So to avoid this I am placing my
code.
Please follow the code and comments for descripiton :
CODE :
#include

using namespace std;
int checkWin( char[]); // rewuired variables
void grid( char[]);
int main()
{
   char cell[10] = {\'o\',\'1\',\'2\',\'3\',\'4\',\'5\',\'6\',\'7\',\'8\',\'9\'}; // placing the cell names
   int player = 1,i,choice; // initialisations
   char symbol;
   do // looping to get the player choices
   {
       grid(cell);
       if(player%2==1) { // checking for the player
           player=1;
       } else {
           player=2;
       }

       // player 2 is the computer
       if(player==2) // if is a player 2
       {
           cout << \"Player \" << player<> choice;
           symbol=\'X\';
           if (choice == 1 && cell[1] == \'1\') {
               cell[1] = symbol;
           }
           else if (choice == 2 && cell[2] == \'2\') {
               cell[2] = symbol;
           }
           else if (choice == 3 && cell[3] == \'3\') {
               cell[3] = symbol;
           }
           else if (choice == 4 && cell[4] == \'4\') {
               cell[4] = symbol;
           }
           else if (choice == 5 && cell[5] == \'5\') {
               cell[5] = symbol;

           }
           else if (choice == 6 && cell[6] == \'6\') {
               cell[6] = symbol;
           }
           else if (choice == 7 && cell[7] == \'7\') {
               cell[7] = symbol;
           }
           else if (choice == 8 && cell[8] == \'8\') {
               cell[8] = symbol;
           }
           else if (choice == 9 && cell[9] == \'9\') {
               cell[9] = symbol;
           } else {
               cout<<\"Invalid move..!!!\"<< endl;
               player--;
               exit(0);
           }
           i=checkWin(cell);
           player++;
       }
   } while(i==-1);
   grid(cell);
   if(i==1) {
       cout<<\"Congratulations! Player \"<<--player<<\" won the Game..!!\";
   } else {
       cout<<\" OOPS..!!!! The Game has been tie..!!\";
   }
   exit(0);
   return 0;
}
void grid(char cell[]) // printing the grid everytime the user has placed the symbol
{
   system(\"cls\");
   cout << \" Tic Tac Toe Game \";
   cout << \"Player 1 (X) - Player 2 (O)\" << endl << endl;
   cout << endl;

   cout << \" | | \" << endl;
   cout << \" \" << cell[1] << \" | \" << cell[2] << \" | \" << cell[3] << endl;
   cout << \"_____|_____|_____\" << endl;
   cout << \" | | \" << endl;
   cout << \" \" << cell[4] << \" | \" << cell[5] << \" | \" << cell[6] << endl;
   cout << \"_____|_____|_____\" << endl;
   cout << \" | | \" << endl;
   cout << \" \" << cell[7] << \" | \" << cell[8] << \" | \" << cell[9] << endl;
   cout << \" | | \" << endl << endl;
}

int checkWin(char cell[])// checking for the win condition
{
   if (cell[1] == cell[2] && cell[2] == cell[3]) {
       return 1;
   }
   else if (cell[4] == cell[5] && cell[5] == cell[6]) {
       return 1;
   }
   else if (cell[7] == cell[8] && cell[8] == cell[9]) {
       return 1;
   }
   else if (cell[1] == cell[4] && cell[4] == cell[7]) {
       return 1;
   }
   else if (cell[2] == cell[5] && cell[5] == cell[8]) {
       return 1;
   }
   else if (cell[3] == cell[6] && cell[6] == cell[9]) {
       return 1;
   }
   else if (cell[1] == cell[5] && cell[5] == cell[9]) {
       return 1;
   }
   else if (cell[3] == cell[5] && cell[5] == cell[7]) {
       return 1;

   }
   else if (cell[1] != \'1\' && cell[2] != \'2\' && cell[3] != \'3\'
           && cell[4] != \'4\' && cell[5] != \'5\' && cell[6] != \'6\'
           && cell[7] != \'7\' && cell[8] != \'8\' && cell[9] != \'9\') {
               return 0;
           }
   else {
       return -1;
   }
}
OUTPUT :
1) For a Tie Game Case :
   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
1 | 2 | 3
_____|_____|_____
| |   
4 | 5 | 6
_____|_____|_____
| |   
7 | 8 | 9
| |   
Player 1, Please enter the grid number to place the symbol :
7

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
1 | 2 | 3
_____|_____|_____
| |   
4 | 5 | 6
_____|_____|_____

| |   
X | 8 | 9
| |   
Player 2
Press Enter for the Computer Turn..!!

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | 2 | 3
_____|_____|_____
| |   
4 | 5 | 6
_____|_____|_____
| |   
X | 8 | 9
| |   
   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | 2 | 3
_____|_____|_____
| |   
4 | 5 | 6
_____|_____|_____
| |   
X | 8 | 9
| |   
Player 1, Please enter the grid number to place the symbol : 5

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   

O | 2 | 3
_____|_____|_____
| |   
4 | X | 6
_____|_____|_____
| |   
X | 8 | 9
| |   
Player 2
Press Enter for the Computer Turn..!!

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | 2 | O
_____|_____|_____
| |   
4 | X | 6
_____|_____|_____
| |   
X | 8 | 9
| |   
   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | 2 | O
_____|_____|_____
| |   
4 | X | 6
_____|_____|_____
| |   
X | 8 | 9
| |   
Player 1, Please enter the grid number to place the symbol : 2

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | X | O
_____|_____|_____
| |   
4 | X | 6
_____|_____|_____
| |   
X | 8 | 9
| |   
Player 2
Press Enter for the Computer Turn..!!

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | X | O
_____|_____|_____
| |   
4 | X | O
_____|_____|_____
| |   
X | 8 | 9
| |   
   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | X | O
_____|_____|_____
| |   
4 | X | O

_____|_____|_____
| |   
X | 8 | 9
| |   
Player 1, Please enter the grid number to place the symbol : 4

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | X | O
_____|_____|_____
| |   
X | X | O
_____|_____|_____
| |   
X | 8 | 9
| |   
Player 2
Press Enter for the Computer Turn..!!

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | X | O
_____|_____|_____
| |   
X | X | O
_____|_____|_____
| |   
X | O | 9
| |   
   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | X | O
_____|_____|_____
| |   
X | X | O
_____|_____|_____
| |   
X | O | 9
| |   
Player 1, Please enter the grid number to place the symbol : 9

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
O | X | O
_____|_____|_____
| |   
X | X | O
_____|_____|_____
| |   
X | O | X
| |   
OOPS..!!!!
The Game has been tie..!!
2) For Win Case :

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
1 | 2 | 3
_____|_____|_____
| |   
4 | 5 | 6
_____|_____|_____

| |   
7 | 8 | 9
| |   
Player 1, Please enter the grid number to place the symbol : 1

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
X | 2 | 3
_____|_____|_____
| |   
4 | 5 | 6
_____|_____|_____
| |   
7 | 8 | 9
| |   
Player 2
Press Enter for the Computer Turn..!!

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
X | 2 | 3
_____|_____|_____
| |   
4 | 5 | 6
_____|_____|_____
| |   
O | 8 | 9
| |   
   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   

X | 2 | 3
_____|_____|_____
| |   
4 | 5 | 6
_____|_____|_____
| |   
O | 8 | 9
| |   
Player 1, Please enter the grid number to place the symbol : 2

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
X | X | 3
_____|_____|_____
| |   
4 | 5 | 6
_____|_____|_____
| |   
O | 8 | 9
| |   
Player 2
Press Enter for the Computer Turn..!!

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
X | X | 3
_____|_____|_____
| |   
4 | O | 6
_____|_____|_____
| |   
O | 8 | 9

| |   
   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
X | X | 3
_____|_____|_____
| |   
4 | O | 6
_____|_____|_____
| |   
O | 8 | 9
| |   
Player 1, Please enter the grid number to place the symbol : 3

   Tic Tac Toe Game
Player 1 (X) - Player 2 (O)

| |   
X | X | X
_____|_____|_____
| |   
4 | O | 6
_____|_____|_____
| |   
O | 8 | 9
| |   
Congratulations!
Player 1 won the Game..!!
Hope this is helpful.
Tags