-4

問題は簡単です。3X3 配列で 3X3 tictactoe ゲームを作成しました。しかし、問題は次のとおりです。

array[0][3] = array[1][0]

まず第一に、私が作成した配列には 4 番目の列がありませんでした。だからarray[0][3]存在すらしない!さらに複雑なことに、次の値が必要です。[1][0]

移動の座標を次のように入力すると問題が発生します: 0 2

void displayBoard(int tictac[3][3])
{
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            cout << tictac[i][j] << " ";
        } cout << "\n" ;
    } cout << "\n";
}



int Horizontal(int x, int y, int tictac[3][3])
{

    if(tictac[x][y+1]==0)
    {
        tictac[x][y+1]=2;
        return 1;
    }

    if(tictac[x][y-1]==0)
    {
        tictac[x][y-1]=2;
        return 1;
    }

    if(tictac[x][y-2]==0)
    {
        tictac[x][y-2]=2;
        return 1;
    }
    if(tictac[x][y+2]==0)
    {
        tictac[x][y+2]=2;
        return 1;
    }

    return 0;
}

int Vertical(int x, int y, int tictac[3][3])
{

    if(tictac[x+1][y]==0)
    {
        tictac[x+1][y]=2;
        return 1;
    }
    if(tictac[x-1][y]==0)
    {
        tictac[x-1][y]=2;
        return 1;
    }
    if(tictac[x-2][y]==0)
    {
        tictac[x-2][y]=2;
        return 1;
    }
    if(tictac[x+2][y]==0)
    {
        tictac[x+2][y]=2;
        return 1;
    }

    return 0;
}

void AI(int X,int Y,int tictac[3][3])
{
    int done = 0;
    cout << "\n-------------------------\nComputer plays: \n";

    done = Horizontal(X,Y,tictac);
    if(done == 0)
    {
    done = Vertical(X,Y,tictac);
    }
}


int main()
{
    int tictac[3][3] = {{0,0,0},{0,0,0}, {0,0,0} };
    int X, Y;
    for(int r=1; r<100; r++)
    {
    cout << "\n-------------------------\nPlayer play a move: \n";
    cin >> X;
    cin >> Y;

    if(tictac[X][Y]==0)
    {
    tictac[X][Y] = 1;
    displayBoard(tictac);
    AI(X,Y,tictac);
    displayBoard(tictac);
    }
    else
    {
        cout << "Space occupied. Try different cell." << endl;
    }


    }





}
4

2 に答える 2