-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinimax.java
More file actions
120 lines (113 loc) · 4.62 KB
/
Copy pathMinimax.java
File metadata and controls
120 lines (113 loc) · 4.62 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
import java.util.Scanner;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.Random;
public class Minimax {
private Game game;
private int choice;
public Minimax(Game g) {
game = g;
}
public void aiMakeMove() throws CloneNotSupportedException {
calculateMove((Game)game.clone());
game.makeMove(choice);
TicTacToe.buttons.get(choice).setText("O");
}
private int calculateMove(Game g) throws CloneNotSupportedException {
//get score if game is over
if (isOver(g)) {
return getScore(g);
}
//arrays have identical size and contain paired data
//might be better to use a map
ArrayList<Integer> scores = new ArrayList<>();
ArrayList<Integer> moves = new ArrayList<>();
//itirate through all available moves recursing on each
for (int i = 0; i < 9; i++) {
Cell c = g.getBoard().getCell(i);
if (!c.getIsTaken()) {
Game possibleGame = (Game)g.clone();
possibleGame.makeMove(i);
possibleGame.switchPlayer();
scores.add(calculateMove(possibleGame));
moves.add(i);
}
}
//decide which move will be made
if (g.getCurrentPlayer().equals("O")) {
//max, computer's turn
ArrayList<Integer> maxIndeces = new ArrayList<>(Arrays.asList(0));
for (int i = 0; i < scores.size(); i++) {
int score = scores.get(i);
int currentMax = scores.get(maxIndeces.get(0));
if (score > currentMax) {
maxIndeces.clear();
maxIndeces.add(i);
} else if (score == currentMax) {
maxIndeces.add(i);
}
}
Random rand = new Random();
int pick = rand.nextInt(maxIndeces.size());
choice = moves.get(maxIndeces.get(pick));
return scores.get(maxIndeces.get(pick));
} else {
//min, human's turn
ArrayList<Integer> minIndeces = new ArrayList<>(Arrays.asList(0));
for (int i = 0; i < scores.size(); i++) {
int score = scores.get(i);
int currentMin = scores.get(minIndeces.get(0));
if (score < currentMin) {
minIndeces.clear();
minIndeces.add(i);
} else if (score == currentMin) {
minIndeces.add(i);
}
}
Random rand = new Random();
int pick = rand.nextInt(minIndeces.size());
choice = moves.get(minIndeces.get(pick));
return scores.get(minIndeces.get(pick));
}
}
//checks if the game is over
private boolean isOver(Game g) {
HashSet<ArrayList<Integer>> winCombs = new HashSet<>(Arrays
.asList(new ArrayList<Integer>(Arrays.asList(0, 1, 2)),
new ArrayList<Integer>(Arrays.asList(3, 4, 5)),
new ArrayList<Integer>(Arrays.asList(6, 7, 8)),
new ArrayList<Integer>(Arrays.asList(0, 3, 6)),
new ArrayList<Integer>(Arrays.asList(1, 4, 7)),
new ArrayList<Integer>(Arrays.asList(2, 5, 8)),
new ArrayList<Integer>(Arrays.asList(0, 4, 8)),
new ArrayList<Integer>(Arrays.asList(2, 4, 6))));
for (ArrayList<Integer> comb : winCombs) {
if (g.playerO.containsAll(comb) || g.playerX.containsAll(comb)) {
return true;
}
}
return g.getBoard().isFull();
}
//gets the score for minimax for an ended game
private int getScore(Game g) {
HashSet<ArrayList<Integer>> winCombs = new HashSet<>(Arrays
.asList(new ArrayList<Integer>(Arrays.asList(0, 1, 2)),
new ArrayList<Integer>(Arrays.asList(3, 4, 5)),
new ArrayList<Integer>(Arrays.asList(6, 7, 8)),
new ArrayList<Integer>(Arrays.asList(0, 3, 6)),
new ArrayList<Integer>(Arrays.asList(1, 4, 7)),
new ArrayList<Integer>(Arrays.asList(2, 5, 8)),
new ArrayList<Integer>(Arrays.asList(0, 4, 8)),
new ArrayList<Integer>(Arrays.asList(2, 4, 6))));
for (ArrayList<Integer> comb : winCombs) {
if (g.playerX.containsAll(comb)) {
return -1;
} else if (g.playerO.containsAll(comb)) {
return 1;
}
}
return 0;
}
}