开发者

C++ Tic Tac Toe Game

I am so confused. I am trying to create a tic tac toe game using windows c++ visual. So far I was doing good until I kept getting errors. I tried looking for help but none of the answers seemed right. This is my practice problem.

  1. Implement displayBoard to display Tic Tac Toe board.
  2. Prompt User for a box on the board to select, i.e. a number between 1 and 9 with 1 being the upper left corner.

    use cin.get(box) to get the box number and isdigit to verify it is a number; 1 | 2 | 3 4 | 5 | 6 7 | 8 | 9 If the box is available put the appropriate X or O in there and switch players, i.e. X becomes O and vice versa. If the box is NOT available warn the user and get another box until they select a valid open box.

  3. After all spots have been select Display "Gam开发者_开发百科e Over!";

  4. Write a main function to use the TicTacToe class and test all of the above functionality.

.

#include<iostream>

using namespace std;


class TicTacToe {
public:
    void displayBoard();
    void getMove();
    void playGame();
private:
    char board[9];
    char player; // Switch after each move.
};

int main ()
{
    TicTacToe ttt;

    // you need to do the following in a loop 9 times
    ttt.playGame();
}

void TicTacToe::playGame()
{
    getMove();
    // Your implementation here...
}

void TicTacToe::displayBoard()
{
    // Your implementation here...
}

void TicTacToe::getMove()
{
    cout << "Enter Box: ";
    char c;
    cin.get(c);
    if (c > '9' || c < '0')
        // Error message here.

    int number = c - '0';

    cout << "your number is " << number;
    // Your implementation here...
}


you need a statement to go after the if

even if its just a ;

but maybe you want

if (c > '9' || c < '0')
        cout << "Not a Number!";


Ok, so the issue here is the "if" statement. The problem is that you haven't closed the if statement. So what the compiler sees is

cout << "Enter box: ";
char c;
cin.get(c);

if(c > '9' || c < '0')
{
    //Compiler thinks that it should only convert the character
    //to a number if you got the *wrong* number
    int number = c - '0';
}

//the integer number when out of scope in the if statement. So now it doesn't exist
//which means you will get a "variable not declared" error
cout << "your number is " << number;

When you create an if statement and you don't put braces around the block of code it should execute, then the next line after the if statement becomes the conditional statement. What you need to do is close the if statement. simply adding a semicolon is sufficient:

if (c > '9' || c < '0');

but it means you don't handle the error, which is pretty bad, so at least put an error message in the if statement to tell the user that they've made a mistake.


//Play Tic Tac Toe game between user and computer

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

using namespace std;

char BLANK='B';
/***************** Display the Matrix **********************************************/
//Display the matrix
void display(char matrix[3][3])
{
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++)
            cout<<matrix[i][j]<<"\t";
        cout<<endl;
    }
}

/************** Chance of WIN Function *****************************************************/

//Funtion to detect the chance of either for user or systemĵ
int chance_of_win(char matrix[3][3],int i, int j,char choice){

int result=0;//This variale is used to return the required position
char other_choice;//This variable is used for other choice of the variable choice

if(choice=='o')
    other_choice='x';
else
    other_choice='o';

int count1=0;//This variable is used to check the count upto 2

    //Diagonal Intelligent
    if(i==j){
        for(int k=0;k<3;k++){
            if(matrix[k][k]==choice)
                count1++;

            if(count1==2){   // That means user is going to win and system has to stop that
                for(int k=0;k<3;k++){
                    if(matrix[k][k]!=choice && matrix[k][k]!=other_choice){
                        int temp=k;
                        temp=temp*10;
                        result=temp+k;
                        return result;
                    }
                }
            }
        }//for looop ends here
    }//If Structure ends here

count1=0; //Reinitilize the count to zero

    //Reverse Diagonal intelligent
    for(int m=0,n=2;m<3,n>=0;m++,n--){
        if(matrix[m][n]==choice){
            count1++;
        }

        if(count1==2){   // That means user/system is going to win reverse diagnally
        for(int m=0,n=2;m<3,n>=0;m++,n--){
                if(matrix[m][n]!=choice && matrix[m][n]!=other_choice){
                    int temp=m;
                    temp=temp*10;
                    result=temp+n;
                    return result;
                }
            }
        }//End of If structure
    }//End for loop

count1=0; //Reinitilize the count to zero

    //Row Intelligent
    for(int k=0;k<3;k++){
        if(matrix[i][k]==choice)
            count1++;

        if(count1==2){   // That means user/system is going to win
            for(int k=0;k<3;k++){
                if(matrix[i][k]!=choice && matrix[i][k]!=other_choice){
                    int temp=i;
                    temp=temp*10;//for the ith coordiante
                    result=temp+k;//for the jth cordinate
                    return result;//Return the required attribute of i and j
                }
            }
        }
    }//for looop ends here

count1=0; //Reinitilize the count to zero

    //Column Intelligent
    for(int k=0;k<3;k++){
        if(matrix[k][j]==choice)
            count1++;

        if(count1==2){   // That means user is going to win and system has to stop that
            for(int k=0;k<3;k++){
                if(matrix[k][j]!=choice && matrix[k][j]!=other_choice){
                    int temp=k;
                    temp=temp*10;//for the ith coordinate
                    result=temp+j;//for the jth coordinate
                    return result;//Return the required attribute of i and j
                }
            }
        }
    }//for looop ends here


return result;
}//function ends here

/******************* Check Win Bool Function ******************************************************/

//This function is used to check the win of the system/user
bool checkwin(char matrix[3][3],int i, int j,char choice){
bool flag=false;//Initialize the chance of win false
int count1=0;
    //Diagonal checkwin forward
    if(i==j){
        for(int k=0;k<3;k++){
            if(matrix[k][k]==choice){
                count1++;
            }
            if(matrix[k][k]==BLANK)
                break;
        }

        if(count1==3)//Means all diagonal elements are equal
            flag=true;
    }

    //If the Diaganoal Forward is same then return
    if(flag){
        cout<<"Diagonal Win\n";
        return flag;
    }

    //Reverse Diagonal checkwin
    for(int m=0,n=2;m<3,n>=0;m++,n--){
        if(matrix[m][n]!=choice || matrix[m][n]==BLANK){
            flag=false;//If diagonal is not same
            break;
        }
         flag=true;
    }

    //If the Reverse Diaganoal Forward is same then return
    if(flag){
        cout<<"Reverse Diagonal Win\n";
        return flag;
    }

    //Row checkwin
    for(int k=0;k<3;k++){
        if(matrix[i][k]!=choice || matrix[i][k]==BLANK){
            flag=false;// Row is not same
            break;
         }
         flag=true;
    }
    //If row is same then return
    if(flag){
        cout<<"Row Win\n";
        return flag;
    }

    //Column checkwin
    for(int k=0;k<3;k++){
        if(matrix[k][j]!=choice || matrix[k][j]==BLANK){
            flag=false;//Column is not same
            break;
         }
         flag=true;
    }
    //If the Column is same then return
    if(flag){
        cout<<"Column Win\n";
        return flag;
    }


    return flag;//return the result false result i.e there is no chance of win
                //as we have checked all the conditions
}

/*************************  Main Function **************************************************/

int main(){

char matrix[3][3];

bool flag;
int toss;
srand(time(NULL));
toss=rand()%2;

if(toss){
    flag=true;
    cout<<"User Wins the Toss\n";
}
else{
    flag=false;
    cout<<"System Wins the Toss\n";
}
//Initialise all the elements of matrix to BLANK i.e. Blank

for(int i=0;i<3;i++)
    for(int j=0;j<3;j++)
        matrix[i][j]=BLANK;

cout<<"For user the choice is o\n";
cout<<"For system the choice is x\n";

int v=1;//Initialise the the variable v , it has the increment till 9 to cover all the elements of the matrix

bool system1=false;//To check the chance of win of system

int user_status=0;//To check the chance of win of user and accordingly system will put his move
int system_status;////To check the chance of win of system and accordingly system will put his move

while(v<=9){

int i,j;// "i" is for the row coordinate and "j" is for the column coordinate

    if(flag==true){// If user win's the toss
        cout<<"Yours turn\n";
        cout<<"Enter the row coordinate";
        cin>>i;
        i--;//For user convenience i th coordinate
        cout<<"Enter the column coordinate";
        cin>>j;
        j--;//For user convenience jth coordinate
        if(matrix[i][j]==BLANK)
            matrix[i][j]='o';//Put the user move
        else{
            cout<<"Already Occupied\n"; //Warn user to fill the blank space
            continue;//Don't count this in "variable v" means don't increment the variable "v"
                    //as it was invalid move
        }

        // After three attempts it will check , this code is for system
        if(v>2)
            user_status=chance_of_win(matrix,i,j,'o');//User chance of win

        //checkwin whether game is over i.e whether user win
        if(v>4){
            if(checkwin(matrix,i,j,'o')){
                cout<<"\n\tBingo !! User win\n\tCongrats Well played\n";
                display(matrix);
                return 0;
            }
         }

        flag=false;// Let the System play next move
        display(matrix);//display the matrix
        cout<<"\nWait! System turns\n";
    }

    else{//System's Turn
        if(system1==true){//Chance of System of winning
            j=system_status%10;//get the j coordinate
            i=system_status/10;//get the i coordinate
            //cout<<"System chance win i = "<<i<<" j = "<<j<<endl;

            /*If Structure of Check whether place is empty for winning the system*/
            if(matrix[i][j]==BLANK){//Is place is empty
                matrix[i][j]='x';
                if(checkwin(matrix,i,j,'x')){
                    display(matrix);//Display the current scenerio of the game
                    cout<<"Sorry You loose !! System wins\n";
                    return 0;
                }//end if structure of check win
            }
            else//Means space is occupied by user, and chance of winning by system is lost
                system1=false;//Now let the system to defense the user's move
            /*Ends If Structure of Check whether place is empty for winning the system*/
        }

        if(system1==false){
            if(user_status!=0){//If User is going to win , warn the system
                j=user_status%10;//get the j coordinate
                i=user_status/10;//get the i coordinate
                //cout<<"User chance win i = "<<i<<" j = "<<j<<endl;
            }
            else{
                if(v==9){//There is no point to check random number if noone is winning at the end
                        cout<<"\t\tMatch draw"<<endl;
                        return 0;
                }
                srand(time(NULL));
                i=rand()%3; //random i coordinate
                srand(time(NULL));
                j=rand()%3; //random j coordinate
            }
            /*If System turn's of writting*/
            if(matrix[i][j]==BLANK)
                matrix[i][j]='x';
            else
                continue;
            /*End If Structure of writting system turn's*/
        }//end If Structure is sytem chance of win = false

        if(v>2){// This condition is necessary to avoid irrevelant check
            system_status=chance_of_win(matrix,i,j,'x'); //System chance of win
            if(system_status==0){
                    system1=false;
                    cout<<"\n Not System Chance of win \n";
            }
            else{
                system1=true;
                cout<<"\n System Chance of win \n";
            }
        }
        else{
            system_status=0;
            system1=false;
        }

        flag=true;//Let the user play his next move
        display(matrix);

    }
v++;
}//end of while v<9

return 0;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜