-
Notifications
You must be signed in to change notification settings - Fork 0
/
WumpusMainClass.java
456 lines (339 loc) · 12.2 KB
/
WumpusMainClass.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package WumpusGame;
import WumpusGame.Edge;
import WumpusGame.Node;
import java.sql.DriverManager;
import java.sql.*;
import javax.print.DocFlavor;
import java.awt.Dialog;
import javax.swing.*;
import javax.swing.JFrame;
import java.util.Hashtable;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import javax.swing.JOptionPane;
public final class World {
// layout will be represnted as a node list and an edge list
// In this game, the nodes (rooms) are attractions, which are serviced by 4 tracks, all
// originating from the park office. The track structure is not modelled explicitly -
// it is captured in the edge list. It can also be inferred from the node list - the numbers
// in the attraction name indicate the track on which the attraction is on.
// In the node list below, there is NO duplication of attractions - attractions that are at
// the junction of two or more tracks are included as comment lines. Note also that there is
// only one track servicing adjacent attractions.
public static String[] rooms = new String[17];
// Edge format is <from, to, track, distance>
// Because we are using an array, the types of all elements must be the same.
// Hence distance is a string, not an int.
public static String [][] exits = new String[100][4] ;
Hashtable <String,Node> nodes;
Hashtable <String,ArrayList> edges;
ArrayList <Bats> b = new ArrayList();;
ArrayList <Treasure> tr = new ArrayList();
private int totalNumberOftreasure = 0;
Node wumpus;
Node location;
//Declare global variables
private Connection con = null;
private Statement st = null;
private ResultSet re = null;
private int MeetSmallBats = 0;
private int MeetBigBats = 0;
private int CollectedGold = 0;
private int CollectedSilver = 0;
private int CollectedDiamond = 0;
public World() {
//Retreive data from database
populateArrays();
}
//Retreive data from database and store in the array
public void populateArrays()
{
nodes = new Hashtable();
edges = new Hashtable();
try
{
con = DriverManager.getConnection("jdbc:derby://localhost:1527/DBWumpus");
st = con.createStatement();
re = st.executeQuery("Select ROOM from ROOMS");
while (re.next())
{
String room = re.getString(1);
Node n = new Node(room);
nodes.put(room,n);
ArrayList <Edge> ale = new ArrayList();
edges.put(room,ale);
}
String from="";
String to="";
String line="";
Integer d=0;//="";
st = con.createStatement();
re = st.executeQuery("Select ROOM_ID,EXIT1_ROOM_ID,EXIT2_ROOM_ID,EXIT3_ROOM_ID from PATHS");
while (re.next())
{
from = re.getString(1);
for(int i=0;i<3;i++)
{
to = re.getString(i+2);
ArrayList <Edge> ale = edges.get(from);
ale.add( new Edge(from,to,"t1",1));
}
}
int index = 0;
String BatType = "";
String Bat_ROOM = "";
int BatNumber = 0;
st = con.createStatement();
re = st.executeQuery("Select TYPE,ROOM_ID,QUANTITY FROM BATS");
while (re.next())
{
BatType = re.getString(1);
Bat_ROOM = re.getString(2);
BatNumber = re.getInt(3);
b.add( new Bats(BatType,Bat_ROOM,BatNumber));
}
String TReasureType = "";
String Treasure_ROOM = "";
int TReasureNumber = 0;
st = con.createStatement();
re = st.executeQuery("Select TYPE,ROOM_ID,QUANTITY FROM TREASURES");
while (re.next())
{
TReasureType = re.getString(1);
Treasure_ROOM = re.getString(2);
TReasureNumber = re.getInt(3);
tr.add( new Treasure(TReasureType,Treasure_ROOM,TReasureNumber));
}
re.close();
st.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "An ODBC error occurs\n"+e.getMessage(),"Warning", JOptionPane.PLAIN_MESSAGE);
}
finally
{
}
}
//Initial the position of player and wumpus
public void start() {
location = nodes.get("1a");
wumpus = nodes.get("8h");
}
public Node getNode(String label) {
return nodes.get(label);
}
public boolean enter(Node n) {
ArrayList <Edge> ale = edges.get( location.label );
for (Edge e : ale) {
if ( (n.label).equals(e.to) ) {
location = n;
return true;
}
}
return false;
}
public String describe(String l) {
String s = ""; //location.describe();
//s += "\nThere are exits to:";
ArrayList <Edge> ale = edges.get(l.trim());
for (Edge e : ale)
s += e.to+",";
//s += "\nThe wumpus is "+distance(location)+" rooms away";
return s;
}
public boolean shoot( Node n ) {
if ( (n.label).equals(wumpus.label) )
return true;
EdgeStack sp = shortestPath(location,n);
//to do: traverse the path; if it passes through the wumpus room, return true
return false;
}
public String shortestPath(Node to) {
EdgeStack es = shortestPath(location,to);
String path = "";
while (es.count() > 0) {
Edge e = es.pop();
path += "\t"+e.from+" to "+e.to+" using "+e.line+"\n";
}
return path;
}
// private methods
private void create() {
}
private Node getNextNode() {
// choose the node with the shortest distance from the start node
// that hasn't been visited
Node next = null;
int d = Integer.MAX_VALUE;
Enumeration <Node> en = nodes.elements();
while ( en.hasMoreElements() ) {
Node n = en.nextElement();
if (n.visited)
continue;
if (n.distance < d) {
d = n.distance;
next = n;
}
}
//if(next == null){next = nodes.get("8h");}
return next;
}
private EdgeStack shortestPath(Node n1, Node n2) {
// To work out the shortest path from n1 to n2, we use Dijkstra's algorithm, as
// described in Weiss, M. (2007) Data Structures and Algorithm Analysis in Java,
// Pearson Education, p. 337
Enumeration <Node> en = nodes.elements();
while ( en.hasMoreElements() ) {
Node n = en.nextElement();
n.step = null;
n.distance = Integer.MAX_VALUE;
n.visited = false;
}
n1.distance = 0;
for (;;) {
Node n = getNextNode();
if (n == null)
break;
n.visited = true;
ArrayList <Edge> ale = edges.get(n.label);
for (Edge e : ale) {
//Check if each of n or e got null
if(n == null || e == null){break;}
Node t = nodes.get(e.to);
//check if t got null
if(t == null){break;}
if (!t.visited) {
if (n.distance + e.distance < t.distance) {
t.distance = n.distance + e.distance;
t.step = e;
}
}
}
}
//The path to n2 is stored in the step fields.
EdgeStack path = new EdgeStack();
Node q = n2;
while ( q.step != null ) {
path.push(q.step);
q = nodes.get(q.step.from);
}
return path;
}
private int distance(Node n1) {
EdgeStack sp = shortestPath(n1,wumpus);
return sp.count();
}
class EdgeStack {
// There is no stack class in java.util, so ...
public ArrayList <Edge> estack;
EdgeStack() {
estack = new ArrayList();
}
int count() {
return estack.size();
}
void push( Edge e) {
estack.add(0,e);
}
Edge pop() {
if ( estack.isEmpty() )
return null;
return estack.remove(0);
}
}
public String MoveAction(String roomId)
{
String result = "statu,WumpusLocation,small,big,gold,silver,dimond";
location = nodes.get(roomId.trim());
if (wumpus.equals(roomId.trim()))
{
result = "dead,"+ Integer.toString(distance(location)) +","+MeetSmallBats+","+MeetBigBats+","+CollectedGold+","+CollectedSilver+","+CollectedDiamond;
}
else
{
for (int i=0;i<b.size();i++) {
//v = "2a";
if(roomId.equals(b.get(i).room_ID.toString().trim()))
{
switch (b.get(i).type) {
case "SMALL":
MeetSmallBats++;
break;
case "BIG":
MeetBigBats++;
break;
}
}
}
for (int j=0;j<tr.size();j++)
{
if(tr.get(j).room_ID == null ? roomId.trim() == null : tr.get(j).room_ID.equals(roomId.trim()))
{
if("SILVER".equals(tr.get(j).type))
{
CollectedSilver = CollectedSilver + tr.get(j).quantity;
tr.get(j).quantity = 0;
}
switch (tr.get(j).type) {
case "DIAMOND":
CollectedDiamond = CollectedDiamond + tr.get(j).quantity;
tr.get(j).quantity = 0;
break;
case "GOLD":
CollectedGold = CollectedGold + tr.get(j).quantity;
tr.get(j).quantity = 0;
break;
}
}
}
result = "alive,"+ distance(location) +","+MeetSmallBats+","+MeetBigBats+","+CollectedGold+","+CollectedSilver+","+CollectedDiamond;
}
return result;
}
public String ShootAction(String roomId)
{
String result = "WumpusStatus";
if(wumpus.label.equals(roomId.trim()))
{
result = "1";
}
else
{
//Shortpath for wumpus
//String WumpusLocation = "10j";
wumpus = getNextNode();
if(wumpus == null) wumpus = nodes.get("8h");
//
location = nodes.get(roomId.trim());
result = Integer.toString(distance(location)) ;
}
return result;
}
public void DropAction(String roomId,String gold,String silver,String diamond)
{
if(!"0".equals(gold))
{
CollectedGold = CollectedGold - Integer.parseInt(gold);
//tr = new ArrayList();
tr.add( new Treasure("Gold",roomId,Integer.parseInt(gold)));
}
if(!"0".equals(silver))
{
CollectedSilver = CollectedSilver - Integer.parseInt(silver);
//tr = new ArrayList();
tr.add( new Treasure("Silver",roomId,Integer.parseInt(silver)));
}
if(!"0".equals(diamond))
{
CollectedDiamond = CollectedDiamond - Integer.parseInt(diamond);
//tr = new ArrayList();
tr.add( new Treasure("Diamond",roomId,Integer.parseInt(diamond)));
}
}
}