Tic tac toe confusion

Pages: 1... 7891011... 13
I am actually surprised after +160 posts, the OP haven't really advanced much...

How long will this thread continue forever?

The OP was not even aware of the free work others did for him. He has actually asked quite many questions on this forum. Maybe he is not studying well and not paying attention in class, he is just relying on this forum too much that's why he can hardly improve himself.

So what do you think, OP?
It's not that, I just don't want to take the code given to me because it's literally being written up for me.

The entire bool function was done by you. I know It's been hard working with me, so I assume people did so for a last resort, but I can only use so much.

I can take endless ideas, but not that much code. No offense to you.

If I use everything offered to me in this thread and just put it in my code, then I could really get in trouble for cheating.

That was a lot of code you guys wrote up. I try to be careful since my professor knows I use this site. He doesn't mind if you help with small bits of code like if-then statements and loops, but I personally believe that was too much. I still thanked everyone.

I continued asking questions because I did it in a way without the bool, but others here did it with the bool, and they did it without me present. It was just straight up written up for me.

Plus, I've posted countless programs here that I've done myself for verification, so I think I'm learning fine. This one is just difficult.

I also said I'd need help with the whole program. I have no idea how to do any of this, but I've turned in the last four without anyone's help. It should be noted for every two to three programs I need help with here I already turned in many on my own.

I can try to write the if-then statements myself, but there are generally errors I've never seen. As I've said, this is the FIRST program of this kind we've done before concerning arrays.

It can't go both ways. I can't accept your help and not understand it and just put it in my program. I rather ask questions about what's going on or about what I did wrong when I don't understand it. That's the only way I'll learn so when faced with a similar program of this type in the future, I can do more of it on my own.

Would you rather me accept the free code and get this over with quickly or slowly move with me why I ask a bunch of stupid questions?

To me, going the longer way works better for improvement. Taking people's code without understanding it won't help you improve.

It's more productive to ask questions--even if they seem obvious--and figure it out slowly, so when it's over, you understand everything you've just done and don't make the same mistakes.
Last edited on
Show me your full code and give us more details of the last part you are working on.
I mentioned an if-then statement, Ars approved, so I've been trying to figure out how to construct how.

However, he also said to remove the position variables (when I passed them) so I'm not quite sure how to do it with the array without writing a ridiculously long && || expression that compares all possible combinations.

I highly doubt that's what is supposed to be done, but I don't see any other way.

Technically, that'd be 3x3x3 (if my math is correct) expression I'd have to write in that if-then statement which doesn't seem very logical.

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
#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);
void 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;

    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;
    }


}

void declareWinner (string ticTac[][COLS])
{
    if {}

}
Last edited on
Can you tell us what function you are trying to perfect?
Is it the declareWinner() function?
Yes! I think it's the last part of the code that needs to be finished, but perhaps I'm wrong.

I've mainly been listening to Ars since he offers good advice.

(You do too so I'm sure you'll comment)
> I think it's the last part of the code that needs to be finished, but perhaps I'm wrong.
So you are indecisive.
If you aren't clear, we can't help you.
I said yes.

What I was implying is I'm not sure if it's the only part of the code left to focus on (for example Ars might recommend to make another function).

I was saying I believe it's the last part of the code that has to be finished. I don't think anymore functions have to be added after that.

If you're still confused, I'm doing if-then statements.
Last edited on
So what can declareWinner() possibly do?

Are you using if-statements to check something like this?
* X *
* X *
* X *
==> Win!

Or :
O * *
* O *
* * O
==> Win!
Yes! I'm pretty sure it will have something to do with making the characters yourself or am I wrong?
Last edited on
Just say "yes" if you agree completely with what I say.
Alright
Are you prepared?
Is two dimensional array concept still a challenge to you?
Do you understand all this?
* * *
* * *
* * *
1
2
3
ticTac[0][0] = "$"; // Modify a ticTac element at the position row 0, column 0
ticTac[0][1] = "$"; // Modify a ticTac element at the position row 0, column 1
ticTac[0][2] = "$"; // Modify a ticTac element at the position row 0, column 2 


==>
$ $ $
* * *
* * *

Ok, let's play again :
* * *
* * *
* * *
1
2
3
ticTac[1][0] = "$"; // Modify a ticTac element at the position row 1, column 0
ticTac[1][1] = "$"; // Modify a ticTac element at the position row 1, column 1
ticTac[1][2] = "$"; // Modify a ticTac element at the position row 1, column 2 


==>
* * *
$ $ $
* * *

Ok, let's play again :
* * *
* * *
* * *
1
2
3
ticTac[2][0] = "$"; // Modify a ticTac element at the position row 2, column 0
ticTac[2][1] = "$"; // Modify a ticTac element at the position row 2, column 1
ticTac[2][2] = "$"; // Modify a ticTac element at the position row 2, column 2 


==>
* * *
* * *
$ $ $
Ok, now you are a player.
* * *
* * *
* * *
ticTac[0][2] = "€";
ticTac[1][1] = "€";
ticTac[2][0] = "€";

Can you display the board after these things are done? Use your brain, do not use your computer for help.
Yes, I understood.

**E
*E*
E**
Ok, good.

But firstly, before we go further, declareWinner() must have a bool return type. So that it can return true if a winner is declared or return false if there is no major event happenning in the game.
So assume a player has managed to do something like this :
X X X
* * *
* * *
The player will definitely win. But can you write an if-statement to prove this is true?
I can think of one way.

1
2
3
4
5
6
7
8
9
10
if (ticTac[0][0]=="X" && ticTac[0][1]=="X" && ticTac[0][2]=="X")
cout << "Congratulations player X, you won!." << endl;

return true;

else

cout << "You did not win player X." << endl.
return false
Last edited on
1
2
3
4
5
if (ticTac[0][0]=="X" && ticTac[0][1]=="X" && ticTac[0][2]=="X")
cout << "Congratulations player X, you won!." << endl;
return true;
else cout << "You did not win player X." << endl.
return false;

I didn't ask you to write an else statement.

==>
1
2
3
4
5
if (ticTac[0][0]=="X" && ticTac[0][1]=="X" && ticTac[0][2]=="X")
{
    cout << "Congratulations player X, you won!." << endl;
    return true;
}

"Missing braces {}" logic error.
So assume one of the two players has managed to do something like this :
X X X
* * *
* * *

Or :
O O O
* * *
* * *

The player will definitely win. But can you write a single if-statement instead of having to write two statements to prove this is true?

Hint : You check if they all equal to each other (without knowing if they are ("X") or ("O"). If they are all equal, good. After that you just check if one of them is ("X") or ("O") and not asterisk ("*").
Pages: 1... 7891011... 13