gpt4 book ai didi

c++ - Tic Tac Toe中如何分出胜负以及如何不让双方都进入同一个位置?

转载 作者:行者123 更新时间:2023-11-28 04:14:49 26 4
gpt4 key购买 nike

我现在想用我的代码做两件事。1) 检查获胜者2) 不让双方玩家在同一个位置进入eg.如果player1已经在board[0][0]='X'处输入了value,player2再次进入board[0][0]='0',如何不让player再次进入board [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<<"\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();
}
}

预期输出:每当两个玩家都进入同一数组位置 board[0][0] 时,它应该为插入该位置的第二个玩家显示“无效选择”。

同样,每当 tictactoe 的获胜条件出现时。即如果1)连续3行有相同的值2)连续3个cols具有相同的值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] 的结果( bool 值,true 或 false)与 board[0 ][2] 这显然不是您想要的。

您不能假设在数学上正确的东西在 C++ 中也适用。

如果您的电路板有一致的索引方案,程序会更简单。您要求您的用户为正方形输入 1 到 9 之间的数字,但在您的程序中您有一个 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";
...

关于c++ - Tic Tac Toe中如何分出胜负以及如何不让双方都进入同一个位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56912558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com