简体   繁体   English

Tic Tac Toe cin输入问题

[英]Tic Tac Toe cin input issue

question on tic tac toe. 关于井字游戏的问题。 I'm trying to allow the user to input "0,0"(row/column) format exactly, and would be invalid otherwise. 我正在尝试允许用户准确输入“ 0,0”(行/列)格式,否则将无效。 Error is that if I input any of the correct grid numbers, it would give an invalid error regardless. 错误是,如果我输入任何正确的网格号,则无论如何都会给出无效的错误。

"ie - '1,2' “即-'1,2'

invalid error 无效错误

invalid error 无效错误

invalid error" 无效错误”

So nothing the user input is inserted in any of the grid if they input the numbers correctly. 因此,如果用户输入正确,则不会在任何网格中插入用户输入的内容。

So here is my code, any advice/help is appreciated on moving forward. 所以这是我的代码,在前进的过程中任何建议/帮助都值得赞赏。 Also note i'm just trying to verify and input the X's and O's, not really checking user if they tie/win 另请注意,我只是在尝试验证和输入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);
}

The problem is that you have defined cNextMove as a char instead of string. 问题是您已将cNextMove定义为char而不是字符串。 So it can only contain one character no matter how many characters the user entered. 因此,无论用户输入了多少个字符,它都只能包含一个字符。 And the comparison will always be false. 并且比较将始终是错误的。

If you compile your code with gcc4.8, you will actually get an warning like: 如果使用gcc4.8编译代码,则实际上会收到如下警告:

test.cpp:45:26: warning: multi-character character constant [-Wmultichar]
         if (cNextMove == '0,0' && board[0][0] == '.') {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM