动态编程,两个玩家矩阵游戏

时间:2013-12-28 18:04:35

标签: java algorithm matrix dynamic-programming

我正在编写一个由两个玩家玩的矩阵游戏。目标是说明玩家1是否可以赢得游戏返回1,如果不能返回0。 播放器可以删除矩阵的最后一行或最后一列,但前提是该行/列中的数字总和是偶数。例如:

original matrix: 
[1 2]
[2 2]

then we can subtract these matrices from this:

sum row, and this becomes:
[1 3]                [0 0]
[2 4]                [1 1]

sum col, and this becomes:
[1 2]                [0 1]
[3 4]                [0 1]

现在我们可以玩游戏了,很明显玩家1可以从行或列矩阵中删除。然后玩家2可以采取行动,最后我们可以看到玩家1在这个例子中无法获胜。

这是我对该计划的实施:

我只提供假设使用行和列矩阵的方法,如果玩家1获胜则返回1,如果玩家1获胜则返回0。

public static int play(int[][] rowM, int[][] colM){
        int player = 1;
        int[][] matrix = new int[rowM.length][colM[0].length];
        for (int i = 0; i < rowM.length; i++) {
            for (int j = 0; j < colM[0].length; j++) {
                //if no possible move
                if(rowM[i][j] == 0 && colM[i][j] == 0){
                    //if player 1 is playing, and losing
                    if(player % 2 == 1){
                        matrix[i][j] = 0;
                        player++;
                    }
                    //player 2 is losing
                    else{
                        matrix[i][j] = 1;
                        player++;
                    }
                    continue;
                }

                //if player 1 is playing
                if(player % 2 == 1){
                    player++;
                    //the value in this position is even
                    if(rowM[i][j] == 1){
                        //if there is only 1 row left, 
                        if(i == 0){
                            matrix[i][j] = 1;
                        }
                        else if(matrix[i-1][j] == 1){
                            matrix[i][j] = 1;
                        }
                        else{
                            matrix[i][j] = 0;
                        }
                    }
                    //the value in the col matrix this position is even
                    else if(colM[i][j] == 1){
                        //if there is only 1 column left, 
                        if(j == 0){
                            //matrix[i][j] = 1;
                            matrix[i][j] = 1;
                            //player++;
                        }
                        else if (matrix[i][j-1] == 1){
                            matrix[i][j] = 1;
                        }
                        else{
                            matrix[i][j] = 0;
                        }
                    }

                }
                //player 2 is playing
                else{
                    player++;
                    //the value in this position is even
                    if(rowM[i][j] == 1){
                        if(i == 0){
                            matrix[i][j] = 1;
                        }
                        else if(matrix[i-1][j] == 1){
                            matrix[i][j] = 0;
                        }
                        else{
                            matrix[i][j] = 1;
                        }
                    }
                    //the value in the col matrix this position is even
                    else if(colM[i][j] == 1){
                        if(j == 0){
                            matrix[i][j] = 1;
                        }
                        else if(matrix[i][j-1] == 1){
                            matrix[i][j] = 0;
                        }
                        else{
                            matrix[i][j] = 1;
                        }
                    }

                }
            }
    }
    return matrix[rowM.length-1][colM[0].length-1];
    }

rowM和colM是内部有1和0的矩阵,我没有把我的方法放到那些矩阵中。这个算法适用于几乎所有的东西,但我发现很少的例子不适合我的实现。我认为在我的代码中的某些地方,特别是其中一个if语句必须是正确的。我使用debug来查找它,但我无法找出它出错的原因。

这是一个使用我的代码的例子:

a matrix 2*1:
[2]
[1]

这给了我1,但它的错误应该是0.可以有人帮助我找到我的问题。这是我看到错误的最后选择。请帮忙

enter image description here

0 个答案:

没有答案