-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameArea.java
164 lines (148 loc) · 5.41 KB
/
GameArea.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class GameArea extends JPanel implements ActionListener {
private final Map<JButton, int[]> buttonMap;
private final GameLogic gameLogic;
private final GridBagConstraints c;
static JButton[][] buttonArr = new JButton[3][3];
private PlayingSide player;
private PlayingSide computer;
private final ArrayList<ResetGameListener> resetGameListeners = new ArrayList<>();
public GameArea(String opponentType) {
setLayout(new GridBagLayout());
c = new GridBagConstraints();
gameLogic = new GameLogic(opponentType);
buttonMap = new HashMap<>(9);
paintGrid();
paintPlayerPanels(opponentType);
}
private void paintGrid() {
JPanel mainPanel = new JPanel(new GridLayout(3, 3));
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
JButton b = new JButton();
b.setPreferredSize(new Dimension(50, 50));
b.setFont(new Font(Font.MONOSPACED, Font.BOLD, 256));
b.setFocusable(false);
b.addActionListener(this);
mainPanel.add(b);
buttonMap.put(b, new int[]{i, j});
buttonArr[i][j] = b;
}
}
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 1;
c.gridx = 1;
c.gridy = 0;
add(mainPanel, c);
}
private void paintPlayerPanels(String opponentType) {
String p1, p2;
if(opponentType.equalsIgnoreCase("HUMAN")) {
p1 = "Player 1";
p2 = "Player 2";
} else {
p1 = "Player ";
p2 = "Computer";
}
p1 = String.format("%10s", p1);
player = new PlayingSide(p1);
c.fill = GridBagConstraints.VERTICAL;
c.weightx = 0.35;
c.weighty = 1;
c.gridx = 0;
c.gridy = 0;
add(player, c);
p2 = String.format("%10s", p2);
computer = new PlayingSide(p2);
c.fill = GridBagConstraints.VERTICAL;
c.weightx = 0.35;
c.weighty = 1;
c.gridx = 2;
c.gridy = 0;
add(computer, c);
player.setActivePlayer();
}
public void addResetGameListener(ResetGameListener listener) {
resetGameListeners.add(listener);
}
private void notifyResetGameListeners() {
ResetGameEvent event = new ResetGameEvent(this);
for (ResetGameListener listener : resetGameListeners) {
listener.resetGameRequested(event);
}
}
@Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
int isOver;
if(gameLogic.getOpponentType().equalsIgnoreCase("AI")) {
if(gameLogic.getCurrentPlayer() == Player.PLAYER) {
b.setText("X");
b.removeActionListener(this);
isOver = gameLogic.makeHumanMove(buttonMap.get(b));
if (isOver == 1) {
gameLogic.switchPlayer();
computer.setActivePlayer();
player.setInactivePlayer();
} else if (isOver == 0) {
notifyResetGameListeners();
return;
} else {
System.exit(1);
}
isOver = gameLogic.makeAIMove();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (buttonArr[i][j].getText().equalsIgnoreCase("O")) {
buttonArr[i][j].removeActionListener(this);
}
}
}
if (isOver == 1) {
gameLogic.switchPlayer();
player.setActivePlayer();
computer.setInactivePlayer();
} else if (isOver == 0) {
notifyResetGameListeners();
} else {
System.exit(1);
}
}
} else {
if (gameLogic.getCurrentPlayer() == Player.PLAYER) {
b.setText("X");
b.removeActionListener(this);
isOver = gameLogic.makeHumanMove(buttonMap.get(b));
if (isOver == 1) {
gameLogic.switchPlayer();
computer.setActivePlayer();
player.setInactivePlayer();
} else if (isOver == 0) {
notifyResetGameListeners();
} else {
System.exit(1);
}
} else {
b.setText("O");
b.removeActionListener(this);
isOver = gameLogic.makeHumanMove(buttonMap.get(b));
if (isOver == 1) {
gameLogic.switchPlayer();
player.setActivePlayer();
computer.setInactivePlayer();
} else if (isOver == 0) {
notifyResetGameListeners();
} else {
System.exit(1);
}
}
}
}
}