如何在TIC TAC TOE中检查获胜者,以及如何不让两个玩家进入同一位置
How to check winner in Tic Tac Toe as well as how to not let the both players to enter at the same position?
我现在想用我的代码做2件事。1(检查获胜者2(不要让两个球员都在同一位置进入例如,如果player1已经在板上输入值[0] [0] ='x',并且player2再次进入董事会[0] [0] ='0',我如何不让播放器再次在板上输入[0] [0]?
同样,我如何检查获胜者?
我试图通过检查
来检查获胜者if(board[0][0]==board[0][1]==board[0][2]
然后获胜者为player1。(Player1按下他的输入后(
但是,它没有用。
我想诚实地说,我仍然没有为下一个问题而获得逻辑。
#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<<"tttttPlayer1[X]ntttttPlayer2[0]n";
cout<<"ttttt | | n";
cout<<"ttttt | | n";
cout<<"ttttt__"<<board[0][0]<<"___ __"<<board[0][1]<<"___ __"<<board[0][2]<<"n";
cout<<"ttttt | | n";
cout<<"ttttt | | n";
cout<<"ttttt__"<<board[1][0]<<"___ __"<<board[1][1]<<"___ __"<<board[1][2]<<"n";
cout<<"ttttt | | n";
cout<<"ttttt | | n";
cout<<"ttttt___"<<board[2][0]<<"__ __"<<board[2][1]<<"___ __"<<board[2][2]<<"n";
cout<<"ttttt | | n";
/// cout<<"ttttt | | n";
}
void player_turn()
{
cout<<"ntPlayer1[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 turnn";
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]中输入时,就应该显示第二个插入该位置的玩家的"无效选择"。
同样,每当发生Tictactoe的获胜条件时。即,如果1(连续3行具有相同的值2(连续3个COL具有相同的值3(对角元素的值相同(3(
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的数字,但是在您的程序中,您的2D数组从0到2。您可以做三件事,以使您的生活更轻松
1(使用户输入两个正方形的0到2的数字。
2(更改您的程序,以使您的董事会是1到9
的数组3(编写一个将用户输入(1至9(转化为董事会使用的功能(两个数字为0到2(。
这些选项中的任何一个都会消除您使用的这些大型开关语句的需求。选项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<<"ntPlayer1[X] turn:";
cin>>choice;
int x, y;
if (translate_coordinates(choice, x, y))
board[x][y] = 'X';
else
cout << "invalid choicen";
display_board();
//checkwin();
cout<<"player 2 turnn";
cin>>choice;
if (translate_coordinates(choice, x, y))
board[x][y] = 'O';
else
cout << "invalid choicen";
...
如何使用此代码?
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;
}
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 如何返回一个类的两个对象相加的结果
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 两个字符串在 c++ 中不相等
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 两个文件使用彼此的功能-如何解决
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 将fold表达式与std::一起用于两个元组
- 如何在C++中比较两个char数组
- 给定两个偶数,求出它们之间所有偶数的平方和
- 比较两个大小不等的映射c++
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 如何在for循环中包含两个索引值的测试条件
- 在声明中合并两个常量"std::set"(不是在运行时)
- 如何在TIC TAC TOE中检查获胜者,以及如何不让两个玩家进入同一位置