Skip to main content
Tweeted twitter.com/StackCodeReview/status/1189240632067416064
Became Hot Network Question
edited tags
Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327
Spelling and grammar
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327

So,iI have been learning CppC++ for 3 months now. AndI decided to make something by myself.so i've, so I've made this little integer based-based TIC TAC TOE console game.I

I need some advice on how to improve my code for better structure and and better readability.

So,i have been learning Cpp for 3 months now. And decided to make something by myself.so i've made this little integer based TIC TAC TOE console game.I need some advice on how to improve my code for better structure and and better readability.

I have been learning C++ for 3 months now. I decided to make something by myself, so I've made this little integer-based TIC TAC TOE console game.

I need some advice on how to improve my code for better structure and and better readability.

Source Link

Tic Tac Toe console program

So,i have been learning Cpp for 3 months now. And decided to make something by myself.so i've made this little integer based TIC TAC TOE console game.I need some advice on how to improve my code for better structure and and better readability.

#include <iostream>
#include <stdlib.h>


using namespace std;

int arr[9]={0,1,2,3,4,5,6,7,8}; //Global values of the GAME BOARD for Every move solt 

void gameboard(int x);      // function for taking the turn of the player and OVERWRITE the player's value i.e [69 for player 1 or 96 for player 2] to the GLOBAL gameboard array as a slot ie [ arr[turn value of the player] ]
void turn(int *player);     // function for checking and validating  player's move and pass that to to the game board function 
void check_win(int player); // function for checking if a player has own or the game has drawn
void initial_board();       //function for printig the initial GAME BOARD before any players turn
void game_over();           //function for endgame condition 

int main(int argc, char** argv) {
    int a=10,b=11;       // player 1 == 69 and player 2 == 96
    
    initial_board();    // printing the initail GAME BOARD for all the empty slots availabe for turn
    
    while(true){        //initiating an infinite loop untill a player has own the game or the game has drawn
        turn(&a);       // executing the function for PLAYER 1's TURN [69]
        turn(&b);       // executing the function for PLAYER 2's TURN [96]  
    }
            
    return 0;
}

void gameboard(int x,int player){ // pass cohice as x and p as player
    system("cls");
    cout<<"        TIC TAC TOE\t\n\n";
    cout<<" Player 1: 10 -- Player 2: 11\n\n";
    arr[x]=player;       // overwrite the element of the array that is the turn input or choice to the player value[example: if (choice is 1 and player valuse is 69) then arr[1]=69 ]
    for(int i=0;i<9;i++){   // display the game board
        if(i%3==0&&i!=0){   // formatting condition
            cout<<"\n";
            cout<<"\n";
        }    
        cout<<"   | "<<arr[i]<<" | ";    // formatting   
    }
    cout<<"\n";
    check_win(player);  // finally check for win and draw conditions and exit if any of them satisfies 
}

void turn(int *player){     // [*player] pass by ref for either player 1 [69] or player 2 [96],,though it can be using pass by valuse as well 
    int choice,p;           // input choice variable for turn and p variable for player value and passing that to the game_board function
    p= *player;             // pass the value of player ie 69 or 96 to p variable 
    cout<<"\n"<<" "<<p<<" it is your Turn!!";
    cin>>choice;            // input turn as choice
    if(choice > 8){         // validate if choice is over 8 or not 
        do{
            cout<<"Out of range: Try again-->";
            cin>>choice;
        }while(choice > 8);  // end validation and go for next validation
    }   
    for(int i=0; i<9; i++)  // iterate the global slot array 
    {
        if(arr[choice]==10||arr[choice]==11)  // check if the turn or choice already exist as player values ie[69 or 96] in the global array
        {
            do{                               // if so then,retake the move because taht is already preasant in the board
                cout<<"invalid move!!";
                cout<<"place your turn!!";
                cin>>choice;                 // input turn as choice(if choice is under 8) again until it does not exists in the global GAMEVOARD array as player value
                if(choice > 8){             //another check for choice over 8. if so,then repeaat input untill its under 8 and doesnt exists in the global gameboard array as player value
                    do{
                        cout<<"Out of range: Try again-->";
                        cin>>choice;
                    }while(choice > 8);
                }   
            }while(arr[choice]==10||arr[choice]==11);
            break;                        // if all the validation condition satisfies then breakout of the loop nad the input[choice] as final value 
        }   
    }
    gameboard(choice,p);                 // pass the final turn input ie choice to the gameboard function and pass the player value as p            
}

/// Need to modify win conditions later ////

void check_win(int player){              //pass the player value as player
    if(arr[1]+arr[2]+arr[0]==3*10||arr[1]+arr[2]+arr[0]==3*11){     //win condition  outputs the winner player's name for all
        cout<<"\n\n\n"<<" player "<<player<<" has won the game.Congratulations!!!"<<endl;  
        game_over();
    }else if(arr[3]+arr[4]+arr[5]==3*10||arr[3]+arr[4]+arr[5]==3*11){  //win condition  outputs the winner player's name for all
        cout<<"\n\n\n"<<" player "<<player<<" has won the game.Congratulations!!!"<<endl;
        game_over();
    }else if(arr[6]+arr[7]+arr[8]==3*10||arr[6]+arr[7]+arr[8]==3*11){  //win condition  outputs the winner player's name for all
        cout<<"\n\n\n"<<" player "<<player<<" has won the game.Congratulations!!!"<<endl;
        game_over();
    }else if(arr[0]+arr[4]+arr[8]==3*10||arr[0]+arr[4]+arr[8]==3*11){  //win condition  outputs the winner player's name for all
        cout<<"\n\n\n"<<" player "<<player<<" has won the game.Congratulations!!!"<<endl;
        game_over();
    }else if(arr[2]+arr[4]+arr[6]==3*10||arr[2]+arr[4]+arr[6]==3*11){  //win condition  outputs the winner player's name for all
        cout<<"\n\n\n"<<" player "<<player<<" has won the game.Congratulations!!!"<<endl;
        game_over();
    }else if(arr[0]+arr[3]+arr[6]==3*10||arr[0]+arr[3]+arr[6]==3*11){  //win condition  outputs the winner player's name for all
        cout<<"\n\n\n"<<" player "<<player<<" has won the game.Congratulations!!!"<<endl;
        game_over();
    }else if(arr[1]+arr[4]+arr[7]==3*10||arr[1]+arr[7]+arr[7]==3*11){  //win condition  outputs the winner player's name for all
        cout<<"\n\n\n"<<" player "<<player<<" has won the game.Congratulations!!!"<<endl;
        game_over();
    }else if(arr[2]+arr[5]+arr[8]==3*10||arr[2]+arr[5]+arr[8]==3*11){  //win condition  outputs the winner player's name for all
        cout<<"\n\n\n"<<" player "<<player<<" has won the game.Congratulations!!!"<<endl;
        game_over();
    }else if(arr[0] != 0 && arr[1] != 1 && arr[2] != 2 && arr[3] != 3 && arr[4] != 4 && arr[5] != 5 && arr[6] != 6 && arr[7] != 7 && arr[8] != 8){
        cout<<"\n\n\n THE GEME IS DRAWN!!!!!!";                       // draw condition
        game_over();
    }                   
}

void initial_board(){   
    cout<<"        TIC TAC TOE\t\n\n";
    cout<<" Player 1: 10 -- Player 2: 11\n\n";
    
    for(int i=0;i<9;i++){   // iterating the global array to show all the slots
        if(i%3==0&&i!=0){   // formatting the slots
            cout<<"\n";
            cout<<"\n";
        }   
        cout<<"   | "<<arr[i]<<" | ";    // printing the slot values as slots
    }
    cout<<"\n";   //formatting
}

void game_over(){                        // function for ending the game 
    cout<<" \n\n :The Game is over:\n\n";
    system("pause");
    exit(1);
}
/*
    #########################################
    #########     END OF PROJECT    #########
    #########################################
*/
```