- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是初学者,所以我的代码很乱。我还没有完整地评论这个游戏,所以如果你需要澄清一些变量,我可以给你。
(顺便说一句,这是一个要求制作井字游戏的c++项目)
我的主要问题是,我将如何重复我的棋盘(每次有人在井字游戏中移动时都会更新)?我想不出这样做的方法,所以如果有人给我想法,而不是直接的答案,我将不胜感激。
下面我的代码只是为了让您了解我在做什么,以及如果您对如何修复我的代码有任何建议(即组织或错误,发生的可能性为 100%)。
#include <iostream>
using namespace std;
char a[3][3];//sets 3x3 matrix
a[0][0]='1';//upper row left corner is 1
a[0][1]='2';//upper row middle is 2
a[0][2]='3';//upper row right corner is 3
a[1][0]='4';//middle row left is 4
a[1][1]='5';//middle row middle is 5
a[1][2]='6';//middle row right is 6
a[2][0]='7';//bottom row left is 7
a[2][1]='8';//bottom row middle is 8
a[2][2]='9';//bottom row right is 9
cout << " | | " << endl;//all these "shapes" make the board.
cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;
cout << " | | " << endl;
bool match = true;//this tells the consul the match has not ended
bool checker;//checks if you actually chose X or O
checker=true;
cout << "play!play!play! you'll need two people" << endl;
cout << "decide who takes X, then press 1 to take X" << endl;
cout << "or press 2 to take O" << endl;
cin >> player;//so, organize will be the thing (1 or 2) that the player will put in
char XO;//helps make X and O
if (player == 1)
{
cout << "you chose X" << endl;
XO = 'X';
}
else if (player == 2)
{
cout << "you chose O" << endl;
XO = 'O';
}
else
{
cout << "press 1 or 2 only please" << endl;
checker=false;
}
bool invalid;//if you "accidentally" put your move in an illegal square, this will help you redo a move.
bool gameover = true;//helps differentiate between draws and wins
int nowwestart;//starts game
cout << "player play your move" << endl;//tells you to move it
cin >> nowwestart;
invalid = true;//you always make a valid move first turn.
if (nowwestart == 1 && a[0][0] == '1')//when you place your marker on square 1, i need to tell consul that your move equals a certain square
{
a[0][0]=XO;
cout << " | | " << endl;//all these "shapes" make the board.
cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;
cout << " | | " << endl;
}
else if (nowwestart == 2 && a[0][1] == '2')
{
a[0][1]=XO;
cout << " | | " << endl;//all these "shapes" make the board.
cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;
cout << " | | " << endl;
}
else if (nowwestart == 3 && a[0][2] == '3')
{
a[0][2]=XO;
cout << " | | " << endl;//all these "shapes" make the board.
cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;
cout << " | | " << endl;
}
else if (nowwestart == 4 && a[1][0] == '4')
{
a[1][0]=XO;
cout << " | | " << endl;//all these "shapes" make the board.
cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;
cout << " | | " << endl;
}
else if (nowwestart == 5 && a[1][1] == '5')
{
a[1][1]=XO;
cout << " | | " << endl;//all these "shapes" make the board.
cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;
cout << " | | " << endl;
}
else if (nowwestart == 6 && a[1][2] == '6')
{
a[1][2]=XO; cout << " | | " << endl;//all these "shapes" make the board.
cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;
cout << " | | " << endl;
}
else if (nowwestart == 7 && a[2][0] == '7')
{
a[2][0]=XO;
cout << " | | " << endl;//all these "shapes" make the board.
cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;
cout << " | | " << endl;
}
else if (nowwestart == 8 && a[2][1] == '8')
{
a[2][1]=XO;
cout << " | | " << endl;//all these "shapes" make the board.
cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;
cout << " | | " << endl;
}
else if (nowwestart == 9 && a[2][2] == '9')
{
a[2][2]=XO;
cout << " | | " << endl;//all these "shapes" make the board.
cout << " " << a[0][0] << " | " << a[0][1] << " | " << a[0][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[1][0] << " | " << a[1][1] << " | " << a[1][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << a[2][0] << " | " << a[2][1] << " | " << a[2][2] << endl;
cout << " | | " << endl;
}
else
{
cout << "you made an invalid move, please do it again" << endl;
invalid=false;//you made an illegal move :(
}
while(!invalid);
match = false;//when match has ended...
if (a[0][0] != '1')//all possible wins through square 1
{
if (a[0][0] == a[1][0] && a[1][0] == a[2][0])
{
match = true;
}
else if (a[0][0] == a[0][1] && a[0][1] == a[0][2])
{
match = true;
}
else if (a[0][0] == a[1][1] && a[1][1] == a[2][2])
{
match= true;
}
}
if (a[0][1] != '2')//all possible wins through square 2
{
if (a[0][1] == a[1][1] && a[1][1] == a[2][1])
{
match = true;
}
}
if (a[0][2] != '3')//all possible wins through square 3
{
if (a[0][2] == a[1][2] && a[1][2] == a[2][2])
{
match = true;
}
else if (a[0][2] == a[1][1] && a[1][1] == a[2][0])
{
match = true;
}
}
if (a[1][0] != '4')//all possible wins through square 4
{
if (a[1][0] == a[1][1] && a[1][1] == a[1][2])
{
match = true;
}
}
if (a[2][0] != '7')//all possible wins through square 7
{
if (a[2][0] == a[2][1] && a[2][1] == a[2][2])
{
match = true;
}
}
else//anything beside win is draw
{
gameover=false;//no one won...
match=true;//but the match is done anyway
}
if (match==true)//if the match is done
{
if (gameover==true)//if someone won
{
cout << "player" << player << "won" << player << endl;
}
cout << "the game has ended. play again? 1-yes, 2-false (press 2 please)" << endl;
if (1)
{
match = false;//dang it, you are still playing. the borad is below.
char a[3][3];//sets 3x3 matrix
a[0][0]='1';//upper row left corner is 1
a[0][1]='2';//upper row middle is 2
a[0][2]='3';//upper row right corner is 3
a[1][0]='4';//middle row left is 4
a[1][1]='5';//middle row middle is 5
a[1][2]='6';//middle row right is 6
a[2][0]='7';//bottom row left is 7
a[2][1]='8';//bottom row middle is 8
a[2][2]='9';//bottom row right is 9
}
player = 1;
}
else
{
if (player == 1)
{
player = 2;
}
else
{
player = 1;
}
}
while (!match);
cout << endl;
return 0;
}
int main()
{
dot();
}
最佳答案
您可以使用用户输入循环整个程序。 while (getline(std::cin, input)
。并用适当的符号更新二维数组。我建议你使用常量来表示 X 和 O。这样代码就会清晰. 而且我还建议你将重复的代码移到函数中,这同样会增加清晰度。
关于c++ - C++ 中的 Tic-Tac-Toe 帮助,如何制作循环以便 Tic Tac Toe 游戏每次都会重复棋盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58619023/
我是初学者,所以我的代码很乱。我还没有完整地评论这个游戏,所以如果你需要澄清一些变量,我可以给你。 (顺便说一句,这是一个要求制作井字游戏的c++项目) 我的主要问题是,我将如何重复我的棋盘(每次有人
我正在为C的Tic Tac Toe代码编写一个简单的游戏。我已经完成了大部分代码,但是我希望AI永不丢失。 我已经阅读了有关minimax算法的信息,但我不理解。如何使用此算法使计算机获胜或平局,但永
感谢这里人们的帮助,我成功地禁用了点击 div 并在已经使用 $(".pos").addClass('already-played'); 选择它们时覆盖它们; 以及 CSS 中的这个: .已经播放{
我有一个井字棋游戏,其中用户(x)玩CPU(o)。游戏开始时,CPU 将 (o) 放置在中心,并在用户之后移动到随机位置。游戏设置为循环,但一旦出现获胜者,它就会重置,并且不会显示“你赢/输的横幅”。
我试图在没有人工智能的情况下实现井字棋游戏。不知怎的,我的点击功能会自动触发。您能帮我理解为什么点击功能会自动触发吗?这是 HTML 代码片段。 Tic Tac Toe Gam
我正在制作一个井字游戏程序。我计划将 minimax 与它一起使用。我制作了一棵树,其中包含所有可能的游戏序列的空间,并且我正在寻找一种方法来填充它。我目前有这种类型: typedef struct
我正在尝试遵循本教程: https://www.youtube.com/watch?v=Db3cC5iPrOM 2:59 我听不懂他在说什么。 我不明白为什么他在构造函数(public static
我在这里为我的java作业编写了井字棋游戏,一切都很好,除了一个小问题,即当您输入最后一步(第九回合)时,最后一个“X”不显示。这不仅很烦人,因为获胜的棋子没有显示,而且还导致了一些问题,即领带方法没
我对编码和 Java 比较陌生,在我的 CS-173 类(class)中,我的任务是创建一个 Tic Tac Toe 游戏。然而,当谈到创建确定获胜者的方法时,每当我获得“胜利”时,代码都不会运行说我
您好,我想尝试制作一个井字游戏,但遇到问题。我仍然是一个初学者,所以请随意提供有关组织和类似内容的提示,但我的问题是我的方法 checkRowWin、checkColoumnWin 和 E.T.C 添
我正在研究 Tic-Tac-Toe 游戏 (3x3) 的 alpha-beta 剪枝算法。目前,对于任何给定的 3x3 网格实例,我都能找出最好的情况: public Best chooseAlpha
我是一名初学者,正在学习 Java super 技能类(class)。我试图尝试 this VS Code 中的 tic tac toe 游戏项目。效果很好。但代码在提交时出错。 代码: packag
我已经研究“死代码”和“无法访问的代码”有一段时间了,但我似乎仍然无法弄清楚我的程序中这个问题是怎么回事。这是我所拥有的一个片段; “gameEnd()”方法检查 Tic Tac Toe 中的获胜者:
我目前正在做一项任务,即创建一个 Tic Tac Toe 游戏。我已经做到了玩家可以在棋盘上放置标记、绘制标记并随后切换回合。但是,只有当玩家将其标记放在左上角(第一个)字段时,我检查是否存在获胜条件
编辑:我注意到,当您为 TicTacToe 表输入错误的数字时,我的程序会输出“无效移动”。什么会导致这种情况呢?我只使用 move(row, col) 方法一次,因此它不会重复无效输入两次。 我一直
import java.util.Scanner; public class TTT{ public static int row, col; public static Scanner scan =
这个问题已经有答案了: Is Java "pass-by-reference" or "pass-by-value"? (91 个回答) 已关闭 7 年前。 我的井字棋程序有一个小问题。我有一个嵌套计
我正在用 python 开发一个 tic-tac-toe 程序。现在,轮到人类了,一切顺利。然而,AI 在玩完第一个回合后,不会再玩任何后续回合。我扫描了代码,似乎找不到任何可能导致此问题的错误。 请
function checkWin(){ if (arro[0] === arro[1] === arro[2] === 1 || arro[3] === arro[4] === arro[5] ==
我尝试更改innerHTML 的所有内容都没有改变任何内容。没有 X 或 O,并且不会显示当前玩家的姓名。我一直在试图解决这个问题。我一直在寻找答案,据我所知,我所做的一切都是我应该做的。我今晚必须交
我是一名优秀的程序员,十分优秀!