Search in sources :

Example 1 with Move

use of de.kimminich.kata.tcg.Move in project kata-tcg by bkimminich.

the class ConsoleInputStrategy method nextMove.

@Override
public Move nextMove(int availableMana, int currentHealth, List<Card> availableCards) {
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
        Integer card = -1;
        Action action = Action.DAMAGE;
        while (card < 0 || card > 8 || card > availableMana || !availableCards.contains(new Card(card))) {
            try {
                String input = br.readLine();
                if (input.endsWith("h")) {
                    action = Action.HEALING;
                    input = input.replace("h", "");
                }
                card = Integer.decode(input);
            } catch (NumberFormatException e) {
                logger.warning("Invalid input: " + e.getMessage());
            }
        }
        return new Move(Optional.of(new Card(card)), action);
    } catch (IOException e) {
        logger.severe("Could not read console input: " + e.getMessage());
        e.printStackTrace();
    }
    return new Move(Optional.empty(), null);
}
Also used : Action(de.kimminich.kata.tcg.Action) InputStreamReader(java.io.InputStreamReader) Move(de.kimminich.kata.tcg.Move) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Card(de.kimminich.kata.tcg.Card)

Aggregations

Action (de.kimminich.kata.tcg.Action)1 Card (de.kimminich.kata.tcg.Card)1 Move (de.kimminich.kata.tcg.Move)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1