SnakeBot implementiert
This commit is contained in:
parent
1a9089e390
commit
0fdbebf021
83
SnakeBot.java
Normal file
83
SnakeBot.java
Normal file
@ -0,0 +1,83 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SnakeBot extends Bot{
|
||||
boolean lastMoveWasTurn = false; // Merker, ob der letzte Zug eine Drehung war
|
||||
|
||||
public static void main(String[] args) {
|
||||
Bot snakeBot = new SnakeBot(args);
|
||||
snakeBot.run();
|
||||
}
|
||||
protected SnakeBot(String[] args) {
|
||||
super(args);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected char nextMove(View view) throws Exception {
|
||||
char nextMove = '^';
|
||||
boolean stoneDetected = view.data.contains("@");
|
||||
System.out.println(view.data + " xxx: ");
|
||||
|
||||
// Das ist nur fuer debug
|
||||
// List<Integer> positions = positionsOfRock(view.data);
|
||||
// for (Integer i : positions)
|
||||
// System.out.println("Position: " + i);
|
||||
|
||||
if (lastMoveWasTurn) {
|
||||
lastMoveWasTurn = false; // Merke fuer die letzte Drehung zuruecksetzen
|
||||
nextMove = '^';
|
||||
}
|
||||
else {
|
||||
if (!stoneDetected)
|
||||
nextMove = '^';
|
||||
else {
|
||||
nextMove = calculateNextMove(view);
|
||||
}
|
||||
}
|
||||
return nextMove;
|
||||
}
|
||||
|
||||
/**
|
||||
* Berechne den naechsten Zug auf Basis der gefundenen Steine im Scan
|
||||
* @param view
|
||||
* @return
|
||||
*/
|
||||
private char calculateNextMove(View view){
|
||||
List<Integer> positions = positionsOfRock(view.data);
|
||||
int firstPostion = positions.get(0);
|
||||
|
||||
|
||||
// Wenn das @Zeichen an Position kleiner 11, dann ist es oberhalb von uns
|
||||
if (firstPostion < 11 )
|
||||
return '^';
|
||||
|
||||
// Ist die Position vom @Zeichen zwischn 11 und 12, dann links von uns
|
||||
// wir muessen uns nach links drehen
|
||||
else if (firstPostion >= 11 && firstPostion < 13) {
|
||||
lastMoveWasTurn = true; // Merker auf true, da wir uns gedreht haben
|
||||
return '<';
|
||||
}
|
||||
// Bei allen anderen Positionen drehen wir uns nach rechts drehen
|
||||
else {
|
||||
lastMoveWasTurn = true; // Merker auf true, da wir uns gedreht haben
|
||||
return '>';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ermittle alle Positionen der Steine innerhalb des flachen Scans
|
||||
* @param row das Ergebnis von View.data (der Scan als String)
|
||||
* @return eine Liste mit den Positionen
|
||||
*/
|
||||
private List<Integer> positionsOfRock (String row){
|
||||
int index = row.indexOf('@');
|
||||
ArrayList<Integer> positions = new ArrayList<>();
|
||||
while (index >= 0) {
|
||||
positions.add(index);
|
||||
index = row.indexOf('@', index + 1);
|
||||
}
|
||||
return positions;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user