Tic Tac Toe Advanced

This is a discussion on Tic Tac Toe Advanced within the C++ Programming forums, part of the General Programming Boards category; Tell me if you find any bugs or holes in the code and if you have any question. Note: You ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    10

    Tic Tac Toe Advanced

    Tell me if you find any bugs or holes in the code and if you have any question.
    Note: You type in the row and column number in together with no space between them. Thank You

    Code:
    // Done by Shazly
    // Tell if you find any bugs or holes in the code!! Thank You :D
    
    #include<iostream>
    using namespace std;
    
    
    void display_board(char board[][8], int limit1, int limit2); //  displays as much as the screen can take
    void fill_board(char board[][8], int limit1, int limit2);    //  fills the board with the mark (-)
    void Player_X(char board[][8]);  //  player X turn
    void Player_O(char board[][8]);  //  player O turn
    bool X_wins_3(char board[][8]);  //  winning with 3 X straight
    bool X_wins_4(char board[][8]);  //  winning with 4 X straight
    bool O_wins_3(char board[][8]);  //  winning with 3 O straight
    bool O_wins_4(char board[][8]);  //  winning with 4 O straight
    void Players_names();  // The players names input
    void board_size();  // the size choise of the user
    int range1;  // the array 1 diminsion size
    int range2;  // the array 2 dimension size
    string player1;
    string player2;
    
    
    int main()
    {
        char board[8][8]; // board size can be changed here and on the functions but 8 is best
        int turn = 0; // to verify which player's turn is it
        cout << "\n\n\t\t\tWelcome To Tic Tac Toe ADVANCED\n\n";
        Players_names();
        board_size();
        fill_board(board, range1, range2);
        display_board(board, range1, range2);
        while(1)
        {
            if(turn == 0)
            {
                Player_X(board);
                turn = 1;
                if(range1 > 3 && range2 > 3)
                {
                    if(X_wins_4(board))
                    {
                        cout << "\n\n\tPlayer " << player1 << " Wins.\n\n";
                        break;
                    }
                }
                else
                {
                    if(X_wins_3(board))
                    {
                        cout << "\n\n\tPlayer " << player1 << " Wins.\n\n";
                        break;
                    }
                }
            }
            else if(turn == 1)
            {
                Player_O(board);
                turn = 0;
                if(O_wins_4(board))
                {
                    cout << "\n\n\tPlayer " << player2 << " Wins.\n\n";
                    break;
                }
                else
                {
                    if(O_wins_3(board))
                    {
                        cout << "\n\n\tPlayer " << player2 << " Wins.\n\n";
                        break;
                    }
                }
            }
        }
    }
    
    
    
    
    void display_board(char board[][8], int limit, int limit2)
    {
        cout << "\n\n\n\t      ";
        for(int r = 0; r < limit2; r++)
        {
            cout << "     " << r;
        }
        cout << "\n\n\t\t";
        for(int i = 0; i < limit; i++)
        {
            cout << "\n\t\t";
            for(int k = 0; k < limit2; k++)
            {
                cout << "______";
            }
            cout << "\n\n\t" << i << "\t|  ";
            for(int j = 0; j < limit2; j++)
            {
                 cout << board[i][j] << "  |  ";
            }
        }
        cout << "\n\t\t";
        for(int p = 0; p < limit2; p++)
        {
            cout << "______";
        }
    }
    
    
    void fill_board(char board[][8], int limit, int limit2)
    {
        int counter = 0;
        for(int i = 0; i < limit; i++)
        {
            for(int j = 0; j < limit2; j++)
            {
                board[i][j] = '-';
            }
        }
    }
    
    
    void board_size()
    {
        while(1)
        {
            cout << "\n\n\n\tPlease Type In The Desired Board Size [ Rows ] [ Columns ]\n\n\tRows:\t\t";
            cin >> range1;
            cout << "\n\tColumns:\t";
            cin >> range2;
            if((range1 > 8) || (range1 < 3) || (range2 > 8) || (range2 < 3))
            {
                cout << "\n\n\tMax Range In Both Criterias Is 8 !! Please Try Again.";
            }
            else
            {
                break;
            }
        }
    }
    
    
    void Players_names()
    {
        cout << "\n\n\tPlease Type In The First Player's Name:\n\n\t\t";
        getline(cin, player1, '\n');
        cout << "\n\n\tPlease Type In The Second Player's Name:\n\n\t\t";
        getline(cin, player2, '\n');
    }
    
    
    void Player_X(char board[][8])
    {
        int input;
        int column;
        int row;
        while(1)
        {
            cout << "\n\n\n\t" << player1 << "'s Move: \t";
            cin >> input;
            row = input / 10;
            column = input % 10;
            if((board[row][column] != 'X' && board[row][column] != 'O') && (row < 8 && row >= 0 && column < 8 && column >= 0))
            {
                board[row][column] = 'X';
                display_board(board, range1, range2);
                break;
            }
            else
            {
                cout << "\n\n\tYou Have Typed In An Invalid Input!! Please Try Again.";
            }
        }
    }
    
    
    bool X_wins_4(char board[][8])
    {
        for(int i = 0; i < range1; i++)
        {
            for(int j = 0; j < range2; j++)
            {
                if(board[i][j] == 'X' && board[i][j+1] == 'X' && board[i][j+2] == 'X' && board[i][j+3] == 'X')
                {
                    return true;
                }
            }
        }
        for(int m = 0; m < range1; m++)
        {
            for(int n = 0; n < range2; n++)
            {
                if(board[m][n] == 'X' && board[m+1][n] == 'X' && board[m+2][n] == 'X' && board[m+3][n] == 'X')
                {
                    return true;
                }
            }
        }
        for(int k = 0; k < range1; k++)
        {
            for(int p = 0; p < range2; p++)
            {
                if(board[k][p] == 'X' && board[k+1][p+1] == 'X' && board[k+2][p+2] == 'X' && board[k+3][p+3] == 'X')
                {
                    return true;
                }
            }
        }
        return false;
    }
    
    
    bool X_wins_3(char board[][8])
    {
        if((board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X') ||
            (board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X') ||
            (board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X') ||
            (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X') ||
            (board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X') ||
            (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X') ||
            (board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X') ||
            (board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X'))
            {
                return true;
            }
            return false;
    }
    
    
    void Player_O(char board[][8])
    {
        int input;
        int column;
        int row;
        while(1)
        {
            cout << "\n\n\n\t" << player2 << "'s Move: \t";
            cin >> input;
            row = input / 10;
            column = input % 10;
            if((board[row][column] != 'X' && board[row][column] != 'O') && (row < 8 && row >= 0 && column < 8 && column >= 0))
            {
                board[row][column] = 'O';
                display_board(board, range1, range2);
                break;
            }
            else
            {
                cout << "\n\n\tYou Have Typed In An Invalid Input!! Please Try Again.";
            }
        }
    }
    
    
    bool O_wins_3(char board[][8])
    {
        if((board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O') ||
            (board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O') ||
            (board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O') ||
            (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O') ||
            (board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O') ||
            (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O') ||
            (board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O') ||
            (board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O'))
            {
                return true;
            }
            return false;
    }
    
    
    bool O_wins_4(char board[][8])
    {
        for(int i = 0; i < range1; i++)
        {
            for(int j = 0; j < range2-3; j++)
            {
                if(board[i][j] == 'O' && board[i][j+1] == 'O' && board[i][j+2] == 'O' && board[i][j+3] == 'O')
                {
                    return true;
                }
            }
        }
        for(int m = 0; m < range1-3; m++)
        {
            for(int n = 0; n < range2; n++)
            {
                if(board[m][n] == 'O' && board[m+1][n] == 'O' && board[m+2][n] == 'O' && board[m+3][n] == 'O')
                {
                    return true;
                }
            }
        }
        for(int k = 0; k < range1; k++)
        {
            for(int p = 0; p < range2; p++)
            {
                if(board[k][p] == 'O' && board[k+1][p+1] == 'O' && board[k+2][p+2] == 'O' && board[k+3][p+3] == 'O')
                {
                    return true;
                }
            }
        }
        return false;
    }
    Last edited by Shazly; 03-03-2013 at 09:28 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Seems legit. i get a warning though
    |111|: unused variable 'counter' [-Wunused-variable]|
    It's also a little confusing ( at least for me ) to input the row and column as a single number, adding a space in between is treated as error. You may want to fix that.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    It's complicated for me too!! that's why I broke it down to many functions. about the error, I don't get any error with the compiler over here. I just though the numbers input once better than asking you twice every time. but i don't know how to make it accept space between the numbers. but i got used to it.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,740
    You appear to have a lot of duplicate code. You may want to try to simplify your program. For example the following two functions could be one function if you added a second parameter.

    Code:
    bool X_wins_3(char board[][8])
    {
        if((board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X') ||
            (board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X') ||
            (board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X') ||
            (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X') ||
            (board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X') ||
            (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X') ||
            (board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X') ||
            (board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X'))
            {
                return true;
            }
            return false;
    }
    
    bool O_wins_3(char board[][8])
    {
        if((board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O') ||
            (board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O') ||
            (board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O') ||
            (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O') ||
            (board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O') ||
            (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O') ||
            (board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O') ||
            (board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O'))
            {
                return true;
            }
            return false;
    }
    There may be other functions that could be duplicate as well, I didn't look too closely.

    If your board is 8 by 8 don't you need to check 8 locations, not 3?

    Code:
     char board[8][8];
    // board size can be changed here and on the functions but 8 is best
    Why the "magic numbers", you should consider using a const to set the dimensions then you would only need to change one location instead of multiple locations.


    Jim

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    The 3 by 3 board only has 8 possibilities of a player wining!! I could make a loop that checks all like
    Code:
     boolX_wins_4 
    But I just copied and pasted this from an older
    version of tic tac toe with board 3 by 3. Besides, All the possibilities are placed as conditions in the if statement.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,740
    All the possibilities are placed as conditions in the if statement.
    Only for a 3 by 3 board, not an 8 by 8 board.

    Plus my point about the code duplication is that both functions only differ in the character they are checking for, X or O. You should be able to simplify this to only use one function to handle the checking for a winner by adding a second parameter, the character to check.


    Jim

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    Quote Originally Posted by jimblumberg View Post
    Only for a 3 by 3 board, not an 8 by 8 board.

    You should be able to simplify this to only use one function to handle the checking for a winner.


    Jim
    I didn't get that at first!! nice advise by the way i modified it!!

    Code:
    bool board_3_by_3_win(char board[][8], char XO)
    {
        if((board[0][0] == XO && board[0][1] == XO && board[0][2] == XO) ||
            (board[1][0] == XO && board[1][1] == XO && board[1][2] == XO) ||
            (board[2][0] == XO && board[2][1] == XO && board[2][2] == XO) ||
            (board[0][0] == XO && board[1][1] == XO && board[2][2] == XO) ||
            (board[0][2] == XO && board[1][1] == XO && board[2][0] == XO) ||
            (board[0][0] == XO && board[1][0] == XO && board[2][0] == XO) ||
            (board[0][1] == XO && board[1][1] == XO && board[2][1] == XO) ||
            (board[0][2] == XO && board[1][2] == XO && board[2][2] == XO))
            {
                return true;
            }
            return false;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advanced AI
    By Demon.killer in forum General AI Programming
    Replies: 36
    Last Post: 03-31-2008, 10:01 AM
  2. Advanced C
    By Vinod Menon in forum C Programming
    Replies: 3
    Last Post: 06-04-2004, 02:40 PM
  3. Advanced C Question
    By Vinod Menon in forum C Programming
    Replies: 11
    Last Post: 05-28-2004, 08:43 AM
  4. Advanced? Not Advanced? Anyone?
    By Jotun in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2004, 08:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21