tic tac toe game
what's wrong in my program???
it compiles, but there is a bug.
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include <iostream>
#include <conio.h>
#include <cstdlib>
using namespace std;
int main () {
int cont = 1, cont2 = 0, cont3 = 0, row, col;
char board[3][3];
for (col = 0; col < 3; col++)
for (row = 0; row < 3; row++)
board[row][col] = ' '; // filling the cross with empty spaces
do {
cout << "Tic Tac Toe game\n\n";
if (cont == 1)
cout << "Player 1 (X):\n\n";
else
cout << "Player 2 (O):\n\n";
cout << " | | \n";
cout << " " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << " \n";
cout << " _____|_____|_____\n";
cout << " | | \n";
cout << " " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " \n";
cout << " _____|_____|_____\n";
cout << " | | \n";
cout << " " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " \n";
cout << " | | \n"; // cross draw
cout << "\nRow: ";
cin >> row;
cout << "Column: ";
cin >> col; // giving the coordinates
system("cls");
if (cont == 1)
board[row - 1][col - 1] = 'x';
else
board[row - 1][col - 1] = 'o'; // filling the cross with an X or O
for (col = 0; col < 3; col++)
for (row = 0; row < 3; row++)
if (board[row][col] == ' ')
cont2++; // see if it lacks space to play
for (row = 0; row < 3; row++) {
if (board[row][0] == 'x' && board[row][1] == 'x' && board[row][1] == 'x') {
cout << "Player 1 wins!";
cont3 = 1; // stop the loop
}
if (board[row][0] == 'o' && board[row][1] == 'o' && board[row][1] == 'o') {
cout << "Player 2 wins!";
cont3 = 1; // stop the loop
}
} // victory in row
for (col = 0; col < 3; col++) {
if (board[0][col] == 'x' && board[1][col] == 'x' && board[2][col] == 'x') {
cout << "Player 1 wins!";
cont3 = 1; // stop the loop
}
if (board[0][col] == 'o' && board[1][col] == 'o' && board[2][col] == 'o') {
cout << "Player 2 wins!";
cont3 = 1; // stop the loop
}
} // victory in column
if (board[0][0] == 'x' && board[1][1] == 'x' && board[2][2] == 'x') {
cout << "Player 1 wins!";
cont3 = 1; // stop the loop
}
if (board[0][0] == 'o' && board[1][1] == 'o' && board[2][2] == 'o') {
cout << "Player 2 wins!"; // victory in the diagonal
cont3 = 1; // stop the loop
}
if (board[2][0] == 'x' && board[1][1] == 'x' && board[2][0] == 'x') {
cout << "Player 1 wins!";
cont3 = 1;
}
if (board[2][0] == 'o' && board[1][1] == 'o' && board[2][0] == 'o') {
cout << "Player 2 wins!"; // victory in the diagonal
cont3 = 1;
}
if (cont == 1)
cont = 2;
else
cont = 1; // changing players
} while (cont2 != 0 && cont3 != 1);
if (cont2 == 0)
cout << "Draw!!!";
getch();
return EXIT_SUCCESS;
}
|
Last edited on
Could you be more specific?
The win check is wrong (in rows and diagonal you are checking the same cell twice)
Also, what are cont[\d]?
?
Last edited on
Topic archived. No new replies allowed.