我正在尝试制作一个播放Tic Tac Toe的程序。我开始询问您是否要播放,但是当我按Y时,我的程序不会开始运行,而是关闭。我不确定是什么原因造成的。这是我的代码:
#include "pch.h"
#include <iostream>
using namespace std;
char square[10] = { '0','1','2','3','4','5','6','7','8','9'};
void showWelcomeMenu();
void showMenu();
int winner();
void board();
int startGame();
int quitApp();
int startGame()
{
int player = 1, i, choice;
char mark;
do
{
board();
player = (player % 2) ? 1 : 2;
cout << "Player " << player << ", enter space number: ";
cin >> choice;
mark = (player == 1) ? 'X' : 'O';
if (choice == 1 && square[1] == '1')
square[1] = mark;
else if (choice == 2 && square[2] == '2')
square[2] = mark;
else if (choice == 3 && square[3] == '3')
square[3] = mark;
else if (choice == 4 && square[4] == '4')
square[4] = mark;
else if (choice == 5 && square[5] == '5')
square[5] = mark;
else if (choice == 6 && square[6] == '6')
square[6] = mark;
else if (choice == 7 && square[7] == '7')
square[7] = mark;
else if (choice == 8 && square[8] == '8')
square[8] = mark;
else if (choice == 9 && square[9] == '9')
square[9] = mark;
else
{
cout << " Invalid move ";
player--;
cin.ignore();
cin.get();
}
i = winner();
player++;
} while (i == -1);
board();
if (i == 1)
cout << "==>\aPlayer " << --player << " wins! ";
else
cout << "==>\aDraw";
cin.ignore();
cin.get();
return 0;
}
void showWelcomeMenu()
{
char answer;
bool quit = false;
cout << "Welcome to Tic Tac Toe!\n" << endl << endl;
cout << "Would you like to play? (Y/N)" << endl << endl;
cin >> answer;
if (answer == 'Y')
{
startGame();
}
else if (answer != 'Y')
{
exit(0);
}
}
int winner()
{
if (square[1] == square[2] && square[2] == square[3])
return 1;
else if (square[4] == square[5] && square[5] == square[6])
return 1;
else if (square[7] == square[8] && square[8] == square[9])
return 1;
else if (square[1] == square[4] && square[4] == square[7])
return 1;
else if (square[2] == square[5] && square[5] == square[8])
return 1;
else if (square[3] == square[6] && square[6] == square[9])
return 1;
else if (square[3] == square[5] && square[5] == square[7])
return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4'
&& square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8'
&& square[9] != '9')
return 0;
else
return -1;
}
int quitApp()
{
exit(0);
}
void board()
{
system("cls");
cout << "\n";
cout << "Tic Tac Toe";
cout << "\n";
cout << "Player 1 (X) : Player 2 (0)" << endl << endl;
cout << "\n";
cout << " | | " << endl;
cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;
cout << " | | " << endl << endl;
}
int main()
{
showWelcomeMenu();
quitApp();
}
任何帮助将不胜感激!谢谢。对不起,如果已经回答!我找不到。
答案 0 :(得分:0)
该代码对我来说是完美的。 请重新检查。
也许您提供的输入错误。例如使用 y 代替 Y (区分大小写)
为确保代码能正常工作,您可以将条件修改为
if (answer == 'Y' || answer == 'y')
{
startGame();
}
此外,我建议您从while循环中简化条件。 例如。
if (choice == 1 && square[1] == '1')
square[1] = mark;
应替换为
if (choice == 1)
square[1] = mark;
与确定要标记哪个单元格的其他条件相同。
为什么?因为choice
是一个整数,不需要对字符进行检查(实际上可能由于隐式类型转换而导致错误)