-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bounty.java
390 lines (351 loc) · 10.5 KB
/
Bounty.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
/*******************************************
* Bounty.java
* Engine for Text-Based Adventure Game
* COMP3411 Artificial Intelligence
* UNSW Session 1, 2012
*/
import java.io.*;
import java.net.*;
public class Bounty {
final static int EAST = 0;
final static int NORTH = 1;
final static int WEST = 2;
final static int SOUTH = 3;
private char[][] map;
private char[][] view;
private int nrows; // number of rows in environment
private int irow,icol; // initial row and column
// current row, column and direction of agent
private int row,col,dirn;
private boolean have_axe = false;
private boolean have_key = false;
private boolean have_gold = false;
private boolean in_boat = false;
private boolean off_map = false;
private boolean game_won = false;
private boolean game_lost = false;
private int num_dynamites_held = 0;
private static void swanSong( String message ) {
System.out.println( message );
System.exit(-1);
}
private void read_map( String mapName ) {
BufferedReader in;
boolean agent_here;
// char ch;
int r,c;
map = new char[1024][];
r=-1;
try {
in = new BufferedReader(new FileReader(mapName));
String oneLine = in.readLine();
while(( oneLine != null )&&( oneLine.length() > 0 )) {
map[++r] = new char[oneLine.length()];
for( c=0; c < oneLine.length(); c++ ) {
map[r][c] = oneLine.charAt(c);
agent_here = true;
switch( map[r][c] ) {
case '^': dirn = NORTH; break;
case '>': dirn = EAST; break;
case 'v': dirn = SOUTH; break;
case '<': dirn = WEST; break;
default: agent_here = false;
}
if( agent_here ) {
row = r;
col = c;
}
}
oneLine = in.readLine();
}
}
catch( FileNotFoundException fnfe ) {
swanSong( "File Not Found: "+ mapName );
}
catch( IOException ioe ) {
swanSong( "IO Error" );
}
nrows = r+1; // number of rows
irow = row; // initial row
icol = col; // initial column
}
private void print_map() {
char ch=' ';
int r,c;
System.out.println();
for( r=0; r < nrows; 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];
}
System.out.print( ch );
}
System.out.println();
}
System.out.println();
}
private boolean apply( char action )
{
int d_row, d_col;
int new_row, new_col;
char ch;
if(( action == 'L' )||( action == 'l' )) {
dirn = ( dirn + 1 ) % 4;
return( true );
}
else if(( action == 'R' )||( action == 'r' )) {
dirn = ( dirn + 3 ) % 4;
return( true );
}
else {
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;
if( (new_row < 0)||(new_row >= nrows)
||(new_col < 0)||(new_col >= map[new_row].length)) {
if(( action == 'F' )||( action == 'f' )) {
if( !off_map ) {
map[row][col] = '~';
off_map = true;
}
row = new_row;
col = new_col;
game_lost = true;
return( true );
}
else {
return( false );
}
}
ch = map[new_row][new_col];
switch( action ) {
case 'F': case 'f':
switch( ch ) { // can't move into an obstacle
case '*': case 'T': case '-':
return( false );
}
if( !off_map ) map[row][col] = ' ';
switch( ch ) {
case '~':
if( in_boat ) {
if( !off_map ) map[row][col] = '~';
}
else {
game_lost = true;
}
break;
case ' ': case 'a': case 'k': case 'g': case 'd':
if( in_boat && !off_map ) map[row][col] = 'B';
in_boat = false;
break;
case 'B':
if( in_boat && !off_map ) map[row][col] = 'B';
in_boat = true;
break;
}
row = new_row;
col = new_col;
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;
}
if( have_gold &&( row == irow )&&( col == icol )) {
game_won = true;
}
if( !off_map ) map[row][col] = ' ';
off_map = false;
return( true );
case 'C': case 'c': // chop
if(( ch == 'T' )&& have_axe ) {
map[new_row][new_col] = ' ';
return( true );
}
break;
case 'O': case 'o': // open
if(( ch == '-' )&& have_key ) {
map[new_row][new_col] = ' ';
return( true );
}
break;
case 'B': case 'b': // blast
if( num_dynamites_held > 0 ) {
switch( ch ) {
case '*': case 'T': case '-':
map[new_row][new_col] = ' ';
num_dynamites_held--;
return( true );
}
}
break;
}
}
return( false );
}
private void get_view() {
// char ch;
int i,j,r=0,c=0;
for( i = -2; i <= 2; i++ ) {
for( j = -2; j <= 2; j++ ) {
switch( dirn ) {
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;
}
if( ( r >= 0 )&&( r < nrows )
&&( c >= 0 )&&( c < map[r].length )) {
view[2+i][2+j] = map[r][c];
}
else {
view[2+i][2+j] = '.';
}
}
}
}
private static void printUsage()
{
swanSong(
"Usage: java Bounty [-p <port>] -i map [-m <maxmoves>] [-s]\n");
}
public static void main( String[] args )
{
Bounty bounty;
boolean silent = false;
String mapName = "";
char action = 'F';
int maxmoves = 10000;
int port = 0;
int k,m;
bounty = new Bounty();
bounty.view = new char[5][5];
k=0;
while( k < args.length ) {
if( args[k].compareTo("-i") == 0 ) {
if( ++k < args.length ) {
mapName = args[k++];
}
else {
printUsage();
}
}
else if( args[k].compareTo("-p") == 0 ) {
if( ++k < args.length ) {
port = Integer.parseInt(args[k++]);
}
else {
printUsage();
}
}
else if( args[k].compareTo("-m") == 0 ) {
if( ++k < args.length ) {
maxmoves = Integer.parseInt(args[k++]);
}
else {
printUsage();
}
}
else if( args[k].compareTo("-s") == 0 ) {
silent = true;
k++;
}
else {
printUsage();
}
}
if( mapName.length() == 0 ) {
printUsage();
}
bounty.read_map( mapName );
if( !silent ) {
bounty.print_map();
}
if( port != 0 ) {
InputStream in = null;
OutputStream out = null;
ServerSocket serverSocket = null;
Socket clientSocket = null;
int i,j;
try {
serverSocket = new ServerSocket( port );
clientSocket = serverSocket.accept();
serverSocket.close();
in = clientSocket.getInputStream();
out = clientSocket.getOutputStream();
}
catch( IOException e ) {
swanSong( "Could not listen on port: "+ port );
}
try {
for( m=1; m <= maxmoves; m++ ) {
bounty.get_view();
for( i=0; i < 5; i++ ) {
for( j=0; j < 5; j++ ) {
if( !(( i == 2 )&&( j == 2 ))) {
out.write( bounty.view[i][j] );
}
}
}
out.flush();
action = (char) in.read();
if( !silent ) {
System.out.println("action = "+ action );
}
bounty.apply( action );
if( !silent ) {
bounty.print_map();
}
if( bounty.game_won ) {
swanSong( "Game Won in "+ m +" moves." );
}
else if( bounty.game_lost ) {
swanSong( "Game Lost." );
}
}
swanSong("Exceeded maximum of "+ maxmoves +" moves.\n");
}
catch( IOException e ) {
swanSong("Lost connection to port: "+ port );
}
finally {
try {
clientSocket.close();
}
catch( IOException e ) {}
}
}
else {
Agent agent = new Agent();
for( m=1; m <= maxmoves; m++ ) {
bounty.get_view();
action = agent.get_action( bounty.view );
bounty.apply( action );
if( !silent ) {
bounty.print_map();
}
if( bounty.game_won ) {
swanSong( "Game Won in "+ m +" moves." );
}
else if( bounty.game_lost ) {
swanSong( "Game Lost." );
}
}
swanSong("Exceeded maximum of "+ maxmoves +" moves.");
}
}
}