package rover;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
public class Start {
static Random r = new Random();
static LinkedHashMap<int[], String> mars;
public static void init() {
mars = new LinkedHashMap<>();
int x = 80;
int y = 20;
int rx = x / 2;
int ry = y / 2;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
int[] p = new int[] { i, j };
if (r.nextDouble() < 0.25 && !(rx == i && ry == j))
mars.put(p, "#");
}
}
mars.put(new int[] { rx, ry }, "n");
}
public static int[] maximum(Set<int[]> set) {
int[] x = new int[2];
for (int[] e : set) {
if (e[0] > x[0])
x[0] = e[0];
if (e[1] > x[1])
x[1] = e[1];
}
return x;
}
public static String get(Map<int[], String> kloetze, int[] p) {
Set<Entry<int[], String>> entrySet = kloetze.entrySet();
for (Entry<int[], String> entry : entrySet) {
if (entry.getKey()[0] == p[0] && entry.getKey()[1] == p[1])
return entry.getValue();
}
return null;
}
public static void out() {
int[] max = maximum(mars.keySet());
for (int j = 0; j < max[1]; j++) {
for (int i = 0; i < max[0]; i++) {
if (get(mars, new int[] { i, j }) == null) {
System.out.print(" ");
continue;
}
if (get(mars, new int[] { i, j }).equals("#"))
System.out.print("#");
else if (get(mars, new int[] { i, j }).equals("n"))
System.out.print("^");
else if (get(mars, new int[] { i, j }).equals("s"))
System.out.print("V");
else if (get(mars, new int[] { i, j }).equals("e"))
System.out.print(">");
if (get(mars, new int[] { i, j }).equals("w"))
System.out.print("<");
}
System.out.println();
}
for (int i = 0; i < max[0]; i++) {
System.out.print("=");
}
System.out.println();
}
public static void main(String[] args) {
if (args.length > 1) {
long seed = Long.parseLong(args[1]);
r.setSeed(seed);
}
init();
String pg = args[0];
out();
for (int i = 0; i < pg.length(); i++) {
make(pg.charAt(i));
out();
}
}
public static void make(char c) {
if (c == 'f') {
int[] p = findeRover();
if (get(mars, p).equals("n"))
p[1]--;
else if (get(mars, p).equals("s"))
p[1]++;
else if (get(mars, p).equals("e"))
p[0]++;
else if (get(mars, p).equals("w"))
p[0]--;
} else if (c == 'b') {
int[] p = findeRover();
if (get(mars, p).equals("s"))
p[1]--;
else if (get(mars, p).equals("n"))
p[1]++;
else if (get(mars, p).equals("w"))
p[0]++;
else if (get(mars, p).equals("e"))
p[0]--;
} else if (c == 'l') {
int[] p = findeRover();
if (get(mars, p).equals("n"))
mars.put(p, "w");
else if (get(mars, p).equals("s"))
mars.put(p, "e");
else if (get(mars, p).equals("e"))
mars.put(p, "n");
else if (get(mars, p).equals("w"))
mars.put(p, "s");
} else if (c == 'r') {
int[] p = findeRover();
if (get(mars, p).equals("w"))
mars.put(p, "n");
else if (get(mars, p).equals("e"))
mars.put(p, "s");
else if (get(mars, p).equals("n"))
mars.put(p, "e");
else if (get(mars, p).equals("s"))
mars.put(p, "w");
}
}
private static int[] findeRover() {
Set<Entry<int[], String>> entrySet = mars.entrySet();
for (Entry<int[], String> entry : entrySet) {
if (entry.getValue() != null && !entry.getValue().equals("#"))
return entry.getKey();
}
throw new IllegalStateException("Rover missing in action");
}
}
ublic class Start {
static Random r = new Random();
static LinkedHashMap<int[], String> marsKarte;
static int BREITE_MARSES = 80;
static int HOEHE_MARSES = 20;
static int ROVER_XPOSITION = BREITE_MARSES / 2;
static int ROVER_YPOSITION = HOEHE_MARSES / 2;
public static void erstelleKarteUndSetzeRoverInDieMitte() {
marsKarte = new LinkedHashMap<>();
for (int i = 0; i < BREITE_MARSES; i++) {
for (int j = 0; j < HOEHE_MARSES; j++) {
int[] positionVomHindernis = new int[] { i, j };
if (r.nextDouble() < 0.25 && !(ROVER_XPOSITION == i && ROVER_YPOSITION == j))
marsKarte.put(positionVomHindernis, "#");
}
}
marsKarte.put(new int[] {ROVER_XPOSITION , ROVER_YPOSITION }, "n");
}
public static int[] maximum(Set<int[]> set) {
int[] x = new int[2];
for (int[] e : set) {
if (e[0] > x[0])
x[0] = e[0];
if (e[1] > x[1])
x[1] = e[1];
}
return x;
}
public static String get(Map<int[], String> kloetze, int[] p) {
Set<Entry<int[], String>> entrySet = kloetze.entrySet();
for (Entry<int[], String> entry : entrySet) {
if (entry.getKey()[0] == p[0] && entry.getKey()[1] == p[1])
return entry.getValue();
}
return null;
}
public static void printeDasFeld() {
int[] max = maximum(marsKarte.keySet());
for (int j = 0; j < max[1]; j++) {
for (int i = 0; i < max[0]; i++) {
if (get(marsKarte, new int[] { i, j }) == null) {
System.out.print(" ");
continue;
}
switch(get(marsKarte, new int[] { i, j })){
case "#" : System.out.print("#"); continue;
case "n" : System.out.print("^"); continue;
case "s" : System.out.print("V"); continue;
case "e" : System.out.print(">"); continue;
default : System.out.print("<");
}
}
System.out.println();
}
ausgabeAbgrenzungVonKarte(max);
}
public static void ausgabeAbgrenzungVonKarte(int[] max){
for (int i = 0; i < max[0]; i++) {
System.out.print("=");
}
System.out.println();
}
public static void main(String[] args) {
if (args.length > 1) {
long seed = Long.parseLong(args[1]);
r.setSeed(seed);
}
erstelleKarteUndSetzeRoverInDieMitte();
String pg = args[0];
printeDasFeld();
for (int i = 0; i < pg.length(); i++) {
make(pg.charAt(i));
printeDasFeld();
}
}
public static void make(char c) {
if (c == 'f') {
int[] p = findeRover();
if (get(marsKarte, p).equals("n"))
p[1]--;
else if (get(marsKarte, p).equals("s"))
p[1]++;
else if (get(marsKarte, p).equals("e"))
p[0]++;
else if (get(marsKarte, p).equals("w"))
p[0]--;
} else if (c == 'b') {
int[] p = findeRover();
if (get(marsKarte, p).equals("s"))
p[1]--;
else if (get(marsKarte, p).equals("n"))
p[1]++;
else if (get(marsKarte, p).equals("w"))
p[0]++;
else if (get(marsKarte, p).equals("e"))
p[0]--;
} else if (c == 'l') {
int[] p = findeRover();
if (get(marsKarte, p).equals("n"))
marsKarte.put(p, "w");
else if (get(marsKarte, p).equals("s"))
marsKarte.put(p, "e");
else if (get(marsKarte, p).equals("e"))
marsKarte.put(p, "n");
else if (get(marsKarte, p).equals("w"))
marsKarte.put(p, "s");
} else if (c == 'r') {
int[] p = findeRover();
if (get(marsKarte, p).equals("w"))
marsKarte.put(p, "n");
else if (get(marsKarte, p).equals("e"))
marsKarte.put(p, "s");
else if (get(marsKarte, p).equals("n"))
marsKarte.put(p, "e");
else if (get(marsKarte, p).equals("s"))
marsKarte.put(p, "w");
}
}
private static int[] findeRover() {
Set<Entry<int[], String>> entrySet = marsKarte.entrySet();
for (Entry<int[], String> entry : entrySet) {
if (entry.getValue() != null && !entry.getValue().equals("#"))
return entry.getKey();
}
throw new IllegalStateException("Rover missing in action");
}
}