Initial Commit for Version 1.0
This commit is contained in:
parent
8fdfa8a7e6
commit
02708b44c9
BIN
Field.class
Normal file
BIN
Field.class
Normal file
Binary file not shown.
11
Field.ham
Normal file
11
Field.ham
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
/*class*/// Dient zum Halten von Informationen über Spielfelder.
|
||||||
|
public class Field{
|
||||||
|
public int column;
|
||||||
|
public int row;
|
||||||
|
public Field parent;
|
||||||
|
|
||||||
|
public Field(int column, int row){
|
||||||
|
this.row = row;
|
||||||
|
this.column = column;
|
||||||
|
}
|
||||||
|
}
|
10
Field.java
Normal file
10
Field.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import de.hamster.debugger.model.Territorium;import de.hamster.debugger.model.Territory;import de.hamster.model.HamsterException;import de.hamster.model.HamsterInitialisierungsException;import de.hamster.model.HamsterNichtInitialisiertException;import de.hamster.model.KachelLeerException;import de.hamster.model.MauerDaException;import de.hamster.model.MaulLeerException;import de.hamster.model.MouthEmptyException;import de.hamster.model.WallInFrontException;import de.hamster.model.TileEmptyException;import de.hamster.debugger.model.Hamster;public class Field{
|
||||||
|
public int column;
|
||||||
|
public int row;
|
||||||
|
public Field parent;
|
||||||
|
|
||||||
|
public Field(int column, int row){
|
||||||
|
this.row = row;
|
||||||
|
this.column = column;
|
||||||
|
}
|
||||||
|
}
|
BIN
FindGrain$Field.class
Normal file
BIN
FindGrain$Field.class
Normal file
Binary file not shown.
BIN
FindGrain$SearchGrain.class
Normal file
BIN
FindGrain$SearchGrain.class
Normal file
Binary file not shown.
BIN
FindGrain.class
Normal file
BIN
FindGrain.class
Normal file
Binary file not shown.
22
FindGrain.ham
Normal file
22
FindGrain.ham
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/*object-oriented program*/import java.util.ArrayList;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
Hamster h = Hamster.getStandardHamster();
|
||||||
|
Search srch = new Search(h);
|
||||||
|
|
||||||
|
// Suche Initialisieren
|
||||||
|
if(srch.init()){
|
||||||
|
// Pfad suchen und entsprechend des Ergebnis eine Info ausgeben
|
||||||
|
Field f = srch.findPath();
|
||||||
|
if(f!=null){
|
||||||
|
if(srch.walkPathGetKorn(f))
|
||||||
|
h.schreib("Hamster hat Korn gefunden und aufgenommen");
|
||||||
|
else
|
||||||
|
h.schreib("Fehler: Hamster konnte Korn nicht aufnehmen!");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
h.schreib("Fehler: Hamster kann Korn nicht erreichen!");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
h.schreib("Fehler: Initialisierung Fehlgeschlagen!\n(Hinweis: ggf. befindet sich kein oder meherer Körner auf dem Spielfeld.)");
|
||||||
|
}
|
22
FindGrain.java
Normal file
22
FindGrain.java
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import de.hamster.debugger.model.Territorium;import de.hamster.debugger.model.Territory;import de.hamster.model.HamsterException;import de.hamster.model.HamsterInitialisierungsException;import de.hamster.model.HamsterNichtInitialisiertException;import de.hamster.model.KachelLeerException;import de.hamster.model.MauerDaException;import de.hamster.model.MaulLeerException;import de.hamster.model.MouthEmptyException;import de.hamster.model.WallInFrontException;import de.hamster.model.TileEmptyException;import de.hamster.debugger.model.Hamster;public class FindGrain implements de.hamster.model.HamsterProgram {public void main() {
|
||||||
|
Hamster h = Hamster.getStandardHamster();
|
||||||
|
Search srch = new Search(h);
|
||||||
|
|
||||||
|
// Suche Initialisieren
|
||||||
|
if(srch.init()){
|
||||||
|
// Pfad suchen und entsprechend des Ergebnis eine Info ausgeben
|
||||||
|
Field f = srch.findPath();
|
||||||
|
if(f!=null){
|
||||||
|
if(srch.walkPathGetKorn(f))
|
||||||
|
h.schreib("Hamster hat Korn gefunden und aufgenommen");
|
||||||
|
else
|
||||||
|
h.schreib("Fehler: Hamster konnte Korn nicht aufnehmen!");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
h.schreib("Fehler: Hamster kann Korn nicht erreichen!");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
h.schreib("Fehler: Initialisierung Fehlgeschlagen!\n(Hinweis: ggf. befindet sich kein oder meherer Körner auf dem Spielfeld.)");
|
||||||
|
}}
|
15
FindGrain.ter
Normal file
15
FindGrain.ter
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
10
|
||||||
|
10
|
||||||
|
> #
|
||||||
|
# ######
|
||||||
|
#
|
||||||
|
# #### #
|
||||||
|
## # #
|
||||||
|
# ## #
|
||||||
|
#### #
|
||||||
|
## # #
|
||||||
|
## # #
|
||||||
|
###### #*
|
||||||
|
0
|
||||||
|
1
|
||||||
|
0
|
BIN
Search.class
Normal file
BIN
Search.class
Normal file
Binary file not shown.
206
Search.ham
Normal file
206
Search.ham
Normal file
@ -0,0 +1,206 @@
|
|||||||
|
/*class*/import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class Search{
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Eigenschaften / Protperties
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
private Hamster sh;
|
||||||
|
// Spielfeldgröße
|
||||||
|
private int fieldRow = Territorium.getAnzahlReihen();
|
||||||
|
private int fieldCol = Territorium.getAnzahlSpalten();
|
||||||
|
// Start- und Ziel-Feld
|
||||||
|
private Field goal;
|
||||||
|
private Field start;
|
||||||
|
// Arrays zum halten von Informationen.
|
||||||
|
private ArrayList<Field> queue = new ArrayList<Field>();
|
||||||
|
private ArrayList<Field> visited = new ArrayList<Field>();
|
||||||
|
boolean bInVisited = false;
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Public Methoden und Konstruktoren
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// Konstruktor -> zu bewegenden Hamster erforderlich
|
||||||
|
public Search(Hamster h){
|
||||||
|
sh = h;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialisiern und ermitteln der Start- und Zielposition
|
||||||
|
public boolean init(){
|
||||||
|
// Startfeld kann immer gefunden werden
|
||||||
|
getStart();
|
||||||
|
// Wenn kein oder mehrere Körner gefunden wurden, sollte ein Fehler ausgegeben werden;
|
||||||
|
return getGrainField();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pfad ermitteln
|
||||||
|
public Field findPath(){
|
||||||
|
return searchPathToGrain();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pfad laufen und Korn aufnehmen
|
||||||
|
public boolean walkPathGetKorn(Field goalField){
|
||||||
|
walkByPath(getPath(goalField));
|
||||||
|
|
||||||
|
if(sh.kornDa()){
|
||||||
|
sh.nimm();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Ermitteln des Weges mit Breitensuche
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
private Field searchPathToGrain(){
|
||||||
|
// ersten Knoten/Hamster in queue stellen
|
||||||
|
queue.add(start);
|
||||||
|
// Solange queue nicht leer, Schleife durchlaufen
|
||||||
|
while(queue.size() > 0){
|
||||||
|
// Aktuelles Feld nehmen und aus queue entfernen
|
||||||
|
Field currField = queue.get(0);
|
||||||
|
queue.remove(0);
|
||||||
|
// Feld auf Liste der besuchten Felder setzten
|
||||||
|
visited.add(currField);
|
||||||
|
|
||||||
|
// Wenn Ziel gefunden Fled zurückgeben
|
||||||
|
if(currField.column == goal.column && currField.row == goal.row){
|
||||||
|
return currField;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Freie Nachbarfelder ermitteln.
|
||||||
|
ArrayList<Field> freeNeighbours = getFreeNeighbours(currField);
|
||||||
|
for(int i = 0; i<freeNeighbours.size(); i++){
|
||||||
|
|
||||||
|
// prüfen ob Nachbarfeld nicht schon besucht wurde...
|
||||||
|
bInVisited = false;
|
||||||
|
for(Field visitedField : visited){
|
||||||
|
if(visitedField.column == freeNeighbours.get(i).column && visitedField.row == freeNeighbours.get(i).row){
|
||||||
|
bInVisited = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ...wenn nicht bearbeiten
|
||||||
|
if(!bInVisited){
|
||||||
|
// Nachbar an queue hinzufügen
|
||||||
|
queue.add(freeNeighbours.get(i));
|
||||||
|
freeNeighbours.get(i).parent = currField;
|
||||||
|
visited.add(freeNeighbours.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Wenn Ziel nich erreicht wurde...
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Bewegung des Hamsters anhand eines Pfades.
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
private void walkByPath(ArrayList<Field> path){
|
||||||
|
// Aktuelle Hamsterpositions
|
||||||
|
Field curHamsterPos = start;
|
||||||
|
// Alle Felder des Pfades durchgehen und ablaufen
|
||||||
|
for(Field nextField : path){
|
||||||
|
// Links-Rechts-Bewegung
|
||||||
|
switch(nextField.column - curHamsterPos.column){
|
||||||
|
case 1: // nach rechts gehen
|
||||||
|
moveHamster(Hamster.OST);
|
||||||
|
break;
|
||||||
|
case -1: // nach links gehen
|
||||||
|
moveHamster(Hamster.WEST);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hoch-Runter-Bewegung
|
||||||
|
switch(nextField.row - curHamsterPos.row){
|
||||||
|
case 1: // nach oben gehen
|
||||||
|
moveHamster(Hamster.SUED);
|
||||||
|
break;
|
||||||
|
case -1: // nach unten gehen
|
||||||
|
moveHamster(Hamster.NORD);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hamsterposition aktualisieren
|
||||||
|
curHamsterPos = nextField;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Hilfsmethoden
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// Pfad anhand der Paren-Child beziehung aus einem Feld aufbauen.
|
||||||
|
private ArrayList<Field> getPath(Field f){
|
||||||
|
boolean bStartFound = false;
|
||||||
|
ArrayList<Field> path = new ArrayList<Field>();
|
||||||
|
Field cf = f;
|
||||||
|
while(!bStartFound){
|
||||||
|
path.add(0,cf);
|
||||||
|
if(cf.parent == null)
|
||||||
|
bStartFound = true;
|
||||||
|
else
|
||||||
|
cf = cf.parent;
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Feld ermittlen, auf dem der Hamster aktuell sitzt
|
||||||
|
private void getStart(){
|
||||||
|
start = new Field(sh.getSpalte(), sh.getReihe());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Feld ermitteln, auf dem das Korn liegt
|
||||||
|
private boolean getGrainField(){
|
||||||
|
int grainCounter = 0;
|
||||||
|
//Ermitteln, auf welchem Feld das Korn liegt
|
||||||
|
for(int col=0; col < fieldCol; col++){ // Spalten durchgehen
|
||||||
|
for(int row=0; row < fieldRow; row++){ // Reihe durchgehen
|
||||||
|
if(Territorium.getAnzahlKoerner(row,col) > 0 ){
|
||||||
|
goal = new Field(col,row); // Zielkoordinate setzen und Schleife abbrechen
|
||||||
|
grainCounter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(grainCounter == 1){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rotiert den Hamster in eine definierte Blickrichtung und bewegt ihn einen schritt vor.
|
||||||
|
private void moveHamster(int blickrichtung){
|
||||||
|
boolean moved = false;
|
||||||
|
while(!moved){
|
||||||
|
if(sh.getBlickrichtung() == blickrichtung){
|
||||||
|
sh.vor();
|
||||||
|
moved = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sh.linksUm();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Freie Nachbarfelder für Field cf ermitteln
|
||||||
|
private ArrayList<Field> getFreeNeighbours(Field cf){
|
||||||
|
ArrayList<Field> neigbours = new ArrayList<Field>();
|
||||||
|
// Feld oben prüfen
|
||||||
|
if(!Territorium.mauerDa(cf.row-1, cf.column)){
|
||||||
|
neigbours.add(new Field(cf.column, cf.row-1));
|
||||||
|
}
|
||||||
|
// Feld rechts prüfen)
|
||||||
|
if(!Territorium.mauerDa(cf.row, cf.column+1)){
|
||||||
|
neigbours.add(new Field(cf.column+1, cf.row));
|
||||||
|
}
|
||||||
|
// Feld unten prüfen)
|
||||||
|
if(!Territorium.mauerDa(cf.row+1, cf.column)){
|
||||||
|
neigbours.add(new Field(cf.column, cf.row+1));
|
||||||
|
}
|
||||||
|
// Feld links prüfen
|
||||||
|
if(!Territorium.mauerDa(cf.row, cf.column-1)){
|
||||||
|
neigbours.add(new Field(cf.column-1, cf.row));
|
||||||
|
}
|
||||||
|
return neigbours;
|
||||||
|
}
|
||||||
|
}
|
206
Search.java
Normal file
206
Search.java
Normal file
@ -0,0 +1,206 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import de.hamster.debugger.model.Territorium;import de.hamster.debugger.model.Territory;import de.hamster.model.HamsterException;import de.hamster.model.HamsterInitialisierungsException;import de.hamster.model.HamsterNichtInitialisiertException;import de.hamster.model.KachelLeerException;import de.hamster.model.MauerDaException;import de.hamster.model.MaulLeerException;import de.hamster.model.MouthEmptyException;import de.hamster.model.WallInFrontException;import de.hamster.model.TileEmptyException;import de.hamster.debugger.model.Hamster;public class Search{
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Eigenschaften / Protperties
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
private Hamster sh;
|
||||||
|
// Spielfeldgröße
|
||||||
|
private int fieldRow = Territorium.getAnzahlReihen();
|
||||||
|
private int fieldCol = Territorium.getAnzahlSpalten();
|
||||||
|
// Start- und Ziel-Feld
|
||||||
|
private Field goal;
|
||||||
|
private Field start;
|
||||||
|
// Arrays zum halten von Informationen.
|
||||||
|
private ArrayList<Field> queue = new ArrayList<Field>();
|
||||||
|
private ArrayList<Field> visited = new ArrayList<Field>();
|
||||||
|
boolean bInVisited = false;
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Public Methoden und Konstruktoren
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// Konstruktor -> zu bewegenden Hamster erforderlich
|
||||||
|
public Search(Hamster h){
|
||||||
|
sh = h;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialisiern und ermitteln der Start- und Zielposition
|
||||||
|
public boolean init(){
|
||||||
|
// Startfeld kann immer gefunden werden
|
||||||
|
getStart();
|
||||||
|
// Wenn kein oder mehrere Körner gefunden wurden, sollte ein Fehler ausgegeben werden;
|
||||||
|
return getGrainField();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pfad ermitteln
|
||||||
|
public Field findPath(){
|
||||||
|
return searchPathToGrain();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pfad laufen und Korn aufnehmen
|
||||||
|
public boolean walkPathGetKorn(Field goalField){
|
||||||
|
walkByPath(getPath(goalField));
|
||||||
|
|
||||||
|
if(sh.kornDa()){
|
||||||
|
sh.nimm();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Ermitteln des Weges mit Breitensuche
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
private Field searchPathToGrain(){
|
||||||
|
// ersten Knoten/Hamster in queue stellen
|
||||||
|
queue.add(start);
|
||||||
|
// Solange queue nicht leer, Schleife durchlaufen
|
||||||
|
while(queue.size() > 0){
|
||||||
|
// Aktuelles Feld nehmen und aus queue entfernen
|
||||||
|
Field currField = queue.get(0);
|
||||||
|
queue.remove(0);
|
||||||
|
// Feld auf Liste der besuchten Felder setzten
|
||||||
|
visited.add(currField);
|
||||||
|
|
||||||
|
// Wenn Ziel gefunden Fled zurückgeben
|
||||||
|
if(currField.column == goal.column && currField.row == goal.row){
|
||||||
|
return currField;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Freie Nachbarfelder ermitteln.
|
||||||
|
ArrayList<Field> freeNeighbours = getFreeNeighbours(currField);
|
||||||
|
for(int i = 0; i<freeNeighbours.size(); i++){
|
||||||
|
|
||||||
|
// prüfen ob Nachbarfeld nicht schon besucht wurde...
|
||||||
|
bInVisited = false;
|
||||||
|
for(Field visitedField : visited){
|
||||||
|
if(visitedField.column == freeNeighbours.get(i).column && visitedField.row == freeNeighbours.get(i).row){
|
||||||
|
bInVisited = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ...wenn nicht bearbeiten
|
||||||
|
if(!bInVisited){
|
||||||
|
// Nachbar an queue hinzufügen
|
||||||
|
queue.add(freeNeighbours.get(i));
|
||||||
|
freeNeighbours.get(i).parent = currField;
|
||||||
|
visited.add(freeNeighbours.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Wenn Ziel nich erreicht wurde...
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Bewegung des Hamsters anhand eines Pfades.
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
private void walkByPath(ArrayList<Field> path){
|
||||||
|
// Aktuelle Hamsterpositions
|
||||||
|
Field curHamsterPos = start;
|
||||||
|
// Alle Felder des Pfades durchgehen und ablaufen
|
||||||
|
for(Field nextField : path){
|
||||||
|
// Links-Rechts-Bewegung
|
||||||
|
switch(nextField.column - curHamsterPos.column){
|
||||||
|
case 1: // nach rechts gehen
|
||||||
|
moveHamster(Hamster.OST);
|
||||||
|
break;
|
||||||
|
case -1: // nach links gehen
|
||||||
|
moveHamster(Hamster.WEST);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hoch-Runter-Bewegung
|
||||||
|
switch(nextField.row - curHamsterPos.row){
|
||||||
|
case 1: // nach oben gehen
|
||||||
|
moveHamster(Hamster.SUED);
|
||||||
|
break;
|
||||||
|
case -1: // nach unten gehen
|
||||||
|
moveHamster(Hamster.NORD);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hamsterposition aktualisieren
|
||||||
|
curHamsterPos = nextField;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Hilfsmethoden
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// Pfad anhand der Paren-Child beziehung aus einem Feld aufbauen.
|
||||||
|
private ArrayList<Field> getPath(Field f){
|
||||||
|
boolean bStartFound = false;
|
||||||
|
ArrayList<Field> path = new ArrayList<Field>();
|
||||||
|
Field cf = f;
|
||||||
|
while(!bStartFound){
|
||||||
|
path.add(0,cf);
|
||||||
|
if(cf.parent == null)
|
||||||
|
bStartFound = true;
|
||||||
|
else
|
||||||
|
cf = cf.parent;
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Feld ermittlen, auf dem der Hamster aktuell sitzt
|
||||||
|
private void getStart(){
|
||||||
|
start = new Field(sh.getSpalte(), sh.getReihe());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Feld ermitteln, auf dem das Korn liegt
|
||||||
|
private boolean getGrainField(){
|
||||||
|
int grainCounter = 0;
|
||||||
|
//Ermitteln, auf welchem Feld das Korn liegt
|
||||||
|
for(int col=0; col < fieldCol; col++){ // Spalten durchgehen
|
||||||
|
for(int row=0; row < fieldRow; row++){ // Reihe durchgehen
|
||||||
|
if(Territorium.getAnzahlKoerner(row,col) > 0 ){
|
||||||
|
goal = new Field(col,row); // Zielkoordinate setzen und Schleife abbrechen
|
||||||
|
grainCounter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(grainCounter == 1){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rotiert den Hamster in eine definierte Blickrichtung und bewegt ihn einen schritt vor.
|
||||||
|
private void moveHamster(int blickrichtung){
|
||||||
|
boolean moved = false;
|
||||||
|
while(!moved){
|
||||||
|
if(sh.getBlickrichtung() == blickrichtung){
|
||||||
|
sh.vor();
|
||||||
|
moved = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sh.linksUm();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Freie Nachbarfelder für Field cf ermitteln
|
||||||
|
private ArrayList<Field> getFreeNeighbours(Field cf){
|
||||||
|
ArrayList<Field> neigbours = new ArrayList<Field>();
|
||||||
|
// Feld oben prüfen
|
||||||
|
if(!Territorium.mauerDa(cf.row-1, cf.column)){
|
||||||
|
neigbours.add(new Field(cf.column, cf.row-1));
|
||||||
|
}
|
||||||
|
// Feld rechts prüfen)
|
||||||
|
if(!Territorium.mauerDa(cf.row, cf.column+1)){
|
||||||
|
neigbours.add(new Field(cf.column+1, cf.row));
|
||||||
|
}
|
||||||
|
// Feld unten prüfen)
|
||||||
|
if(!Territorium.mauerDa(cf.row+1, cf.column)){
|
||||||
|
neigbours.add(new Field(cf.column, cf.row+1));
|
||||||
|
}
|
||||||
|
// Feld links prüfen
|
||||||
|
if(!Territorium.mauerDa(cf.row, cf.column-1)){
|
||||||
|
neigbours.add(new Field(cf.column-1, cf.row));
|
||||||
|
}
|
||||||
|
return neigbours;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user