# another newbie question (switch...case)

This is a discussion on another newbie question (switch...case) within the C++ Programming forums, part of the General Programming Boards category; I know this is messy but i can't figure out why it's falling through; theBoard is a [3][3] int array. ...

1. ## another newbie question (switch...case)

I know this is messy but i can't figure out why it's falling through;

theBoard is a [3][3] int array.
in this case move is an int thats 1-9.
and player is either 1 or 0. (0 in this case, 1 is the computer).

So the problem is, if move=1, then it falls through without the return you see on case 1. But i don't understand, if move==1 then case 2: shouldn't work, right?

Code:
```void Board::makeMove(int move, int player){
switch (move){
case 1:
if(player==ONE){
theBoard[0][0]=88;
}
else{
theBoard[0][0]=79;
}
this->drawBoard();
return;
cout << "xxxxxx";
case 2:
if(player==ONE){
theBoard[1][0]=88;
}
else{
theBoard[1][0]=79;
}
case 3:
if(player==ONE){
theBoard[2][0]=88;
}
else{
theBoard[2][0]=79;
}
case 4:
if(player==ONE){
theBoard[0][1]=88;
}
else{
theBoard[0][1]=79;
}
case 5:
if(player==ONE){
theBoard[1][1]=88;
}
else{
theBoard[1][1]=79;
}
case 6:
if(player==ONE){
theBoard[2][1]=88;
}
else{
theBoard[2][1]=79;
}
case 7:
if(player==ONE){
theBoard[0][2]=88;
}
else{
theBoard[0][2]=79;
}
case 8:
if(player==ONE){

theBoard[1][2]=88;
}
else{
theBoard[1][2]=79;
}
case 9:
if(player==ONE){
theBoard[2][2]=88;
}
else{
theBoard[2][2]=79;
}
}

}```

2. Put "break;" after each case.
Code:
```void Board::makeMove(int move, int player){
switch (move){
case 1:
if(player==ONE){
theBoard[0][0]=88;
}
else{
theBoard[0][0]=79;
}
this->drawBoard();
break;
case 2:
if(player==ONE){
theBoard[1][0]=88;
}
else{
theBoard[1][0]=79;
}
break;
/* ... */```
Unless you explicitly tell it to break, it will fall through cases, one by one.

3. So the problem is, if move=1, then it falls through without the return you see on case 1. But i don't understand, if move==1 then case 2: shouldn't work, right?
Yes, as in if move == 1, the return means that cout << "xxxxxx"; is not executed. Of course, it could be that drawBoard() is confusing you, making you think the case 2 is executed when it is not in this context.

4. the break; did it, ahh, i'm such a douche lol. thanks.

5. No switch needed (Such use of switches hurts a bit...)

Code:
```void Board::makeMove(int move, int player)
{
//move is in range [1, 9], convert it to range [0, 9) to ease index calculations
--move;

//if in valid range
if (move >= 0 && move < 9) {
//calculate indices:
int i = move &#37; 3;
int j = move / 3;

//now do the logic:
if (player == ONE) {
theBoard[i][j] = 88;
}
else {
theBoard[i][j] = 79;
}
}
}```
The next improvement might be to get rid of the magic values 88 and 79...

Edit: Wait, those are for 'X' and 'O'? Wouldn't it be more readable this way?

6. >> Wait, those are for 'X' and 'O'? Wouldn't it be more readable this way?
Yes, and as you know it would be more correct. Why assume a character set when 'X' and 'O' would work everywhere?

7. why? because i'm a newb and i could get
Code:
`char theBoard[3][3] = {1,2,3,4,5,6,7,8,9};`
to work, i kept getting a can't convert const* char to char error.

years ago when i first made this prog i used X and O, but since i'm trying to do this using classes i'm just hacking it together (like the newb i am).

Just wait till i post the full code so people can nit pick!

8. So building on top of that, if i was going to have the computer check if the player was about to win, what would be the simplest way to check if the human player was one move away from winning? Because the way i was going to do it was to use a but of if...elseif statements that check to see if it needs to intercept a win then return the number of the space (1-9)? Any easier solutions that won't take 80 lines of code? lol

9. >> i kept getting a can't convert const* char to char error.

Use single quotes instead of double quotes for single characters:
Code:
`char theBoard[3][3] = {'1','2','3','4','5','6','7','8','9'};`

10. aye dios mio! i should have known that...

11. And instead of board[x][y] = 88 or = 79, why not use 'X' or 'O' ? Or are you trying to hide which character is which? I had to check in the ASCII table (actually I used the console to check, but I did not know which character is 88 or 79 - and I have been doing this programming stuff for about 22 years now).

--
Mats

12. Like i said, i couldn't rememer how to make a char array, so i went with how i knew to do it. I'm almost done with it now, it's just a matter of making the AI work, i'm just dicking around with it right now.