/*
* Human
*
* Thomas David Baker, 2002-03-10
*/
package net.bluebones.tictactoe;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
/**
* Implementation of Player
that allows for a human
* to play via STDIN.
*
* @author Thomas David Baker, bakert+tictactoe@gmail.com
* @version 1.1
*/
public class Human implements Player {
/**
* Gets the next move of this player.
*
* @param board Board
representation of the current game
* state.
* @param prev Move
representing the previous move in the
* the game.
* @param color int
representing the pieces this
* Player
is playing with. One of
* TicTacToe.NOUGHTS
or
* TicTacToe.CROSSES
* @return Move
Next move of this player.
*/
public Move getMove(byte[][] board, Move prev, byte color) {
// ask for move
System.out.print("X position? ");
byte x = getCoord();
System.out.print("Y position? ");
byte y = getCoord();
return new Move(x, y, color);
}
// helper method for get move - asks stdin for a coordinate.
private byte getCoord() {
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
byte n = -1;
try {
n = Byte.parseByte(in.readLine());
} catch (IOException e) {
System.out.println("Problem reading input!");
return getCoord();
} catch (NumberFormatException e) {
System.out.println("Not a number!");
return getCoord();
}
if (n < 0 || n >= Board.SIZE) {
System.out.println("Not within range (0-" + (Board.SIZE - 1)
+ ")!");
return getCoord();
}
return n;
}
}