-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOthelloFrame.java
More file actions
184 lines (149 loc) · 4.16 KB
/
OthelloFrame.java
File metadata and controls
184 lines (149 loc) · 4.16 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class OthelloFrame extends JFrame implements MouseListener, ActionListener{
private OthelloCanvas myCanvas;
private JLabel score;
private JLabel turn;
private JLabel comment;
private JButton restart;
private JRadioButton blackComp;
private JRadioButton whiteComp;
private JRadioButton noComp;
private char computer;
private String player = "black";
private String opponent = "white";
public OthelloFrame () {
super("Othello");
setLocation(0, 0);
addWindowListener(new MyWindowAdapter());
myCanvas = new OthelloCanvas();
myCanvas.addMouseListener(this);
score = new JLabel("");
turn = new JLabel("");
comment = new JLabel("");
restart = new JButton("Start New Game");
restart.addMouseListener(this);
noComp = new JRadioButton("Play against user");
noComp.setSelected(true);
noComp.addActionListener(this);
blackComp = new JRadioButton("Play against black computer");
blackComp.addActionListener(this);
whiteComp = new JRadioButton("Play against white computer");
whiteComp.addActionListener(this);
ButtonGroup group = new ButtonGroup();
group.add(noComp);
group.add(blackComp);
group.add(whiteComp);
JPanel canvasPanel = new JPanel();
canvasPanel.setLayout(new FlowLayout());
canvasPanel.add(myCanvas);
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new FlowLayout());
radioPanel.add(noComp);
radioPanel.add(blackComp);
radioPanel.add(whiteComp);
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container.add(canvasPanel);
container.add(score);
score.setAlignmentX(0.5f);
container.add(turn);
turn.setAlignmentX(0.5f);
container.add(comment);
comment.setAlignmentX(0.5f);
container.add(restart);
restart.setAlignmentX(0.5f);
container.add(radioPanel);
add(container);
setSize(600, 550);
setVisible(true);
newGame();
}
//Listening mouse event
public void mouseClicked(MouseEvent e) {
if (e.getSource() == myCanvas) {
play(e.getX(), e.getY());
}
else if (e.getSource() == restart) {
newGame();
}
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
//Listening action event
public void actionPerformed(ActionEvent e) {
if (noComp.isSelected()) {
computer = 'n';
}
else if (blackComp.isSelected()) {
computer = 'b';
}
else if (whiteComp.isSelected()) {
computer = 'w';
}
}
private void newGame() {
Othello.freshBoard();
player = "black";
opponent = "white";
myCanvas.repaint();
score.setText(Othello.getScore());
turn.setText("It's " + player + "'s turn.");
comment.setText(" ");
if (computer == 'b') getCompMove();
}
public void play(int row, int col) {
if (Othello.isReversed(player, opponent, row, col)) {
myCanvas.repaint();
score.setText(Othello.getScore());
comment.setText(" ");
//Get turn
switchPlayer();
if (!Othello.isPossible(player, opponent)) {
if (!Othello.isPossible(opponent, player)) {
endGame();
return;
}
else {
comment.setText("Oops, "+player+" has no valid move.");
switchPlayer();
}
}
turn.setText("It's "+player+"'s turn.");
//Get computer's move
if ((computer == 'b' && player == "black") ||
(computer == 'w' && player == "white")) {
getCompMove();
}
}
else comment.setText("Invalid move. Please try again.");
}
private void getCompMove() {
int[] compMove = Othello.computer(player, opponent);
int compRow = compMove[0]*50, compCol = compMove[1]*50;
play(compCol, compRow);
}
private void switchPlayer() {
if (player == "black") {
player = "white";
opponent = "black";
}
else {
player = "black";
opponent = "white";
}
}
private void endGame() {
turn.setText("No more turns.");
comment.setText("Game over. "+Othello.findWinner());
}
private class MyWindowAdapter extends WindowAdapter {
public void windowClosing (WindowEvent e) {
dispose();
System.exit(0);
}
}
}