-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
34 lines (31 loc) · 983 Bytes
/
Player.java
File metadata and controls
34 lines (31 loc) · 983 Bytes
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
import java.awt.event.*;
import javax.swing.*;
public class Player implements ActionListener{
public static final int BLACK = 1;
public static final int WHITE = 2;
private Game game;
private int whosTurn;
private int[]selected;
public Player (Game _game) {
game = _game;
whosTurn = WHITE;
}
public void actionPerformed(ActionEvent ae){
JButton jb = (JButton)ae.getSource();
int [] position = game.getPosition(jb);
if (selected == null && game.positionColor(position[0],position[1])==whosTurn){
selected = position;
game.selectPosition(position[0],position[1]);
} else if(selected == position){
selected = null;
game.unselectPosition(position[0],position[1]);
} else if(selected != null && game.validMove(selected[0],selected[1],position[0],position[1],whosTurn)) {
game.move(selected[0],selected[1],position[0],position[1],whosTurn);
if (whosTurn == BLACK)
whosTurn = WHITE;
else
whosTurn = BLACK;
selected = null;
}
}
}