gpt4 book ai didi

c++ - Noughts and Crosss/Tic Tac Toe Player又去了

转载 作者:行者123 更新时间:2023-12-02 10:37:56 25 4
gpt4 key购买 nike

嗨,我是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/

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