View Javadoc

1   package de.jos.game.logic;
2   
3   import java.util.Stack;
4   
5   import de.jos.game.objects.Bullet;
6   import de.jos.game.objects.SimplifiedBullet;
7   import de.jos.game.xml.Level;
8   
9   public class PuzzleGameBoard extends AbstractGameBoard {
10  
11    private boolean renderPuzzleSolved = false;
12  
13    private Stack<SimplifiedBullet[][]> moveHistory = new Stack<SimplifiedBullet[][]>();
14  
15    public void initLevel(Level level) {
16      super.initLevel(level);
17      this.updateBoard();
18    }
19  
20    public boolean isRenderPuzzleSolved() {
21      return renderPuzzleSolved;
22    }
23  
24    public void setRenderPuzzleSolved(boolean renderPuzzleSolved) {
25      this.renderPuzzleSolved = renderPuzzleSolved;
26    }
27  
28    /***
29     * Create a snapshot for the undo function with simplified bullets. Bullets
30     * only contain the minimum information required to perform an undo.
31     * 
32     * @return Returns a SimplifiedBullet 2-dimensional Array
33     */
34    private SimplifiedBullet[][] getBoardSnapshot() {
35      SimplifiedBullet[][] board = new SimplifiedBullet[BOARD_WIDTH_X][BOARD_WIDTH_Y];
36  
37      for (int x = 0; x < BOARD_WIDTH_X; x++) {
38        for (int y = 0; y < BOARD_WIDTH_Y; y++) {
39          Bullet tmpBullet = getBullet(x, y);
40          if (tmpBullet != null) {
41            board[x][y] = new SimplifiedBullet(tmpBullet.getColor(), tmpBullet.getStatus());
42          }
43        }
44      }
45  
46      return board;
47    }
48  
49    public void updateBoardHistory() {
50      moveHistory.push(getBoardSnapshot());
51    }
52  
53    public void undoBoardHistory() {
54      SimplifiedBullet[][] bullets = moveHistory.pop();
55  
56      for (int x = 0; x < BOARD_WIDTH_X; x++) {
57        for (int y = 0; y < BOARD_WIDTH_Y; y++) {
58          SimplifiedBullet simpleBullet = bullets[x][y];
59          if (simpleBullet != null) {
60            Bullet bullet = new Bullet(simpleBullet.getColor(), simpleBullet.getStatus());
61  
62            bullet.setBoardX(x);
63            bullet.setBoardY(y);
64            bullet.setResourceContainer(getResourceContainer());
65            bullet.init();
66  
67            setBullet(x, y, bullet);
68          } else {
69            setBullet(x, y, null);
70          }
71        }
72      }
73  
74    }
75  
76  }