Commit 173a90e7 authored by Stefan Michel's avatar Stefan Michel

uebungsblatt2

- model und grundsaetzliche logik gebaut
parent 4b9f75d7
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="" vcs="" /> <mapping directory="$PROJECT_DIR$" vcs="Git" />
</component> </component>
</project> </project>
\ No newline at end of file
This diff is collapsed.
...@@ -3,13 +3,17 @@ package application; ...@@ -3,13 +3,17 @@ package application;
import javafx.application.Application; import javafx.application.Application;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.stage.Stage; import javafx.stage.Stage;
import model.Hamster;
import model.ModelTest; import model.ModelTest;
import model.Terrain;
import view.SceneBuilderFX; import view.SceneBuilderFX;
public class Main extends Application { public class Main extends Application {
public static void main(String[] args) {
// launch(args);
ModelTest test = new ModelTest();
test.test();
}
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
...@@ -22,11 +26,4 @@ public class Main extends Application { ...@@ -22,11 +26,4 @@ public class Main extends Application {
} }
public static void main(String[] args) {
// launch(args);
ModelTest test = new ModelTest();
test.test();
}
} }
This diff is collapsed.
package model; package model;
import model.exceptions.NoCornException;
import model.exceptions.WallException;
/** /**
* Created by Stefan Michel on 23.10.17. * Created by Stefan Michel on 23.10.17.
*/ */
...@@ -18,15 +21,59 @@ public class Hamster { ...@@ -18,15 +21,59 @@ public class Hamster {
this.corns = 0; this.corns = 0;
} }
public void setTerrain(Terrain terrain) { // public void setTerrain(Terrain terrain) {
this.terrain = terrain; // this.terrain = terrain;
//}
public void forward() throws WallException {
int nextRow = terrain.hamsterRow;
int nextCol = terrain.hamsterCol;
switch (terrain.getHamsterDirection()) {
case (Terrain.NORTH):
nextRow--;
break;
case (Terrain.EAST):
nextCol++;
break;
case (Terrain.SOUTH):
nextRow++;
break;
case (Terrain.WEST):
nextCol--;
break;
}
if (terrain.setHamsterPostion(nextRow, nextCol) == false) {
throw new WallException();
}
}
//hebt ein Korn auf und
public void pickUp() throws NoCornException {
if (terrain.foundCorn() > 0) {
corns++;
terrain.map[terrain.hamsterRow][terrain.hamsterCol]--;
} else throw new NoCornException();
}
//legt ein Korn ab
public void putDown() {
if (corns > 0) {
corns--;
terrain.map[terrain.hamsterRow][terrain.hamsterCol]++;
} else throw new NoCornException();
} }
public void turnLeft() { public void turnLeft() {
if (terrain.hamsterDirection != 0) { if (terrain.hamsterDirection != Terrain.NORTH) {
terrain.hamsterDirection = terrain.hamsterDirection - 1; terrain.hamsterDirection = terrain.hamsterDirection - 1;
} else { } else {
terrain.hamsterDirection = 3; terrain.hamsterDirection = Terrain.WEST;
} }
} }
public int getCorns() {
return this.corns;
}
} }
package model; package model;
import io.IO;
import model.exceptions.BorderException;
import model.exceptions.NoCornException;
import model.exceptions.WallException;
/** /**
* Created by Stefan Michel on 23.10.17. * Created by Stefan Michel on 23.10.17.
* <p>
* Ich habe einfach deine IO Klasse genommen Dibo.
*/ */
public class ModelTest { public class ModelTest {
public void test() { Terrain terrain;
Terrain terrain = new Terrain(); Hamster hamster;
Hamster hamster = new Hamster(terrain);
public ModelTest() {
terrain = new Terrain();
hamster = new Hamster(terrain);
terrain.setHamster(hamster); terrain.setHamster(hamster);
}
public void test() {
// hier wird der map zum testen elemente eingefügt
terrain.map[0][1] = 2;
terrain.map[1][1] = -1;
for (int i = 0; i < terrain.maxRows; i++) { while (true) {
for (int j = 0; j < terrain.maxCols; j++) { drawBoard();
System.out.print(terrain.map[i][j]); System.out.println();
readCommand(IO.readChar("Weitere Befehle (l, v, k, h, p): "));
}
}
void drawBoard() {
for (int i = 0; i < terrain.maxCols; i++) {
for (int j = 0; j < terrain.maxRows; j++) {
if (i == terrain.hamsterRow && j == terrain.hamsterCol) {
switch (terrain.getHamsterDirection()) {
case (Terrain.NORTH):
System.out.print("^");
break;
case (Terrain.EAST):
System.out.print(">");
break;
case (Terrain.SOUTH):
System.out.print("v");
break;
case (Terrain.WEST):
System.out.print("<");
break;
}
} else
System.out.print(terrain.map[i][j]);
} }
System.out.println(); System.out.println();
} }
} }
void readCommand(char command) {
switch (command) {
case ('l'):
hamster.turnLeft();
break;
case ('v'):
try {
hamster.forward();
} catch (WallException e) {
System.out.println("Hier ist eine Wand!");
} catch (BorderException e) {
}
break;
case ('k'):
System.out.println("Koerner im Tile: " + terrain.foundCorn());
readCommand(IO.readChar("Weitere Befehle (l, v, k, h, p): "));
break;
case ('h'):
try {
hamster.pickUp();
} catch (NoCornException e) {
System.out.println("Hier liegt kein Korn!");
} finally {
System.out.println("Koerner im Maul: " + hamster.getCorns());
}
break;
case ('p'):
try {
hamster.putDown();
} catch (NoCornException e) {
System.out.println("Keine Körner im Maul!");
}
System.out.println("Koerner im Maul: " + hamster.getCorns());
break;
}
}
} }
package model; package model;
import model.exceptions.BorderException;
/** /**
* Created by Stefan Michel on 23.10.17. * Created by Stefan Michel on 23.10.17.
*/ */
public class Terrain { public class Terrain {
Hamster hamster; public final static int NORTH = 0;
public final static int EAST = 1;
final static int NORTH = 0; public final static int SOUTH = 2;
final static int EAST = 1; public final static int WEST = 3;
final static int SOUTH = 2;
final static int WEST = 3;
final static int WALL = -1; final static int WALL = -1;
final static int MAXCORNS = 9;
final static int MINTILES = 9;
Hamster hamster;
int maxRows = 10; int maxRows = 10;
int maxCols = 10; int maxCols = 10;
int[][] map; int hamsterRow = 0;
int hamsterCol = 0;
int hamsterRow; int hamsterDirection = SOUTH;
int hamsterCol;
int hamsterDirection = NORTH;
int[][] map;
public Terrain() { public Terrain() {
map = new int[maxRows][maxCols]; map = new int[maxRows][maxCols];
for (int i=0; i < maxRows; i++) { for (int i = 0; i < maxRows; i++) {
for (int j=0; j < maxCols; j++) { for (int j = 0; j < maxCols; j++) {
map[i][j] = 0; map[i][j] = 0;
} }
} }
setHamsterPostion(hamsterRow, hamsterCol);
} }
public Terrain(Hamster hamster) { // public Terrain(Hamster hamster) {
map = new int[maxRows][maxCols]; // map = new int[maxRows][maxCols];
for (int i=0; i < maxRows; i++) { // for (int i=0; i < maxRows; i++) {
for (int j=0; j < maxCols; j++) { // for (int j=0; j < maxCols; j++) {
map[i][j] = 0; // map[i][j] = 0;
} // }
} // }
//
// this.hamster = hamster;
// }
public void setHamster(Hamster hamster) {
this.hamster = hamster; this.hamster = hamster;
} }
public void setHamster(Hamster hamster) { public boolean setHamsterPostion(int row, int col) throws BorderException {
this.hamster = hamster; if (col < 0 || row < 0 || col > maxCols || row > maxRows) {
throw new BorderException();
} else {
if (!checkWall(row, col)) {
this.hamsterRow = row;
this.hamsterCol = col;
return true;
} else
return false;
}
} }
public int getHamsterDirection() { public int getHamsterDirection() {
...@@ -56,10 +69,74 @@ public class Terrain { ...@@ -56,10 +69,74 @@ public class Terrain {
} }
public void setHamsterDirection(int direction) throws IllegalArgumentException { public void setHamsterDirection(int direction) throws IllegalArgumentException {
if (direction>=0 && direction <= 4) { if (direction >= NORTH && direction <= WEST) {
this.hamsterDirection = direction; this.hamsterDirection = direction;
} else { } else {
throw new IllegalArgumentException("ungültige Richtung"); throw new IllegalArgumentException("ungültige Richtung");
} }
} }
//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
public boolean deleteTile(int col, int row) {
if (col > 0 || row > 0 || col > maxCols || row > maxRows) {
this.map[row][col] = 0;
return true;
} else
return false;
}
// fuegt dem Feld ein korn hinzu
public boolean addKorn(int col, int row) {
if (this.map[row][col] < MAXCORNS) {
this.map[row][col]++;
return true;
} else if (this.map[row][col] >= MAXCORNS) {
return false;
} else {
//hier steht eine Wand, exception werfen
throw new RuntimeException("Auf dieser Position befindet sich eine Wand");
}
}
// veraendert die anzahl der zeilen und prueft ob der hamster
// sich im verkleinerten bereich befindet
public void setMaxRows(int maxRows) {
if (maxRows > MINTILES) {
if (maxRows < this.hamsterRow) {
if (this.map[0][0] < 0) {
this.map[0][0] = 0;
}
this.hamsterRow = 0;
this.hamsterCol = 0;
}
this.maxRows = maxRows;
}
}
// analog zu setMaxRows
public void setMaxCols(int maxCols) {
if (maxCols > MINTILES) {
if (maxCols < this.hamsterCol) {
if (this.map[0][0] < 0) {
this.map[0][0] = 0;
}
this.hamsterRow = 0;
this.hamsterCol = 0;
}
this.maxCols = maxCols;
}
}
//prueft, ob sich an dieser position eine wand befindet
public boolean checkWall(int row, int col) {
return this.map[row][col] == -1;
}
} }
package model.exceptions;
/**
* Created by Stefan Michel on 25.10.17.
*/
public class BorderException extends RuntimeException {
String message;
public BorderException() {
message = "Ausserhalb des Bereichs";
}
}
package model.exceptions;
/**
* Created by Stefan Michel on 25.10.17.
*/
public class NoCornException extends RuntimeException {
public NoCornException() {
}
}
package model.exceptions;
/**
* Created by Stefan Michel on 25.10.17.
*/
public class WallException extends RuntimeException {
}
package view; package view;
import javafx.scene.Parent;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
/** /**
......
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