Please follow the data 1) For Line 23 In the IF - Condition yo.pdf
35 views
25 slides
Apr 09, 2023
Slide 1 of 25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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...
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 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 << \"\ \ \\tTic 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[])// check.
Size: 41.08 KB
Language: en
Added: Apr 09, 2023
Slides: 25 pages
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);
| |
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;