Tic tac toe confusion

Pages: 1... 910111213
> Don't we need a return false statement?
The first thing that you need to do is to check for all winning possibilities. After that, you may place a "return false" at the end of declareWinner() function. "return true" means a winner has been declared and vice-versa.
1
2
3
4
if(ticTac[0][0] == "X")
    cout_this << "X won!";
    if(ticTac[0][0] == "O")
    cout_this << "O won!";

Can you rewrite this piece of code in just a single cout statement?
You say all possibilities? Wouldn't that mean I'm going to be writing quite a bit of if statements or am I misinterpreting?

For each subscript combination, wouldn't I have to write an if-then statement as displayed previously?

(sorry your comment didn't load)

I never have before, so I'm going to say no.

I've always done it as you've shown for every program I've ever made.
Last edited on
So can you answer the question above?

You are actually learning how to check for a winning possibility the proper way.
The one about rewriting the code into a single cout statement?

Well,
1
2
3
4
if(ticTac[0][0] == "X")
    cout_this << "X won!";
    if(ticTac[0][0] == "O")
    cout_this << "O won!";


Makes sense to me as this is how I've always done it, but I can attempt to.

Why are we trying to, though? Is there something wrong with what I'm trying to do?

The cout statements just display who wins
> Why are we trying to, though?
It is a must, though the code is not wrong.
And the only thing I have to figure out is how to put the two cout_this together on one line without changing what's inside ()?
The goal : There are four statements in total in this piece of code. Remove all if-statements (two if-statements) and one cout statement, leaving only one cout statement.
(ticTac[0][0] == "X")

(ticTac[0][0] == "O")
cout_this << "O won!";

Well this is awfully an interesting experiment so far.
You give yourself another ten minutes. Let me know if you finally give up.

Note : The new piece of code must retain the same meaning the old piece of code has.
I have no idea why this is so hard to do.

One cout statement same function
1
2
3
4
5
6
7
8
9
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!";
    else
    cout_this << "O won!"; // (1)

     return true;
}


You must have to declare the player X or the player O as the winner soon or later. There is one way that allows you to do it in a single cout statement :

1
2
3
4
5
6
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"; // (2)

     return true;
}
Last edited on

Later? Well, alright.

What does this display exactly? ticTac[0][0]

How would that show what player won?

(when I say player I meant you put it congratulations player * * as if it will show)

(I can't currently test it on my computer thus why I'm asking)
Last edited on
A tic tac toe board's square may contain one of the three values : asterisk ("*"), X ("X") or ("O").

For example :
ticTac[1][0] = "O";
cout << "I am " << ticTac[1][0];

Output :
I am O 


In the case you mentioned, cout << ticTac[0][0] can output either "X" or "O". And don't worry, all the three squares are equal.

And there is no asterisk ("*"), because ticTac[0][0] has been very strictly guaranteed along with ticTac[0][1], and ticTac[0][2] that it can't be an asterisk.
Thanks for the example. I understand now.

1
2
3
4
5
6
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;
}


Congrats. You have successfully checked for a winning possibility.
> You say all possibilities?
Look at this, you will understand.

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 * *
So can you tell which one is better?
1
2
3
4
5
6
7
8
9
10
11
12
13
if (ticTac[0][0]=="X" && ticTac[0][1] == "X" && ticTac[0][2] == "X")
{
     cout << "Congratulations player X, you won!." << "\n\n";

     return true;
}

if (ticTac[0][0]=="O" && ticTac[0][1] == "O" && ticTac[0][2] == "O")
{
     cout << "Congratulations player O, you won!." << "\n\n";

     return true;
}


And :
1
2
3
4
5
6
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;
}
Last edited on
I apologize for falling asleep.

That makes sense, but something tells me you don't want me to write eight if-else statements.

(note I decided I'm going to write the eight if-then statement)
Last edited on
something tells me you don't want me to write eight if-else statements.

That's certainly one way to do it.

Consider this:
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
struct Combo
{   int row;
    int col;
};
    
bool check_winner (string tictac[3][3], string who)
{  const Combo combo[8][3] = 
   {  0,0, 0,1, 0,2,    //  Row 0
      1,0, 1,1, 1,2,    //  Row 1
      2,0, 2,1, 2,2,    //  Row 2
      0,0, 1,0, 2,0,    //  Col 0
      1,0, 1,1, 2,1,    //  Col 1
      2,0, 2,1, 2,2,    //  Col 2
      0,0, 1,1, 2,2,    //  LR Diagonal
      0,2, 1,1, 2,0,    //  RL Diagonal
    };
    
    for (int i=0; i<8; i++) //  Check each combo
    {   int count = 0;
        for (int j=0; j<3; j++)     // Check row and col
        {   if (tictac[combo[i][j].row][combo[i][j].col] == who)
                count++;  //  One cell matched
        }
        if (count == 3)
            return true;    //  All three cells matched
    }
    return false;   //  No combo's matched
}                
Pages: 1... 910111213