AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
RetroMaze.java
Go to the documentation of this file.
1package aud.example.graph;
2
3import java.io.File;
4import java.util.Random;
5import java.util.Scanner;
6
7import aud.HashMap;
8
10public class RetroMaze {
11 MyGraph g_ = null;
12 MyNode room_ = null;
13 MyNode entry_ = null;
14 MyNode exit_ = null;
15 boolean hints_ = false;
16 Scanner input = new Scanner(System.in);
17 int count_ = 0;
18
19 public RetroMaze(MyGraph g,MyNode entry,MyNode exit) {
20 g_=g;
21 entry_=room_=entry;
22 exit_=exit;
23 }
24
25 void start() {
26 System.out.println
27 ("You find youself in a maze, and you have to find the exit.\n"+
28 "You start in room "+entry_.getLabel()+
29 " and the exit is in room "+exit_.getLabel()+".\n"+
30 "Good luck!\n");
31
32 while (room_!=exit_) {
33 go();
34 }
35 System.out.println("Congratulations you found the exit!");
36 }
37
38 void go() {
39 System.out.println
40 ("You are in room "+room_.getLabel()+
41 ((hints_ && room_.ord>=0) ? " (been there)" : "")+".\n"+
42 "There are corridors leading to the following rooms:\n");
43 int i=1;
45 for (MyEdge e : g_.getOutEdges(room_)) {
46 MyNode node=(MyNode) e.destination();
47 if (node==room_)
48 node=(MyNode) e.source(); // undirected graph
49
50
51 options.insert(i,node);
52 System.out.println
53 ("["+i+"] "+node.getLabel()+
54 ((node==exit_ ? " with big sign saying \"EXIT\"" : ""))+
55 ((hints_ && node.ord>=0) ? "(been there)" : ""));
56 ++i;
57 }
58 MyNode next = null;
59
60 while (next==null) {
61 System.out.print("Which way to go now? ('0' toggles hints)\n> ");
62 i=input.nextInt();
63 if (i==0) {
64 hints_=!hints_;
65 go();
66 return;
67 }
68 if (i==-1) {
69 System.out.println("Give up?\n");
70 System.exit(-1);
71 }
72 next=options.find(i);
73 }
74
75 room_=next;
76 room_.ord=++count_; // been there
77 }
78
79 static MyNode pickNode(MyGraph g) {
80 while (true) { // graph must have more than 2 nodes
81 int i=(new Random()).nextInt()%g.getNumNodes();
82 for (MyNode node : g) {
83 if (i--==0)
84 return node;
85 }
86 }
87 }
88
89 public static void main(String[] args) {
90 MyGraph g=new MyGraph(false); // easy mode: undirected graph
91
92 if (args.length==0)
93 g=new GraphP88();
94 else {
95 new GraphParser(g.getAbstractGraph()).parse(new File(args[0]));
96 }
97
98 MyNode entry=pickNode(g), exit=null;
99 while ((exit=pickNode(g))==entry) {}
100
101 RetroMaze maze=new RetroMaze(g,entry,exit);
102
103 maze.start();
104 }
105}
Implementation of an unordered map based on a hash table.
Definition: HashMap.java:31
Value insert(Key key, Value value)
insert key-value pair
Definition: HashMap.java:260
Value find(Key key)
find value for key @endiliteral
Definition: HashMap.java:281
undirected (weighted or unweighted )example graph (Sedgewick, Algorithms in Java.
Definition: GraphP88.java:7
Parse text to build graph.
void parse(String input)
parse input
graph based on aud.graph.GraphAM
Definition: MyGraph.java:11
AbstractGraph< AbstractNode, AbstractEdge > getAbstractGraph()
view this graph as an AbstractGraph
Definition: MyGraph.java:29
node with all possible attributes that we require ;-)
Definition: MyNode.java:6
int ord
time when node is (first marked/put into front)
Definition: MyNode.java:13
a really simple game, which lets you explore a maze... pardon: a graph
Definition: RetroMaze.java:10
static void main(String[] args)
Definition: RetroMaze.java:89
RetroMaze(MyGraph g, MyNode entry, MyNode exit)
Definition: RetroMaze.java:19
Vector< Edge > getOutEdges(Node node)
Definition: GraphAM.java:98
String getLabel()
get text description
Definition: SimpleNode.java:12
AuD lecture: Data structures, algorithms, examples.
Definition: A234Tree.java:1