我很困惑。我正在尝试使用Windows c ++ visual创建一个tic tac toe游戏。到目前为止,我一直做得很好,直到我不断出错。我试着寻求帮助,但没有一个答案是正确的。这是我的练习题。
提示用户选择棋盘上的方框,即1到9之间的数字,其中1位于左上角。
使用cin.get(box)获取框号,并使用isdigit来验证它是否为 数; 1 | 2 | 3 4 | 5 | 6 7 | 8 | 9 如果盒子可用,在那里放入适当的X或O并切换球员,即X变为O,反之亦然。 如果该框不可用,则警告用户并获取另一个框,直到他们选择有效的打开框。
选择所有位置后显示“游戏结束!”;
#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...
}
答案 0 :(得分:3)
你需要一个声明来追踪if
即使它只是一个;
但也许你想要
if (c > '9' || c < '0')
cout << "Not a Number!";
答案 1 :(得分:0)
好的,所以这里的问题是“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语句。只需添加分号即可:
if (c > '9' || c < '0');
但这意味着你没有处理错误,这是非常糟糕的,所以至少在if语句中输入一条错误消息告诉用户他们犯了错误。
答案 2 :(得分: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;