Show ComputerPlayer.java syntax highlighted
package geronimo.hoshigo.model.external;
import geronimo.hoshigo.model.game.AbstractPlayer;
import geronimo.hoshigo.model.game.Game;
import geronimo.hoshigo.model.game.Move;
import geronimo.hoshigo.model.game.PassMove;
import geronimo.hoshigo.model.game.Score;
import geronimo.hoshigo.model.goban.GoColor;
public class ComputerPlayer extends AbstractPlayer
{
// programme attaché au go ProgramPlayer
public final GoProgram program;
public String name;
public ComputerPlayer(GoProgram program, GoColor color)
{
super(color);
this.program = program;
}
/* (non-Javadoc)
* @see geronimo.hoshigo.model.game.AbstractPlayer#start()
*/
public synchronized void start()
{
super.start();
try
{
this.program.launchProgram();
this.name = this.getName();
}
catch (GoProgramException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public String getName()
{
if (this.name != null)
{
return this.name;
}
else
{
try
{
this.name =this.program.getProgramName();
if (! this.program.options.isEmpty())
{
this.name += " " +this.program.options;
}
return this.name;
}
catch (GoProgramException e)
{
e.printStackTrace();
}
return "unknown";
}
}
@Override
protected Move genMove()
{
try
{
this.program.synchronizeWithGame(this.game);
Move move = this.program.genMove(this.getColor());
// si le coup n'est pas contenu dans les coups légaux alors
// on fait un undo et on fait jouer pass
if ( ! this.legalMoves.contains(move))
{
move = new PassMove(this.getColor());
}
this.program.undo();
return move;
}
catch (GoProgramException e)
{
// TODO Auto-generated catch block
if (this.program.programProcess == null)
{
// le programme a été terminé donc pas de stack
// et coup joué de pass
return new PassMove(this.getColor());
}
else
{
e.printStackTrace();
return null;
}
}
}
public boolean isHuman()
{
return false;
}
public void gameEnded(Game game, Score score)
{
// ne fait rien - this.program.stopProgram();
}
public void destroy()
{
super.destroy();
this.program.killProgram();
}
}
See more files for this project here