-
Notifications
You must be signed in to change notification settings - Fork 0
/
AIplayer.java
275 lines (233 loc) · 7.76 KB
/
AIplayer.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package ubco.ai.games;
import ubco.ai.GameRoom;
import ubco.ai.connection.ServerMessage;
import ubco.ai.games.GameClient;
import ubco.ai.games.GameMessage;
import ubco.ai.games.GamePlayer;
import net.n3.nanoxml.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
public class AIplayer implements GamePlayer {
GameClient gameClient = null;
ArrayList<GameRoom> roomList = null;
GameRoom currentRoom = null;
int roomID = 0;
GameBoard board = null;
boolean whitePlayer = true;
boolean gameStarted = false;
String name = "";
String passwd = "";
chessBoard gameBoard;
AI ai = null;
public AIplayer(String name, String passwd)
{
this.gameBoard = new chessBoard();
this.name = name;
this.passwd = passwd;
this.ai = new AI(board);
// setup server game
gameClient = new GameClient(name, passwd, this);
roomList = gameClient.getRoomLists();
// join a particular room (room 0)
//gameClient.roomList.get(0);
// join the first open room
for(int i=0; i<roomList.size(); i++)
{
if(roomList.get(i).userCount < 2)
{
currentRoom = gameClient.roomList.get(i);
roomID = currentRoom.roomID;
gameClient.joinGameRoom(currentRoom.roomName);
System.out.println("Joined Room: " + roomList.get(i).roomName);
break;
}
}
}
public void sendToServer(String action, int roomID, int posX, int posY, int arow, int acol, int qfr, int qfc){
/*try {
Thread.sleep(4000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}*/
String actionMsg = "<action type='" + action + "'>";
Character c = new Character((char) (97 + qfr));
char xFrom = c;
actionMsg = actionMsg + "<queen move='" + c.charValue() + String.valueOf(qfc) + "-";
c = new Character((char)(97 + posX));
char xTo = c;
actionMsg = actionMsg + c.charValue() + String.valueOf(posY) + "'>" + "</queen> ";
c = new Character((char) (97 + arow));
char arrowX = c;
actionMsg = actionMsg + "<arrow move='" + c.charValue() + String.valueOf(acol) + "'>" + "</arrow>";
actionMsg = actionMsg + "</action>";
System.out.println(actionMsg);
if(whitePlayer)
{
gameBoard.writeMove("white", "White player moved from " + xFrom + qfc + " to " + xTo + posY + " and fired arrow to " + arrowX + acol);
}
else
{
gameBoard.writeMove("black", "Black player moved from " + xFrom + qfc + " to " + xTo + posY + " and fired arrow to " + arrowX + acol);
}
String msg = ServerMessage.compileGameMessage(GameMessage.MSG_GAME, roomID, actionMsg);
gameClient.sendToServer(msg, true);
}
private void handleOpponentMove(IXMLElement xml){
System.out.println("Opp Move");
IXMLElement c1 = xml.getFirstChildNamed("queen");
String qmove = c1.getAttribute("move", "default");
IXMLElement c2 = xml.getFirstChildNamed("arrow");
String amove = c2.getAttribute("move", "defalut");
int qX = 0;
int qY = 0;
char c = qmove.charAt(3);
char xTo = c;
qX = 9 - (c - 97);
qY = Integer.parseInt(qmove.substring(4,5));
int qfX = 0;
int qfY = 0;
c = qmove.charAt(0);
char xFrom = c;
qfX = 9 - (c - 97);
qfY = Integer.parseInt(qmove.substring(1, 2));
int aX = 0;
int aY = 0;
c = amove.charAt(0);
char arrowX = c;
aX = 9 - (c - 97);
aY = Integer.parseInt(amove.substring(1, amove.length()));
int amazonId = board.getAmazonId(qfX, qfY);
// move opponent amazon and fire arrow
board.moveOpponent(amazonId, qfX, qfY, qX, qY, whitePlayer);
if(whitePlayer)
{
gameBoard.writeMove("black", "Black player moved from " + xFrom + qfY + " to " + xTo + qY + " and fired arrow to " + arrowX + aY);
}
else
{
gameBoard.writeMove("white", "White player moved from " + xFrom + qfY + " to " + xTo + qY + " and fired arrow to " + arrowX + aY);
}
board.fireArrow(aX, aY);
if(whitePlayer){
gameBoard.moveTheirPieces(amazonId-4, qX, qY);
}
else{
gameBoard.moveOurPieces(amazonId-4, qX, qY);
}
gameBoard.fireArrow(aX, aY);
moveOurAmazon();
}
public void moveOurAmazon()
{
// get our move based on the opponents move
MoveAndBoard nextMove = ai.getNextMove(this.board);
int y = nextMove.newMove.col;
int x = (nextMove.newMove.row);
int arrowY = nextMove.newMove.arrow_col;
int arrowX = (nextMove.newMove.arrow_row);
int aId = nextMove.newMove.amazon_id;
int fromX = (this.board.Amazons[aId].row);
int fromY = this.board.Amazons[aId].column;
this.board = nextMove.newBoard;
moveGamePieceGui(aId, x, y, arrowX, arrowY);
// send move to server
sendToServer(GameMessage.ACTION_MOVE, roomID, 9-x, y, 9-arrowX, arrowY, 9-fromX, fromY);
}
public static void main(String[] args) {
System.out.println("Do you want to play against the computer? (Yes or No): ");
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
if(input.equals("Yes"))
{
Human humanPlayer = new Human();
humanPlayer.getOpponentMove();
}
else
{
AIplayer client = new AIplayer("FooGoos", "amazons");
}
}
@Override
public boolean handleMessage(GameMessage msg)
{
IXMLElement xml = ServerMessage.parseMessage(msg.msg);
String type = xml.getAttribute("type", "WRONG!");
System.out.println(msg);
if(type.equals(GameMessage.ACTION_ROOM_JOINED))
{
onJoinRoom(xml);
}
else if (type.equals(GameMessage.ACTION_GAME_START)) // handle game start
{
this.gameStarted = true;
IXMLElement usrlist = xml.getFirstChildNamed("usrlist");
int ucount = usrlist.getAttribute("ucount", -1);
Enumeration ch = usrlist.enumerateChildren();
// start the game and set up board
while(ch.hasMoreElements())
{
IXMLElement usr = (IXMLElement)ch.nextElement();
int id = usr.getAttribute("id", -1);
String name = usr.getAttribute("name", "nnn");
if(!name.equalsIgnoreCase(this.name)){
continue;
}
// decide if we are white or black
String role = usr.getAttribute("role", "W");
if(role.equalsIgnoreCase("W")){
this.whitePlayer = true;
// setup up the board
this.board = new GameBoard(whitePlayer);
gameBoard.writeWhiteAndBlack(whitePlayer);
System.out.println("Game Start: " + msg.msg);
moveOurAmazon();
}
else{
this.whitePlayer = false;
// setup up the board
this.board = new GameBoard(whitePlayer);
gameBoard.writeWhiteAndBlack(whitePlayer);
System.out.println("Game Start: " + msg.msg);
}
}
System.out.println("Game Start: " + msg.msg);
}
else if(type.equals(GameMessage.ACTION_MOVE)) // handle an opponent move
{
//System.out.println("GOT OPPONENT MOVE");
this.handleOpponentMove(xml);
}
return true;
}
private void moveGamePieceGui(int aId, int x, int y, int arrowX, int arrowY)
{
// move piece on GUI
if(whitePlayer){
gameBoard.moveOurPieces(aId, x, y);
}
else{
gameBoard.moveTheirPieces(aId, x, y);
}
gameBoard.fireArrow(arrowX, arrowY);
}
//handle the response of joining a room
private void onJoinRoom(IXMLElement xml){
IXMLElement usrlist = xml.getFirstChildNamed("usrlist");
int ucount = usrlist.getAttribute("ucount", -1);
Enumeration ch = usrlist.enumerateChildren();
while(ch.hasMoreElements()){
IXMLElement usr = (IXMLElement)ch.nextElement();
int id = usr.getAttribute("id", -1);
String name = usr.getAttribute("name", "NO!");
}
}
@Override
public boolean handleMessage(String arg0) throws Exception {
// TODO Auto-generated method stub
return false;
}
}