-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoardView.java
145 lines (134 loc) · 4.33 KB
/
BoardView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* A JPanel class that provides a view for the game board.
*
* @author Mohamed Haryz Izzudin bin Mohamed Rafy (1141127874)
*/
public class BoardView extends JPanel {
/**
* The model instance for the game board.
*/
private Board board;
/**
* A 2-dimensional array of square buttons that are present on the board.
*/
private SquareButton[][] buttons;
/**
* A boolean that indicates whether the current game has ended or not.
*/
private boolean gameEnded;
/**
* Constructor for the BoardView class.
* Sets up a 9x9 GridLayout to house the square buttons, and adds those buttons.
*
* @author Haryz
*/
public BoardView() {
super(new GridLayout(9, 9));
gameEnded = false;
board = Board.getInstance();
buttons = new SquareButton[9][9];
board.initializeBoard();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
Square square = board.getSquare(i, j);
buttons[i][j] = new SquareButton(square);
buttons[i][j].addActionListener(new SquareClickListener(this, buttons[i][j]));
buttons[i][j].setOpaque(true);
if (i == 4 && j == 4) {
square.getSpecialPiece().addListener(new ChestUnlockListener(this));
}
add(buttons[i][j]);
}
}
refreshBoard();
}
/**
* Gets a square button on the board.
*
* @author Haryz
* @param x The x-coordinate of the button.
* @param y The y-coordinate of the button.
* @return A square button.
*/
public SquareButton getSquareButton(int x, int y) {
return buttons[x][y];
}
/**
* Repaints the icons on the board according to their new locations.
*
* @author Haryz
* @return a boolean - true if the refresh is successful
*/
public boolean refreshBoard() {
if (!gameEnded) {
board = Board.getInstance();
ArrayList<Point> validPoints = board.getCurrentPlayer().getValidMoveLocations();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
SquareButton button = buttons[i][j];
button.setIcon(button.getSquare().getTopMostImageIcon());
button.setBackground(null);
if (validPoints.contains(new Point(i, j))) {
button.setBackground(Color.GREEN);
}
else if (button.getSquare().equals(board.getCurrentPlayer().getSquare())) {
button.setBackground(Color.ORANGE);
}
if (i == 4 && j == 4 && !button.getSquare().getSpecialPiece().hasListeners()) {
button.getSquare().getSpecialPiece().addListener(new ChestUnlockListener(this));
}
}
}
}
else {
endOfGame();
}
return true;
}
/**
* Repaints the buttons of the board and detaches click listeners when the game ends.
*
* @author Haryz
*/
public void endOfGame() {
gameEnded = true;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
SquareButton button = buttons[i][j];
button.setIcon(button.getSquare().getTopMostImageIcon());
button.setBackground(Color.GREEN);
button.setEnabled(false);
}
}
}
/**
* Gets the boolean that indicates if the game has ended.
*
* @return The gameEnded attribute
*/
public boolean isGameEnded() {
return gameEnded;
}
/**
* Resets the end-of-game boolean value and reattaches button listeners, then refreshes the board.
*
* @author Haryz
*/
public void newGame() {
gameEnded = false;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
buttons[i][j].setEnabled(true);
}
}
refreshBoard();
}
}