0

私はC++に非常に慣れていないので、これを行うためのより良い方法があるかどうか疑問に思っていました。Arduinoで実行されるので、ArrayListsなどは使用できません。

byte GetFreeCell(short x, short y)
{
    byte possibleMoves[4] = {0,0,0,0};
    if (y - 2 >= 0 && _grid[y - 2][x] == 0)
        possibleMoves[0] = 1;
    if (x + 2 < WIDTH && _grid[y][x + 2] == 0)
        possibleMoves[1] = 2;
    if (y + 2 < HEIGHT && _grid[y + 2][x] == 0)
        possibleMoves[2] = 3;
    if (x - 2 >= 0 && _grid[y][x - 2] == 0)
        possibleMoves[3] = 4;

    if (possibleMoves[0] == 0 && possibleMoves[1] == 0 && possibleMoves[2] == 0 && possibleMoves[3] == 0) {
        return 0;
    }

    byte move = 0;
    while(move == 0){
        move = possibleMoves[random(4)];
    }
    return move;
}

ありがとう、

ジョー

4

2 に答える 2

4
byte GetFreeCell(short x, short y)
{
    byte possibleMoves[4];
    byte index = 0;
    if (y - 2 >= 0 && _grid[y - 2][x] == 0)
        possibleMoves[index++] = 1;
    if (x + 2 < WIDTH && _grid[y][x + 2] == 0)
        possibleMoves[index++] = 2;
    if (y + 2 < HEIGHT && _grid[y + 2][x] == 0)
        possibleMoves[index++] = 3;
    if (x - 2 >= 0 && _grid[y][x - 2] == 0)
        possibleMoves[index++] = 4;

    return index ? possibleMoves[random(index)] : 0;
}
于 2013-01-25T20:09:49.647 に答える
0

あなたは自分に有利に働き、これを使うことができます:

https://github.com/maniacbug/StandardCplusplus/#readme

次に、標準のコンテナーを使用してコードをサニタイズできます。

また、C++にはArrayListはありません。それがJavaです。上記のライブラリでは、代わりにstd::vectorを使用できます。

于 2013-01-25T20:09:21.113 に答える