Tic tac toe confusion

Pages: 1... 8910111213
Could you define what you mean by "they"?

You said without the X or O correct?

Am I comparing positions at this point (not the variables but the dimensions)
Last edited on
A tic tac toe game's board has 9 squares. "They" basically are the three squares you want to check so that you can declare the winner if the condition is fulfilled.

Can you write an if-statement to check if the three squares I am referring to are equal?
I'll certainly try.

 
if (ticTac[0][0]==ticTac[0][1] && ticTac[0][0]==ticTac[0][2] && ticTac[0][2]==ticTac[0][1])



Last edited on
if (ticTac[0][0]==ticTac[0][1] && ticTac[0][0]==ticTac[0][2] && ticTac[0][2]==ticTac[0][1])
Well, your if-statement is ok.
Oh, I see why it was unnecessary.



I assume I did everything else correctly.
Last edited on
If the three squares are all equal, they can be ("X"), or ("O"), or asterisks ("*"). But you only need to check one of them. Just one of them being a ("X") or ("O") and you will be able to find the winner.
That makes sense now, but wouldn't this be an issue with two players without an else statement?
> That makes sense now, but wouldn't this be an issue with two players without an else statement?
There is absolutely no else statement in the declareWinner() function. It is just for the sake of being as simple as possible.

Anyway, can you use all what I have said to make a complete if-statement yourself?
1
2
3
if (ticTac[0][0]==ticTac[0][1] && ticTac[0][0]==ticTac[0][2])
{
}


I assume you mean this by complete.

I'm very interested in understanding how we can use this for player 1 (they're X for this homework) and player 2 (they're O for this homework).

Last edited on
if (ticTac[0][0]==ticTac[0][1] && ticTac[0][0]==ticTac[0][2])
No, that is still not complete.

Let me repeat : This if-statement only checks if the three squares are equal generally, but you still do not know whether they can actually be ("X"), or ("O"), or asterisk ("*"). You cannot let this if-statement pass if they are ("*"), because the winner can't be an asterisk. Thus an additional checking is required.
 
if (ticTac[0][0]==ticTac[0][1] && ticTac[0][0]==ticTac[0][2] && ticTac!="*")


Hopefully I'm getting there. If not, I'll keep thinking.
if (ticTac[0][0]==ticTac[0][1] && ticTac[0][0]==ticTac[0][2] && ticTac[0][0]!="*")

> If the three squares are all equal, they can be ("X"), or ("O"), or asterisks ("*"). But you only need to check one of them. Just one of them being a ("X") or ("O") and you will be able to find the winner.

"One of them" mean you check one of the the three squares : ticTac[0][0], ticTac[0][1] or ticTac[0][2]

Do you get that?
Haha, I hope so.

What I got out of that was this

1
2
if(ticTac[0][0]==ticTac[0][1] && ticTac[0][0]==ticTac[0][2] && ticTac[0][0]!="*" && ticTac[0][0]=="X")
{ << "You've won player X." << endl;}


I'm curious why we only had to use ticTac[0][0]!="*" for the program to check to make sure it wasn't an *?

Last edited on
if(ticTac[0][0]==ticTac[0][1] && ticTac[0][0]==ticTac[0][2] && ticTac[0][0]!="*" && ticTac[0][0]=="X")

Ok, all done.
But how will you display proper output for this?
That ones a bit more tricky since it doesn't address if the user is player 1 or player 2.

Player 1 is supposed to be X, and player 2 is supposed to be O, so it's not as easy as writing out a cout statement.

> I'm curious why we only had to use ticTac[0][0] != "*"

This code is actually equivalent to :
(ticTac[0][0] == "X" || ticTac[0][0] == "O")

If a square is not a "*", it means it can only be a "X" or "O". Simple.
So I'm allowed to write an else statement now?
> It's not as easy as writing out a cout statement.
Just use everything you have got. Something like that.

1
2
3
4
5
6
7
8
if (ticTac[0][0]==ticTac[0][1] && ticTac[0][0]==ticTac[0][2] && ticTac[0][0]!="*")
{
    if(ticTac[0][0] == "X")
    cout_this << "X won!";
    if(ticTac[0][0] == "O")
    cout_this << "O won!";
    return true;
}


And there is no asterisk ("*"), because the three squares have been very strictly guaranteed that can't be asterisks.
> So I'm allowed to write an else statement now?

>> There is absolutely no else statement in the declareWinner() function. It is just for the sake of being as simple as possible.
That makes sense.

Your check one statement applied here as well.

Don't we need a return false statement? We've always used them.

If it's a tie, I assume I'd just do if ticTac=X && ticTac==O (with real code this is just pseudo)
Last edited on
Pages: 1... 8910111213