我现在想用我的代码做两件事。 1)检查获胜者 2)不要让两个玩家进入同一位置 例如,如果玩家1已经在board [0] [0] ='X'中输入了值,而玩家2又进入了board [0] [0] ='0',我怎么不能让玩家再次进入board [0] [0] ='0' [0] [0]?
同样,我如何检查获胜者?
我试图通过检查来检查获胜者
if(board[0][0]==board[0][1]==board[0][2]
然后获胜者是玩家1。(玩家1按下输入后)
但是,它没有用。
说实话,我仍然对下一个问题仍然没有逻辑,即如何不让两个玩家进入同一位置?
#include <cstdlib>
#include<iostream>
using namespace std;
int choice;
char board[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};
void display_board()
{
system("cls");
cout<<"..............................................Tick Cross Game by Pakistani coder........................................"<<endl;
cout<<"\t\t\t\t\tPlayer1[X]\n\t\t\t\t\tPlayer2[0]\n";
cout<<"\t\t\t\t\t | | \n";
cout<<"\t\t\t\t\t | | \n";
cout<<"\t\t\t\t\t__"<<board[0][0]<<"___ __"<<board[0][1]<<"___ __"<<board[0][2]<<"\n";
cout<<"\t\t\t\t\t | | \n";
cout<<"\t\t\t\t\t | | \n";
cout<<"\t\t\t\t\t__"<<board[1][0]<<"___ __"<<board[1][1]<<"___ __"<<board[1][2]<<"\n";
cout<<"\t\t\t\t\t | | \n";
cout<<"\t\t\t\t\t | | \n";
cout<<"\t\t\t\t\t___"<<board[2][0]<<"__ __"<<board[2][1]<<"___ __"<<board[2][2]<<"\n";
cout<<"\t\t\t\t\t | | \n";
/// cout<<"\t\t\t\t\t | | \n";
}
void player_turn()
{
cout<<"\n\tPlayer1[X] turn:";
cin>>choice;
switch(choice)
{
case 1:
board[0][0]='X';
break;
case 2:
board[0][1]='X';
break;
case 3:
board[0][2]='X';
break;
case 4:
board[1][0]='X';
break;
case 5:
board[1][1]='X';
break;
case 6:
board[1][2]='X';
break;
case 7:
board[2][0]='X';
break;
case 8:
board[2][1]='X';
break;
case 9:
board[2][2]='X';
break;
default:
cout<<"invalid choice";
break;
///value will go there but need to display the board also
}
display_board();
//checkwin();
cout<<"player 2 turn\n";
cin>>choice;
switch(choice)
{
case 1:
board[0][0]='0';
break;
case 2:
board[0][1]='0';
break;
case 3:
board[0][2]='0';
break;
case 4:
board[1][0]='0';
break;
case 5:
board[1][1]='0';
break;
case 6:
board[1][2]='0';
break;
case 7:
board[2][0]='0';
break;
case 8:
board[2][1]='0';
break;
case 9:
board[2][2]='0';
break;
default:
cout<<"invalid choice";
break;
}
display_board();
}
int main()
{
/*for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<board[i][j];
}
}*/
while(1)
{
display_board();
player_turn();
display_board();
}
}
预期输出: 每当两个玩家进入同一阵列位置板[0] [0]时,第二个插入该位置的玩家都应显示“无效选择”。
同样,每当出现触须的获胜条件时。即如果 1)连续的3行具有相同的值 2)连续3个col具有相同的值 3)对角元素的值相同(3)
答案 0 :(得分:1)
if(board[0][0]==board[0][1]==board[0][2]
应该是
if (board[0][0] == board[0][1] && board[0][1] == board[0][2]
第一个版本将board[0][0]==board[0][1]
的结果(布尔值,true或false)与board[0][2]
进行比较,而这显然不是您想要的。
您不能假设数学上正确的东西也可以在C ++中工作。
如果您的电路板具有一致的索引编制方案,则该程序会更简单。您要求用户输入一个从1到9的平方数,但是在您的程序中您有一个2数组从0到2。您可以通过做三件事来使您的生活更轻松
1)让用户输入从0到2的两个数字作为正方形。
2)更改程序,使您的开发板从1到9为阵列
3)编写一个函数,将用户输入的内容(从1到9)转换为您的开发板使用的东西(0到2之间的两个数字)。
所有这些选项都将消除您正在使用的大量switch语句的需求。选项3可能是最好的。
类似这样的事情(您也可以使用%
和/
来做一些聪明的事情,这会大大缩短此功能,但是暂时保持简单)。
bool translate_coordinates(int choice, int& x, int& y)
{
switch (choice)
{
case 1:
x = 0;
y = 0;
return true;
case 2:
x = 0;
y = 1;
return true;
case 3:
x = 0;
y = 2;
return true;
case 4:
x = 1;
y = 0;
return true;
case 5:
x = 1;
y = 1;
return true;
case 6:
x = 1;
y = 2;
return true;
case 7:
x = 2;
y = 0;
return true;
case 8:
x = 2;
y = 1;
return true;
case 9:
x = 2;
y = 2;
return true;
default:
return false; // invalid choice, let the caller handle this
}
}
然后您的代码将变成这样
void player_turn()
{
cout<<"\n\tPlayer1[X] turn:";
cin>>choice;
int x, y;
if (translate_coordinates(choice, x, y))
board[x][y] = 'X';
else
cout << "invalid choice\n";
display_board();
//checkwin();
cout<<"player 2 turn\n";
cin>>choice;
if (translate_coordinates(choice, x, y))
board[x][y] = 'O';
else
cout << "invalid choice\n";
...
答案 1 :(得分:0)
如何使用此代码?
int checkwin()
{
if (square[1] == square[2] && square[2] == square[3])
return 1;
else if (square[4] == square[5] && square[5] == square[6])
return 1;
else if (square[7] == square[8] && square[8] == square[9])
return 1;
else if (square[1] == square[4] && square[4] == square[7])
return 1;
else if (square[2] == square[5] && square[5] == square[8])
return 1;
else if (square[3] == square[6] && square[6] == square[9])
return 1;
else if (square[1] == square[5] && square[5] == square[9])
return 1;
else if (square[3] == square[5] && square[5] == square[7])
return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
&& square[4] != '4' && square[5] != '5' && square[6] != '6'
&& square[7] != '7' && square[8] != '8' && square[9] != '9')
return 0;
else
return -1;
}