【发布时间】:2019-12-19 10:09:02
【问题描述】:
您好,我是 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;
}
}
【问题讨论】:
标签: c++ tic-tac-toe