EOS 2  1.1.0
Einfache Objektbasierte Sprache
Labyrinth.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.game.tools;
2 
3 import java.util.Random;
4 
24 public class Labyrinth {
25 
29  public static final int FREE = 0;
33  public static final int WALL = 1;
37  private int width;
41  private int height;
45  private int[][] cell;
46 
53  public Labyrinth(int width, int height) {
54  this.width = width;
55  this.height = height;
56  cell = new int[width][height];
57  clear();
58  }
59 
63  public void clear() {
64  //center
65  for (int x = 0; x < width; x++) {
66  for (int y = 0; y < height; y++) {
67  cell[x][y] = FREE;
68  }
69  }
70  //border
71  for (int i = 0; i < width; i++) {
72  cell[i][0] = WALL;
73  cell[i][height - 1] = WALL;
74  }
75  for (int i = 0; i < height; i++) {
76  cell[0][i] = WALL;
77  cell[width - 1][i] = WALL;
78  }
79  }
87  public void createLabyrinth(int labNr) {
88  clear();
89  createLabyrinth(labNr, width * height * 10, 2, 1, 1, width - 2, height - 2);
90  }
100  public void createLabyrinth(int labNr, int wallNr, int raster) {
101  clear();
102  createLabyrinth(labNr, wallNr, raster, 1, 1, width - 2, height - 2);
103  }
104 
117  public void createLabyrinth(int labNr, int wallNr, int raster, int xa, int ya, int wa, int ha) {
118  createLabyrinth(new Random(labNr), wallNr, raster, xa, ya, wa, ha);
119  }
120 
132  public void createLabyrinth(Random dice, int wallNr, int raster, int xa, int ya, int wa, int ha) {
133  createLabyrinth(dice, wallNr, raster, xa, ya, wa, ha, wa / raster / 2);
134  }
135 
148  public void createLabyrinth(Random dice, int wallNr, int raster, int xa, int ya, int wa, int ha, int wallLength) {
149  final int MIN_LENGTH = 1;
150  int DIFF_LENGTH = wallLength;
151 
152  //fill up labyrinth
153  for (int m = 0; m < wallNr; m++) {
154  int x = raster * dice.nextInt((wa - 1) / raster) + xa + 1;
155  int y = raster * dice.nextInt((ha - 1) / raster) + ya + 1;
156  int direction = dice.nextInt(4);
157  int length = raster * (dice.nextInt(DIFF_LENGTH) + MIN_LENGTH) + 1;
158  for (int i = 0; i < length; i++) {
159  int xw;
160  int yw;
161  switch (direction) {
162  case 0:
163  xw = x;
164  yw = y + i;
165  break;
166  case 1:
167  xw = x + i;
168  yw = y;
169  break;
170  case 2:
171  xw = x;
172  yw = y - i;
173  break;
174  default:
175  xw = x - i;
176  yw = y;
177  break;
178  }
179  if (xa <= xw && xw < xa + wa
180  && ya <= yw && yw < ya + ha
181  && cell[xw][yw] != WALL) {
182  cell[xw][yw] = WALL;
183  } else {
184  break;
185  }
186 
187  }
188  }
189  }
190 
196  public int getWidth() {
197  return width;
198  }
199 
205  public int getHeight() {
206  return height;
207  }
208 
217  public int getCell(int x, int y) {
218  return cell[x][y];
219  }
220 
228  public void setCell(int x, int y, int value) {
229  cell[x][y] = value;
230  }
231 
237  public int[][] getLabyrinth() {
238  return cell;
239  }
240 }
Klasse für Zufallslabyrinthe.
Definition: Labyrinth.java:24
void setCell(int x, int y, int value)
Definition: Labyrinth.java:228
void createLabyrinth(int labNr, int wallNr, int raster, int xa, int ya, int wa, int ha)
Definition: Labyrinth.java:117
Labyrinth(int width, int height)
Definition: Labyrinth.java:53
static final int FREE
Leeres Feld.
Definition: Labyrinth.java:29
void createLabyrinth(int labNr, int wallNr, int raster)
Definition: Labyrinth.java:100
void createLabyrinth(Random dice, int wallNr, int raster, int xa, int ya, int wa, int ha)
Definition: Labyrinth.java:132
void createLabyrinth(Random dice, int wallNr, int raster, int xa, int ya, int wa, int ha, int wallLength)
Definition: Labyrinth.java:148
Impressum