Thread: Overall function

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105

    Overall function

    Hello everyone. I'm having troubles implementing a certain function so I hope you can help me out. Will try to be simple and quick.

    Ok, so not that long ago I posted a couple of doubts regarding a "Slidding Puzzle" code. I am now trying to make the code shorter by not having to test every possibility, considering I'm willing to implement a 3x3, 4x4 and 5x5 board.

    This is what I got so far:

    Code:
    #include <iostream>
    #include <algorithm>
    
    
    //Checks end game conditions (Working)
    bool Verif(int array[], int ArraySize)
    {
        int counter = 0;
    
    
        for (int i = 0; i < (ArraySize - 1); i++)
        {
            if (array[i] == (i + 1) && array[ArraySize - 1] == 0)
            {
                counter = counter + 1;
            }
            else
            {
                counter = counter;
            }
        }
    
    
        if (counter == (ArraySize - 1))
        {
            return true;
            std::cout<<"You win!";
        }
    
    
    }
    
    
    
    
    //Validates a move (Not working)
    
    
    bool ValidatesMove(int Move, int array[], int ArraySize)
    {
    
    
        int n;
    
    
        switch (ArraySize)
        {
        case 9:
            if (Move = array[n])
            {
                if (array[n - 1] == 0)
                {
                    std::swap(array[n], array[n - 1]);
                    return true;
                }
                else if (array[n - 3] == 0)
                {
                    std::swap(array[n], array[n - 3]);
                    return true;
                }
                else if (array[n + 1] == 0)
                {
                    std::swap(array[n], array[n + 1]);
                    return true;
                }
                else if (array[n + 3] == 0)
                {
                    std::swap(array[n], array[n + 3]);
                    return true;
                }
                else 
                {
                    return false;
                }
            }
            break;
        default:
            break;
        }
    }
    
    
    
    
    int main()
    {
    
    
        while (true)
        {
    
    
    
    
            int GameBoard[9];
    
    
            GameBoard[0] = 1; GameBoard[1] = 2; GameBoard[2] = 3; GameBoard[3] = 4; GameBoard[4] = 5; GameBoard[5] = 0; GameBoard[6] = 7; GameBoard[7] = 8; GameBoard[8] = 6;
    
    
            std::cout<<std::endl <<std::endl <<std::endl;
            std::cout<<"  " <<GameBoard[0] <<"  |  " <<GameBoard[1] <<"  |  " <<GameBoard[2] <<std::endl;
            std::cout<<"-----+-----+-----" <<std::endl;
            std::cout<<"  " <<GameBoard[3] <<"  |  " <<GameBoard[4] <<"  |  " <<GameBoard[5] <<std::endl;
            std::cout<<"-----+-----+-----" <<std::endl;
            std::cout<<"  " <<GameBoard[6] <<"  |  " <<GameBoard[7] <<"  |  " <<GameBoard[8] <<std::endl <<std::endl;
    
    
            int Play;
            std::cin >> Play;
    
    
            //If the piece selected is next to "0" then it should swap with it. If not, then the move is invalid.
            ValidatesMove(Play, GameBoard, 9);
    
    
            //Checks for end game conditions
            if (Verif(GameBoard, 9) == true)
            {
                break;
            }
    
    
        }
    
    
        std::cin.ignore();
        std::cin.get();
    
    
    }

    A move is supposed to be valid when the piece selected is next to a zero:

    1 2 3
    4 5 0
    7 8 6

    6

    1 2 3
    4 5 6
    7 8 0

    You win!

    ^That's what it is supposed to work but the pieces won't swap.
    Last edited by Khabz; 03-27-2013 at 06:09 PM.

  2. #2
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    I'll give an example if anyone is missing the point of the following portion of code:

    Code:
    int n;
    
    
        switch (ArraySize)
        {
        case 9:
            if (Move = array[n])
            {
                if (array[n - 1] == 0)
                {
                    std::swap(array[n], array[n - 1]);
                    return true;
                }
                else if (array[n - 3] == 0)
                {
                    std::swap(array[n], array[n - 3]);
                    return true;
                }
                else if (array[n + 1] == 0)
                {
                    std::swap(array[n], array[n + 1]);
                    return true;
                }
                else if (array[n + 3] == 0)
                {
                    std::swap(array[n], array[n + 3]);
                    return true;
                }
                else
                {
                    return false;
                }
            }
    1 | 2 | 3
    4 | 5 | 0
    7 | 8 | 6

    If you move piece 5 (= GameBoard[n]), it will check if :
    GameBoard[n-3] = 0, which obviously isn't considering it is 2.
    GameBoard[n-1] = 0, which is not (4).
    GameBoard[n+3] = 0, which is not (8).
    GameBoard[n+1] = 0, which is in this case, so it should swap GameBoard[n] (5) with GameBoard[n+1] (0).

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Sorry this piece of code:

    > intGameBoard[9];
    > GameBoard[0] = 1; GameBoard[1] = 2; GameBoard[2] = 3; GameBoard[3] = 4; GameBoard[4] = 5; GameBoard[5] = 0; GameBoard[6] = 7; GameBoard[7] = 8; GameBoard[8] = 6;

    goes before the while loop, but it's not working

  4. #4
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Ok, I fixed as it usually happens.

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    First, n was never initialized. so array[n - *] is undefined.
    Second, "if (Move = array[n])", you're assigning, not comparing.

    you will need something like this:
    Code:
    for ( int i =0 ; i < arraysize ; ++i)
      if (Move == array[i]) then 
        { n = i; break;}
    Be careful with the boundary. Example, when n=2, a[n-3] is a[-1] which is bad... very very bad.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  6. #6
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Yeah it was like 3 am when I was working on the algorithm so I kind of derped. It works fine now though.

    And yeah, the program will come to a point where it has to check for negative values of the array which is something dangerous. I don't know what would I do to fix it though, considering it works alright at this point and it doesn't crash.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    I did the following so it won't check for invalid positions of the array:

    Code:
    //Checks if is a valid move regardless of the size of the board
    void ValidMove(int Move, int array[], int position, int BoardConfig, int arraySIZE)
    {
    
    
            if (Move == array[position])
            {
                if  ((position - 1) >= 0)
                {
                    if (array[position - 1] == 0)
                    {
                        std::swap(array[position], array[position -1]);
                    }
                }
                else if ((position - BoardConfig) >= 0)
                {
                    if (array[position - BoardConfig] == 0)
                    {
                        std::swap(array[position], array[position - BoardConfig]);
                    }
                }
                else if ((position + BoardConfig) < arraySIZE)
                {
                    if (array[position + BoardConfig]  == 0)
                    {
                        std::swap(array[position], array[position + BoardConfig]);
                    }
                }
                else if ((position + 1) < arraySIZE)
                {
                    if (array[position + 1] == 0)
                    {
                        std::swap(array[position], array[position + 1]);
                    }
                }
                else
                {
                    std::cout<<"Error";
                }
            }
    }
    Last edited by Khabz; 03-28-2013 at 05:11 AM.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
               if (array[position - 1] == 0 && (position - 1) >= 0)
    If you write the check this way it doesn't help much.


    Try this way
    Code:
               if ((position - 1) >= 0 && array[position - 1] == 0 )
    This way the array at position -1 will not be accessed if position -1 is smaller then 0

    Kur

  9. #9
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Yeah, I ninja edited and changed to something similar :P

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Khabz View Post
    it works alright at this point and it doesn't crash.
    worst reason I've ever heard for not fixing obvious bugs.

  11. #11
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Quote Originally Posted by Elkvis View Post
    worst reason I've ever heard for not fixing obvious bugs.
    1st. I was stating a fact, not an excuse.
    2nd. 5 minutes later I had it fixed.
    Last edited by Khabz; 03-28-2013 at 06:16 AM.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Khabz View Post
    1st. I was stating a fact, not an excuse.
    2nd. 5 minutes later I had it fixed.
    I wasn't necessarily referring to this situation. it's just generally a good idea to avoid thinking that way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  2. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  3. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM