-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgent.java
441 lines (389 loc) · 12.9 KB
/
Agent.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*********************************************
* Agent.java *
* Agent for Text-Based Adventure Game *
* John Aiden Rohde (z3343276) *
* Sam Basset (z3372468) *
* COMP3411 Artificial Intelligence *
* UNSW Session 1, 2013 *
*********************************************/
import java.util.*;
import java.io.*;
import java.net.*;
public class Agent {
final static int EAST = 0;
final static int NORTH = 1;
final static int WEST = 2;
final static int SOUTH = 3;
final static int ROW = 0;
final static int COL = 1;
final static char PROMPT_USER = 'X';
private char[][] map;
private int irow,icol; // initial row and column
// current row, column and direction of agent
private int row,col,dirn;
// for walking around, remember last direction.
// is updated each time
private int lastDirn;
private boolean firstRun = true;
private boolean willAdvance = false;
private int rowStartWalk, colStartWalk;
private boolean have_axe = false;
private boolean have_key = false;
private boolean have_gold = false;
private boolean game_won = false;
private boolean game_lost = false;
private int num_dynamites_held = 0;
public char get_action(char[][] view) {
// REPLACE THIS CODE WITH AI TO CHOOSE ACTION
int ch=0;
char action = walkAround();
if (action == PROMPT_USER) {
System.out.print("Enter Action(s): ");
try {
while ( ch != -1 ) {
// read character from keyboard
ch = System.in.read();
switch( ch ) { // if character is a valid action, return it
case 'F': case 'L': case 'R': case 'C': case 'O': case 'B':
case 'f': case 'l': case 'r': case 'c': case 'o': case 'b':
return((char) ch );
}
}
}
catch (IOException e) {
System.out.println ("IO error:" + e );
}
}
return action;
}
/* A* with limited visibility. To execute, need a couple of data structures:
* OPEN list can be represented as a Priority Queue
* CLOSED list could be a HashMap (to maybe speed up checking) or simple Vector
*/
/* The hard part here is choosing a good heuristic, especially since most of the
* map won't be immediately visible. Maybe prioritise exploration unless a tool
* is easily reachable or an obstacle is surmountable. Wrong usage of dynamite is
* especially fatal to getting a good solution.
*
* If nothing obvious available, continue on current path until an obstacle is encountered.
* Then turn and continue straight.
*/
// walk around, remembering as much as possible of the map.
// TODO: implement checking each step for reachable, important things.
// get them if possible, otherwise ignore and continue exploring.
private char walkAround() {
// if next tile is walkable, walk forward.
// otherwise, remember last direction and keep making sure there's
// an unwalkable tile there.
// If last dirn walkable, turn towards it.
if (firstRun && isWalkable(getAdjacentTile(dirn))) {
return 'F';
} else {
return followCoast();
}
}
// to find coast first, go forwards blindly. Once found, calibrate that as
// first direction. make sure an obstacle remains on that side, otherwise turn towards it.
// if obstacle on side of original direction and in front, update new direction to current facing
// direction and repeat.
private char followCoast() {
if (firstRun) {
lastDirn = dirn;
firstRun = false;
colStartWalk = col;
rowStartWalk = row;
return 'R';
} else {
if(isWalkable(getAdjacentTile(lastDirn))) {
// turn towards it
// left turn = bigger number, right = smaller.
if ((dirn < lastDirn) || (dirn == SOUTH && lastDirn == EAST)) {
return 'L';
} else if ((dirn > lastDirn) || (dirn == EAST && lastDirn == SOUTH)) {
return 'R';
} else {
willAdvance = true;
}
} else if (!isWalkable(getAdjacentTile(lastDirn)) && !isWalkable(getAdjacentTile(dirn))) {
// else if not walkable and forward is not walkable, update lastDirn and turn R
lastDirn = dirn;
return 'R';
} else {
willAdvance = true;
}
}
if (willAdvance) {
// if next action is to advance, check whether we've done a full loop.
// if so, stop looping around coast (and willadvance = false.)
// if not, advance (and willadvance = false.)
int[] nextTile = getTilePosition(dirn);
if (nextTile[ROW] == rowStartWalk && nextTile[COL] == colStartWalk) {
willAdvance = false;
return PROMPT_USER;
} else {
willAdvance = false;
return 'F';
}
}
return PROMPT_USER;
}
private char getAdjacentTile(int direction) {
char next = '~'; // assume unsafe unless otherwise known
int d_row = 0; int d_col = 0;
int nextRow = 0; int nextCol = 0;
switch( direction ) {
case NORTH:
d_row = -1; break;
case SOUTH:
d_row = 1; break;
case EAST:
d_col = 1; break;
case WEST:
d_col = -1; break;
}
nextRow = row + d_row;
nextCol = col + d_col;
//ch is the object in front of us
next = map[nextRow][nextCol];
return next;
}
// returns an array of size 2 ([row,col]) of point in direction
private int[] getTilePosition(int direction) {
int tile[] = new int[2];
int d_row = 0; int d_col = 0;
tile[0] = 0; tile[1] = 0;
switch( direction ) {
case NORTH:
d_row = -1; break;
case SOUTH:
d_row = 1; break;
case EAST:
d_col = 1; break;
case WEST:
d_col = -1; break;
}
tile[ROW] = row + d_row;
tile[COL] = col + d_col;
return tile;
}
private boolean isWalkable(char tile) {
switch(tile) {
case '*': case 'T': case '-': case '~':
return false;
default:
return true;
}
}
void print_view(char view[][] )
{
int i,j;
System.out.println("\n+-----+");
for( i=0; i < 5; i++ ) {
System.out.print("|");
for( j=0; j < 5; j++ ) {
if(( i == 2 )&&( j == 2 )) {
System.out.print('^');
}
else {
System.out.print( view[i][j] );
}
}
System.out.println("|");
}
System.out.println("+-----+");
}
/****************************************************************
* DISCLAIMER: update_world and update_map are inspired in part *
* by code supplied in Rogue.java *
****************************************************************/
//Update the world view based on the previous action taken
private boolean update_world( char action, char view[][] ){
int d_row, d_col;
int new_row, new_col;
char ch;
// run the first time only
if ( action == 'i') {
init_world(view);
return( true );
}
// make changes to direction only if dir'n changed
if (( action == 'L' )||( action == 'l' )) {
dirn = ( dirn + 1 ) % 4;
return( true );
} else if (( action == 'R' )||( action == 'r' )) {
dirn = ( dirn + 3 ) % 4;
return( true );
} else {
// if direction not changed, look ahead 1 space to see what's there
d_row = 0; d_col = 0;
switch( dirn ) {
case NORTH:
d_row = -1; break;
case SOUTH:
d_row = 1; break;
case EAST:
d_col = 1; break;
case WEST:
d_col = -1; break;
}
new_row = row + d_row;
new_col = col + d_col;
//ch is the object in front of us
ch = map[new_row][new_col];
}
// if no direction changes to be made:
switch( action ) {
case 'F': case 'f': // forward
switch( ch ) { // can't move into an obstacle or water
case '*': case 'T': case '-': case '~':
return( false );
}
map[row][col] = ' '; // clear current location
row = new_row;
col = new_col;
map[row][col] = ' '; // clear new location
switch( ch ) {
case 'a':
have_axe = true; break;
case 'k':
have_key = true; break;
case 'g':
have_gold = true; break;
case 'd':
num_dynamites_held++; break;
case '~':
game_lost = true; break;
}
if( have_gold &&( row == irow )&&( col == icol )) {
game_won = true;
}
update_map(view);
return( true );
case 'L': case 'R': case 'C': case 'O': case 'B':
case 'l': case 'r': case 'c': case 'o': case 'b':
update_map(view);
return(true);
}
return(false);
}
//Perform the inital setup of the agent and the world map
private void init_world(char view[][] ) {
//The intial setup of agent position
row = 100;
col = 100;
irow = 100;
icol = 100;
//The way the agent is facing is considered north
dirn = NORTH;
//Initialize the map
map = new char[200][200];
for (int i = 0; i < 200; i++) {
for (int j = 0 ;j < 200; j++) {
map[i][j] = 'X';
}
}
update_map( view );
}
private void update_map(char view[][] ) {
int r=0,c=0,i,j;
for( i = -2; i <= 2; i++ ) {
for( j = -2; j <= 2; j++ ) {
switch( dirn ) { // Adjust the orientation
case NORTH: r = row+i; c = col+j; break;
case SOUTH: r = row-i; c = col-j; break;
case EAST: r = row+j; c = col-i; break;
case WEST: r = row-j; c = col+i; break;
}
map[r][c] = view[2+i][2+j];
}
}
print_map();
}
//Print the larger map for fun while debugging
private void print_map() {
char ch=' ';
int r,c;
boolean interestingLine = false;
System.out.println("\n");
for( r=0; r < 200; r++ ) {
for( c=0; c < map[r].length; c++ ) {
if(( r == row )&&( c == col )) { // agent is here
switch( dirn ) {
case NORTH: ch = '^'; break;
case EAST: ch = '>'; break;
case SOUTH: ch = 'v'; break;
case WEST: ch = '<'; break;
}
}
else {
ch = map[r][c];
}
if (ch != 'X') {
System.out.print( ch );
interestingLine = true;
}
}
if (interestingLine) {
System.out.println();
interestingLine = false;
}
}
System.out.println();
}
public static void main( String[] args ){
InputStream in = null;
OutputStream out= null;
Socket socket = null;
Agent agent = new Agent();
char view[][] = new char[5][5];
//Special action i used to intialize the map
char action = 'i';
int port;
int ch;
int i,j;
if( args.length < 2 ) {
System.out.println("Usage: java Agent -p <port>\n");
System.exit(-1);
}
port = Integer.parseInt( args[1] );
try { // open socket to Game Engine
socket = new Socket( "localhost", port );
in = socket.getInputStream();
out = socket.getOutputStream();
}
catch( IOException e ) {
System.out.println("Could not bind to port: "+port);
System.exit(-1);
}
//GAME LOOP
try { // scan 5-by-5 wintow around current location
while( true ) {
for( i=0; i < 5; i++ ) {
for( j=0; j < 5; j++ ) {
if( !(( i == 2 )&&( j == 2 ))) {
ch = in.read();
if( ch == -1 ) {
System.exit(-1);
}
view[i][j] = (char) ch;
}
}
}
agent.update_world( action, view ); //Update the map and other things
agent.print_view( view ); // COMMENT THIS OUT BEFORE SUBMISSION
action = agent.get_action(view);
out.write( action );
}
}
catch( IOException e ) {
System.out.println("Lost connection to port: "+ port );
System.exit(-1);
}
finally {
try {
socket.close();
}
catch( IOException e ) {}
}
}
}