Skip to content
Snippets Groups Projects

Add command line input class

Merged Hans-Georg Sommer requested to merge input into master
1 file
+ 48
0
Compare changes
  • Side-by-side
  • Inline
+ 48
0
package flowerwarspp;
import flowerwarspp.preset.Move;
import flowerwarspp.preset.MoveFormatException;
import flowerwarspp.preset.PlayerColor;
import flowerwarspp.preset.Requestable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Simple input class which reads moves from the command line.
*/
public class MoveRequesterCli implements Requestable {
/**
* Name used in input prompt.
*/
private String playerName;
/**
* Reader used to read input from stdin.
*/
private BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
/**
* Create a new MoveRequesterCli for the given player color.
* @param color PlayerColor used in input prompt
*/
public MoveRequesterCli(PlayerColor color) {
playerName = color.name();
}
/*
* @see flowerwars.preset.Requestable#request(flowerwars.preset.Requestable)
*/
@Override
public Move request() {
Move move = null;
while (move == null) {
try {
System.out.printf("Enter move for player %s: ", playerName);
move = Move.parseMove(input.readLine());
} catch (IOException | MoveFormatException e) {
System.out.println("Error: " + e);
}
}
return move;
}
}
Loading