在爪哇的蛇梯棋盘上得到一个计数器



我目前正试图让计数器沿着我的蛇和梯子游戏板移动,我似乎不能让它正常工作。这是我的代码,我似乎无法让"计数器"出现在打印网格中,以显示玩家的位置。任何帮助都非常感谢!

public static void printBoard() {
    int numRows = 10;
    int numColumns = 10;
    int numSpaces = numRows * numColumns;
    int[][] board = new int[numRows][numColumns];
    for (int space = 0; space < numSpaces; space++)
    {
        int row = space / numRows;
        int column = space % numColumns;
        if (row % 2 == 0)
        {
            board[row][column] = (numSpaces - space);
        }
        else
        {
            board[row][(numColumns - column) - 1] = (numSpaces - space);
        }
    }
    for (int[] row : board)
    {
        for (int col : row)
        {
            System.out.printf("%4d", col);
        }
        System.out.println();
        for (int i=0; i<100;i++)
        {
            if(SnakesAndLadders.computerPos == i)
            {
                System.out.print("<"+SnakesAndLadders.computerPos+">");
            }//if
            else if(SnakesAndLadders.player1Pos == i)
            {
                System.out.print("(" + SnakesAndLadders.player1Pos + ")");
            }//else if
        }
    }
}

下面是对打印board

的for循环的一个简单修复
for (int[] row : board) {
    for (int col : row) {
        if(col==SnakesAndLadders.computerPos)
            System.out.printf("%4s", "<" + SnakesAndLadders.computerPos + ">");
        else if(col==SnakesAndLadders.player1Pos)
            System.out.printf("%4s", "(" + SnakesAndLadders.player1Pos + ")");
        else
            System.out.printf("%4d", col);
    }
    System.out.println();
}

相关内容

  • 没有找到相关文章

最新更新