1 10 package examples.gp.tictactoe; 11 12 import org.jgap.gp.*; 13 import org.jgap.*; 14 import org.jgap.gp.impl.*; 15 16 public class PutStone 17 extends CommandGene { 18 19 private final static String CVS_REVISION = "$Revision: 1.2 $"; 20 21 private Board m_board; 22 23 private int m_color; 24 25 public PutStone(final GPConfiguration a_conf, Board a_board, int a_color) 26 throws InvalidConfigurationException { 27 this(a_conf, a_board, a_color, 0, null); 28 } 29 30 public PutStone(final GPConfiguration a_conf, Board a_board, int a_color, 31 int a_subReturnType, int[] a_subChildTypes) 32 throws InvalidConfigurationException { 33 super(a_conf, 2, CommandGene.VoidClass, a_subReturnType, a_subChildTypes); 34 m_board = a_board; 35 m_color = a_color; 36 } 37 38 public String toString() { 39 return "put_stone(&1, &2)"; 40 } 41 42 public void execute_void(ProgramChromosome c, int n, Object [] args) { 43 check(c); 44 int x = c.execute_int(n, 0, args); 45 int y = c.execute_int(n, 1, args); 46 boolean gameWon = m_board.putStone(x, y, m_color); 49 if (gameWon) { 52 throw new GameWonException(m_color, "Game won by color " + m_color); 53 } 54 } 55 56 protected void check(ProgramChromosome a_program) { 57 if (m_board.getLastColor() == m_color) { 58 throw new IllegalStateException ("Only one stone of a color per round!"); 59 } 60 } 61 62 72 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 73 return CommandGene.IntegerClass; 75 } 77 } 78 | Popular Tags |