我编写了这个Tic Tac Toe和玩家1,它完美地运行(呵呵)。结束游戏检查所有工作,因此如果Xs进入空间1,2和3,游戏结束并要求重复等。
但是,我遇到的问题是它永远不会切换到播放器2.它总是要求播放器1进行下一步移动,因此将始终放置X. (见下面的截图)。最底层的if语句是为了做到这一点而创建的,它似乎不起作用。如果有人能告诉我需要添加/更改以让它切换玩家,我会很感激。 (我知道代码是基本的,可能格式很差等,但是我几天前开始学习C ++,这是我的第一个真正的项目)。感谢您的帮助!
#include <iostream>
using namespace std;
// prepare the game
bool GameOver = false;
bool Repeat = false;
bool Winner = false;
int PlayerTurn = 1;
int ChangeNumber;
char Counter = 'Z';
char Board1 = '1';
char Board2 = '2';
char Board3 = '3';
char Board4 = '4';
char Board5 = '5';
char Board6 = '6';
char Board7 = '7';
char Board8 = '8';
char Board9 = '9';
void welcome()
{
cout << "Welcome to Tic Tac Toe! This game requires 2 players." << endl;
cout << "Player 1 is represented by X whereas Player 2 is represented by O." << endl << endl;
}
int main()
{
welcome();
cout << Board1 << " - " << Board2 << " - " << Board3 << endl;
cout << Board4 << " - " << Board5 << " - " << Board6 << endl;
cout << Board7 << " - " << Board8 << " - " << Board9 << endl;
do
{
if(PlayerTurn == 1)
{
cout << endl << "Player 1, it is your turn! Enter the number you wish to change! ";
Counter = 'X';
}
else
{
cout << endl << "Player 2, it is your turn! Enter the number you wish to change! ";
Counter = 'O';
}
do{
cin >> ChangeNumber;
switch(ChangeNumber)
{
case 1:
Board1 = Counter;
break;
case 2:
Board2 = Counter;
break;
case 3:
Board3 = Counter;
break;
case 4:
Board4 = Counter;
break;
case 5:
Board5 = Counter;
break;
case 6:
Board6 = Counter;
break;
case 7:
Board7 = Counter;
break;
case 8:
Board8 = Counter;
break;
case 9:
Board9 = Counter;
break;
default:
cout << "That is not a valid placement!";
Repeat = true;
}
}while(Repeat == true);
//check for end of game
if (Board1 != '1') {
if (Board2 == Board1 && Board3 == Board1){
GameOver = true;
}
if (Board4 == Board1 && Board7 == Board1){
GameOver = true;
}
}
if (Board5 != '5'){
if (Board1 == Board5 && Board9 == Board5){
GameOver = true;
}
if (Board2 == Board5 && Board8 == Board5){
GameOver = true;
}
if (Board4 == Board5 && Board6 == Board5){
GameOver = true;
}
if (Board3 == Board5 && Board7 == Board5){
GameOver = true;
}
}
if (Board9 != '9') {
if (Board3 == Board9 && Board6 == Board9){
GameOver = true;
}
if (Board7 == Board9&& Board8 == Board9){
GameOver = true;
}
}
//Check not a draw
if (Board1 != '1' && Board2 != '2' && Board1 != '3' && Board4 != '4' && Board5 != '5' && Board6 != '6' && Board7 != '7' && Board8 != '8' && Board9 != '9')
{
GameOver = true;
Winner = false;
}
if(GameOver) {
if(Winner){
cout << "Player" << PlayerTurn << " wins!" << endl;
}
cout << Board1 << " - " << Board2 << " - " << Board3 << endl;
cout << Board4 << " - " << Board5 << " - " << Board6 << endl;
cout << Board7 << " - " << Board8 << " - " << Board9 << endl << endl;
cout << "Game over!" << endl;
cout << "Play again? y/n ";
char PlayAgain;
cin >> PlayAgain;
if (PlayAgain == 'y'){
GameOver = false;
Board1 = '1';
Board2 = '2';
Board3 = '3';
Board4 = '4';
Board5 = '5';
Board6 = '6';
Board7 = '7';
Board8 = '8';
Board9 = '9';
}
PlayerTurn = 1;
}
else {
if (PlayerTurn == 1){
PlayerTurn == 2;
} else {
PlayerTurn == 1;
}
}
}while(GameOver == false);
}
答案 0 :(得分:4)
if (PlayerTurn == 1){
PlayerTurn == 2;
应该是
if (PlayerTurn == 1){
PlayerTurn = 2;
说实话,你应该成功:
PlayerTurn = PlayerTurn == 1 ? 2 : 1;
答案 1 :(得分:3)
==用于测试相等性; =用于分配。