-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
184 lines (164 loc) · 5.28 KB
/
TicTacToe.java
File metadata and controls
184 lines (164 loc) · 5.28 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
180
181
182
183
184
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JButton;
public class TicTacToe extends JFrame implements ActionListener {
boolean multiplayer;
String[] symbol_options = {"X", "O"};
String player_symbol = symbol_options[0];
String computer_symbol = symbol_options[1];
JButton tl = new JButton();
JButton tm = new JButton();
JButton tr = new JButton();
JButton ml = new JButton();
JButton mm = new JButton();
JButton mr = new JButton();
JButton bl = new JButton();
JButton bm = new JButton();
JButton br = new JButton();
JButton[] cells = { tl, tm, tr, ml, mm, mr, bl, bm, br };
TicTacToe(boolean multiplayer) {
this.multiplayer = multiplayer;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(3, 3));
for (JButton cell : cells) {
cell.setBackground(new Color(240, 240, 255));
cell.setPreferredSize(new Dimension(100, 100));
cell.setFont(new Font("Geneva", Font.BOLD, 30));
cell.setFocusable(false);
cell.addActionListener(this);
this.add(cell);
}
this.pack();
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton cell;
if (e.getSource() == tl) {
cell = tl;
} else if (e.getSource() == tm) {
cell = tm;
} else if (e.getSource() == tr) {
cell = tr;
} else if (e.getSource() == ml) {
cell = ml;
} else if (e.getSource() == mm) {
cell = mm;
} else if (e.getSource() == mr) {
cell = mr;
} else if (e.getSource() == bl) {
cell = bl;
} else if (e.getSource() == bm) {
cell = bm;
} else if (e.getSource() == br) {
cell = br;
} else {
cell = new JButton();
System.out.println("UH OH");
}
setCell(cell);
if (multiplayer) {
return;
}
setCell(cells[compMove(getBoard())]);
// detect win function
}
////////////////////////////////////////////// SETTING CELL//////////////////////////////////////////////
private void setCell(JButton cell) {
if (cell.getText() == "") {
cell.setText(String.valueOf(symbol_options[0]));
String a = this.symbol_options[0], b = this.symbol_options[1];
this.symbol_options[0] = b;
this.symbol_options[1] = a;
}
}
////////////////////////////////////////////// COMPUTER MOVE//////////////////////////////////////////////
private int compMove(String[] board) {
int bestMove = 0;
int bestScore = Integer.MIN_VALUE;
for (int i = 0; i < 9; i++) {
if (board[i] == computer_symbol || board[i] == player_symbol) {
continue;
}
board[i] = computer_symbol;
int moveScore = minimax(board, getBlankSpaces(board), false);
if (moveScore > bestScore) {
bestMove = i;
bestScore = moveScore;
}
board[i] = "";
}
return bestMove;
}
private int minimax(String[] board, int depth, boolean maximizing_player) {
int score = getScore(board);
if (depth == 0 || Math.abs(score) == 10) {
return score;
}
if (maximizing_player) {
int max_eval = Integer.MIN_VALUE;
for (int i = 0; i < 9; i++) {
if (board[i] == computer_symbol || board[i] == player_symbol) {
continue;
}
board[i] = computer_symbol;
int eval = minimax(board, depth - 1, false);
max_eval = Math.max(max_eval, eval);
board[i] = "";
}
return max_eval;
} else {
int min_eval = Integer.MAX_VALUE;
for (int i = 0; i < 9; i++) {
if (board[i] == computer_symbol || board[i] == player_symbol) {
continue;
}
board[i] = player_symbol;
int eval = minimax(board, depth - 1, true);
min_eval = Math.min(min_eval, eval);
board[i] = "";
}
return min_eval;
}
}
private String[] getBoard() {
String[] board = new String[9];
for (int i = 0; i < 9; i++) {
board[i] = cells[i].getText();
}
return board;
}
private int getScore(String[] board) {
String[][] lines = new String[8][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
lines[i][j] = board[j + 3 * i];
lines[i + 3][j] = board[i + 3 * j];
}
lines[6][i] = board[4 * i];
lines[7][i] = board[2 + 2 * i];
}
for (String[] line : lines) {
if (line[0].equals(computer_symbol) && line[1].equals(computer_symbol) && line[2].equals(computer_symbol)) {
return 10;
} else if (line[0].equals(player_symbol) && line[1].equals(player_symbol) && line[2].equals(player_symbol)) {
return -10;
}
}
return 0;
}
private int getBlankSpaces(String[] board) {
int blanks = 0;
for (int i = 0; i < 9; i++) {
if (board[i].equals("")) {
blanks++;
}
}
return blanks;
}
}