2011-07-26 11 views
-1

私はとても混乱しています。私は窓を使用してチックタックつま先のゲームを作成しようとしている+ + Visual。これまでのところ、私は間違いを犯すまで、うまくやっていました。私は助けを求めようとしましたが、答えのどれも正しいように見えませんでした。これは私の練習問題です。C++ Tic Tac Toe Game

  1. Tic Tac Toeボードを表示するdisplayBoardを実装します。
  2. プロンプトボード上のボックスを選択するユーザ。つまり、1と9の間の数字を1つは左上隅にします。

    cin.get(ボックス)を使用してボックス番号を取得し、それが の番号であることを確認します。 1 | 2 | 3 4 | 5 | 6 7 | 8 | 9 ボックスが利用可能な場合は、そこに適切なXまたはOを入れてプレイヤーを切り替えます。つまり、XはOになり、その逆もあります。 ボックスが利用可能でない場合は、ユーザーに警告し、有効な開いたボックスを選択するまで別のボックスを取得します。

  3. すべてのスポットが選択された後「ゲームオーバー!

  4. TicTacToeクラスを使用し、上記のすべての機能をテストする主な機能を記述します。

。あなたも、そのわずか場合であれば

後に行くためのステートメントを必要とする

#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... 
} 
+1

どのようなエラーが表示されますか?そして、エラーを生成するコードは何ですか? – Jodaka

+0

だから?質問はなんですか?コードはどこですか?エラーはどこですか? – blottedscience

+0

コード内のコメント –

答えて

3

が、ここでの問題は、 "IF" 文ですので、多分あなたは、

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

if文はif(c> '9' || c <'0')int number = c - '0'; 'と等しいので、ifの後の文が与えられます。しかし、あなたが正しく指摘したように、それはおそらく彼が望んでいたものではありません。 – evnu

+0

問題は変数の数値スコープです。 –

0

[OK]をします。問題はif文を閉じていないことです。文は条件文になるとした後、次の行をif文あなたが作成し、それから、それが実行すべきコードのブロックの周りにカッコを置かないときそれでは、コンパイラが見ていることは

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; 

です。あなたがする必要があるのはif文を閉じることです。したがって、少なくとも、彼らが作ったユーザーに伝えるためにif文でエラーメッセージを入れ、

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

が、それはあなたがかなり悪いですエラーを処理しないことを意味:単純にセミコロンを追加するだけで十分です間違い。

0
//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;