switch or ifs

I am trying to check wheather certain combinatins of characters occur in an array can I use a switch or do I have to use multiple ifs and if/elses
Personal preference.

If I'm using lots and lots of if statements, it starts to look a bit ugly. I tend to use switch statements in those situations.

For smaller checks, I always use if.
this array has 9 elements and I am looking for combinations of three there are sixteen possible combinations I need to find if and when they occur so a switch would be better but I need to know how to start this switch and if there is an even easier way
What are you doing, Tic-Tac-Toe?
yes it is
its tic-tac-toe and Im splitting it up into several functions the one I am asking about checks wheather the game the game has ended or not and acts accordingly
You probably won't need 16 checks then.

There are 16 win conditions (8 per player) but the same check can be used for each.

Say that your array of 9 represents the board like this:

[0][1][2]
[3][4][5]
[6][7][8]


An example of a winning row is if 0,1 and 2 are all O or all X. So you can check them all in one statement:
1
2
3
4
if (board[0] == board[1] == board[2])
{
   return board[0];
}


You could even store all of the winning positions sequentially in an array, and loop through that, effectively reducing your original 16 if statements down to just one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int win_array[24] = {0,1,2,
                     3,4,5,
                     6,7,8,
                     0,3,6,
                     1,4,7,
                     2,5,8,
                     0,4,8,
                     2,4,6};

for (int i=0; i <= 21; i+=3)
{
   if(board[win_array[i]] == board[win_array[i]+1] == board[win_array[i]+2])
   {
      return board[win_array[i]];
   }
}


By returning the value in the check when there is a win, you can tell who's won in your function call:
1
2
3
4
5
6
7
8
9
10
11
12
if(HasWon() == 'O')
{
   cout << "O has won";
}
else if (HasWon() == 'X')
{
   cout << "X has won";
}
else
{
   // No winner yet
}


Hope this is of some use.
would this work
if (gameBoard[1] == gameBoard[4] == gameBoard[7] == P1_CHAR || P2_CHAR)
{
gameState = 1 || 2;
}



P1_CHAR being X
P2_CHAR being O
and gameState = 1 if X wins 2 if O wins
Last edited on
if (board[0] == board[1] == board[2])


Correct me if I'm wrong but I don't think that would work. The operator == returns a bool value so say board[0] - [1] - [2] all equal 'a' then the first "==" will check

board[0] == board[1]

which is true so then the second one will check

true == board[2]

which is false.
if board[2] does infact equal a as do [0] and [1] why wouldnt the second check be true

what I was really worrying about not working is the P1_CHAR || P2_CHAR and the gameState = 1 || 2
In short no. P1_CHAR || P2_CHAR will return true if either are non zero.
gameState = 1 || 2
Will make gameState true if either are non zero.
I want it to return the appropriate value (1 or 2) depending on which char is in the three squares (gameBoard[0], [1], [2]) how can I get this to happen
1
2
3
4
if(
gameBoard[0]==gameBoard[1] &&
gameBoard[0]==gameBoard[2] && 
gameBoard[0]==P1_CHAR )

will be true if all values are equal to P1_CHAR so return 1.
Then do an else if to check them against P2_CHAR
I see that this would work but this would turn my 90 lines of code into about 150
yep 153 lines and there is still one more function to write
Yeah, Lachlan, you're probably right.

I didn't have a compiler handy to test that out at the time. It's easily fixed by adding an 'and' clause in there:

1
2
3
4
if(board[0] == board [1]  && board[1] == board[2])
{
   return board[0];
}
Topic archived. No new replies allowed.