Tic Tac Toe Program does work, but....

Hello Everyone,
My daughter and I are about 4 weeks into C++. I have a tic tac toe program that I just finished and it works quite well. I am curious if there is a way to delete the board that is displayed and reprint based on the user input; if you run the program you will see what I mean as the new board prints below the prior. Also, any suggestion on coding more efficiently would be appreciated.

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
 #include <iostream>
#include <iomanip>
#include <string>

using namespace std;
string board[3][3] = {" ", " ", " ", " ", " ", " ", " ", " ", " "};
bool is_win = false;


void print_board()
{
    cout << "-----------------------\n";
    cout << "| 1" << setw(7) << "| 2" << setw(8) << "| 3" << setw(6) << "|" "\n";
    cout << "|" << setw(7) << "|" << setw(8) << "|" << setw(8) << "|" "\n";
   cout << "|" << setw(3) << board [0][0] << setw(4) <<"|" << setw(4) << board [0][1] << setw(4) << "|" << setw(4)<< board [0][2] << setw(4) << "|" "\n";
    cout << "|" << setw(7) << "|" << setw(8) << "|" << setw(8) << "|" "\n";
    cout << "-----------------------\n";
    cout << "| 4" << setw(7) << "| 5" << setw(8) << "| 6" << setw(6) << "|" "\n";
    cout << "|" << setw(7) << "|" << setw(8) << "|" << setw(8) << "|" "\n";
    cout << "|" << setw(3) << board [1][0] << setw(4) <<"|" << setw(4) << board [1][1] << setw(4) << "|" << setw(4)<< board [1][2] << setw(4) << "|" "\n";
    cout << "|" << setw(7) << "|" << setw(8) << "|" << setw(8) << "|" "\n";
    cout << "-----------------------\n";
   cout << "| 7" << setw(7) << "| 8" << setw(8) << "| 9" << setw(6) << "|" "\n";
   cout << "|" << setw(7) << "|" << setw(8) << "|" << setw(8) << "|" "\n";
    cout << "|" << setw(3) << board [2][0] << setw(4) <<"|" << setw(4) << board [2][1] << setw(4) << "|" << setw(4)<< board [2][2] << setw(4) << "|" "\n";
    cout << "|" << setw(7) << "|" << setw(8) << "|" << setw(8) << "|" "\n";
    cout << "-----------------------\n";

}

void select_grid (int a, string b)
{

    switch (a)
    {
    case 1:
        board [0][0] = b;
        break;
    case 2:
        board [0][1] = b;
        break;
    case 3:
        board [0][2] = b;
        break;
    case 4:
        board [1][0] = b;
        break;
    case 5:
        board [1][1] = b;
        break;
    case 6:
        board [1][2] = b;
        break;
    case 7:
        board [2][0] = b;
        break;
    case 8:
        board [2][1] = b;
        break;
    case 9:
        board [2][2] = b;
        break;

    }

}

void win_condition ()
{

    // player x wins across
    if ( board[0][0] == "x" && board[0][1] == "x" && board[0][2] == "x")
    {
        is_win = true;
        cout << "Tic Tac Toe, three in a row.  Player X wins the game!";
    }
    else if ( board[1][0] == "x" && board [1][1] == "x" && board [1][2] == "x")
    {
        is_win = true;
        cout << "Tic Tac Toe, three in a row.  Player X wins the game!";
    }
    else if ( board[2][0] == "x" && board [2][1] == "x" && board [2][2] == "x")
    {
        is_win = true;
        cout << "Tic Tac Toe, three in a row.  Player X wins the game!";
    }

    // payer O wins across
     else if ( board[0][0] == "o" && board[0][1] == "o" && board[0][2] == "o")
    {
        is_win = true;
        cout << "Tic Tac Toe, three in a row.  Player O wins the game!";
    }
    else if ( board[1][0] == "o" && board [1][1] == "o" && board [1][2] == "o")
    {
        is_win = true;
        cout << "Tic Tac Toe, three in a row.  Player O wins the game!";
    }
    else if ( board[2][0] == "o" && board [2][1] == "o" && board [2][2] == "o")
    {
        is_win = true;
        cout << "Tic Tac Toe, three in a row.  Player O wins the game!";
    }

    // player X wins down
    else if ( board[0][0] == "x" && board[1][0] == "x" && board[2][0] == "x")
    {
        is_win = true;
        cout << "Tic Tac Toe, three in a row.  Player X wins the game!";
    }
    else if ( board[0][1] == "x" && board [1][1] == "x" && board [2][1] == "x")
    {
        is_win = true;
        cout << "Tic Tac Toe, three in a row.  Player X wins the game!";
    }
    else if ( board[0][2] == "x" && board [1][2] == "x" && board [2][2] == "x")
    {
        is_win = true;
        cout << "Tic Tac Toe, three in a row.  Player X wins the game!";
    }

    // player O wins down
    else if ( board[0][0] == "o" && board[1][0] == "o" && board[2][0] == "o")
    {
        is_win = true;
        cout << "Tic Tac Toe, three in a row.  Player O wins the game!";
    }
    else if ( board[0][1] == "o" && board [1][1] == "o" && board [2][1] == "o")
    {
        is_win = true;
        cout << "Tic Tac Toe, three in a row.  Player O wins the game!";
    }
    else if ( board[0][2] == "o" && board [1][2] == "o" && board [2][2] == "o")
    {
        is_win = true;
        cout << "Tic Tac Toe, three in a row.  Player O wins the game!";
    }
     // player X wins diagonally
     else if ( board[0][0] == "x" && board[1][1] == "x" && board[2][2] == "x")
    {
        is_win = true;
        cout << "Tic Tac Toe, three in a row.  Player X wins the game!";
    }
    else if ( board[0][2] == "x" && board [1][1] == "x" && board [2][0] == "x")
    {
        is_win = true;
        cout << "Tic Tac Toe, three in a row.  Player X wins the game!";
    }

    // player O wins diagonally
    else if ( board[0][0] == "o" && board[1][1] == "o" && board[2][2] == "o")
    {
        is_win = true;
        cout << "Tic Tac Toe, three in a row.  Player O wins the game!";
    }
    else if ( board[0][2] == "o" && board [1][1] == "o" && board [2][0] == "o")
    {
        is_win = true;
        cout << "Tic Tac Toe, three in a row.  Player O wins the game!";
    }

}


int main()
{
    int a;
    string b;

    cout << "\t\t***Lets play Tic Tac Toe***\n";
    cout << "\n\n";
    cout<< "Here is how we play:\n";
    cout << "\n";
    cout << "On your turn, enter the number of the box where you want to go\n";
    cout << "and then enter whether you are placing an 'X' or an 'O'\n";
    cout << "\n\n";
    print_board();
    cout << "\n\n";
    for (int i = 0; i < 9; i++)
    {

        while (is_win != true)
        {
        cout << "Enter the number of the box you want to play: ";
        cin >> a;
        cout << "Enter if you are 'X' or 'O': ";
        cin >> b;
        select_grid(a, b);
        cout << "\n\n";
        print_board();
        win_condition();
        }
    }
    cout << "\n";
    cout << "Game over!\n";
    cout << "\n";
}
Here is a good article on this site about clearing the console screen:
http://www.cplusplus.com/articles/4z18T05o/

Glancing through, efficiency-wise, it looks good, better than most that I have seen. With simple console programs like this, you are not going to need to worry about efficiency, anyway. But I do understand that once they get more advanced, it is nice to already know how to make the efficient code.
More suggestion, if you use more loop or calculating.
There'll be something you would say WOW.

i.e., calculate something,
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
void select_grid (int a, string b)
{

    switch (a)
    {
    case 1:
        board [0][0] = b;
        break;
    case 2:
        board [0][1] = b;
        break;
    case 3:
        board [0][2] = b;
        break;
    case 4:
        board [1][0] = b;
        break;
    case 5:
        board [1][1] = b;
        break;
    case 6:
        board [1][2] = b;
        break;
    case 7:
        board [2][0] = b;
        break;
    case 8:
        board [2][1] = b;
        break;
    case 9:
        board [2][2] = b;
        break;

    }

}

and
1
2
3
4
void select_grid (int a, string b)
{
	board[(a-1)/3][(a-1)%3] = b;
}
Last edited on
Thanks, that is much easier. I was not aware that was possible to do with an array. I wish the book would have told me that.
Quick Question for Lsk,

board[(a-1)/3][(a-1)%3] = b;

What is the "%" doing in the formula. I changed it to "/" and realized that it would not work for several of the array locations. Can you give a brief explanation of this syntax?

Thanks in advance for your help!
modulo operator, represented by a percentage sign (%), gives the remainder of a division of two values.
http://www.cplusplus.com/doc/tutorial/operators/

If we rewrite your line of code as follows it should be more clear:
1
2
3
4
  a--;  // Convert from 1 based to 0 based 
  int row = a/3;  
  int col = a%3;  
  board[row][col] = b;



Last edited on
% is the modulus operator. This will return the remainder after division. so 0/3 returns 0, 1/3 returns 1, and 2/3 returns 2.
Likewise, 3/3 returns 0, 4/3 returns 1, and 5/3 returns 2 ...


The '/' on the other hand just returns the actual division as it is cast into an integer.
1/3 = .33333, but once it is cast into an integer, it becomes 0
the same is true for 0/3 and 2/3.

3/3, 4/3 and 5/3 all equal 1 (or 1.??? but anything after the . gets drops since you are doing integer math)

then 6/3, 7/3, and 8/3 are all 3

You can enter numbers 1-9 into the function (as opposed to 0-8) because lsk has written the -1 into the equation.
Topic archived. No new replies allowed.