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"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
This diff is collapsed.
......@@ -3,13 +3,17 @@ package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import model.Hamster;
import model.ModelTest;
import model.Terrain;
import view.SceneBuilderFX;
public class Main extends Application {
public static void main(String[] args) {
// launch(args);
ModelTest test = new ModelTest();
test.test();
}
@Override
public void start(Stage primaryStage) throws Exception {
......@@ -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;
import model.exceptions.NoCornException;
import model.exceptions.WallException;
/**
* Created by Stefan Michel on 23.10.17.
*/
......@@ -18,15 +21,59 @@ public class Hamster {
this.corns = 0;
}
public void setTerrain(Terrain terrain) {
this.terrain = terrain;
// public void setTerrain(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() {
if (terrain.hamsterDirection != 0) {
if (terrain.hamsterDirection != Terrain.NORTH) {
terrain.hamsterDirection = terrain.hamsterDirection - 1;
} else {
terrain.hamsterDirection = 3;
terrain.hamsterDirection = Terrain.WEST;
}
}
public int getCorns() {
return this.corns;
}
}
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.
* <p>
* Ich habe einfach deine IO Klasse genommen Dibo.
*/
public class ModelTest {
public void test() {
Terrain terrain = new Terrain();
Hamster hamster = new Hamster(terrain);
Terrain terrain;
Hamster hamster;
public ModelTest() {
terrain = new Terrain();
hamster = new Hamster(terrain);
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;
while (true) {
drawBoard();
System.out.println();
readCommand(IO.readChar("Weitere Befehle (l, v, k, h, p): "));
}
}
for (int i = 0; i < terrain.maxRows; i++) {
for (int j = 0; j < terrain.maxCols; j++) {
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();
}
}
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;
import model.exceptions.BorderException;
/**
* Created by Stefan Michel on 23.10.17.
*/
public class Terrain {
Hamster hamster;
final static int NORTH = 0;
final static int EAST = 1;
final static int SOUTH = 2;
final static int WEST = 3;
public final static int NORTH = 0;
public final static int EAST = 1;
public final static int SOUTH = 2;
public final static int WEST = 3;
final static int WALL = -1;
final static int MAXCORNS = 9;
final static int MINTILES = 9;
Hamster hamster;
int maxRows = 10;
int maxCols = 10;
int[][] map;
int hamsterRow;
int hamsterCol;
int hamsterDirection = NORTH;
int hamsterRow = 0;
int hamsterCol = 0;
int hamsterDirection = SOUTH;
int[][] map;
public Terrain() {
map = new int[maxRows][maxCols];
for (int i=0; i < maxRows; i++) {
for (int j=0; j < maxCols; j++) {
for (int i = 0; i < maxRows; i++) {
for (int j = 0; j < maxCols; j++) {
map[i][j] = 0;
}
}
setHamsterPostion(hamsterRow, hamsterCol);
}
public Terrain(Hamster hamster) {
map = new int[maxRows][maxCols];
for (int i=0; i < maxRows; i++) {
for (int j=0; j < maxCols; j++) {
map[i][j] = 0;
}
}
// public Terrain(Hamster hamster) {
// map = new int[maxRows][maxCols];
// for (int i=0; i < maxRows; i++) {
// for (int j=0; j < maxCols; j++) {
// map[i][j] = 0;
// }
// }
//
// this.hamster = hamster;
// }
public void setHamster(Hamster hamster) {
this.hamster = hamster;
}
public void setHamster(Hamster hamster) {
this.hamster = hamster;
public boolean setHamsterPostion(int row, int col) throws BorderException {
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() {
......@@ -56,10 +69,74 @@ public class Terrain {
}
public void setHamsterDirection(int direction) throws IllegalArgumentException {
if (direction>=0 && direction <= 4) {
if (direction >= NORTH && direction <= WEST) {
this.hamsterDirection = direction;
} else {
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;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
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