检查2D阵列游戏

时间:2015-09-18 17:41:33

标签: java arrays

我正在创建一个名为“Shapeshifter”的游戏,并且无法弄清楚如何检查2D阵列游戏板以查看用户使用光标制作的形状是否实际上是所需的形状。任何数量的代码都可以工作,它只需要与游戏的其余部分一起工作。

以下是我所做的代码:

public static final int COLS = 7;
public static final int ROWS = 7;
public static int ZEE = 0;
public static int TEE = 1;
public static int ES = 2;
public static int OH = 3;
public static int JAY = 4;
public static int EL = 5;
public static int NUM_POLYOMINOS = 6;
public static int PIECE_SIZE = 4;
public static int UP = 8;
public static int LEFT = 4;
public static int DOWN = 2;
public static int RIGHT = 6;
public static int QUIT = 5;
public static int EMPTY = 0;
public static int POLY_PIECE = 1;
public static int PLAYER = 2;

private static int[][] getRandomPoly()
{
    int rint = new java.util.Random().nextInt(NUM_POLYOMINOS);
    if (rint == ZEE)
        return new int[][]{{1, 1, 0}, 
            {0, 1, 1}};
    else if (rint == TEE)
        return new int[][]{{0, 1, 0}, 
            {1, 1, 1}};
    else if (rint == ES)
        return new int[][]{{0, 1, 1}, 
            {1, 1, 0}};
    else if (rint == OH)
        return new int[][]{{1, 1}, 
            {1, 1}};
    else if (rint == JAY)
        return new int[][]{{1, 0, 0}, 
            {1, 1, 1}};
    else //if (rint == EL)
        return new int[][]{{0, 0, 1}, 
            {1, 1, 1}};
}

public void printCurrentPoly()
{
    if (currentPoly == null)
        return;
    System.out.println("Current polyomino:");
    System.out.println();
    for (int i = 0; i < currentPoly.length; i++)
    {
        for (int j = 0; j < currentPoly[0].length; j++)
            if (currentPoly[i][j] == 0)
                System.out.print(" ");
            else
                System.out.print("#");
        System.out.println();
    }
    System.out.println();
}


public void initializeBoard()
{
    for (int i = 0; i < board.length; i++)
        for (int j = 0; j < board[0].length; j++)
            board[i][j] = 0;
    currentRow = board.length / 2;
    currentCol = board[0].length / 2;
    board[currentRow][currentCol] = PLAYER;
    for (int i = 0; i < PIECE_SIZE; i++)
    {
        int rrow = new java.util.Random().nextInt(ROWS),
        rcol = new java.util.Random().nextInt(COLS);
        while ((rrow == 0 || rrow == board.length - 1) ||
        (rcol == 0 || rcol == board[0].length - 1) ||
        (rrow == board.length / 2 && rcol == board[0].length / 2) ||
        (board[rrow][rcol] == POLY_PIECE))
        {
            rrow = new java.util.Random().nextInt(ROWS);
            rcol = new java.util.Random().nextInt(COLS);
        }
        board[rrow][rcol] = POLY_PIECE;
    }
    currentPoly = getRandomPoly();
}

public static void main(String args[])
{
    boolean done = false;
    ShapeShifter ss = new ShapeShifter();
    ss.initializeBoard();
    ss.printCurrentPoly();
    ss.printBoard();
    while (!done)
    {
        int move = ss.getMove();
        if (move == QUIT)
            done = true;
        else
        {
            ss.executeMove(move);
            if (ss.checkForMatch())
                done = true;
            System.out.println("\n");
            ss.printCurrentPoly();
            ss.printBoard();
        }
    }
    ss.printResult();
}

private int[][] board = new int[ROWS][COLS];
private int[][] currentPoly;
private int numMoves, currentRow, currentCol;
private boolean winner;
private Scanner stdin = new Scanner(System.in);

public void printBoard()
{
    System.out.println("+-------+");
    for(int i = 0; i < board.length; i++)
    {
        System.out.print("|");
        for(int j = 0; j < board[0].length; j++)
        {

            if(board[i][j] == POLY_PIECE)
                System.out.print("#");
            else if (board[i][j] == PLAYER)
                System.out.print("H");
            else
                System.out.print(" ");
        }
        System.out.println("|");
    }
    System.out.println("+-------+");
}

public int getMove()
{
    System.out.println("Were do you want to move? (4=left, 2=down, 8=up, 6=right or 5=quit)");
    int move = stdin.nextInt();
    while(move != LEFT && move != RIGHT && move != UP && move != DOWN && move != QUIT)
    {
        System.out.println("Invalid, only valid moves allowed are 4=left, 2=down, 8=up, 6=right or 5=quit");
        move = stdin.nextInt();
    }
    return move;
}

public void executeMove(int move)
{
    if(move == LEFT)
        moveLeft(move);
    if(move == RIGHT)
        moveRight(move);
    if(move == UP)
        moveUp(move);
    if(move == DOWN)
        moveDown(move);
    numMoves++;
}

private void moveLeft(int move)
{
    int currentRow = 0, currentCol = 0; 
    for(int i = 0; i < board.length; i++)
        for(int j = 0; j < board[0].length; j++)
            if(board[i][j] == 2)
            {   currentRow = i;
                currentCol = j;
            }
    if(move == LEFT)
    {    
        if(currentCol != 0 && board[currentRow][currentCol - 1] != POLY_PIECE)
        {    
            board[currentRow][currentCol - 1] = PLAYER;
            board[currentRow][currentCol] = EMPTY;
        }
        if(currentCol != 0 && board[currentRow][currentCol - 1] == POLY_PIECE && board[currentRow][currentCol - 2] != POLY_PIECE)
        {
            board[currentRow][currentCol - 1] = PLAYER;
            board[currentRow][currentCol - 2] = POLY_PIECE;
            board[currentRow][currentCol] = EMPTY;
        }
    }
}

private void moveRight(int move)
{
    int currentRow = 0, currentCol = 0; 
    for(int i = 0; i < board.length; i++)
        for(int j = 0; j < board[0].length; j++)
            if(board[i][j] == 2)
            {   currentRow = i;
                currentCol = j;
            }
    if(move == RIGHT)
    {    if (currentCol != 6 && board[currentRow][currentCol + 1] != POLY_PIECE)
        {    
            board[currentRow][currentCol + 1] = PLAYER;
            board[currentRow][currentCol] = EMPTY;
        }
        if(currentCol != 6 && board[currentRow][currentCol + 1] == POLY_PIECE && board[currentRow][currentCol + 2] != POLY_PIECE)
        {
            board[currentRow][currentCol + 1] = PLAYER;
            board[currentRow][currentCol + 2] = POLY_PIECE;
            board[currentRow][currentCol] = EMPTY;
        }
    }
}

private void moveUp(int move)
{
    int currentRow = 0, currentCol = 0; 
    for(int i = 0; i < board.length; i++)
        for(int j = 0; j < board[0].length; j++)
            if(board[i][j] == 2)
            {   currentRow = i;
                currentCol = j;
            }
    if(move == UP)
    {
        if(currentRow != 0 && board[currentRow - 1][currentCol] != POLY_PIECE)
        {    
            board[currentRow - 1][currentCol] = PLAYER;
            board[currentRow][currentCol] = EMPTY;
        }
        if(currentRow != 0 && board[currentRow - 1][currentCol] == POLY_PIECE && board[currentRow - 2][currentCol] != POLY_PIECE)
        {
            board[currentRow - 1][currentCol] = PLAYER;
            board[currentRow - 2][currentCol] = POLY_PIECE;
            board[currentRow][currentCol] = EMPTY;
        }
    }
}

private void moveDown(int move)
{
    int currentRow = 0, currentCol = 0; 
    for(int i = 0; i < board.length; i++)
        for(int j = 0; j < board[0].length; j++)
            if(board[i][j] == 2)
            {   currentRow = i;
                currentCol = j;
            }
    if(move == DOWN)
    {    if(currentRow != 6 && board[currentRow + 1][currentCol] != POLY_PIECE)
        {    
            board[currentRow + 1][currentCol] = PLAYER;
            board[currentRow][currentCol] = EMPTY;
        }
        if(currentRow != 6 && board[currentRow + 1][currentCol] == POLY_PIECE && board[currentRow + 2][currentCol] != POLY_PIECE)
        {
            board[currentRow + 1][currentCol] = PLAYER;
            board[currentRow + 2][currentCol] = POLY_PIECE;
            board[currentRow][currentCol] = EMPTY;
        }
    }
}


public void printResult()
{
    if(checkForMatch() == false)
        System.out.println("You did not win. You used " + numMoves + " moves to fail.");
    else
        System.out.println("You won!!n/" + "It took " + numMoves + " moves to complete.");
    }
}

以下是我遇到麻烦的游戏的最后一块:

    public boolean checkForMatch()
{

}

2 个答案:

答案 0 :(得分:0)

你没有说多联骨牌(非多项式)是否可以处于任何方向。如果是这样,我认为你必须循环旋转和反射并测试每一个。对于每个方向,没有多少位置可以与电路板对齐。对于每一个,您需要检查每个位置,看看玩家是否已将它们全部填满。

由于您的多联骨牌是事先定义的,为了优化,您还可以为每个多边形定义常量,说明哪些方式是对称的,这样您就可以避免多余的测试。并非额外的测试确实很重要。

答案 1 :(得分:0)

您可以“重复使用”已为printCurrentPolyprintBoard编写的许多代码来实施checkForWinner

这个想法是通过董事会的所有细胞。对于每个单元格检查:如果当前多边形将从此单元格开始,则多边形中的所有#单元格与棋盘上的玩家字符(ox)匹配。如果是,则该玩家是赢家。否则转到下一个单元格。

这里有相关代码:

/**
 * Returns true when the current polyTicTacToc can be found at x,y of the 
 * board, with its cells filled for the given player.
 */
private boolean isPoly(int x, int y, char player) {
    if (currentPoly == null)
        return false;
    for (int i = 0; i < currentPoly.length; i++) {
        for (int j = 0; j < currentPoly[0].length; j++) {
            char c = currentPoly[i][j];
            if (c == '#') {
                if (boardCellAt(x + i, y + j) != player) {
                    return false;
                }
            }

        }
    }
    return true;
}

private char boardCellAt(int i, int j) {
    if (i >= board.length || j >= board[0].length) {
        return ' ';
    }
    return board[i][j];
}

/**
 * Check the entire game board to see if the given player has     constructed
 * the polyomino.
 */
public boolean checkForWinner(char player)// Lab2
{
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[0].length; j++) {
            if (isPoly(i, j, player)) {
                return true;
            }
        }
    }
    return false;
}

顺便说一句:printBoard中有一个错误。循环应如下所示:

    for(int j = 0; j < board[0].length; j++)

或者你得到ArrayIndexOutOfBoundsException

同样在printBoard中,您应该将第一次打印更改为System.out.print("|");,否则看起来电路板上还会留下额外的单元格。

相关问题