class XOException extends RuntimeException {
private static final long serialVersionUID = 1L;
public XOException(String message) {
super(message);
}
}
class XO extends XOInterface {
private int moves;
public XO() {
super();
this.moves = 0;
}
@Override
public void start(){
if(getPlayer() > 0)
throw new XOException("Game already started");
setPlayer(1);
}
@Override
public boolean play(int field){
boolean win;
int player = getPlayer();
if(field < 0 || field > 8)
throw new XOException("Invalid field");
if(getFields()[field] > 0)
throw new XOException("Field already taken");
if(player == 0)
throw new XOException("Game not started");
win = setField(field, player);
moves += 1;
if(win) {
stop();
}else if (moves == 9){
stop();
}else{
setPlayer((2*player) % 3);
}
return win;
}
@Override
public void stop(){
if(getPlayer() == 0)
throw new XOException("Game already stopped");
resetFields();
setPlayer(0);
moves = 0;
}
}