-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAIAgent.java
More file actions
150 lines (135 loc) · 5 KB
/
Copy pathAIAgent.java
File metadata and controls
150 lines (135 loc) · 5 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
import java.util.*;
public class AIAgent{
Random rand;
public AIAgent(){
rand = new Random();
}
/*
The method randomMove takes as input a stack of potential moves that the AI agent
can make. The agent uses a rondom number generator to randomly select a move from
the inputted Stack and returns this to the calling agent.
*/
public Move randomMove(Stack possibilities){
int moveID = rand.nextInt(possibilities.size());
System.out.println("Agent randomly selected move : "+moveID);
for(int i=1;i < (possibilities.size()-(moveID));i++){
possibilities.pop();
}
Move selectedMove = (Move)possibilities.pop();
return selectedMove;
}
public Move nextBestMove(Stack whiteMoves, Stack blackMoves){
Stack whiteBackup = (Stack) whiteMoves.clone();
Stack blackMove = (Stack) blackMoves.clone();
Move whiteMove, standardMove, attackMove;
Square blackPosition;
int point = 0;
int chosenPiece = 0;
attackMove = null;
while(!whiteMoves.empty()){
whiteMove = (Move) whiteMoves.pop();
standardMove = whiteMove;
if ((standardMove.getStart().getYC() < standardMove.getLanding().getYC())
&& (standardMove.getLanding().getXC() == 3) && (standardMove.getLanding().getYC() == 3)
|| (standardMove.getLanding().getXC() == 4) && (standardMove.getLanding().getYC() == 3)
|| (standardMove.getLanding().getXC() == 3) && (standardMove.getLanding().getYC() == 4)
|| (standardMove.getLanding().getXC() == 4) && (standardMove.getLanding().getYC() == 4)) {
point = 0;
// set best move
if (point > chosenPiece) {
chosenPiece = point;
attackMove = standardMove;
}
}
while(!blackMove.isEmpty()){
point = 0;
blackPosition = (Square) blackMove.pop();
if ((standardMove.getLanding().getXC() == blackPosition.getXC()) && (standardMove.getLanding().getYC() == blackPosition.getYC())){
// set value to pieces
if(blackPosition.getName().equals("BlackPawn")){
point = 1;
}
else if(blackPosition.getName().equals("BlackKnight") || blackPosition.getName().equals("BlackBishop")){
point = 3;
}
else if(blackPosition.getName().equals("BlackRook")){
point = 5;
}
else if(blackPosition.getName().equals("BlackQueen")){
point = 9;
}
else if(blackPosition.getName().equals("BlackKing")){
point = 1000;
}
}
if(point > chosenPiece){
chosenPiece = point;
attackMove = standardMove;
}
}
blackMove = (Stack) blackMoves.clone();
}
if(chosenPiece > 0){
System.out.println("The best move selected by the AI is: " + chosenPiece);
return attackMove;
}
return randomMove(whiteBackup);
}
public Move twoLevelsDeep(Stack whiteMoves, Stack blackMoves){
Stack whiteBackup = (Stack) whiteMoves.clone();
Stack blackMove = (Stack) blackMoves.clone();
Move whiteMove, standardMove, attackMove;
Square blackPosition;
int point = 0;
int chosenPiece = 0;
attackMove = null;
while(!whiteMoves.empty()){
whiteMove = (Move) whiteMoves.pop();
standardMove = whiteMove;
if ((standardMove.getStart().getYC() < standardMove.getLanding().getYC())
&& (standardMove.getLanding().getXC() == 3) && (standardMove.getLanding().getYC() == 3)
|| (standardMove.getLanding().getXC() == 4) && (standardMove.getLanding().getYC() == 3)
|| (standardMove.getLanding().getXC() == 3) && (standardMove.getLanding().getYC() == 4)
|| (standardMove.getLanding().getXC() == 4) && (standardMove.getLanding().getYC() == 4)) {
point = 0;
// set best move
if (point > chosenPiece) {
chosenPiece = point;
attackMove = standardMove;
}
}
while(!blackMove.isEmpty()){
point = 0;
blackPosition = (Square) blackMove.pop();
if ((standardMove.getLanding().getXC() == blackPosition.getXC()) && (standardMove.getLanding().getYC() == blackPosition.getYC())){
// set value to pieces
if(blackPosition.getName().equals("BlackPawn")){
point = 1;
}
else if(blackPosition.getName().equals("BlackKnight") || blackPosition.getName().equals("BlackBishop")){
point = 3;
}
else if(blackPosition.getName().equals("BlackRook")){
point = 5;
}
else if(blackPosition.getName().equals("BlackQueen")){
point = 9;
}
else if(blackPosition.getName().equals("BlackKing")){
point = 1000;
}
}
if(point > chosenPiece){
chosenPiece = point;
attackMove = standardMove;
}
}
blackMove = (Stack) blackMoves.clone();
}
if(chosenPiece > 0){
System.out.println("The best move selected by the AI is: " + chosenPiece);
return attackMove;
}
return randomMove(whiteBackup);
}
}