- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我是C++编程的新手,我的游戏遇到了麻烦。当玩家选择一个已经被占用的空间或无效移动时,它将跳过其前进。我希望播放器能够再次播放。
如果您看到其他任何问题或任何提示,可以给我整理一下,那就太好了,或者只是任何反馈。
谢谢
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
char matrix[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int rear;
char player = 'X';
void mainMenu();
void play();
//strings for the player names to be chosen//
string charName = "Player1";
string charName2 = "Player 2";
//Noughts and Crosses//
int main()
{
system("Color 2E"); //Background and text colour//
mainMenu();
return 0;
}
void mainMenu()
{
system("CLS");
char userChoice = 'o';
do
{
//Main Menu//
cout << "\n Welcome to Noughts and Crosses" << endl
<< "\n\n 1. Play Game " << endl
<< " 2. How to Play" << endl
<< " 3. Who to refer to if the program malfunctions" << endl
<< " 4. Credits" << endl
<< " 5. Exit" << endl;
cout << "\n Please make a choice: ";
cin >> userChoice;
system("CLS");
if (userChoice == '1')
play();
else if (userChoice == '2')
cout <<
"\n\n\n Instructions:\n\n Enter the player names then press enter to proceed
with the game.\n To select the square you would like to place your marker enter the
square name followed by the ENTER key.\n Repeat this until a player has three of their
markers in a row. This can be in any direction.\n\n BE CAREFUL NOT TO CHOOSE AN ALREADY
TAKEN SPACE. OTHERWISE YOU WILL MISS A GO!!!\n\n Enjoy.\n\n\n"
<< endl;
else if (userChoice == '3')
cout << "\n\n\n Made in Visual Studios using C++\n Version 2.0\n\n" <<
endl;
else if (userChoice == '4')
cout << "\n\n\n Created by Jamie Clifford.\n Made in Visual Studios using
C++\n Version 2.0\n\n\n " << endl;
else if (userChoice == '5')
{
cout << " Good bye" << endl;
}
else
cout << " Error - Please choose again" << endl;
} while (userChoice != '5');
}
void Draw()
{
system("CLS");
//Board//
cout << "\n\n Noughts and Crosses \n\n";
for (int i = 0; i < 3; i++)
{
cout << " ";
for (int j = 0; j < 3; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
void Input()
{
int a;
++rear;
player;
do
{
//Beginning of game, where player input their chosen space//
{ if (player == 'X')
cout << "\n It's " << charName << "'s turn. Enter a number: ";
else if (player == 'O')
cout << "\n It's " << charName2 << "'s turn. Enter a number: ";
}
cin >> a;
if (a == 1 && matrix[0][0] == '1')
matrix[0][0] = player;
else if (a == 2 && matrix[0][1] == '2')
matrix[0][1] = player;
else if (a == 3 && matrix[0][2] == '3')
matrix[0][2] = player;
else if (a == 4 && matrix[1][0] == '4')
matrix[1][0] = player;
else if (a == 5 && matrix[1][1] == '5')
matrix[1][1] = player;
else if (a == 6 && matrix[1][2] == '6')
matrix[1][2] = player;
else if (a == 7 && matrix[2][0] == '7')
matrix[2][0] = player;
else if (a == 8 && matrix[2][1] == '8')
matrix[2][1] = player;
else if (a == 9 && matrix[2][2] == '9')
matrix[2][2] = player;
else {
cout << "\n Invalid number, please try again.\n\n ";
system("pause");
rear--;
cin.ignore();
cin.get();
}
} while (a == -1);
}
void togglePlayer()
{
if (player == 'X')
player = 'O';
else player = 'X';
}
char Win()
{
//first player//
if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][2] == 'X')
return 'X';
if (matrix[1][0] == 'X' && matrix[1][1] == 'X' && matrix[1][2] == 'X')
return 'X';
if (matrix[2][0] == 'X' && matrix[2][1] == 'X' && matrix[2][2] == 'X')
return 'X';
if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X')
return 'X';
if (matrix[0][1] == 'X' && matrix[1][1] == 'X' && matrix[2][1] == 'X')
return 'X';
if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == 'X')
return 'X';
if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X')
return 'X';
if (matrix[2][0] == 'X' && matrix[1][1] == 'X' && matrix[0][2] == 'X')
return 'X';
//second player//
if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][2] == 'O')
return 'O';
if (matrix[1][0] == 'O' && matrix[1][1] == 'O' && matrix[1][2] == 'O')
return 'O';
if (matrix[2][0] == 'O' && matrix[2][1] == 'O' && matrix[2][2] == 'O')
return 'O';
if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O')
return 'O';
if (matrix[0][1] == 'O' && matrix[1][1] == 'O' && matrix[2][1] == 'O')
return 'O';
if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == 'O')
return 'O';
if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O')
return 'O';
if (matrix[2][0] == 'O' && matrix[1][1] == 'O' && matrix[0][2] == 'O')
return 'O';
return '/';
}
void play()
{//Players enter their names//
cout << "\n\n Player 1 enter your name: ";
cin >> charName;
cout << "\n\n Player 2 enter your name: ";
cin >> charName2;
char choice;
Draw();
Start:
while (1)
{
Input();
Draw();
if (Win() == 'X')
{
cout << "\n " << charName << " Wins The Game ";
break;
}
else if (Win() == 'O')
{
cout << "\n " << charName2 << " Wins The Game " << endl;
break;
}
else if (rear == 9)
{
cout << " Draw" << endl;
break;
}
togglePlayer();
}
//Choice to play the game again or return to the main menu//
cout << "\n\n ";
cout << " Do you want to go play again? ";
cout << "\n\n 1. Yes\n";
cout << " 2. No\n\n";
cout << " ";
cin >> choice;
if (choice == '1')
cout <<"\n Enjoy\n";
else if (choice == '2')
mainMenu();
while (choice != '1');
{
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = i * 3 + (j + 1) + 48;
}
}
rear = 0;
player = 'X';
goto Start;
}
}
最佳答案
只需在Input()中添加else条件
a = -1;
关于c++ - Noughts and Crosss/Tic Tac Toe Player又去了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59407419/
我是初学者,所以我的代码很乱。我还没有完整地评论这个游戏,所以如果你需要澄清一些变量,我可以给你。 (顺便说一句,这是一个要求制作井字游戏的c++项目) 我的主要问题是,我将如何重复我的棋盘(每次有人
本文实例讲述了Python实现的井字棋(Tic Tac Toe)游戏。分享给大家供大家参考,具体如下: 说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意。另外,90%+
这个教程,我们将展示如何用python创建一个井字游戏。 其中我们将使用函数、数组、if条件语句、while循环语句和错误捕获等。 首先我们需要创建两个函数,第一个函数用来显示游戏板:
我正在尝试从命令行(使用终端)以相反的顺序搜索大文件。我找到了 tac 命令:http://clifgriffin.com/2008/11/25/tac-and-reverse-grep/ tac 是
在阅读时,我遇到了“中级语言”和“3AC”这两个术语。 据我了解,IL 是源代码编译过程中的中间“步骤”。更具体地说,我正在阅读有关字节码(Java)和 C 的内容。 我解释它的方式(如果错了请纠正我
我正在为C的Tic Tac Toe代码编写一个简单的游戏。我已经完成了大部分代码,但是我希望AI永不丢失。 我已经阅读了有关minimax算法的信息,但我不理解。如何使用此算法使计算机获胜或平局,但永
我正在尝试使用reactjs创建一个简单的井字棋应用程序,其中有两种模式:经典和图像,在经典模式下我可以选择显示 X 和 O,在图像模式下,我可以选择两个显示下面提到的两个图像。我的文件结构是: sr
我想将普通的三地址代码文件转换为 Java 字节码。已经有一些与此主题相关的问题,但没有得到解答 properly或question远远超出了我正在寻找的范围。 以《龙书》中的编译器前端生成的这段代码
我试图解决 Schwartz 的“学习 Perl”中的一个练习,这时我在编写的代码中偶然发现了意外的输出。我想知道我做错了什么。 Qn:实现一个类似于 unix 实用程序的简单 tac。 我的解决方案
我有一份非常通用的工作,不同的参数作为来自不同文件的上下文参数传递。但我仍然需要“硬编码”上下文文件名并在 TAC (Talend Administration Console) 中创建多个作业以供执
我现在想用我的代码做两件事。1) 检查获胜者2) 不让双方玩家在同一个位置进入eg.如果player1已经在board[0][0]='X'处输入了value,player2再次进入board[0][0
我有一个扭曲的 tac 文件 (twisted_service.py),其中包含代码: from twisted.application import service # application.py
我是 UNIX 编码的新手,我有一个文件需要逐行反向读取。该文件在 {} 中有代码段。然后我需要使用这个反向文件作为输入来运行一个 awk 脚本。我正在让我们的支持人员安装 tac,但在他们安装之前,
感谢这里人们的帮助,我成功地禁用了点击 div 并在已经使用 $(".pos").addClass('already-played'); 选择它们时覆盖它们; 以及 CSS 中的这个: .已经播放{
我有一个井字棋游戏,其中用户(x)玩CPU(o)。游戏开始时,CPU 将 (o) 放置在中心,并在用户之后移动到随机位置。游戏设置为循环,但一旦出现获胜者,它就会重置,并且不会显示“你赢/输的横幅”。
我试图在没有人工智能的情况下实现井字棋游戏。不知怎的,我的点击功能会自动触发。您能帮我理解为什么点击功能会自动触发吗?这是 HTML 代码片段。 Tic Tac Toe Gam
我正在制作一个井字游戏程序。我计划将 minimax 与它一起使用。我制作了一棵树,其中包含所有可能的游戏序列的空间,并且我正在寻找一种方法来填充它。我目前有这种类型: typedef struct
我在完成这项学校作业时遇到了问题。我想实现一种方法,其中代码显示 //call method to check for Winner,在每轮后检查获胜者。 我不确定该怎么做。我尝试过各种不同的方法。然
我正在尝试遵循本教程: https://www.youtube.com/watch?v=Db3cC5iPrOM 2:59 我听不懂他在说什么。 我不明白为什么他在构造函数(public static
问题很简单。我有一个 IMEI,我想从中检索 TAC。我该怎么做?如果我只有 IMEI,是否有办法识别 TAC 应该有多少位数字?是否需要明确知道设备的生产年份才能知道? 最佳答案 从头开始读取 8
我是一名优秀的程序员,十分优秀!