changes
[IRC.git] / Robust / src / Benchmarks / MMG / Nor / Map.java
1 public class Map {
2     flag init;
3     flag updateGhost;
4     flag updatePac;
5     flag next;
6     flag finish;
7     
8     // maze
9     private int m_nrofblocks;
10     public int[] m_map;
11     public Node[] m_mapNodes;
12     
13     // pacmen information
14     public int m_nrofpacs;
15     public int[] m_pacMenX;
16     public int[] m_pacMenY;
17     public int[] m_directions;
18     public int[] m_desX;
19     public int[] m_desY;
20     public int m_paccount;
21     public int m_deathcount;
22     
23     // ghosts information
24     public int m_nrofghosts;
25     public int[] m_ghostsX;
26     public int[] m_ghostsY;
27     public int[] m_ghostdirections;
28     public int[] m_targets;
29     public int m_ghostcount;
30     public int m_failghostcount;
31     
32     // helper member
33     public Random m_r;
34     
35     public Map(int nrofpacs, int nrofghosts) {
36         //System.printString("step 1\n");
37         this.m_nrofblocks = 15;
38         this.m_map = new int[this.m_nrofblocks*this.m_nrofblocks];
39         this.m_mapNodes = new Node[this.m_nrofblocks*this.m_nrofblocks];
40         
41         this.m_nrofpacs = nrofpacs;
42         this.m_pacMenX = new int[this.m_nrofpacs];
43         this.m_pacMenY = new int[this.m_nrofpacs];
44         this.m_directions = new int[this.m_nrofpacs];
45         this.m_desX = new int[this.m_nrofpacs];
46         this.m_desY = new int[this.m_nrofpacs];
47         this.m_paccount = 0;
48         this.m_deathcount = 0;
49         
50         this.m_nrofghosts = nrofghosts;
51         this.m_ghostsX = new int[this.m_nrofghosts];
52         this.m_ghostsY = new int[this.m_nrofghosts];
53         this.m_ghostdirections = new int[this.m_nrofghosts];
54         this.m_targets = new int[this.m_nrofghosts];
55         this.m_ghostcount = 0;
56         this.m_failghostcount = 0;
57         
58         this.m_r = new Random();
59         
60         for(int i = 0; i < this.m_nrofblocks*this.m_nrofblocks; i++) {
61             this.m_map[i] = -1;
62             this.m_mapNodes[i] = new Node(i%this.m_nrofblocks, i/this.m_nrofblocks, i);
63         }
64         
65         //System.printString("step 2\n");
66         for(int i = 0; i < this.m_nrofpacs; i++) {
67             this.m_pacMenX[i] = this.m_pacMenY[i] = -1;
68             this.m_desX[i] = this.m_desY[i] = -1;
69         }
70         //System.printString("step 3\n");
71         for(int i = 0; i < this.m_nrofghosts; i++) {
72             this.m_ghostsX[i] = this.m_ghostsY[i] = -1;
73             this.m_targets[i] = -1;
74         }
75         //System.printString("step 4\n");
76     }
77     
78     public void init() {
79         // initilize the maze
80         int i = 0;
81         this.m_map[i++]=3;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=10;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=6;
82         this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=14;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=15;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=11;this.m_map[i++]=14;this.m_map[i++]=5;
83         this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=11;this.m_map[i++]=6;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=3;this.m_map[i++]=14;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=4;
84         this.m_map[i++]=5;this.m_map[i++]=3;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=6;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=7;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=3;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=6;this.m_map[i++]=5;
85         this.m_map[i++]=5;this.m_map[i++]=9;this.m_map[i++]=8;this.m_map[i++]=14;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=8;this.m_map[i++]=12;this.m_map[i++]=5;
86         this.m_map[i++]=9;this.m_map[i++]=2;this.m_map[i++]=10;this.m_map[i++]=2;this.m_map[i++]=8;this.m_map[i++]=2;this.m_map[i++]=12;this.m_map[i++]=5;this.m_map[i++]=9;this.m_map[i++]=2;this.m_map[i++]=8;this.m_map[i++]=2;this.m_map[i++]=10;this.m_map[i++]=2;this.m_map[i++]=12;
87         this.m_map[i++]=6;this.m_map[i++]=5;this.m_map[i++]=7;this.m_map[i++]=5;this.m_map[i++]=7;this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=8;this.m_map[i++]=14;this.m_map[i++]=5;this.m_map[i++]=7;this.m_map[i++]=5;this.m_map[i++]=7;this.m_map[i++]=5;this.m_map[i++]=3;
88         this.m_map[i++]=4;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=1;
89         this.m_map[i++]=12;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=10;this.m_map[i++]=14;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=9;
90         this.m_map[i++]=3;this.m_map[i++]=8;this.m_map[i++]=10;this.m_map[i++]=8;this.m_map[i++]=10;this.m_map[i++]=0;this.m_map[i++]=10;this.m_map[i++]=2;this.m_map[i++]=10;this.m_map[i++]=0;this.m_map[i++]=10;this.m_map[i++]=8;this.m_map[i++]=10;this.m_map[i++]=8;this.m_map[i++]=6;
91         this.m_map[i++]=5;this.m_map[i++]=3;this.m_map[i++]=2;this.m_map[i++]=2;this.m_map[i++]=6;this.m_map[i++]=5;this.m_map[i++]=15;this.m_map[i++]=5;this.m_map[i++]=15;this.m_map[i++]=5;this.m_map[i++]=3;this.m_map[i++]=2;this.m_map[i++]=2;this.m_map[i++]=6;this.m_map[i++]=5;
92         this.m_map[i++]=5;this.m_map[i++]=9;this.m_map[i++]=8;this.m_map[i++]=8;this.m_map[i++]=4;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=8;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=1;this.m_map[i++]=8;this.m_map[i++]=8;this.m_map[i++]=12;this.m_map[i++]=5;
93         this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=6;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=2;this.m_map[i++]=14;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=3;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=4;
94         this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=14;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=8;this.m_map[i++]=6;this.m_map[i++]=13;this.m_map[i++]=3;this.m_map[i++]=8;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=11;this.m_map[i++]=14;this.m_map[i++]=5;
95         this.m_map[i++]=9;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=10;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=12; // 15*15
96         
97         // initilize the graph of the maze
98         for(i = 0; i < this.m_nrofblocks*this.m_nrofblocks; i++) {
99             int tmp = this.m_map[i];
100             Node tmpNode = this.m_mapNodes[i];
101             int locX = tmpNode.getXLoc();
102             int locY = tmpNode.getYLoc();
103             if((int)(tmp & 1) == 0) {
104                 // can go left
105                 if(locX == 0) {
106                     tmpNode.addNeighbour(this.m_mapNodes[locY * this.m_nrofblocks + this.m_nrofblocks - 1]);
107                 } else {
108                     tmpNode.addNeighbour(this.m_mapNodes[i - 1]);
109                 }
110             } 
111             if((int)(tmp & 2) == 0) {
112                 // can go up
113                 if(locY == 0) {
114                     tmpNode.addNeighbour(this.m_mapNodes[(this.m_nrofblocks - 1) * this.m_nrofblocks + locX]);
115                 } else {
116                     tmpNode.addNeighbour(this.m_mapNodes[(locY - 1) * this.m_nrofblocks + locX]);
117                 }
118             }
119             if((int)(tmp & 4) == 0) {
120                 // can go right
121                 if(locX == this.m_nrofblocks - 1) {
122                     tmpNode.addNeighbour(this.m_mapNodes[locY * this.m_nrofblocks]);
123                 } else {
124                     tmpNode.addNeighbour(this.m_mapNodes[i + 1]);
125                 }
126             }
127             if((int)(tmp & 8) == 0) {
128                 // can go down
129                 if(locY == this.m_nrofblocks - 1) {
130                     tmpNode.addNeighbour(this.m_mapNodes[locX]);
131                 } else {
132                     tmpNode.addNeighbour(this.m_mapNodes[(locY + 1) * this.m_nrofblocks + locX]);
133                 }
134             }
135         }
136     } 
137
138     public void placePacman(Pacman t) {
139         this.m_pacMenX[t.m_index] = t.m_locX;
140         this.m_pacMenY[t.m_index] = t.m_locY;
141         this.m_paccount++;
142     }
143     
144     public void placeGhost(Ghost t) {
145         this.m_ghostsX[t.m_index] = t.m_locX;
146         this.m_ghostsY[t.m_index] = t.m_locY;
147         this.m_ghostcount++;
148     }
149     
150     public boolean check(Pacman t) {
151         boolean death = false;
152         int i = 0;
153         while((!death) && (i < this.m_ghostsX.length)) {
154             if((t.m_locX == this.m_ghostsX[i]) && (t.m_locY == this.m_ghostsY[i])) {
155                 death = true;
156             }
157             i++;
158         }
159         if((!death) && (t.m_locX == t.m_tx) && (t.m_locY == t.m_ty)) {
160             // reach the destination
161             //System.printString("Hit destination!\n");
162             death = true;
163         }
164         if(death) {
165             // pacman caught by ghost
166             // set pacman as death
167             t.m_death = true;
168             // kick it out
169             //this.m_map[t.y * this.m_nrofblocks + t.x - 1] -= 16;
170             this.m_deathcount++;
171             this.m_pacMenX[t.m_index] = -1;
172             this.m_pacMenY[t.m_index] = -1;
173         }
174         return death;
175     }
176     
177     public boolean isfinish() {
178         return this.m_nrofpacs == 0;
179     }
180 }