Thread: Logic

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

    Logic

    Isn't this the same thing? Coz they give different outputs, aka the shorter one doesn't work.

    Code:
    for (int i = 0; i < 9; i++)
    			{
    				if (P == GameBoard[i])
    				{
    					MoveValidation(P, GameBoard, i, 3, 9, Val);
    					Val = true;
    				}
    			}
    and

    Code:
    if (P == GameBoard[0]) {
    				MoveValidation(P, GameBoard, 0, 3, 9, Val); 
                                    Val = true;
    			}
    			else if (P == GameBoard[1]) {
    				MoveValidation(P, GameBoard, 1, 3, 9, Val);
    				Val = true;
    			}
    			else if (P == GameBoard[2]) {
    				MoveValidation2(P, GameBoard, 2, 3, 9, Val);
    				Val = true;
    			}
    			else if (P == GameBoard[3]) {
    				MoveValidation3(P, GameBoard, 3, 3, 9, Val);
    				Val = true;
    			}
    			else if (P == GameBoard[4]) {
    				MoveValidation(P, GameBoard, 4, 3, 9, Val);
    				Val = true;
    			}
    			else if (P == GameBoard[5]) {
    				MoveValidation2(P, GameBoard, 5, 3, 9, Val);
    				Val = true;
    			}
    			else if (P == GameBoard[6]) {
    				MoveValidation3(P, GameBoard, 6, 3, 9, Val);
    				Val = true;
    			}
    			else if (P == GameBoard[7]) {
    				MoveValidation(P, GameBoard, 7, 3, 9, Val);
    				Val = true;
    			}
    			else if (P == GameBoard[8]) {
    				MoveValidation(P, GameBoard, 8, 3, 9, Val);
    				Val = true;

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Well let's see. In the short one you call one function only: MoveValidation. In the long one you call MoveValidation, MoveValidation2, and MoveValidation3.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why doesn't the small one work? What are you actually having problems with? How is MoveValidation() defined and implemented? In the small snippet you call MoveValidation() 9 times in the larger snippet you only call this function once.

    Jim

  4. #4
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Yeah, thanks for pointing that out rags, forgot about that. I changed to this, but it's still not working:

    Code:
    for (int i = 0; i <= 8; i++)
    			{
    				if (P == GameBoard[i])
    				{
    					if (i == 2 || i == 5)
    					{
    						MoveValidation2(P, GameBoard, i, 3, 9, Val);
    						Val = true;
    					}
    					else if (i == 3 || i == 6)
    					{
    						MoveValidation3(P, GameBoard, i, 3, 9, Val);
    						Val = true;
    					}
    					else
    					{
    						MoveValidation(P, GameBoard, i, 3, 9, Val);
    						Val = true;
    					}
    				}
    			}

  5. #5
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    I'm going to put here the whole code. Only 3x3 is available atm:

    Code:
    #include <iostream>
    #include <conio.h>
    #include <algorithm>
    #include <Windows.h>
    #include <ctime>
    
    
    //Function which converts a char into an int
    int digit_to_int(char d)
    {
        char str[2];
    
    
        str[0] = d;
        str[1] = '\0';
        return (int) strtol(str, NULL, 10);
    }
    
    
    //Checks for end game conditions regardless of the board size
    bool EndGameVerif(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;
        }
    }
    
    
    void MoveValidation(int Move, int array[], int position, int BoardConfig, int arraySIZE, int Valid)
    {
    
    
        if (Move == array[position])
        {
    
    
            if ((position - 1) >= 0 && array[position - 1] == 0)
            {
                Valid = true;
                std::swap(array[position], array[position -1]);
            }
            else if ((position - BoardConfig) > 0 && array[position - BoardConfig] == 0)
            {
                Valid = true;
                std::swap(array[position], array[position - BoardConfig]);
            }
            else if ((position + BoardConfig) < arraySIZE && array[position + BoardConfig]  == 0)
            {
                Valid = true;
                std::swap(array[position], array[position + BoardConfig]);
            }
            else if ((position + 1) < arraySIZE && array[position + 1] == 0)
            {
                Valid = true;
                std::swap(array[position], array[position + 1]);
            }
            else
            {
                Valid = false;
                std::cout<<"Invalid input, try again: ";
            }
        }
    }
    
    
    void MoveValidation2(int Move, int array[], int position, int BoardConfig, int arraySIZE, int Valid)
    {
        if (Move == array[position])
        {
            if ((position - BoardConfig) >= 0 && array[position - BoardConfig] == 0)
            {
                Valid = true;
                std::swap(array[position], array[position - BoardConfig]);
            }
            else if (array[position - 1] == 0)
            {
                Valid = true;
                std::swap(array[position], array[position - 1]);
            }
            else if (array[position + BoardConfig] == 0)
            {
                Valid = true;
                std::swap(array[position], array[position + BoardConfig]);
            }
            else
            {
                Valid = false;
                std::cout<<"Invalid input, try again: ";
            }
        }
    }
    
    
    void MoveValidation3(int Move, int array[], int position, int BoardConfig, int arraySIZE, int Valid)
    {
        if (Move == array[position])
        {
            if (array[position - BoardConfig] == 0)
            {
                Valid = true;
                std::swap(array[position], array[position - BoardConfig]);
            }
            else if (array[position + 1] == 0)
            {
                Valid = true;
                std::swap(array[position], array[position + 1]);
            }
            else if ((position + BoardConfig) < arraySIZE && array[position + BoardConfig] == 0)
            {
                Valid = true;
                std::swap(array[position], array[position + BoardConfig]);
            }
            else
            {
                Valid = false;
                std::cout<<"Invalid input, try again: ";
            }
        }
    }
    
    
    int main()
    {
        std::cout<<"[1] Training Mode" <<std::endl;
        std::cout<<"[2] Competition Mode" <<std::endl <<std::endl;
    
    
        bool ValidOption2 = false;
    
    
        while (ValidOption2 != true)
        {
    
    
            char Option = ' ';
            Option = _getch();
    
    
    
    
            if (Option == '2')
            {
                ValidOption2 = false;
                std::cout<<"\nNot yet developed, choose another option: ";
            }
            else if (Option == '1')
            {
                ValidOption2 = true;
            }
            else
            {
                ValidOption2 = false;
                std::cout<<"\nInvalid Option, try again: ";
            }
        }
    
    
    Restart:
    
    
        system("CLS");
    
    
        std::cout<<"Choose your width in the format NxN: ";
    
    
        //Only allows a string 3 characters long (NxN).
    
    
        bool ValidOption = false;
    
    
        while (ValidOption != true)
        {
    
    
            char format1, x, format2;
    
    
            format1 = _getch();
            std::cout<<format1;
    
    
            x = _getch();
            std::cout<<x;
    
    
            format2 = _getch();
            std::cout<<format2;
    
    
            if (format1 == format2 && format1 == '3')
            {
                if (x == 'x' || x == 'X')
                {
                    ValidOption = true;
                    goto Boardx3;
                }
    
    
            }
            else if (format1 == format2 && format1 == '4')
            {
                ValidOption = true;
                goto Boardx4;
            }
            else if (format1 == format2 && format1 == '5')
            {
                ValidOption = true;
                goto Boardx5;
            }
            else
            {
                ValidOption = false;
                std::cout<<"\n\nInvalid/Unavailable width. Try again: ";
            }
        }
    
    
    
    
    Boardx3:
    
    
        system("CLS");
    
    
        //3x3 Slide Puzzle
    
    
        int GameBoard[9];
    
    
        GameBoard[0] = 2; GameBoard[1] = 1; GameBoard[2] = 4; GameBoard[3] = 3; GameBoard[4] = 0; GameBoard[5] = 5; GameBoard[6] = 7; GameBoard[7] = 8; GameBoard[8] = 6;
    
    
        //Starts timing
        clock_t begin2 = clock();
    
    
    
    
    
    
        while (true)
        {
            bool Val = false;
    
    
            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;
    
    
    
    
            while (Val != true)
            {
    
    
                char Play = ' ';
                Play = _getch();
    
    
                int P = digit_to_int(Play);        
    
    
                system("CLS");
    
    
                //Checks for (in)valid moves
    
    
                //Exits whenever
    
    
                if (Play == 'q' || Play == 'Q')
                {
                    exit(0);
                }
    
    
    
    
                for (int i = 0; i <= 8; i++)
                {
                    if (P == GameBoard[i])
                    {
                        if (i == 2 || i == 5)
                        {
                            MoveValidation2(P, GameBoard, i, 3, 9, Val);
                            Val = true;
                        }
                        else if (i == 3 || i == 6)
                        {
                            MoveValidation3(P, GameBoard, i, 3, 9, Val);
                            Val = true;
                        }
                        else
                        {
                            MoveValidation(P, GameBoard, i, 3, 9, Val);
                            Val = true;
                        }
                    }
                }
    
    
                    //Calls MoveValidation function for each possiblity of the board
                    /*
                    if (P == GameBoard[0]) {
                    MoveValidation(P, GameBoard, 0, 3, 9, Val); 
                    }
                    else if (P == GameBoard[1]) {
                    MoveValidation(P, GameBoard, 1, 3, 9, Val);
                    Val = true;
                    }
                    else if (P == GameBoard[2]) {
                    MoveValidation2(P, GameBoard, 2, 3, 9, Val);
                    Val = true;
                    }
                    else if (P == GameBoard[3]) {
                    MoveValidation3(P, GameBoard, 3, 3, 9, Val);
                    Val = true;
                    }
                    else if (P == GameBoard[4]) {
                    MoveValidation(P, GameBoard, 4, 3, 9, Val);
                    Val = true;
                    }
                    else if (P == GameBoard[5]) {
                    MoveValidation2(P, GameBoard, 5, 3, 9, Val);
                    Val = true;
                    }
                    else if (P == GameBoard[6]) {
                    MoveValidation3(P, GameBoard, 6, 3, 9, Val);
                    Val = true;
                    }
                    else if (P == GameBoard[7]) {
                    MoveValidation(P, GameBoard, 7, 3, 9, Val);
                    Val = true;
                    }
                    else if (P == GameBoard[8]) {
                    MoveValidation(P, GameBoard, 8, 3, 9, Val);
                    Val = true;
                    }*/
                }
    
    
    
    
                //End Game Verification
                if (EndGameVerif(GameBoard, 9) == true)
                {
                    break;
                }
            }
    
    
            system("CLS");
    
    
            //Obtains the total amount of seconds taken
            clock_t end2 = clock();
            double elapsed_secs2 = double(end2 - begin2) / CLOCKS_PER_SEC;
    
    
            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;
    
    
            std::cout<<"You win!";
    
    
            std::cout<< std::endl <<std::endl;
            std::cout<< "You took " <<elapsed_secs2 <<" seconds to solve the puzzle." <<std::endl <<std::endl;
    
    
            std::cout<<"Play again? (y/n): ";
    
    
            bool ValidOption3 = false;
    
    
            while (ValidOption3 != true)
            {
    
    
                char Again = ' ';
                Again = _getch();
    
    
                if (Again == 'y')
                {
                    ValidOption3 = true;
                    goto Restart;
                }
                else if (Again == 'Y')
                {
                    ValidOption3 = true;
                    goto Restart;
                }
                else if (Again == 'n')
                {
                    ValidOption3 = true;
                    exit(0);
                }
                else if (Again == 'N')
                {
                    ValidOption3 = true;
                    exit(0);
                }
                else
                {
                    ValidOption3 = false;
                    std::cout<<"Invalid input, try again: ";
                }
            }
    
    
    
    
    
    
    Boardx4:
    
    
    Boardx5:
    
    
            std::cin.get();
            exit(0);
    
    
        }
    The for loop is supposed to do the same as the commented part of the program, which is working fine.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Part of your problem is that you are calling the MoveValidation() functions multiple times. These functions are altering the array.

    Also don't do this:
    Code:
    for (int i = 0; i <= 8; i++)
    You had it right in the first post.

    Jim

  7. #7
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Lol solved it. The player is supposed to make 1 move at a time, so the loop has to 'break' after P == GameBoard[i]:

    Code:
        for (int i = 0; i <= 8; i++)
                {
                    if (P == GameBoard[i])
                    {
                        if (i == 2 || i == 5)
                        {
                            MoveValidation2(P, GameBoard, i, 3, 9, Val);
                            Val = true;
                            break;
                        }
                        else if (i == 3 || i == 6)
                        {
                            MoveValidation3(P, GameBoard, i, 3, 9, Val);
                            Val = true;
                            break;
                        }
                        else
                        {
                            MoveValidation(P, GameBoard, i, 3, 9, Val);
                            Val = true;
                            break;
    
    
                        }
                    }
                }
    
    It works now, thank for the help!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Welcome to the world of C++. You are committing a grave sin by using C arrays and not doing bounds checking. You pass some sizes along, but you never use them.
    This opens up the world of undefined behaviour and buffer overruns. Not very fun.
    So my suggestion to you: check those bounds! Make absolutely 100% sure that you stay in bounds. You can do this by checking your indexes. Or, if you don't want to bother with such a thing, you can use some facilities in the language. Particularly, for fixed arrays, you can use std::array along with its .at() member function. This will probably mean you have to take a look at exceptions if you haven't already, though.
    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.

  9. #9
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Quote Originally Posted by Elysia View Post
    Welcome to the world of C++. You are committing a grave sin by using C arrays and not doing bounds checking. You pass some sizes along, but you never use them.
    This opens up the world of undefined behaviour and buffer overruns. Not very fun.
    So my suggestion to you: check those bounds! Make absolutely 100% sure that you stay in bounds. You can do this by checking your indexes. Or, if you don't want to bother with such a thing, you can use some facilities in the language. Particularly, for fixed arrays, you can use std::array along with its .at() member function. This will probably mean you have to take a look at exceptions if you haven't already, though.
    Yeah I commited some flaws when I only had one version of MoveValidation function, which caused buffer overruns, considering the program would make a verification for invalid indexes (GameBoard[-1] and so on). Yet, I split the function in 3 for 3 different situations. This way, the verification will stay within the array boundaries.

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Khabz View Post
    Yet, I split the function in 3 for 3 different situations. This way, the verification will stay within the array boundaries.

    In general, if you find yourself copying and pasting code, or otherwise making very similar functions, this is one example of "code smell" - you should train yourself to identify this and think "there should be another way". The use of 'goto' is another case of code smell. If you find yourself "needing" to do either, chances are there's a deeper underlying problem in your code that you should solve instead.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    In tic tac toe there are only a few possible configurations for winning.

    Code:
    ? ? X	X ? ?
    ? X ?	? X ?
    X ? ? 	? ? X
    
    X X X	? ? ?	? ? ?
    ? ? ?   X X X	? ? ?
    ? ? ?	? ? ?	X X X
    
    X ? ?	? X ?	? ? X
    X ? ?	? X ?	? ? X
    X ? ?	? X ?	? ? X
    Same for O.

    You might as well check the whole board for these patterns specifically after five total moves (because after the first player moves three times, the opponent has moved twice) and every move after. After nine moves, check the winning configurations a final time. Someone has to win by then, otherwise it's a cat game. You should be able to do it in 24 comparisons.

    The only "move validation" required that doesn't also involve winning the game: a player trying to move in a filled square. X and O can't move anywhere they or their opponent went before.

    Checking your array subscripts is also a good idea but it probably doesn't help you figure out what MoveValidation should be doing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. XOR Logic Help
    By Yabssato in forum C Programming
    Replies: 8
    Last Post: 09-27-2011, 01:29 PM
  2. Better up my logic
    By C_programmer.C in forum C Programming
    Replies: 11
    Last Post: 01-10-2011, 12:48 PM
  3. War Logic
    By Dr Saucie in forum C Programming
    Replies: 3
    Last Post: 02-16-2010, 10:00 PM
  4. help on logic
    By wiresite in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2004, 03:30 PM
  5. help with logic please
    By Tryin in forum C Programming
    Replies: 7
    Last Post: 01-28-2003, 02:54 AM