Thread: another newbie question (switch...case)

  1. #1
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100

    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. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    the break; did it, ahh, i'm such a douche lol. thanks.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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?
    Last edited by anon; 05-23-2008 at 10:06 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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. #7
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    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. #8
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    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. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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. #10
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    aye dios mio! i should have known that...

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  5. Extra printed stmts...why?
    By mangoz in forum C Programming
    Replies: 4
    Last Post: 12-19-2001, 07:56 AM