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 { ...@@ -35,29 +35,24 @@ public class Terrain extends Observable {
public Terrain() { public Terrain() {
map = new int[maxRows][maxCols]; map = new int[maxCols][maxRows];
for (int i = 0; i < maxRows; i++) { for (int i = 0; i < maxCols; i++) {
for (int j = 0; j < maxCols; j++) { for (int j = 0; j < maxRows; j++) {
map[i][j] = 0; map[i][j] = 0;
} }
} }
setHamsterPostion(hamsterRow, hamsterCol); setHamsterPostion(hamsterCol, hamsterRow);
hamster = new Hamster(this); hamster = new Hamster(this);
} }
public void setHamster(Hamster hamster) { public boolean setHamsterPostion(int col, int row) throws BorderException {
this.hamster = hamster;
hamster.setTerrain(this);
}
public boolean setHamsterPostion(int row, int col) throws BorderException {
if (col < 0 || row < 0 || col > maxCols || row > maxRows) { if (col < 0 || row < 0 || col > maxCols || row > maxRows) {
throw new BorderException(); throw new BorderException();
} else { } else {
if (!checkWall(row, col)) { if (!checkWall(col, row)) {
this.hamsterRow = row;
this.hamsterCol = col; this.hamsterCol = col;
this.hamsterRow = row;
setChanged(); setChanged();
notifyObservers(this); notifyObservers(this);
return true; return true;
...@@ -66,28 +61,25 @@ public class Terrain extends Observable { ...@@ -66,28 +61,25 @@ public class Terrain extends Observable {
} }
} }
public int getHamsterDirection() { //gibt die anzahl an koernern zurueck die auf dem gleichen feld liegen wie der Hamster
return this.hamsterDirection; public int foundCorn() {
if (map[hamsterCol][hamsterRow] > 0) {
return map[hamsterCol][hamsterRow];
} else
return 0;
} }
public void setHamsterDirection(int direction) throws IllegalArgumentException { // fuegt dem Feld ein korn hinzu
if (direction >= NORTH && direction <= WEST) { public void addCorn(int col, int row) {
this.hamsterDirection = direction; if (this.map[col][row] < MAXCORNS && this.map[col][row] >= 0) {
this.map[col][row]++;
setChanged(); setChanged();
notifyObservers(this); notifyObservers(this);
} else { } 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 // entfernt ein Feld
public void deleteTile(int row, int col) { public void deleteTile(int row, int col) {
if (row > 0 || col > 0 || row > maxCols || col > maxRows) { if (row > 0 || col > 0 || row > maxCols || col > maxRows) {
...@@ -97,23 +89,21 @@ public class Terrain extends Observable { ...@@ -97,23 +89,21 @@ public class Terrain extends Observable {
} }
} }
// fuegt dem Feld ein korn hinzu public void addWall(int col, int row) {
public void addCorn(int row, int col) { if (!(col == hamsterCol && row == hamsterRow)) {
if (this.map[col][row] < MAXCORNS && this.map[col][row] >= 0) { this.map[col][row] = WALL;
this.map[col][row]++;
setChanged(); setChanged();
notifyObservers(this); notifyObservers(this);
} else {
throw new WallException("Auf dieser Position befindet sich eine Wand");
} }
} }
public void addWall(int row, int col) { public void putDown() {
if (!(row == hamsterCol && col == hamsterRow)) { if (this.hamsterCorns > 0) {
this.map[col][row] = WALL; this.hamsterCorns--;
setChanged(); this.map[this.hamsterRow][this.hamsterCol]++;
notifyObservers(this); } else throw new NoCornException();
} setChanged();
notifyObservers();
} }
// veraendert die anzahl der zeilen und prueft ob der hamster // veraendert die anzahl der zeilen und prueft ob der hamster
...@@ -189,21 +179,27 @@ public class Terrain extends Observable { ...@@ -189,21 +179,27 @@ public class Terrain extends Observable {
notifyObservers(); notifyObservers();
} }
public void putDown() {
if (this.hamsterCorns > 0) {
this.hamsterCorns--;
this.map[this.hamsterRow][this.hamsterCol]++;
} else throw new NoCornException();
}
public void pickUp() { public void pickUp() {
if (this.foundCorn() > 0) { if (this.foundCorn() > 0) {
this.hamsterCorns++; this.hamsterCorns++;
this.map[this.hamsterRow][this.hamsterCol]--; this.map[this.hamsterRow][this.hamsterCol]--;
} else throw new NoCornException(); } 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() { public int getMaxRows() {
return maxRows; return maxRows;
...@@ -213,11 +209,6 @@ public class Terrain extends Observable { ...@@ -213,11 +209,6 @@ public class Terrain extends Observable {
return maxCols; 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() { public int getHamsterCorns() {
return this.hamsterCorns; return this.hamsterCorns;
} }
...@@ -230,8 +221,8 @@ public class Terrain extends Observable { ...@@ -230,8 +221,8 @@ public class Terrain extends Observable {
return hamsterCol; return hamsterCol;
} }
public int getCornAmount(int row, int col) { public int getHamsterDirection() {
return this.map[row][col]; return this.hamsterDirection;
} }
public String getName() { public String getName() {
...@@ -246,5 +237,20 @@ public class Terrain extends Observable { ...@@ -246,5 +237,20 @@ public class Terrain extends Observable {
return hamster; 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 { ...@@ -48,8 +48,8 @@ public class TerritoriumPanelFX extends Region implements Observer {
private void drawField() { private void drawField() {
//Feld zeichnen //Feld zeichnen
for (int j = 0; j < terrain.getMaxRows(); j++) { for (int j = 0; j < terrain.getMaxCols(); j++) {
for (int i = 0; i < terrain.getMaxCols(); i++) { for (int i = 0; i < terrain.getMaxRows(); i++) {
Tile tile = new Tile(i, j); Tile tile = new Tile(i, j);
// koerner setzen // koerner setzen
...@@ -63,7 +63,7 @@ public class TerritoriumPanelFX extends Region implements Observer { ...@@ -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); 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