“即 - '1,2'
无效错误
无效错误
无效错误“
因此,如果正确输入数字,用户输入就不会插入任何网格中。
所以这是我的代码,任何建议/帮助都会在前进中受到赞赏。另请注意,我只是想验证并输入X和O,而不是真正检查用户是否绑定/赢了
#include <iostream>
#include "Tictactoe.h"
using namespace std;
int main(){
char board[3][3]={{'.','.','.'},{'.','.','.'},{'.','.','.'}};
bool gameover(true);
int iPlayerTurn(1);
do {
// Print board
cout << " - 0 1 2" << endl;
cout << " +---+---+---+" << endl;
cout << " 0" << " | " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << " | " << endl;
cout << " +---+---+---+" << endl;
cout << " 1" << " | " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " | " << endl;
cout << " +---+---+---+" << endl;
cout << " 2" << " | " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " | " << endl;
cout << " +---+---+---+" << endl;
char cPlayerMark;
if (iPlayerTurn == 1) {
cPlayerMark = 'X';
} else {
cPlayerMark = 'O';
}
// check if move is valid
std::cout << "Player" << iPlayerTurn << "'s move:" << std::endl;
bool bValidMove;
// Loop until we get a valid move
do {
char cNextMove;
cin >> cNextMove;
bValidMove = true;
// Check for a valid move
if (cNextMove == '0,0' && board[0][0] == '.') {
board[0][0] = cPlayerMark;
} else if (cNextMove == '0,1' && board[0][1] == '.') {
board[0][1] = cPlayerMark;
} else if (cNextMove == '0,2' && board[0][2] == '.') {
board[0][2] = cPlayerMark;
} else if (cNextMove == '1,0' && board[1][0] == '.') {
board[1][0] = cPlayerMark;
} else if (cNextMove == '1,1' && board[1][1] == '.') {
board[1][1] = cPlayerMark;
} else if (cNextMove == '1,2' && board[1][2] == '.') {
board[1][2] = cPlayerMark;
} else if (cNextMove == '2,0' && board[2][0] == '.') {
board[2][0] = cPlayerMark;
} else if (cNextMove == '2,1' && board[2][1] == '.') {
board[2][1] = cPlayerMark;
} else if (cNextMove == '2,2' && board[2][2] == '.') {
board[2][2] = cPlayerMark;
} else {
cout << "Invalid Move. Try again." <<endl;
bValidMove = false;
}
} while (!bValidMove);
}while (!gameover);
}
答案 0 :(得分:1)
问题是您已将cNextMove
定义为char而不是字符串。因此,无论用户输入多少个字符,它都只能包含一个字符。而且比较总是错误的。
如果使用gcc4.8编译代码,实际上会收到如下警告:
test.cpp:45:26: warning: multi-character character constant [-Wmultichar]
if (cNextMove == '0,0' && board[0][0] == '.') {