c++ - 将数组中的位置存储为要在 if 语句中更改的变量

标签 c++ arrays function if-statement

有没有办法将用户输入的 x 和 y 存储到数组的某个位置。

我有这个:

if (x == 1 && y == 1)
        {
            board[0][0] = playerMarker;
        } 

我想将 x 和 y 存储到一个与数组中某个点的名称相匹配的变量中。

但我想尝试让它更像这样:

if (currentMove == '1' && squareOne == '1')
                squareOne = playerMarker;

这是我在上面看到的一段代码。下面的代码是我自己的。

void playerMove(char* CurrentPlayer, char (&board)[3][3], char &playerMarker)
    {
        bool bValidMove;
        int x,y;
    // Prompt the player for a move
    cout << "Player " << CurrentPlayer << "'s move:" << endl;



    // Loop until we get a valid move
    do 
    {
        cout << "Please enter the row number for the place you wish to mark: " << endl;
        cin >> x;
        cout << "Please enter the column number for the place you wish to mark" << endl;
        cin >> y;
        bValidMove = true;

        // Check for a valid move
        if (x == 1 && y == 1)
        {
            board[0][0] = playerMarker;
        } 
        else if (x == 1 && y == 2) 
        {
            board[0][1] = playerMarker;
        } 
        else if (x == 1 && y == 3) 
        {
            board[0][2] = playerMarker;
        }
        else if (x == 2 && y == 1) 
        {
            board[1][0] = playerMarker;
        }
        else if (x == 2 && y == 2)
        {
            board[1][1] = playerMarker;
        }
        else if (x == 2 && y == 3)
        {
            board[1][2] = playerMarker;
        }
        else if (x == 3 && y == 1)
        {
            board[2][0] = playerMarker;
        }
        else if (x == 3 && y == 2)
        {
            board[2][1] = playerMarker;
        }
        else if (x == 3 && y == 3) 
        {
            board[2][2] = playerMarker;
        }
        else 
        {
            cout << "Invalid Move. Try again." << endl;
            bValidMove = false;
        }

    } 
    while (!bValidMove);

}

这是我要自己编写的代码。

// Ask current player for a move
        cout << currentPlayer << "'s move: ";
        bool validMove;
        // Loop until a valid move is chosen
        do
        {
            char currentMove;
            cin >> currentMove;
            validMove = true;

            // Check for a valid move
            if (currentMove == '1' && squareOne == '1')
                squareOne = playerMarker;
            else if (currentMove == '2' && squareTwo == '2')
                squareTwo = playerMarker;
            else if (currentMove == '3' && squareThree == '3')
                squareThree = playerMarker;
            else if (currentMove == '4' && squareFour == '4')
                squareFour = playerMarker;
            else if (currentMove == '5' && squareFive == '5')
                squareFive = playerMarker;
            else if (currentMove == '6' && squareSix == '6')
                squareSix = playerMarker;
            else if (currentMove == '7' && squareSeven == '7')
                squareSeven = playerMarker;
            else if (currentMove == '8' && squareEight == '8')
                squareEight = playerMarker;
            else if (currentMove == '9' && squareNine == '9')
                squareNine = playerMarker;
            else
            {
                cout << "Invalid Move. Try again: ";
                validMove = false;
            }
        } while ( !validMove );

最佳答案

好吧,这只是对你想要什么的猜测,但它是这样的:

do 
{
    cout << "Please enter the row number for the place you wish to mark: " << endl;
    cin >> x;
    cout << "Please enter the column number for the place you wish to mark" << endl;
    cin >> y;
    bValidMove = true;

    int xx = x-'1';
    int yy = y-'1';

    if ( xx < 0 || xx > 2 || yy < 0 || yy > 2 )
    {
            cout << "Invalid Move (outside board). Try again: ";
            validMove = false;
    } else if ( board[xx][yy] == 0 ) {
            board[xx][yy] = playerMarker;
    } else {
            cout << "Invalid Move (position already taken). Try again: ";
            validMove = false;
    }

等等等等

这假设棋盘是一个 3x3 数组,当玩家没有占据该位置时包含 0,并将第 1/2/3 行和第 1/2/3 列作为输入。

希望这有助于...

关于c++ - 将数组中的位置存储为要在 if 语句中更改的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3375093/

相关文章:

javascript - 从 localStorage 获取特定项目的位置

c++ - 数据库表在内存中表示的容器

c++ - 为什么指针指向内存而不是值?

android - 如何将字符数组从 JNI 发送到 android

java - 在 Java 中将像素数组转换为图像

arrays - 找到成本函数比率的最佳方法

sql - 以 'select statement' 格式从函数返回结果

c++ - 关于 boost::asio 套接字和阻塞

c++ - 视频文件中的感兴趣区域

css - 根据类设置 SASS 变量