-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquareClickListener.java
56 lines (54 loc) · 1.72 KB
/
SquareClickListener.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
import java.awt.event.*;
/**
* A class for listening mouse clicks on the squares.
*
* @author Ramanan R Muralitharan (1141128291)
* @author Mohamed Haryz Izzudin bin Mohamed Rafy (1141127874)
*/
public class SquareClickListener implements ActionListener{
/**
* The board view.
*/
private BoardView boardView;
/**
* The button that is clicked.
*/
private SquareButton button;
/**
* Default constructor for the SquareClickListener class.
* Sets the square button that is clicked.
*
* @author Ramanan
*/
public SquareClickListener(BoardView boardView, SquareButton button) {
this.boardView = boardView;
this.button = button;
}
/**
* Moves the player to the clicked square.
* If successful, the player reference is removed from the square he was in, and the player turn advances.
*
* @author Ramanan
* @author Haryz
* @param evt The mouse click on the square.
*/
public void actionPerformed(ActionEvent evt) {
Board board = Board.getInstance();
Scoreboard scoreboard = Scoreboard.getInstance();
Player player = board.getCurrentPlayer();
Square newSquare = button.getSquare();
Square oldSquare = player.getSquare();
if (player.move(newSquare)) {
oldSquare.removePlayer(player);
SpecialPiece specialPiece = newSquare.getSpecialPiece();
if (specialPiece != null) {
specialPiece.interact(player);
}
if (!boardView.isGameEnded()) {
board.advanceTurn();
boardView.refreshBoard();
scoreboard.refreshScoreboard();
}
}
}
}