-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAI1.java
More file actions
127 lines (105 loc) · 3.46 KB
/
Copy pathAI1.java
File metadata and controls
127 lines (105 loc) · 3.46 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
import java.util.ArrayList;
import javax.swing.*;
import java.awt.event.*;
public class AI1 extends IAI{
private ArrayList<Integer> moves;
private ArrayList<Integer> tracker;
private ArrayList<boolean[][]> stateTracker;
private int size;
public AI1(){
moves = new ArrayList();
tracker = new ArrayList();
stateTracker = new ArrayList();
size = 0;
}
public void playGame(){
Board b = new Board();
b.addRandomValue();
b.addRandomValue();
moves = new ArrayList();
tracker = new ArrayList();
stateTracker = new ArrayList();
size = stateTracker.size();
do{
//choose move
int[][] boardState = b.getBoard();
int move = findBestMove(boardState);
if (moves.size() < 1000){
moves.add(move);
tracker.add(b.getNumberTiles());
boolean[][] stateCopy = new boolean[4][4];
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
if (boardState[i][j] > 0){
stateCopy[i][j] = true;
} else {
stateCopy[i][j] = false;
}
}
}
stateTracker.add(stateCopy);
}
move(b,move);
} while (!b.isGameOver());
setScore(b.getPoints());
filterData(5);
}
public void playGUI(){
GameEngine g = new GameEngine();
BoardGUI b = g.getBoard();
Timer t = new Timer(500, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
int move = findBestMove(b.getBoard());
g.aIPlay(move);
if (b.isGameOver()){
Timer t = (Timer) e.getSource();
t.stop();
playGUI();
}
}
});
t.start();
}
public void filterData(int size){
for (int i = 0; i < moves.size();){
if (!checkDecrease(i,size)){
for (int j = 0; j < size; j++){
stateTracker.remove(i);
moves.remove(i);
tracker.remove(i);
}
} else {
i++;
}
}
this.size = stateTracker.size();
}
//true if states are the same, false otherwise
public static boolean checkState(int[][] a, boolean[][] b){
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
if ((a[i][j] > 0 && !b[i][j]) || (a[i][j] == 0 && b[i][j])){
return false;
}
}
}
return true;
}
public boolean checkDecrease(int index, int size){
if (index+size >= tracker.size()) return true;
if (tracker.get(index) - tracker.get(index+size) < 0){
return true;
}
return false;
}
public int findBestMove(int[][] boardState){
int r = size;
for (int i = 0; i < r; i++){
if (checkState(boardState,stateTracker.get(i))){
return moves.get(i);
}
}
return (int)(Math.random()*4+1);
}
}