Hello!

I wanna ask you guys for help with this program. Here is my problem explanation: I want that user gives an input in the beginning of the program how big should tictactoe board would be. For example: input: 4 so board would be 4x4.

Code of the current problem is:

#include <iostream>

using namespace std;

int main(){
    //These variables are holding the value of each square (0 = free, 1 = x, 2 = o)
    int one = 0;
    int two = 0;
    int three = 0;
    int four = 0;
    int five = 0;
    int six = 0;
    int seven = 0;
    int eight = 0;
    int nine = 0;
    //This holds the number of the current player (1 = player1(x), 2 = player2(o)
    int turn = 1;
    //These variables are holding the row and column number inputted by a player
    int row;
    int column;
    //These variables will be used later to check if a move is valid or not
    bool validmove;
    bool validrow;
    bool validcolumn;
    //The bellow variable holds the number of the winner, when the value is not 0 means the game is over
    int winner = 0;
    //The game loops again and again until somebody wins or it`s tie (winner = 3)
    while(winner == 0){
        //Draw the board from simple +,- and | characters
        cout << "1 2 3" << endl << "+---+---+---+" << endl << "1 | ";
        if(one == 0){
            //For each square the variable of that square is checked, and based on it`s value a character is written (0 - nothing, 1 = x, 2 = o)
            cout << "|";
        }
        else if(one == 1){
            cout << "x |";
        }
        else{
            cout << "o |";
        }
        if(two == 0){
            cout << " |";
        }
        else if(two == 1){
            cout << " x |";
        }
        else{
            cout << " o |";
        }
        if(three == 0){
            cout << " |";
        }
        else if(three == 1){
            cout << " x |";
        }
        else{
            cout << " o |";
        }
        cout << endl << "+---+---+---+" << endl << "2 | ";
        if(four == 0){
            cout << "|";
        }
        else if(four == 1){
            cout << "x |";
        }
        else{
            cout << "o |";
        }
        if(five == 0){
            cout << " |";
        }
        else if(five == 1){
            cout << " x |";
        }
        else{
            cout << " o |";
        }
        if(six == 0){
            cout << " |";
        }
        else if(six == 1){
            cout << " x |";
        }
        else{
            cout << " o |";
        }
        cout << endl << "+---+---+---+" << endl << "3 | ";
        if(seven == 0){
            cout << "|";
        }
        else if(seven == 1){
            cout << "x |";
        }
        else{
            cout << "o |";
        }
        if(eight == 0){
            cout << " |";
        }
        else if(eight == 1){
            cout << " x |";
        }
        else{
            cout << " o |";
        }
        if(nine == 0){
            cout << " |";
        }
        else if(nine == 1){
            cout << " x |";
        }
        else{
            cout << " o |";
        }
        cout << endl << "+---+---+---+" << endl << endl;
        //The following several lines will check if the game is over or not
        //Check`s the first line of the board for a winner combination (xxx or ooo)
        if(one == two && two == three && one != 0){
            //If the three squares have the same value and they are not free, then the winner is the player with the number from one of those squares
            winner = one;
        }
        //Checks the second line
        if(four == five && five == six && four != 0){
            winner = four;
        }
        //Checks the third line
        if(seven == eight && eight == nine && seven != 0){
            winner = seven;
        }
        //Checks the first column
        if(one == four && four == seven && one != 0){
            winner = one;
        }
        //Checks the second column
        if(two == five && five == eight && two != 0){
            winner = two;
        }
        //Checks the third column
        if(three == six && six == nine && three != 0){
            winner = three;
        }
        //Checks the diagonal from top left to bottom right
        if(one == five && five == nine && one != 0){
            winner = one;
        }
        //Checks the diagonal from bottom left to top right
        if(three == five && five == seven && three != 0){
            winner = three;
        }
        //If nobody wins this turn it checks to see if the board is full or not
        if(one != 0 && two != 0 && three != 0 && four != 0 && five != 0 && six != 0 && seven != 0 && eight != 0 && nine != 0 && winner == 0){
            //If the table is full, then it`s tie
            winner = 3;
        }
        //If somebody won or it`s a tie shows a message and break out from the loop - the game is over
        if(winner == 1 || winner == 2){
            cout << "Congratulations! Player " << winner << " is the winner!";
            break;
        }
        else if(winner == 3){
            cout << "Tie!";
            break;
        }
        // If the game is nor over yet than it writes a "Player x`s turn:" - where x is the number of the player (1 or 2)
        cout << "Player " << turn << "'s turn:" << endl;
        validmove = false;
        //Loops until the player makes a valid move
        while(!validmove){
            validrow = false;
            //Loops until the player chooses a valid row
            while(!validrow){
                cout << "Row: ";
                cin >> row;
                if(row == 1 || row == 2 || row == 3){
                    validrow = true;
                }
                else{
                    cout << endl << "Invalid row!" << endl;
                }
            }
            validcolumn = false;
            //Loops until the player chooses a valid column
            while(!validcolumn){
                cout << "Column: ";
                cin >> column;
                if(column == 1 || column == 2 || column == 3){
                    validcolumn = true;
                }
                else{
                    cout << endl << "Invalid column!" << endl;
                }
            }
            //After the user selects a valid row and column number the changes are applied to the game board and the variable of the selected square if it`s not occupied and it changes the value of the validmove variable from false to true
            switch(row){
                case 1:
                    if(column == 1){
                        if(one == 0){
                            one = turn;
                            validmove = true;
                        }
                    }
                    else if(column == 2){
                        if(two == 0){
                            two = turn;
                            validmove = true;
                        }
                    }
                    else{
                        if(three == 0){
                            three = turn;
                            validmove = true;
                        }
                    }
                    break;
                case 2:
                    if(column == 1){
                        if(four == 0){
                            four = turn;
                            validmove = true;
                        }
                    }
                    else if(column == 2){
                        if(five == 0){
                            five = turn;
                            validmove = true;
                        }
                    }
                    else{
                        if(six == 0){
                            six = turn;
                            validmove = true;
                        }
                    }
                    break;
                case 3:
                    if(column == 1){
                        if(seven == 0){
                            seven = turn;
                            validmove = true;
                        }
                    }
                    else if(column == 2){
                        if(eight == 0){
                            eight = turn;
                            validmove = true;
                        }
                    }
                    else{
                        if(nine == 0){
                            nine = turn;
                            validmove = true;
                        }
                    }
            }
            //If validmove is still false, then the selected square is occupied and a message is displayed
            if(!validmove){
                cout << "The sellected square is occupied!" << endl << "Select again:" << endl;
            }
            else{
                //Else if it was a valid move, then it`s the other player`s turn
                if(turn == 1){
                    turn = 2;
                }
                else{
                    turn = 1;
                }
            }
            cout << endl;
        }
        //The loop start`s over
    }   
    system("pause");
}

Thanks for the help in advance.

Hello!

I wanna ask you guys for help with this program. Here is my problem explanation: I want that user gives an input in the beginning of the program how big should tictactoe board would be. For example: input: 4 so board would be 4x4.

Code of the current problem is:

#include <iostream>

using namespace std;

int main(){
    //These variables are holding the value of each square (0 = free, 1 = x, 2 = o)
    int one = 0;
    int two = 0;
    int three = 0;
    int four = 0;
    int five = 0;
    int six = 0;
    int seven = 0;
    int eight = 0;
    int nine = 0;
    //This holds the number of the current player (1 = player1(x), 2 = player2(o)
    int turn = 1;
    //These variables are holding the row and column number inputted by a player
    int row;
    int column;
    //These variables will be used later to check if a move is valid or not
    bool validmove;
    bool validrow;
    bool validcolumn;
    //The bellow variable holds the number of the winner, when the value is not 0 means the game is over
    int winner = 0;
    //The game loops again and again until somebody wins or it`s tie (winner = 3)
    while(winner == 0){
        //Draw the board from simple +,- and | characters
        cout << "1 2 3" << endl << "+---+---+---+" << endl << "1 | ";
        if(one == 0){
            //For each square the variable of that square is checked, and based on it`s value a character is written (0 - nothing, 1 = x, 2 = o)
            cout << "|";
        }
        else if(one == 1){
            cout << "x |";
        }
        else{
            cout << "o |";
        }
        if(two == 0){
            cout << " |";
        }
        else if(two == 1){
            cout << " x |";
        }
        else{
            cout << " o |";
        }
        if(three == 0){
            cout << " |";
        }
        else if(three == 1){
            cout << " x |";
        }
        else{
            cout << " o |";
        }
        cout << endl << "+---+---+---+" << endl << "2 | ";
        if(four == 0){
            cout << "|";
        }
        else if(four == 1){
            cout << "x |";
        }
        else{
            cout << "o |";
        }
        if(five == 0){
            cout << " |";
        }
        else if(five == 1){
            cout << " x |";
        }
        else{
            cout << " o |";
        }
        if(six == 0){
            cout << " |";
        }
        else if(six == 1){
            cout << " x |";
        }
        else{
            cout << " o |";
        }
        cout << endl << "+---+---+---+" << endl << "3 | ";
        if(seven == 0){
            cout << "|";
        }
        else if(seven == 1){
            cout << "x |";
        }
        else{
            cout << "o |";
        }
        if(eight == 0){
            cout << " |";
        }
        else if(eight == 1){
            cout << " x |";
        }
        else{
            cout << " o |";
        }
        if(nine == 0){
            cout << " |";
        }
        else if(nine == 1){
            cout << " x |";
        }
        else{
            cout << " o |";
        }
        cout << endl << "+---+---+---+" << endl << endl;
        //The following several lines will check if the game is over or not
        //Check`s the first line of the board for a winner combination (xxx or ooo)
        if(one == two && two == three && one != 0){
            //If the three squares have the same value and they are not free, then the winner is the player with the number from one of those squares
            winner = one;
        }
        //Checks the second line
        if(four == five && five == six && four != 0){
            winner = four;
        }
        //Checks the third line
        if(seven == eight && eight == nine && seven != 0){
            winner = seven;
        }
        //Checks the first column
        if(one == four && four == seven && one != 0){
            winner = one;
        }
        //Checks the second column
        if(two == five && five == eight && two != 0){
            winner = two;
        }
        //Checks the third column
        if(three == six && six == nine && three != 0){
            winner = three;
        }
        //Checks the diagonal from top left to bottom right
        if(one == five && five == nine && one != 0){
            winner = one;
        }
        //Checks the diagonal from bottom left to top right
        if(three == five && five == seven && three != 0){
            winner = three;
        }
        //If nobody wins this turn it checks to see if the board is full or not
        if(one != 0 && two != 0 && three != 0 && four != 0 && five != 0 && six != 0 && seven != 0 && eight != 0 && nine != 0 && winner == 0){
            //If the table is full, then it`s tie
            winner = 3;
        }
        //If somebody won or it`s a tie shows a message and break out from the loop - the game is over
        if(winner == 1 || winner == 2){
            cout << "Congratulations! Player " << winner << " is the winner!";
            break;
        }
        else if(winner == 3){
            cout << "Tie!";
            break;
        }
        // If the game is nor over yet than it writes a "Player x`s turn:" - where x is the number of the player (1 or 2)
        cout << "Player " << turn << "'s turn:" << endl;
        validmove = false;
        //Loops until the player makes a valid move
        while(!validmove){
            validrow = false;
            //Loops until the player chooses a valid row
            while(!validrow){
                cout << "Row: ";
                cin >> row;
                if(row == 1 || row == 2 || row == 3){
                    validrow = true;
                }
                else{
                    cout << endl << "Invalid row!" << endl;
                }
            }
            validcolumn = false;
            //Loops until the player chooses a valid column
            while(!validcolumn){
                cout << "Column: ";
                cin >> column;
                if(column == 1 || column == 2 || column == 3){
                    validcolumn = true;
                }
                else{
                    cout << endl << "Invalid column!" << endl;
                }
            }
            //After the user selects a valid row and column number the changes are applied to the game board and the variable of the selected square if it`s not occupied and it changes the value of the validmove variable from false to true
            switch(row){
                case 1:
                    if(column == 1){
                        if(one == 0){
                            one = turn;
                            validmove = true;
                        }
                    }
                    else if(column == 2){
                        if(two == 0){
                            two = turn;
                            validmove = true;
                        }
                    }
                    else{
                        if(three == 0){
                            three = turn;
                            validmove = true;
                        }
                    }
                    break;
                case 2:
                    if(column == 1){
                        if(four == 0){
                            four = turn;
                            validmove = true;
                        }
                    }
                    else if(column == 2){
                        if(five == 0){
                            five = turn;
                            validmove = true;
                        }
                    }
                    else{
                        if(six == 0){
                            six = turn;
                            validmove = true;
                        }
                    }
                    break;
                case 3:
                    if(column == 1){
                        if(seven == 0){
                            seven = turn;
                            validmove = true;
                        }
                    }
                    else if(column == 2){
                        if(eight == 0){
                            eight = turn;
                            validmove = true;
                        }
                    }
                    else{
                        if(nine == 0){
                            nine = turn;
                            validmove = true;
                        }
                    }
            }
            //If validmove is still false, then the selected square is occupied and a message is displayed
            if(!validmove){
                cout << "The sellected square is occupied!" << endl << "Select again:" << endl;
            }
            else{
                //Else if it was a valid move, then it`s the other player`s turn
                if(turn == 1){
                    turn = 2;
                }
                else{
                    turn = 1;
                }
            }
            cout << endl;
        }
        //The loop start`s over
    }   
    system("pause");
}

Thanks for the help in advance.

You need a dynamic array or a vector to do this in a non-ghetto way. Do you know how to use those?

You need a dynamic array or a vector to do this in a non-ghetto way. Do you know how to use those?

Sorry I don't :( If you could help me, I would really appreciate it! Thanks for reply.

Hello!

I wanna ask you guys for help with this program. Here is my problem explanation: I want that user gives an input in the beginning of the program how big should tictactoe board would be. For example: input: 4 so board would be 4x4.

Code of the current problem is:

Lots of uncommentted code

Thanks for the help in advance.

Help with what? A problem statement and code does not explain the problem you are having.

And Greywolf333 is almost correct. While you do not need dynamic arrays, you really do need arrays.

I need an program, that ask user at the beginning how big board should be... If he inputs 4 then the board should be 4x4... This is only problem that haunts me :) Thanks for help.

Why does that haunt you? It will mean starting the code over, but you already know how to do a lot of it thanks to the code you posted.

Input n
Use a loop from 0 to n-1 to output your n rows.
Inside that, use another loop to output your n columns.
There's your board.

Get that part working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.