首页 > 解决方案 > C ++将二维数组索引与条件语句中的函数值进行比较

问题描述

我想知道我可以将二维数组的索引与函数的值进行比较。如果可能的话,我想知道我的 if 语句有什么问题。代码如下:

void updateboard()
{
    for(int i = 0 ; i < 7 ; i++)
    {
        for(int j = 0 ; j < 7 ; j++)
        {
            if(myarray[i][0] == get_p1r() && myarray[0][j] == get_p1c())
            {
                myarray[i][j] = {1};
                drawboard();
                cout << "Hello";
                break;
                system("pause");
            } else
            {
                cout << "Bye";
                break;
            }
        }
    }
}

在 get_p1r() 和 get_p1c() 中,我得到从 0 到 7 的整数值。该程序是制作一个有 7 行和 7 列的 tictactoe 游戏。主要问题是如何更新玩家选择的索引。对不起,如果我没有很好地解释它,英语对我来说很难。

这是完整的代码:

class Game{
private:

int myarray[7][7] = {{0}};
int p1r, p1c, p2r, p2c;

public:



void set_p1r(int r1){
        p1r = r1;
    }
    void set_p1c(int c1){
        p1c = c1;
    }
    void set_p2r(int r2){
        p2r = r2;
    }
    void set_p2c(int c2){
        p2c = c2;
    }
    int get_p1r(){
        return p1r;
    }
    int get_p1c(){
        return p1c;
    }
    int get_p2r(){
        return p2r;
    }
    int get_p2c(){
        return p2c;
    }


void drawboard(){
    for(int i = 0 ; i < 7 ; i++){
        for(int j = 0 ; j < 7 ; j++){
            cout << myarray[i][j];
        }
        cout << endl;
    }
}

void playerinput(){
    int p1row, p1col, p2row, p2col;
    cout << "Enter Player 1 Row : ";
    cin >> p1row;
    set_p1r(p1row);
    cout << "Enter Player 1 Column : ";
    cin >> p1col;
    set_p1c(p1col);
    cout << "Enter Player 2 Row : ";
    cin >> p2row;
    set_p2r(p2row);
    cout << "Enter Player 2 Column : ";
    cin >> p2col;
    set_p2c(p2col);
}

void updateboard(){
    for(int i = 0 ; i < 7 ; i++){
        for(int j = 0 ; j < 7 ; j++){
            if(myarray[i][0] == get_p1r() && myarray[0][j] == get_p1c()){
                myarray[i][j] = {1};
                drawboard();
                cout << "Hello";
                break;
                system("pause");
            }
            else{
                cout << "Bye";
                break;
            }
        }
    }
}
};

int main(){
    Game mygame;
    for(;true;){
        system("cls");
        mygame.drawboard();
        mygame.playerinput();
        mygame.updateboard();
        system("pause");
    }
    return 1;
}

标签: c++

解决方案


我想我明白你在问什么。您不需要将函数的返回值与索引进行比较。您所要做的就是使用输入的值作为索引。由于updateboard()是类的成员,它可以直接访问变量。您不需要调用 getter 函数。您的updateboard()函数可能如下所示:

void updateboard()
{
    if ( (p1r >= 0) && (p1r < 7) && (p1c >= 0) && (p1c < 7) )
    {
        if ( myarray[p1r][p1c] != 0 )
        {
            cout << "Player 1 Row/Column already used";
            return;
        }
    }
    else
    {
        cout << "Player 1 Row/Column is not valid";
        return;
    }
    if ( (p2r >= 0) && (p2r < 7) && (p2c >= 0) && (p2c < 7) )
    {
        if ( myarray[p2r][p2c] != 0 )
        {
            cout << "Player 2 Row/Column already used";
            return;
        }
    }
    else
    {
        cout << "Player 2 Row/Column is not valid";
        return;
    }
    myarray[p1r][p1c] = 1;
    myarray[p2r][p2c] = 2;
}

推荐阅读