好的,现在当我尝试确定有效输入再次播放时,它会在显示无效输入后立即再次显示游戏结果,然后要求用户再次输入“y”或“n”。我上传了一张图片给你看。我无法理解我的生活。
这是图片: http://imgur.com/SRsMo4P
// GAME OVER
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
while (bGameOver)
{
// Display game board - PRINT
for (int i = 0; i<ROW; ++i)
{
cout << " " << board[i][0] << " | " << board[i][1] << " | " << board[i][2] << endl;
if (i==0 || i==1)
{
cout << " - + - + -" << endl;
}
}
// Player wins - OUTPUT
if(bWinGame)
{
cout << endl;
cout << " Player "<< playerTurn << " wins! HOORAH! " << endl << endl;
cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
}
// Play again - OUTPUT & USER INPUT
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// VARIABLES
char again; // Play again = "Y" or "N"
cout << endl;
cout << " Want to play again? ( Y / N )" << endl << endl;
cout << " "; cin >> again; cout << endl;
// Play again - if YES
if (again == 'y' || again == 'Y')
{
bGameOver=false; // Reset game state
bWinGame=true; // Reset assumption
board[0][0] = '*'; // Rest game board - Array
board[0][1] = '*';
board[0][2] = '*';
board[1][0] = '*';
board[1][1] = '*';
board[1][2] = '*';
board[2][0] = '*';
board[2][1] = '*';
board[2][2] = '*';
}
// Play again - if NO
else if (again == 'n' || again == 'N')
{
cout << " Awe oh well, thanks for playing. " << endl << endl;
cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
break;
}
else
{
cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
}
}
// Game play continue
if (!bGameOver)
{
// Switch player turn
if (playerTurn == 1)
{
playerTurn = 2;
}
else
{
playerTurn = 1;
}
}
}
cout << " "; return 0;
}
答案 0 :(得分:1)
此break
声明中没有else
:
else
{
cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
}
代码将在没有break
的情况下继续运行,从而导致奇怪的行为。试试这个:
else
{
cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
break;
}
答案 1 :(得分:1)
将输入验证码放入循环中。如果选择Y或N,则打破循环。
P.S。在代码中添加coorrections,break;
现在正处于假想状态。
// GAME OVER
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
while (bGameOver)
{
// Display game board - PRINT
for (int i = 0; i<ROW; ++i)
{
cout << " " << board[i][0] << " | " << board[i][1] << " | " << board[i][2] << endl;
if (i==0 || i==1)
{
cout << " - + - + -" << endl;
}
}
// Player wins - OUTPUT
if(bWinGame)
{
cout << endl;
cout << " Player "<< playerTurn << " wins! HOORAH! " << endl << endl;
cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
}
// Play again - OUTPUT & USER INPUT
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// VARIABLES
bool bInvalidInput = true;
while(bInvalidInput)
{
char again; // Play again = "Y" or "N"
cout << endl;
cout << " Want to play again? ( Y / N )" << endl << endl;
cout << " "; cin >> again; cout << endl;
// Play again - if YES
if (again == 'y' || again == 'Y')
{
bGameOver=false; // Reset game state
bWinGame=true; // Reset assumption
bInvalidInput = false; //stop dialog
board[0][0] = '*'; // Rest game board - Array
board[0][1] = '*';
board[0][2] = '*';
board[1][0] = '*';
board[1][1] = '*';
board[1][2] = '*';
board[2][0] = '*';
board[2][1] = '*';
board[2][2] = '*';
}
// Play again - if NO
else if (again == 'n' || again == 'N')
{
cout << " Awe oh well, thanks for playing. " << endl << endl;
cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
bInvalidInput = false; //stop dialog
// break; //need no more
}
else
{
cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
}
}
if (bGameOver == true)
{
break; //get here only if bInvalidInput is false and N pressed
}
}
// Game play continue
if (!bGameOver)
{
// Switch player turn
if (playerTurn == 1)
{
playerTurn = 2;
}
else
{
playerTurn = 1;
}
}
}
答案 2 :(得分:0)
尝试在Display game board - PRINT
循环之前(之前)移动标有Player wins - OUTPUT
和while
的部分。
答案 3 :(得分:0)
感谢您提出建议,但由于某些原因他们没有正常工作,所以我最终在这里完成的是完成的代码:
// FILE: Tic Tac Toe.cpp
// PROGRAMMER: Karolina Sabat CPSC 1103 Section: S11
// ~~ TWO PLAYER TIC TAC TOE GAME ~~
// Asks the users (player 1, followed by player 2) to select a row and column by
entering the corresponding row and column number.
// The program will substitute the player's selection with either an "X" or "O".
// A horizontal, vertical or diagonal row of either X's or O's results in a win or
otherwise game ends in a draw.
// For subsequent plays, player 1 and player 2 alternate going first.
#include<iostream> // For cin, cout
using namespace std;
// MAIN FUNCTION
int main()
{
// VARIABLES
int playerTurn = 1; // Player's turn - Player 1
const int ROW=3; // Table - Number of rows - For array
const int COL=3; // Table - Number of columns - For array
bool bGameOver= false; // Game state - True = Game over, False = Continue play
char again; // Play again = "Y" or "N"
char board[ROW][COL] = { {'*', '*', '*'},
{'*', '*', '*'},
{'*', '*', '*'} }; // Game board - Array
// TITLE
cout << endl;
cout << " TIC TAC TOE" << endl;
cout << " ____________________________________________________" << endl;
cout << " A two player game." << endl;
cout << endl;
// Game state = Continue play - Game is NOT over.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Continue play - LOOP
while (!bGameOver)
{
// Game board - Array - PRINT
for (int i = 0; i < ROW; ++i)
{
cout << " " << board[i][0] <<" | " << board[i][1] <<" | " << board[i][2] << endl;
if (i==0 || i==1)
{
cout << " - + - + -" << endl;
}
}
// Set player mark ( "X" or "O" )
// VARIABLES
char playerMark;
if (playerTurn == 1) // Player 1 = "X"
{
playerMark = 'X';
}
else
{
playerMark = 'O'; // Player 2 = "O"
}
// Player move - USER INPUT
// VARIABLES
int move_r; // Row position - USER INPUT
int move_c; // Column position - USER INPUT
bool validMove = false; // Bool ValidMove - Assume false
cout << endl;
cout << " Player " << playerTurn << " , please pick a row and column to place "<< playerMark << "." << endl;
cout << " Separate the row and column number with a space and press ENTER." << endl;
while (!validMove) // DETERMINE VALIDITY - Check play move entries
{
validMove = true;
cout << endl;
cout << " "; cin >> move_r >> move_c; cout << endl;
// Row 1, Column 1
if(move_r == 1 && move_c == 1 && board[0][0] == '*')
{
board[0][0] = playerMark;
}
// Row 1, Column 2
else if(move_r == 1 && move_c == 2 && board[0][1] == '*')
{
board[0][1] = playerMark;
}
// Row 1, Column 3
else if(move_r == 1 && move_c == 3 && board[0][2] == '*')
{
board[0][2] = playerMark;
}
// Row 2, Column 1
else if(move_r == 2 && move_c == 1 && board[1][0] == '*')
{
board[1][0] = playerMark;
}
// Row 2, Column 2
else if(move_r == 2 && move_c == 2 && board[1][1] == '*')
{
board[1][1] = playerMark;
}
// Row 2, Column 3
else if(move_r == 2 && move_c == 3 && board[1][2] == '*')
{
board[1][2] = playerMark;
}
// Row 3, Column 1
else if(move_r == 3 && move_c == 1 && board[2][0] == '*')
{
board[2][0] = playerMark;
}
// Row 3, Column 2
else if(move_r == 3 && move_c == 2 && board[2][1] == '*')
{
board[2][1] = playerMark;
}
// Row 3, Column 3
else if(move_r == 3 && move_c == 3 && board[2][2] == '*')
{
board[2][2] = playerMark;
}
// INVALID ENTRY
else
{
cout << " Invalid Move, please try again!" << endl << endl;
// Will clear characters
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
validMove = false;
}
}
// Check if game over conditions are met
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// If row 0 or column 0 = same playerMark
if (board[0][0] != '*')
{
if (board[0][0] == board[0][1] && board[0][0] == board[0][2]) // Check row 0
{
bGameOver=true;
}
if (board[0][0] == board[1][0] && board[0][0] == board[2][0]) // Check column 0
{
bGameOver=true;
}
}
// If row 1 or column 1 = same playerMark
if (board[1][1] != '*')
{
if(board[1][1] == board[1][0] && board[1][1] == board[1][2]) // Check row 1
{
bGameOver=true;
}
if(board[1][1] == board[0][1] && board[1][1] == board[2][1]) // Check column 1
{
bGameOver=true;
}
if(board[1][1] == board[0][0] && board[1][1] == board[2][2]) // Diagonals - Check if 1,1 and 3,3 are equal to 2,2
{
bGameOver=true;
}
if(board[1][1] == board[2][0] && board[1][1] == board[0][2]) // Diagnonals - Check if 1,3 and 3,1 are equal to 2,2
{
bGameOver=true;
}
}
// If row 2 or column 2 = same playerMark
if (board[2][2] != '*')
{
if (board[2][2] == board[2][1] && board[2][2] == board[2][0]) // Check row 2
{
bGameOver=true;
}
if (board[2][2] == board[1][2] && board[2][2] == board[0][2]) // Check column 2
{
bGameOver=true;
}
}
// Check if DRAW
bool bWinGame=true; // ASSUMPTION - A player won
if (board[0][0] !='*' && board[0][1] !='*' && board[0][2] !='*' &&
board[1][0] !='*' && board[1][1] !='*' && board[1][2] !='*' &&
board[2][0] !='*' && board[2][1] !='*' && board[2][2] !='*' && !bGameOver)
{
bGameOver=true;
bWinGame=false;
// Tie - OUTPUT
cout << " Oh, won't you look at that, it's a TIE!"<< endl << endl;
cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
}
// GAME OVER
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
while (bGameOver)
{
// Display game board - PRINT
for (int i = 0; i<ROW; ++i)
{
cout << " " << board[i][0] << " | " << board[i][1] << " | " << board[i][2] << endl;
if (i==0 || i==1)
{
cout << " - + - + -" << endl;
}
}
// Player wins - OUTPUT
if(bWinGame)
{
cout << endl;
cout << " Player "<< playerTurn << " wins! HOORAH! " << endl << endl;
cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
}
// Play again - OUTPUT & USER INPUT
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// VARIABLES
bool bValidInput = false;
while (bValidInput != true)
{
cout << endl;
cout << " Want to play again? ( Y / N )" << endl << endl;
cout << " ";
// Will clear characters
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> again; cout << endl;
// Play again - if YES
if (again == 'y' || again == 'Y')
{
bValidInput = true;
bGameOver=false; // Reset game state
bWinGame=true; // Reset assumption
board[0][0] = '*'; // Rest game board - Array
board[0][1] = '*';
board[0][2] = '*';
board[1][0] = '*';
board[1][1] = '*';
board[1][2] = '*';
board[2][0] = '*';
board[2][1] = '*';
board[2][2] = '*';
}
// Play again - if NO
else if (again == 'n' || again == 'N')
{
bValidInput = true; // Assumes
cout << " Awe oh well, thanks for playing. " << endl << endl;
cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
cout << " "; return 0;
}
// Play again - INVALID ENTRY
else
{
cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
bValidInput = false;
}
}
}
// Game play continue
if (!bGameOver)
{
// Switch player turn
if (playerTurn == 1)
{
playerTurn = 2;
}
else
{
playerTurn = 1;
}
}
}
// EXIT PROGRAM
cout << " "; return 0;
}