„ManualBot.java“ ändern

Ich habe die manuelle Steugerung durch Unterklasse ManuelBot erlaubt.
Eingabe „w“ - Vorwärts
Eingabe „s“ -  Rückwärts
Eingabe „a“- Linksdrehung
Eingabe „d“ - Rechtsdrehung
Eingabe „q“  -Abbruch der Verbindung
This commit is contained in:
Isabella Nawratil 2024-02-01 15:06:35 +00:00
parent 3f64df6fc4
commit eae8ae3f64

View File

@ -8,28 +8,34 @@ public class ManualBot extends Bot {
} }
public static void main(String[] args) { public static void main(String[] args) {
ManualBot manualbot = new ManualBot(args); ManualBot manualBot = new ManualBot(args);
manualbot.run(); manualBot.run();
} }
@Override @Override
protected char nextMove(View view) throws Exception { protected char nextMove(View view) throws Exception {
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
char move = scanner.nextLine().charAt(0); char move;
do {
System.out.println("Bitte geben Sie einen Steuerbefehl ein (w, s, a, d, q): ");
move = scanner.nextLine().toLowerCase().charAt(0);
} while (!isValidMove(move));
return move;
}
private boolean isValidMove(char move) {
switch (move) { switch (move) {
case 'w': case 'w':
case 's': case 's':
case 'a': case 'a':
case 'd': case 'd':
case 'q': case 'q':
return move; return true;
default: default:
System.out.println("Falsche Taste. Bitte geben sie w,s,a,d,q ein danke."); System.out.println("Falsche Taste. Bitte geben Sie w, s, a, d oder q ein.");
return nextMove(view); return false;
} }
} }
} }