Commit e179b2f8 authored by Stefan Michel's avatar Stefan Michel

Model (Terrain) leicht veraendert, da methoden nicht einheitliche struktur hatten

parent 223ab26a
......@@ -35,29 +35,24 @@ public class Terrain extends Observable {
public Terrain() {
map = new int[maxRows][maxCols];
for (int i = 0; i < maxRows; i++) {
for (int j = 0; j < maxCols; j++) {
map = new int[maxCols][maxRows];
for (int i = 0; i < maxCols; i++) {
for (int j = 0; j < maxRows; j++) {
map[i][j] = 0;
}
}
setHamsterPostion(hamsterRow, hamsterCol);
setHamsterPostion(hamsterCol, hamsterRow);
hamster = new Hamster(this);
}
public void setHamster(Hamster hamster) {
this.hamster = hamster;
hamster.setTerrain(this);
}
public boolean setHamsterPostion(int row, int col) throws BorderException {
public boolean setHamsterPostion(int col, int row) throws BorderException {
if (col < 0 || row < 0 || col > maxCols || row > maxRows) {
throw new BorderException();
} else {
if (!checkWall(row, col)) {
this.hamsterRow = row;
if (!checkWall(col, row)) {
this.hamsterCol = col;
this.hamsterRow = row;
setChanged();
notifyObservers(this);
return true;
......@@ -66,26 +61,23 @@ public class Terrain extends Observable {
}
}
public int getHamsterDirection() {
return this.hamsterDirection;
//gibt die anzahl an koernern zurueck die auf dem gleichen feld liegen wie der Hamster
public int foundCorn() {
if (map[hamsterCol][hamsterRow] > 0) {
return map[hamsterCol][hamsterRow];
} else
return 0;
}
public void setHamsterDirection(int direction) throws IllegalArgumentException {
if (direction >= NORTH && direction <= WEST) {
this.hamsterDirection = direction;
// fuegt dem Feld ein korn hinzu
public void addCorn(int col, int row) {
if (this.map[col][row] < MAXCORNS && this.map[col][row] >= 0) {
this.map[col][row]++;
setChanged();
notifyObservers(this);
} else {
throw new IllegalArgumentException("ungültige Richtung");
}
throw new WallException("Auf dieser Position befindet sich eine Wand");
}
//gibt die anzahl an koernern zurueck die auf dem gleichen feld liegen wie der Hamster
public int foundCorn() {
if (map[hamsterRow][hamsterCol] > 0) {
return map[hamsterRow][hamsterCol];
} else
return 0;
}
// entfernt ein Feld
......@@ -97,23 +89,21 @@ public class Terrain extends Observable {
}
}
// fuegt dem Feld ein korn hinzu
public void addCorn(int row, int col) {
if (this.map[col][row] < MAXCORNS && this.map[col][row] >= 0) {
this.map[col][row]++;
public void addWall(int col, int row) {
if (!(col == hamsterCol && row == hamsterRow)) {
this.map[col][row] = WALL;
setChanged();
notifyObservers(this);
} else {
throw new WallException("Auf dieser Position befindet sich eine Wand");
}
}
public void addWall(int row, int col) {
if (!(row == hamsterCol && col == hamsterRow)) {
this.map[col][row] = WALL;
public void putDown() {
if (this.hamsterCorns > 0) {
this.hamsterCorns--;
this.map[this.hamsterRow][this.hamsterCol]++;
} else throw new NoCornException();
setChanged();
notifyObservers(this);
}
notifyObservers();
}
// veraendert die anzahl der zeilen und prueft ob der hamster
......@@ -189,21 +179,27 @@ public class Terrain extends Observable {
notifyObservers();
}
public void putDown() {
if (this.hamsterCorns > 0) {
this.hamsterCorns--;
this.map[this.hamsterRow][this.hamsterCol]++;
} else throw new NoCornException();
}
public void pickUp() {
if (this.foundCorn() > 0) {
this.hamsterCorns++;
this.map[this.hamsterRow][this.hamsterCol]--;
} else throw new NoCornException();
setChanged();
notifyObservers();
}
//prueft, ob sich an dieser position eine wand befindet
public boolean checkWall(int col, int row) {
return this.map[col][row] == WALL;
}
public int getCornAmount(int col, int row) {
return this.map[col][row];
}
// ----------------------
// TRIVIAL GETTER SETTER
// ----------------------
public int getMaxRows() {
return maxRows;
......@@ -213,11 +209,6 @@ public class Terrain extends Observable {
return maxCols;
}
//prueft, ob sich an dieser position eine wand befindet
public boolean checkWall(int row, int col) {
return this.map[row][col] == WALL;
}
public int getHamsterCorns() {
return this.hamsterCorns;
}
......@@ -230,8 +221,8 @@ public class Terrain extends Observable {
return hamsterCol;
}
public int getCornAmount(int row, int col) {
return this.map[row][col];
public int getHamsterDirection() {
return this.hamsterDirection;
}
public String getName() {
......@@ -246,5 +237,20 @@ public class Terrain extends Observable {
return hamster;
}
// TODO: evtl loeschen
public void setHamsterDirection(int direction) throws IllegalArgumentException {
if (direction >= NORTH && direction <= WEST) {
this.hamsterDirection = direction;
setChanged();
notifyObservers(this);
} else {
throw new IllegalArgumentException("ungültige Richtung");
}
}
public void setHamster(Hamster hamster) {
this.hamster = hamster;
hamster.setTerrain(this);
}
}
......@@ -48,8 +48,8 @@ public class TerritoriumPanelFX extends Region implements Observer {
private void drawField() {
//Feld zeichnen
for (int j = 0; j < terrain.getMaxRows(); j++) {
for (int i = 0; i < terrain.getMaxCols(); i++) {
for (int j = 0; j < terrain.getMaxCols(); j++) {
for (int i = 0; i < terrain.getMaxRows(); i++) {
Tile tile = new Tile(i, j);
// koerner setzen
......@@ -63,7 +63,7 @@ public class TerritoriumPanelFX extends Region implements Observer {
}
if (terrain.getHamsterRow() == i && terrain.getHamsterCol() == j)
if (terrain.getHamsterCol() == i && terrain.getHamsterRow() == j)
drawHamster(tile);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment