simple tic tac toe game

I'm with a problem with this game. It compiles perfectly, it runs fine. The problem is only that I don't know how to forbid the player of play in a space that was already played changing the X or the O by an O or an X.

Does somebody knows the solution to this logic problem?

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream> // cout and cin library
#include <conio.h> // getch(); library
#include <cstdlib> // system("cls") library

using namespace std; // necessary when you use <iostream>

int main () {
    int cont = 1; // player one or two
    int cont2; // draw case
    int cont3 = 0; // victory case
    int row, col; // row and column
    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";
    
    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"; // drawing of the cross
    
    if (cont == 1)
       cout << "\nPlayer 1 (X):\n";
       else
           cout << "\nPlayer 2 (O):\n"; // selecting the player
    
    cout << "\nRow: ";
    cin >> row;
    cout << "Column: ";
    cin >> col; // giving the coordinates
     
    if (cont == 1)
       board[row - 1][col - 1] = 'x';
       else
           board[row - 1][col - 1] = 'o'; // filling the cross
           
    cont2 = 0; // there's empty spaces in the cross
    
    for (col = 0; col < 3; col++)
        for (row = 0; row < 3; row++)
            if (board[row][col] == ' ')
               cont2++;      // checking if there are spaces in the cross, if no, stop the game
    
    for (row = 0; row < 3; row++) {
        if (board[row][0] == 'x' && board[row][1] == 'x' && board[row][2] == 'x') {
           cout << "Player 1 wins!\n";
           cont3 = 1; // stop the game
        }
        if (board[row][0] == 'o' && board[row][1] == 'o' && board[row][2] == 'o') {
           cout << "Player 2 wins!\n";
           cont3 = 1; // stop the game
        }
    } // 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!\n";
           cont3 = 1; // stop the game
        }
        if (board[0][col] == 'o' && board[1][col] == 'o' && board[2][col] == 'o') {
           cout << "Player 2 wins!\n";
           cont3 = 1; // stop the game
        }
    } // victory in column
    
    if (board[0][0] == 'x' && board[1][1] == 'x' && board[2][2] == 'x') {
       cout << "Player 1 wins!\n";
       cont3 = 1; // stop the game
    }
    if (board[0][0] == 'o' && board[1][1] == 'o' && board[2][2] == 'o') {
       cout << "Player 2 wins!\n";                                                   // victory in the diagonal
       cont3 = 1; // stop the game
    }

    if (board[2][0] == 'x' && board[1][1] == 'x' && board[0][2] == 'x') {
       cout << "Player 1 wins!\n";
       cont3 = 1; // stop the game
    }
    if (board[2][0] == 'o' && board[1][1] == 'o' && board[0][2] == 'o') {
       cout << "Player 2 wins!\n";                                                  // victory in the diagonal
       cont3 = 1; // stop the game
    }    
    
       
    if (cont == 1)
       cont = 2;
       else
           cont = 1; // changing players
           
    system("cls"); // cleaning the screen
           
    } while (cont2 != 0 && cont3 != 1);
    
    if (cont2 == 0 && cont3 != 1)
       cout << "Draw!!!"; // draw check
    
    cout << "\n      |     |     \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"; // drawing od the cross. only to the user see.
    
    getch(); // pause the screen
    return EXIT_SUCCESS;
}
Last edited on
up!
Topic archived. No new replies allowed.