-1

我必须完成 4 种方法,但我很难理解它们。我正在努力完成whitemove(). 我知道我必须遍历电路板,但我不能完全理解我应该怎么做!

import java.util.List;
import java.util.ArrayList;

public class HexapawnBoard {
private static final int WHITE_PIECE = 1;
private static final int BLACK_PIECE = 2;
private static final int EMPTY = 0;

private static final int BOARD_SIZE = 3;

private int[][] board;

/**
 * Create a board position from a string of length BOARD_SIZE*BOARD_SIZE (9).
 * The first BOARD_SIZE positions in the string correspond to the black player's home
 * position. The last BOARD_SIZE positions in the string correspond ot the white player's
 * home position. So, the string "BBB   WWW" corresponds to a starting position.
 * 
 * @param pos the string encoding the position of the board
 */
public HexapawnBoard(String pos)
{
    if(pos.length() != BOARD_SIZE * BOARD_SIZE)
        throw new RuntimeException("HexapawnBoard string must be of length BOARD_SIZExBOARD_SIZE");

    board = new int[BOARD_SIZE][BOARD_SIZE];
    for(int row=0;row<BOARD_SIZE;row++)
    {
        for(int col=0;col<BOARD_SIZE;col++)
        {
            switch(pos.charAt(row*BOARD_SIZE+col))
            {
            case 'B':
            case 'b':
                board[row][col] = BLACK_PIECE;
                break;
            case 'W':
            case 'w':
                board[row][col] = WHITE_PIECE;
                break;
            case ' ':
                board[row][col] = EMPTY;
                break;
            default:
                throw new RuntimeException("Invalid Hexapawn board pattern " + pos);
            }
        }
    }
}

/**
 * A copy constructor of HexapawnBoard
 * 
 * @param other the other instance of HexapawnBoard to copy
 */
public HexapawnBoard(HexapawnBoard other)
{
    board = new int[BOARD_SIZE][BOARD_SIZE];
    for(int i=0;i<BOARD_SIZE;i++)
        for(int j=0;j<BOARD_SIZE;j++)
            this.board[i][j] = other.board[i][j];
}


/**
 * Return a string version of the board. This uses the same format as the 
 * constructor (above)
 * 
 * @return a string representation of the board.
 */
public String toString()
{
    StringBuffer sb = new StringBuffer();
    for(int row=0;row<BOARD_SIZE;row++)
    {
        for(int col=0;col<BOARD_SIZE;col++)
        {
            switch(board[row][col])
            {
            case BLACK_PIECE:
                sb.append('B');
                break;
            case WHITE_PIECE:
                sb.append('W');
                break;  
            case EMPTY:
                sb.append(' ');
                break;
            }
        }
    }
    return sb.toString();
}

/**
 * Determine if this board position corresponds to a winning state.
 * 
 * @return true iff black has won.
 */
public boolean blackWins()
{
    for(int col=0;col<BOARD_SIZE;col++)
        if(board[BOARD_SIZE-1][col] == BLACK_PIECE)
            return true;
    return false;
}

/**
 * Determine if this board position corresponds to a winning state
 * 
 * @return true iff white has won
 */
public boolean whiteWins()
{
    for(int col=0;col<BOARD_SIZE;col++)
        if(board[0][col] == WHITE_PIECE)
            return true;
    return false;
}

/**
 * Determine if black has a move
 * 
 * @ return truee iff black has a move
 */
public boolean blackCanMove()
{

    return false;
}

/**
 * Return a List of valid moves of white. Moves are represented as valid 
 * Hexapawn board positions. If white cannot move then the list is empty.
 * 
 * @return A list of possible next moves for white
 */
public List<HexapawnBoard> whiteMoves()
{
    return null
}

/**
 * Determine if two board positions are equal
 * @param other the other board position
 * @return true iff they are equivalent board positions
 */
public boolean equals(HexapawnBoard other)
{

    return true;
}

/**
 * Determine if two board positions are reflections of each other
 * @param other the other board position
 * @return true iff they are equivalent board positions
 */
public boolean equalsReflection(HexapawnBoard other)
{

    return true;
}

 }
4

1 回答 1

0

首先,到目前为止您的代码似乎是正确的。

现在,对于你剩下的四种方法,我真的无法帮助你使用blackCanMove()这些whiteMoves()方法,因为我不知道这个六人棋游戏中的移动规则是什么。如果你能解释一下规则,我可以帮忙。

equals()andequalsReflection()方法而言,您基本上需要做您在复制构造函数中所做的事情,但不是复制,而是进行条件测试。如果两个位置与应有的位置不匹配,则返回false。否则,一旦您检查了每个方块并且它们都正确,请返回true

whiteMoves()方法而言,您需要遍历棋盘并找到所有白色棋子。对于每个白色棋子,可能有几个(最多 3 个)有效动作。检查每个位置,看看它们是否可能。如果该位置有黑色棋子,则该棋子可能会沿对角线移动,如果该位置根本没有棋子,则该棋子可能会向前移动。对于每个有效的移动,创建一个新的HexapawnBoard代表棋盘在该移动之后的样子。在方法结束时,返回HexapawnBoard您创建的所有 s 的列表。

于 2012-06-26T13:17:36.557 回答