在多维数组中移动对象

时间:2012-03-01 03:10:03

标签: java multidimensional-array move

我正在为国际象棋和西洋棋棋子制作棋盘游戏(以及我想制作的一些变化)。我有一个扩展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();
  }
}

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

编辑:我甚至没有询问鼠标监听器或类似的东西,我只是想要一些伪代码来解释制作一个片段从阵列中的一个点移动到另一个点。

编辑2:这是我的Checkers课程的代码。

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]

2 个答案:

答案 0 :(得分: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坐标;我只是觉得这会更容易理解。

答案 1 :(得分:0)

通过移动Piece,您将更改2个数组板[] []的至少2个元素,这些元素位于Board类中。最好在Board类中实现该方法,因为Piece只能自行更改。如果你把move()放在Piece类中,并且Piece需要更换另一块,它需要与Board交谈并要求Board将其他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(){
}
相关问题