Tic tac toe confusion

Pages: 1... 10111213
closed account (E0p9LyTq)
@AbstractionAnon,

interesting solution, time to plunk it into my growing personal list of "Learn C++" snippets. :)
@AbstractionAnon
I can't really understand what you mean :)
@OP
Not much work to do. Just fill the gaps with if-statements.

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
bool declareWinner(string ticTac[][COLS])
{
     // If-statement 1
     if (ticTac[0][0]==ticTac[0][1] && ticTac[0][0] ==ticTac[0][2] && ticTac[0][0] != "*")
     {
          cout << "Congratulations player " << ticTac[0][0] << ", you won!." << "\n\n";

          return true;
     }

     // If-statement 2
     if (ticTac[1][0]==ticTac[1][1] && ticTac[1][0] ==ticTac[1][2] && ticTac[1][0] != "*")     
     {
          cout_this << "What" << "\n\n";

          return true;
     }

     // If-statement 3
     if (...)
     {
          return true;
     }

     // If-statement 4
     if (...)
     {
          return true;
     }

     // If-statement 5
     if (...)
     {
          return true;
     }

     // If-statement 6
     if (...)
     {
          return true;
     }

     // If-statement 7
     if (...)
     {
          return true;
     }

     // If-statement 8
     if (...)
     {
          return true;
     }

     return false;
}


This is a piece of cake. Even a baby will finish it within an hour :)
A good reference for you :

1.
X X X
* * *
* * *

2.
* * *
X X X
* * *

3.
* * *
* * *
X X X

4.
X * *
X * *
X * *

5.
* X *
* X *
* X *

6.
* * X
* * X
* * X

7.
X * *
* X *
* * X

8.
* * X
* X *
X * *
@AbstractionAnon
You are way better than me :)
Haha, I guess what he's doing must be different from around here?

Account, what's your advice if it's a tie and want it to end if it is?

(I'm going to fill the if-then statements out later you don't have to do any of them for me) : )



> What's your advice if it's a tie?

Just check your Tic tac toe's board and see if it is completely filled. If there is an asterisk in the board, it means the board is yet to be completely filled.

You handle the message yourself :)
1
2
3
4
5
6
7
8
9
10
11
12
// return true if the board is completely filled, and return false otherwise
bool isCompletelyFilled(string ticTac[][COLS])
{
    for (int i = 0; i < ROWS; i++)
     for (int j = 0; j < COLS; j++)
    {
        if(ticTac[i][j] == "*") return ???;
    }

    cout_this << "What?";
    return true;
}
Can you explain how the loop determines a tie?

And why are the two functions bools? Why not voids?
If the function isCompletelyFilled() returns true, it means the game is a tie (or the draw)

You use the return value from the function isCompletelyFilled() to terminate the game (if it returns true)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <iostream>

using namespace std;


const int ROWS=3;
const int COLS=3;

void printBoard(string ticTac[][COLS]);
void userInput(string ticTac[][COLS], int positionRow, int positionCol);
void inputValidation (string ticTac[][COLS], int &positionRow, int &positionCol);
bool declareWinner (string ticTac[][COLS]);
bool isCompletelyFilled(string ticTac[][COLS]);



int main()
{

    int positionRow=0;
    int positionCol=0;


    string ticTac[ROWS][COLS]= {"*","*","*",
                                "*", "*","*",
                                "*","*","*"
                               };
    printBoard(ticTac);
    userInput(ticTac, positionRow, positionCol);
    declareWinner (ticTac);
    isCompletelyFilled(ticTac);




    return 0;
}

void printBoard(string ticTac[][COLS])
{
    for (int i = 0; i < ROWS; i++)
    {
        cout << '\t';
        for (int j = 0; j < COLS; j++)
        {
            cout << ticTac[i][j] << ' ';
        }
        cout << endl;

    }


}

void userInput(string ticTac[][COLS], int positionRow, int positionCol)
{

    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    inputValidation(ticTac, positionRow, positionCol);
    ticTac[positionRow][positionCol] = "X";
    printBoard(ticTac);


    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;



    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "O";

    printBoard(ticTac);


    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "X";


    printBoard(ticTac);


    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "O";

    printBoard(ticTac);

    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "X";

    printBoard(ticTac);

    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "O";

    printBoard(ticTac);
}

void inputValidation (string ticTac[][COLS], int &positionRow, int &positionCol)
{
    while(positionRow >= 0 && positionRow < ROWS
            && positionCol >= 0 && positionCol < COLS
            && (ticTac[positionRow][positionCol] == "X" || ticTac[positionRow][positionCol] == "O"))
    {
        cout << "That position has either already been taken or you entered an invalid row or column! Please enter another row and column: " << endl;
        cin >> positionRow >> positionCol;
    }


}


bool declareWinner(string ticTac[][COLS])
{
    // If-statement 1
    if (ticTac[0][0]==ticTac[0][1] && ticTac[0][0] ==ticTac[0][2] && ticTac[0][0] != "*")
    {
        cout << "Congratulations player " << ticTac[0][0] << ", you won!." << "\n\n" << endl;

        return true;
    }

    // If-statement 2
    if (ticTac[1][0]==ticTac[1][1] && ticTac[1][0] ==ticTac[1][2] && ticTac[1][0] != "*")
    {
        cout << "Congratulations player " << ticTac[0][0] << ", you won!." << "\n\n" << endl;


             return true;
    }

    // If-statement 3
    if (ticTac[2][0]==ticTac[2][1] && ticTac[2][0]==ticTac[2][2] && ticTac[2][0] != "*")
    {
        cout << "Congratulations player " << ticTac[0][0] << ", you won!." << "\n\n" << endl;
             return true;
    }

    // If-statement 4
    if (ticTac[0][0]==ticTac[1][0] && ticTac[0][0]==ticTac[2][0] && ticTac[0][0] != "*")
    {
        cout << "Congratulations player " << ticTac[0][0] << ", you won!." << "\n\n" << endl;
             return true;
    }

    // If-statement 5
    if (ticTac[0][1]==ticTac[1][1] && ticTac[0][1]==ticTac[2][1] && ticTac[0][1] != "*")
    {
        cout << "Congratulations player " << ticTac[0][0] << ", you won!." << "\n\n" << endl;
             return true;
    }

    // If-statement 6
    if (ticTac[0][2]==ticTac[1][2] && ticTac[0][2]==ticTac[2][2] && ticTac[0][2]!="*")

        {
            cout << "Congratulations player " << ticTac[0][0] << ", you won!." << "\n\n" << endl;
                 return true;
        }

    // If-statement 7
    if (ticTac[0][0]==ticTac[1][1] && ticTac[0][0]==ticTac[2][2] && ticTac[0][0] != "*")
    {
        cout << "Congratulations player " << ticTac[0][0] << ", you won!." << "\n\n" << endl;
             return true;
    }

    // If-statement 8
    if (ticTac[0][2]==ticTac[1][1] && ticTac[0][2]==ticTac[2][0] && ticTac[0][2] != "*")
    {
        cout << "Congratulations player " << ticTac[0][0] << ", you won!." << "\n\n" << endl;
             return true;
    }

    return false;
}

bool isCompletelyFilled(string ticTac[][COLS])
{
    for (int i = 0; i < ROWS; i++)
     for (int j = 0; j < COLS; j++)
    {
        if(ticTac[i][j] == "*") return false;
    }

    cout << "It's a tie!" << endl;
    return true;
}



I think I did everything right!

Though, I'm still a bit confused while we're using bools and not voids.

In my class we use functions with return statements, but we return them to a variable to use in another function. Why not just pass the array by value, make it a void, and use if-then statements without the return or bool to find the same value since we're not returning the bool value to another variable?

If we're turning a value as true or false, I believe my professor will then expect me to use the value in main.

Then again, I'm just a noob, so feel free to educate me on why I can't do this (my class isn't very good at explaining why you can't do other things).

I apply the same question to the isTie function. Why not just use a void since the value isn't going to be used anywhere else in the code?

It's so weird to me using a bool without assigning the return value to another variable and then using it in main (which is what we usually do haha).

Last edited on
1
2
3
4
5
if (ticTac[1][0]==ticTac[1][1] && ticTac[1][0] ==ticTac[1][2] && ticTac[1][0] != "*")
    {
        cout << "Congratulations player " << ticTac[1][0] << ", you won!." << "\n\n" << endl; // Same!
             return true;
    }

So fix all your remaining mistakes please.
Last edited on
Problems with your if statements (2,3,5,6,8). You're displaying tictac[0][0] as the winner. You have no guarantee that the cells you're checking in the if statement are the same player as tictac[0][0] because the if statement does not check tictac[0][0].

I believe this is what closed account was trying to point out.

I'm still a bit confused while we're using bools and not voids.

Returning a bool allows you to make a processing decision in the calling code. I notice that you're not calling declareWinner anywhere. You want to modify userInput() to call declareWinner() after each move. If declareWinner returns true, you DO NOT want to continue with further moves in userInput(). So after each printBoard() in userInput(), you should call declareWinner().
1
2
3
4
5
6
7
...
    inputValidation(ticTac, positionRow, positionCol);
    ticTac[positionRow][positionCol] = "X";
    printBoard(ticTac);
    if (declareWinner (ticTac))    // Test the result of declareWinner
      return;  // no point in continuing, we have a winner
...


Now isCompletelyFilled() is a different matter. Because of the linear nature of your logic in userInput(), you really don't need this function. If you get to the 9th move and do not have a winner, then it can only be a tie.

It's so weird to me using a bool without assigning the return value to another variable

You don't necessarily need to assign a bool return value to another variable. Bool functions are handy to use in conditional statements as I've just shown.





Last edited on
@abstract

Thanks for teaching me that about bools! We didn't learn that in our class. We simply learned that it can pass values back to variables and then that variable can be used to resemble the return value.

I did what was asked of me, but I was uncomfortable with the fact you could input values besides 0, 1, and 2, so I made a while loop, but it's not outputting correctly.

Even if I press 0 and 1 for example it still says you have to enter a value between 0 and 1.

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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <iostream>

using namespace std;


const int ROWS=3;
const int COLS=3;

void printBoard(string ticTac[][COLS]);
void userInput(string ticTac[][COLS], int positionRow, int positionCol);
void inputValidation (string ticTac[][COLS], int &positionRow, int &positionCol);
bool declareWinner (string ticTac[][COLS]);





int main()
{

    int positionRow=0;
    int positionCol=0;


    string ticTac[ROWS][COLS]= {"*","*","*",
                                "*", "*","*",
                                "*","*","*"
                               };
    printBoard(ticTac);
    userInput(ticTac, positionRow, positionCol);
    declareWinner (ticTac);




    return 0;
}

void printBoard(string ticTac[][COLS])
{
    for (int i = 0; i < ROWS; i++)
    {
        cout << '\t';
        for (int j = 0; j < COLS; j++)
        {
            cout << ticTac[i][j] << ' ';
        }
        cout << endl;

    }


}

void userInput(string ticTac[][COLS], int positionRow, int positionCol)
{

    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;
    while (positionRow!=0 || positionRow!=1 || positionRow!=2 || positionCol!=0 || positionCol!=1 || positionCol!=2)
    {
        cout << "You must enter values between 0 and 2." << endl;
        cout << "Enter row #: ";
        cin >> positionRow;
        cout << "Enter column #: ";
        cin >> positionCol;

    }
    inputValidation(ticTac, positionRow, positionCol);
    ticTac[positionRow][positionCol] = "X";
    printBoard(ticTac);
    if (declareWinner (ticTac))
        return;


    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;
    while (positionRow!=0 || positionRow!=1 || positionRow!=2 || positionCol!=0 || positionCol!=1 || positionCol!=2)
    {
        cout << "You must enter values between 0 and 2." << endl;
        cout << "Enter row #: ";
        cin >> positionRow;
        cout << "Enter column #: ";
        cin >> positionCol;

    }
    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "O";

    if (declareWinner (ticTac))
        return;



    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;
    while (positionRow!=0 || positionRow!=1 || positionRow!=2 || positionCol!=0 || positionCol!=1 || positionCol!=2)
    {
        cout << "You must enter values between 0 and 2." << endl;
        cout << "Enter row #: ";
        cin >> positionRow;
        cout << "Enter column #: ";
        cin >> positionCol;

    }

    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "X";


    printBoard(ticTac);


    if (declareWinner (ticTac))
        return;


    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;
    while (positionRow!=0 || positionRow!=1 || positionRow!=2 || positionCol!=0 || positionCol!=1 || positionCol!=2)
    {
        cout << "You must enter values between 0 and 2." << endl;
        cout << "Enter row #: ";
        cin >> positionRow;
        cout << "Enter column #: ";
        cin >> positionCol;

    }
    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "O";

    if (declareWinner (ticTac))
        return;




    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;
    while (positionRow!=0 || positionRow!=1 || positionRow!=2 || positionCol!=0 || positionCol!=1 || positionCol!=2)
    {
        cout << "You must enter values between 0 and 2." << endl;
        cout << "Enter row #: ";
        cin >> positionRow;
        cout << "Enter column #: ";
        cin >> positionCol;

    }

    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "X";

    printBoard(ticTac);


    if (declareWinner (ticTac))
        return;

    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;
    while (positionRow!=0 || positionRow!=1 || positionRow!=2 || positionCol!=0 || positionCol!=1 || positionCol!=2)
    {
        cout << "You must enter values between 0 and 2." << endl;
        cout << "Enter row #: ";
        cin >> positionRow;
        cout << "Enter column #: ";
        cin >> positionCol;

    }
    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "O";

    printBoard(ticTac);


    if (declareWinner (ticTac))
        return;
    else
        cout << "It's a tie." << endl;
}

void inputValidation (string ticTac[][COLS], int &positionRow, int &positionCol)
{
    while(positionRow >= 0 && positionRow < ROWS
            && positionCol >= 0 && positionCol < COLS
            && (ticTac[positionRow][positionCol] == "X" || ticTac[positionRow][positionCol] == "O"))
    {
        cout << "That position has either already been taken or you entered an invalid row or column! Please enter another row and column: " << endl;
        cin >> positionRow >> positionCol;
    }


}


bool declareWinner(string ticTac[][COLS])
{
    // If-statement 1
    if (ticTac[0][0]==ticTac[0][1] && ticTac[0][0] ==ticTac[0][2] && ticTac[0][0] != "*")
    {
        cout << "Congratulations player " << ticTac[0][0] << ", you won!." << "\n\n" << endl;

        return true;
    }

    // If-statement 2
    if (ticTac[1][0]==ticTac[1][1] && ticTac[1][0] ==ticTac[1][2] && ticTac[1][0] != "*")
    {
        cout << "Congratulations player " << ticTac[1][0] << ", you won!." << "\n\n" << endl;


        return true;
    }

    // If-statement 3
    if (ticTac[2][0]==ticTac[2][1] && ticTac[2][0]==ticTac[2][2] && ticTac[2][0] != "*")
    {
        cout << "Congratulations player " << ticTac[2][0] << ", you won!." << "\n\n" << endl;
        return true;
    }

    // If-statement 4
    if (ticTac[0][0]==ticTac[1][0] && ticTac[0][0]==ticTac[2][0] && ticTac[0][0] != "*")
    {
        cout << "Congratulations player " << ticTac[0][0] << ", you won!." << "\n\n" << endl;
        return true;
    }

    // If-statement 5
    if (ticTac[0][1]==ticTac[1][1] && ticTac[0][1]==ticTac[2][1] && ticTac[0][1] != "*")
    {
        cout << "Congratulations player " << ticTac[0][1] << ", you won!." << "\n\n" << endl;
        return true;
    }

    // If-statement 6
    if (ticTac[0][2]==ticTac[1][2] && ticTac[0][2]==ticTac[2][2] && ticTac[0][2]!="*")

    {
        cout << "Congratulations player " << ticTac[0][2] << ", you won!." << "\n\n" << endl;
        return true;
    }

    // If-statement 7
    if (ticTac[0][0]==ticTac[1][1] && ticTac[0][0]==ticTac[2][2] && ticTac[0][0] != "*")
    {
        cout << "Congratulations player " << ticTac[0][0] << ", you won!." << "\n\n" << endl;
        return true;
    }

    // If-statement 8
    if (ticTac[0][2]==ticTac[1][1] && ticTac[0][2]==ticTac[2][0] && ticTac[0][2] != "*")
    {
        cout << "Congratulations player " << ticTac[0][2] << ", you won!." << "\n\n" << endl;
        return true;
    }

    return false;
}


while (positionRow!=0 || positionRow!=1 || positionRow!=2 || positionCol!=0 || positionCol!=1 || positionCol!=2)

==>
while (!(positionRow >= 0 && positionRow <= 2) || !(positionCol >= 0 && positionCol <= 2))
Does that help? :)
Last edited on
Yep! Man, this was such a long thread.

1
2
3
4
5
6
7
8
9
10
11
12
 // If-statement 8
    if (ticTac[0][2]==ticTac[1][1] && ticTac[0][2]==ticTac[2][0] && ticTac[0][2] != "*")
    {
        cout << "Congratulations player " << ticTac[0][2] << ", you won!." << "\n\n" << endl;
        return true;
    }
    else
        cout << "It's a tie" << endl;

    return false;
}


It's safe to put it's going to be a tie right?
Last edited on
1
2
3
4
5
6
7
8
// If-statement 8
if (ticTac[0][2]==ticTac[1][1] && ticTac[0][2]==ticTac[2][0] && ticTac[0][2] != "*")
    {
        cout << "Congratulations player " << ticTac[0][2] << ", you won!." << "\n\n" << endl;
        return true;
    }
    else        cout << "It's a tie" << endl;
    return false;
Just a little note, I somehow accidentally reported your comment.

So, to the mods, I didn't mean to report him. He's been lovely.

Closed account, how do I announce it's a tie in my code?
> How do I announce it's a tie in my code?
You already did it earlier in your code, at the end of the function userInput().

1
2
3
4
5
6
if (declareWinner (ticTac))
        return;
   else
  cout << "It's a tie." << endl;
}
> I somehow accidentally reported your comment.
Don't worry. It is nothing :)
Last edited on
Pages: 1... 10111213