我正在编写一个使用C ++的游戏代码,并且当一个玩家应该获胜时,例如在顶行上的所有x,游戏继续提示下一个玩家的移动,即使应该已经是胜利者了。我不知道它有什么问题,所以对于解决问题的任何帮助表示赞赏!以下是我的完整代码。
#include <iostream>
#include <string>
using namespace std;
enum status{X_WON, O_WON, DRAW, UNFINISHED};
class Board{
public:
char boardArray[3][3];
Board();
bool makeMove(int, int, char);
status gameState();
void print();
};
Board::Board(){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
boardArray[i][j] = '*';
};
};
};
bool Board::makeMove(int xIn, int yIn, char player){
if (boardArray[xIn][yIn] == '*'){
boardArray[xIn][yIn] = player;
return true;
}else{
return false;
};
};
status Board::gameState(){
status result;
if(boardArray[0][0] == 'x' && boardArray[0][1] == 'x' && boardArray[0] [2] == 'x'){
result = X_WON;
};
if(boardArray[0][0] == 'o' && boardArray[0][1] == 'o' && boardArray[0][2] == 'o'){
result = O_WON;
};
if(boardArray[1][0] == 'x' && boardArray[1][1] == 'x' && boardArray[1][2] == 'x'){
result = X_WON;
};
if(boardArray[1][0] == 'o' && boardArray[1][1] == 'o' && boardArray[1][2] == 'o'){
result = O_WON;
};
if(boardArray[2][0] == 'x' && boardArray[2][1] == 'x' && boardArray[2][2] == 'x'){
result = X_WON;
};
if(boardArray[2][0] == 'o' && boardArray[2][1] == 'o' && boardArray[2][2] == 'o'){
result = O_WON;
};
if(boardArray[0][0] == 'x' && boardArray[1][0] == 'x' && boardArray[2][0] == 'x'){
result = X_WON;
};
if(boardArray[0][0] == 'o' && boardArray[1][0] == 'o' && boardArray[2][0] == 'o'){
result = O_WON;
};
if(boardArray[0][1] == 'x' && boardArray[1][1] == 'x' && boardArray[2][1] == 'x'){
result = X_WON;
};
if(boardArray[0][1] == 'o' && boardArray[1][1] == 'o' && boardArray[2][1] == 'o'){
result = O_WON;
};
if(boardArray[0][2] == 'x' && boardArray[1][2] == 'x' && boardArray[2][2] == 'x'){
result = X_WON;
};
if(boardArray[0][2] == 'o' && boardArray[1][2] == 'o' && boardArray[2][2] == 'o'){
result = O_WON;
};
if(boardArray[0][0] == 'x' && boardArray[1][1] == 'x' && boardArray[2][2] == 'x'){
result = X_WON;
};
if(boardArray[0][0] == 'o' && boardArray[1][1] == 'o' && boardArray[2][2] == 'o'){
result = O_WON;
};
if(boardArray[0][2] == 'x' && boardArray[1][1] == 'x' && boardArray[2][0] == 'x'){
result = X_WON;
};
if(boardArray[0][2] == 'o' && boardArray[1][1] == 'o' && boardArray[2][0] == 'o'){
result = O_WON;
};
int taken = 0;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(boardArray[i][j] != '*'){
taken++;
};
};
};
if(taken == 9 && (result != X_WON || result != O_WON)){
result = DRAW;
};
if(taken != 9 && (result != X_WON || result != O_WON || result != DRAW)){
result = UNFINISHED;
};
return result;
};
void Board::print(){
cout << " " << "0 " << "1 " << "2" << endl;
for(int i = 0; i < 3; i++){
cout << i << " ";
for(int j = 0; j < 3; j++){
cout << boardArray[i][j] << " ";
};
cout << endl;
};
};
class Ttt {
private:
char player;
public:
Board newBoard;
Ttt();
void play();
};
Ttt::Ttt(){
do{
cout << "Who should have the first move? x or o?" << endl;
cin >> player;
if (player == 'x' || player == 'o'){
break;
}else{
cout << "Not a valid player. Try again." << endl;
};
}while(true);
};
void Ttt::play(){
do{
int xcoord, ycoord;
do{
cout << "Player " << player << " , please enter your move. Example: 0 [enter] 0 (numbers from 0 - 2)" << endl;
cin >> xcoord >> ycoord;
if(newBoard.makeMove(xcoord, ycoord, player) == false){
cout << "That move is not valid. Try again." << endl;
}else{
break;
};
}while(true);
newBoard.makeMove(xcoord, ycoord, player);
newBoard.print();
if(player == 'x'){
player = 'o';
}else{
player = 'x';
};
if(newBoard.gameState() == X_WON){
cout << "Player X is the winner!" << endl;
break;
};
if(newBoard.gameState() == O_WON){
cout << "Player O is the winner!" << endl;
break;
};
if(newBoard.gameState() == DRAW){
cout << "Draw!" << endl;
break;
};
}while(true);
};
int main(){
Ttt newgame;
newgame.play();
}
答案 0 :(得分:1)
测试
(result != X_WON || result != O_WON || result != DRAW)
始终为true,因为结果不能等于所有3个值。
result
应初始化为UNFINISHED
,测试更改为:
(taken != 9 && result == UNFINISHED)