Thread: Code not working w/ Char

  1. #16
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Recently changed the main code so it would work with a loop, but it's only working for 1 move and only when I choose to move piece '6'.
    I am most certainly missing something out on the loop. If anyone could give me a hand I'd be grateful.

    Code:
    #include <algorithm>
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
        char GameBoard[9];
    
    
        GameBoard[0] = '1';
        GameBoard[1] = '2';
        GameBoard[2] = '3';
        GameBoard[3] = '4';
        GameBoard[4] = '5';
        GameBoard[5] = ' ';
        GameBoard[6] = '7';
        GameBoard[7] = '8';
        GameBoard[8] = '6';
    
    bool WinGame = false;
    
    while (WinGame != true)
    {
        cout<<"  " <<GameBoard[0] <<"  |  " <<GameBoard[1] <<"  |  " <<GameBoard[2] <<endl;
        cout<<"-----+-----+-----" <<endl;
        cout<<"  " <<GameBoard[3] <<"  |  " <<GameBoard[4] <<"  |  " <<GameBoard[5] <<endl;
        cout<<"-----+-----+-----" <<endl;
        cout<<"  " <<GameBoard[6] <<"  |  " <<GameBoard[7] <<"  |  " <<GameBoard[8] <<endl <<endl;
    
    
    
        char Play;
        cout<<"Choose a number to move: ";
        cin>>Play;
        cin.ignore();
    
    
        //Checks for (in)valid moves
        if (Play == GameBoard[0])
        {
            if (GameBoard[1] == ' ')
            {
                swap(GameBoard[0], GameBoard[1]);
            }
            if (GameBoard[3] == ' ')
            {
                swap(GameBoard[0], GameBoard[3]);
            }
        }
        if (Play == GameBoard[1])
        {
            if (GameBoard[0] == ' ')
            { 
                swap(GameBoard[1], GameBoard[0]);
            }
            if (GameBoard[2] == ' ')
            {
                swap(GameBoard[1], GameBoard[2]);
            }
            if (GameBoard[4] == ' ')
            {
                swap(GameBoard[1], GameBoard[4]);
            }
        }
        if (Play == GameBoard[2])
        {
            if (GameBoard[1] == ' ')
            {
                swap(GameBoard[2], GameBoard[1]);
            }
            if (GameBoard[5] == ' ')
            {
                swap(GameBoard[2], GameBoard[5]);
            }
        }
        if (Play == GameBoard[3])
        {
            if (GameBoard[0] == ' ')
            {
                swap(GameBoard[3], GameBoard[0]);
            }
            if (GameBoard[4] == ' ')
            {
                swap(GameBoard[3], GameBoard[4]);
            }
            if (GameBoard[6] == ' ')
            {
                swap(GameBoard[3], GameBoard[6]);
            }
        }
        if (Play == GameBoard[4])
        {
            if (GameBoard[1] == ' ')
            {
                swap(GameBoard[4], GameBoard[1]);
            }
            if (GameBoard[3] == ' ')
            {
                swap(GameBoard[4], GameBoard[3]);
            }
            if (GameBoard[5] == ' ')
            {
                swap(GameBoard[4], GameBoard[5]);
            }
            if (GameBoard[7] == ' ')
            {
                swap(GameBoard[4], GameBoard[7]);
            }
        }
        if (Play == GameBoard[5])
        {
            if (GameBoard[2] == ' ')
            {
                swap(GameBoard[5], GameBoard[2]);
            }
            if (GameBoard[4] == ' ')
            {
                swap(GameBoard[5], GameBoard[4]);
            }
            if (GameBoard[8] == ' ')
            {
                swap(GameBoard[5], GameBoard[8]);
            }
        }
        if (Play == GameBoard[6])
        {
            if (GameBoard[3] == ' ')
            {
                swap(GameBoard[6], GameBoard[3]);
            }
            if (GameBoard[7] == ' ')
            {
                swap(GameBoard[6], GameBoard[7]);
            }
        }
        if (Play == GameBoard[7])
        {
            if (GameBoard[6] == ' ')
            {
                swap(GameBoard[7], GameBoard[6]);
            }
            if (GameBoard[4] == ' ')
            {
                swap(GameBoard[7], GameBoard[4]); 
            }
            if (GameBoard[8] == ' ')
            {
                swap(GameBoard[7], GameBoard[8]);
            }
        }
        if (Play == GameBoard[8])
        {
            if (GameBoard[7] == ' ')
            {
                swap(GameBoard[8], GameBoard[7]);
            }
            if (GameBoard[5] == ' ')
            {
                swap(GameBoard[8], GameBoard[5]);
            }
        }
        else {
            cout<<"You can not move that piece. Please, try again!" <<endl <<endl;
        }
    
          //Checks end game conditions
          if (GameBoard[0] == '1' && GameBoard[1] == '2')
          {
              if (GameBoard[2] == '3' && GameBoard[3] == '4')
              {
                  if (GameBoard[4] == '5' && GameBoard[5] == '6')
                  { 
                      if (GameBoard[6] == '7' && GameBoard[7] == '8')
                      {
                          if (GameBoard[8] == ' ')
                          {
                              WinGame = true;
                          }
                      }
                   }
               }
          }
    }
    //Cycle ends
     
        //displays the final board
    
    
        cout<<"  " <<GameBoard[0] <<"  |  " <<GameBoard[1] <<"  |  " <<GameBoard[2] <<endl;
        cout<<"-----+-----+-----" <<endl;
        cout<<"  " <<GameBoard[3] <<"  |  " <<GameBoard[4] <<"  |  " <<GameBoard[5] <<endl;
        cout<<"-----+-----+-----" <<endl;
        cout<<"  " <<GameBoard[6] <<"  |  " <<GameBoard[7] <<"  |  " <<GameBoard[8] <<endl;
    
    
    
        cout<<"You win! :)";
    
    
        cin.get();
    
    
        }
    It works fine if you move piece '6' to the right place, yet if you try moving any other piece it will give an error message (even if that piece is close to the blank space). Also, I can't move '6' back.
    Last edited by Khabz; 03-16-2013 at 07:23 AM.

  2. #17
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    instead of the if ( (Play == GameBoard[x]) ) use else if for each check but the first.

    Even better would be to use a switch statement instead.
    Kurt

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I think part of your problem is in the following snippet:
    Code:
          int Play;
          cout << "Choose a number to move: ";
          cin >> Play;
          cin.ignore();
    
    
          //Checks for (in)valid moves
          if (Play == GameBoard[0])
    The GameBoard variable is an array of char and Play is an int. Remember that the int 6 is not the same as the char '6'.

    Jim

  4. #19
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    @Jim: Yes I fixed it before, I just copy and pasted an old code (without some fancy ass menus) so it would be readable at this point.

    @ZuK: I'll try it out. It's weird if it works tho, because it used to work fine as is with goto's.

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    @Jim: Yes I fixed it before, I just copy and pasted an old code (without some fancy ass menus) so it would be readable at this point
    Well if you can't post the correct code, don't expect much in the way of correct answers.

    Jim

  6. #21
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    It is fixed now tho.

    @Kurt: Tyvm, surprisingly your solution worked. This is another I-have-no-idea-why-it-works solution imo, considering it worked fine with normal if statements when I used goto's.

  7. #22
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Khabz View Post
    @ZuK: I'll try it out. It's weird if it works tho, because it used to work fine as is with goto's.
    The way it is you will always get an error message if GameBoard[0] is not selected.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with char
    By Mentallic in forum C Programming
    Replies: 12
    Last Post: 09-06-2011, 08:35 AM
  2. Replies: 9
    Last Post: 11-18-2008, 02:59 AM
  3. Copying one char* to another not working?
    By Saggio in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2006, 06:03 AM
  4. map<char*, word> not working
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 03-13-2006, 02:02 AM
  5. working with char
    By kennny2004 in forum C++ Programming
    Replies: 20
    Last Post: 11-07-2005, 06:33 AM