- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我现在想用我的代码做两件事。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/
如果我 mov, eax 12345 和之后的 mov var, eax (假设 var 是一个 32 位的 int 等..等等)并输出 var 稍后它会正确输出。 与 ax 相同。 mov ax,
我有这个代码: for($nrt=0; $nrt"; if($sidesIndexes[$nrt]==$nrt) { echo "am I in??? ".$sidesInde
我正在阅读The Go Programming Language的8.5章,并陷入一些代码。下面的代码列表。 func main() { naturals := make(chan int)
我写了一个 MySQL 查询用于将数据导出到文本文件。 查询运行成功,但结果与我的预期不符。 我想在列之间没有间距的结果。 select sample_export_record1_2013.
在普通的 Excel 窗口中,我可以打开 VBE 并通过触摸键序列插入一个新模块:ALT+F11、ALTim 全部不使用鼠标。有没有办法打开 VBE 并导航到 本工作手册 不使用鼠标的代码区域? 最佳
我正在使用 axios 发出 http 请求。在 .then() 内部,我正在使用另一个 axios 调用。最后,我有第三个 then(),它应该在第二个 then 之后运行,但实际上并没有这样做。
我需要在 cocos2d 项目中播放视频..我的问题是:如何将 MPMoviePlayerController 放入我的 View 中,如下所示:? UIView *theView = [[CCDir
我正在学习 Angular。以下代码有效: .controller('abc', function ($scope, $http) { $http.get("/Handlers/Authenticat
目前我正在使用 WPF 学习 C#。我的主要方法是尽我所能使用 MVVM 模式,但现在我有点困惑。 在我所有 View 的应用程序中,我有一个 View 模型: private DruckviewVi
关于将 G 邮件提取到 Google 电子表格,我该如何添加 IF 以按主题驳回特定电子邮件?例如:电子邮件回复(主题中带有“RE:”)。我不希望这些电子邮件出现在我的电子表格中。 我尝试过使用 LO
我正在尝试使用 Spotify API 并进入数组。 const App = () => { const [isLoading, setIsLoading] = useState(true);
我有一个 html 模板,并且有一个条件为 --> 的代码 --> window.jQuery || document.write(""+"");
我正在开发一个 Android 应用程序,该应用程序会暴力破解从 int 创建的 MD5 和。 暴力破解部分工作正常。 (我可以sysout最终值并且它是正确的。) 我在将输出值发送到警报对话框时遇到
我正在创建一个界面,用户可以通过该界面生成多系列折线图,并控制绘制哪些数据集。 要绘制哪些数据集由复选框控制。页面加载时,默认数据集以图表形式呈现,并且 $('input:checkbox.data-
我尝试将有向无环图绘制为力布局。 但是我注意到,尽管为每个组元素灌输了进入/退出条件,弹出的节点/链接并没有从 DOM 中删除它们自己。 相反,弹出的节点/链接在力布局中卡住;这意味着没有调用进入/退
这里是新手。我不知道它是怎么发生的,但我正在处理一个程序,当我去调试并进入时,黄色箭头走到了我代码的最后并跳过了整个 block 。有快速解决方法吗? 最佳答案 按 F11,或单击工具栏上的“Step
我的 Action 栏 sherlock 中有一个列表。我想在用户点击该列表时得到。我不想知道用户何时点击某个项目,我已经知道 (onNavigationItemSelected)。 在我的 onCr
** 你好 **我如何从 ci 中的 mysql 日期获取 eurodate 来工作......无法弄清楚 - 请帮忙 想要获取日期 YY-mm- dd -> dd-mm-yy提前致谢 最佳答案 $t
我有以下脚本: #!/bin/bash ls -1 | while read d do [[ -f "$d" ]] && continue echo $d cd $d done
TL;DR - 跳转到最后一段 背景 我正在执行一些数据驱动测试,并将日志文件用作测试输出之一。它的工作原理是这样的- 读取文件夹中的第一个文件 处理第一行并转换为测试 运行测试 执行验证 1 ...
我是一名优秀的程序员,十分优秀!