2012-03-01 91 views
0

我正在为国际象棋和跳棋(以及我想要制作的一些变体)制作棋盘游戏。我有一个扩展JPanel的Board类,并设置了一个JPanel的双精度数组来充当我的板。这里是我的板级的一些代码:在多维数组中移动对象

public class Board extends JPanel { 

    private static final int COLS = 8; 
    private static final int ROWS = 8; 

    private JPanel[][] board = new JPanel[COLS][ROWS]; 
    private JPanel chessBoard; 

    public Board() { 
    super(); 
    super.setLayout(new BorderLayout()); 

    chessBoard = new JPanel(); 
    chessBoard.setLayout(new GridLayout(COLS,ROWS)); 

    // Set up JPanels on bottom and right to display letters and numbers for the board 
    // JPanels are called south and west 
    super.add(chessBoard, BorderLayout.CENTER); 
    super.add(south, BorderLayout.SOUTH); 
    super.add(west, BorderLayout.WEST); 

    for (int i=0; i<COLS; i++) { 
     for (int j=0; j<ROWS; j++) { 
     // Set up the grid 
     board[i][j] = new JPanel(); 
     board[i][j].setBackground(getColor(i,j)); 
     chessBoard.add(board[i][j]); 
     } 
    } 
    super.validate(); 
    } 

    private Color getColor(int x, int y) { 
    if ((x + y) % 2 == 0) { 
     return Constants.GOLD; 
    } else { 
     return Constants.PURPLE; 
    } 
    } 

    public void addPiece(Piece piece) { 
    JLabel p = piece.getImage(); 

    board[piece.getX()][piece.getY()].add(p); 
    chessBoard.validate(); 
    } 
} 

件是我将用于我所有的作品的接口。我设置了接口并设置了一个实现接口的类(Checker类)。我已经完成了所有这些工作。这些作品是带有ImageIcons的JLabel。我到目前为止唯一的问题是写一个移动方法。我可以找出逻辑确定一个行动是有效的,我只是不知道如何实际行动。

编辑:我甚至不问有关鼠标监听器或类似的东西,我只是想要一些伪代码来解释如何从数组中的一个点移动到另一个点。

编辑2:这是我的跳棋类的代码。

public class Checker implements Piece { 
    private int side,xPos,yPos; 
    private JLabel img; 

    public Checker(int team, int x, int y) { 
    BufferedImage image; 

    try { 
     if (team == 0) 
     image = ImageIO.read(new File("img/RedPiece.png")); 
     else 
     image = ImageIO.read(new File("img/BlackPiece.png")); 
    } catch(IOException e) { 
     image = null; 
     System.out.printf("Image file wasn't found!!!"); 
     System.exit(1); 
    } 

    img = new JLabel(new ImageIcon(image), SwingConstants.CENTER); 
    img.setVerticalAlignment(SwingConstants.CENTER); 

    xPos = x; 
    yPos = y; 
    } 

    // TODO Figure out move method 
    public void move(int dx, int dy) { 

    } 

    // Also typical gets and sets for instance variables 

所以我的想法是我要求跳棋一块移动方法和假设我从屏幕顶部的底部,这将是piece.move(-1,1);移动,我不得不删除piece从它的当前位置,那么它的阵列中的新位置[x + dx][y + dy]

回答

1

你可以使用像这样:

public void movePiece(Piece piece, int dx, int dy) { 
    // Save current position, so we can erase the piece. 
    int oldX = piece.getX(); 
    int oldY = piece.getY(); 

    // Update the location. 
    piece.setX(oldX + dx); 
    piece.setY(oldY + dy); 

    // Remove piece from old position 
    board[oldX][oldY].clear(); 

    // Add it to the new position. 
    addPiece(piece); 
} 

你可能想使用一个Point,而不是个别的X和Y coordi纳茨;我只是觉得这会更容易理解。

+0

因此,这发生在板类,而不是在单个块!这使得更有意义。 – CaldwellYSR 2012-03-01 03:21:42

0

通过移动一个Piece,您将更改2维阵列板[] []中的至少2个元素,该元素位于Board类中。在Board类中实现该方法会更好,因为Piece只能对其自身进行更改。如果你把Piece中的move()放在Piece类中,并且Piece需要更换另一个Piece,那么它需要与委员会交谈并要求委员会将其他Piece的参考传递给它。你要改变的每件作品完全取决于你的设计,比如颜色,图片。这里是我的建议:

局:

move(Piece from, Piece to){ 
    ... // validation 
    from.remove(); 
    to.put(team); 
    // over is the one you jump over, optional, some if statements here 
    Piece over = Board.getOver(Piece from, Piece to); 
    over.remove(); 
    // check for further move is also optional 
    if(board.isFurthermoveAvailable(to)){ 
     from = to; 
     to = board.getFurthermoveLocation(from); 
     board.move(from, to); 
    } 
} 
getOver(Piece from, Piece to){ 
    return board[(from.getX()+to.getX())/2][(from.getY()+to.getY())/2]; 
} 
isFurthermoveAvailable(Piece from){ 
    int team = from.getTeam(); 
    ... // your logic 
} 
// depends on what you return by isFurthermoveAvailable(Piece from) 
// if you return boolean, then you also need the method below 
// or, you can let the above method return null or a Piece 
getFurthermoveLocation(Piece from){ 
    ... // your logic 
} 

海贼王:

remove(){ 
    ... // changes such as the picture, colour 
} 
put(int Team){ // this method is obviously depends on team 
    ... 
} 
// other methods may assist to the process 
getTeam(){ 
} 
getX(){ 
} 
getY(){ 
}