1 10 package examples.gp.tictactoe; 11 12 import org.jgap.*; 13 import org.jgap.gp.*; 14 import org.jgap.gp.impl.*; 15 16 23 public class IfIsFree 24 extends CommandGene { 25 26 private final static String CVS_REVISION = "$Revision: 1.1 $"; 27 28 private Class m_type; 29 30 private Board m_board; 31 32 public IfIsFree(final GPConfiguration a_conf, Board a_board, Class a_type) 33 throws InvalidConfigurationException { 34 this(a_conf, a_board, a_type, 0, null); 35 } 36 37 public IfIsFree(final GPConfiguration a_conf, Board a_board, Class a_type, 38 int a_subReturnType, int[] a_subChildTypes) 39 throws InvalidConfigurationException { 40 super(a_conf, 3, CommandGene.VoidClass, a_subReturnType, a_subChildTypes); 41 m_type = a_type; 42 m_board = a_board; 43 } 44 45 public String toString() { 46 return "if free(&1, &2) then (&3)"; 47 } 48 49 55 public String getName() { 56 return "If Free"; 57 } 58 59 public void execute_void(ProgramChromosome c, int n, Object [] args) { 60 check(c); 61 boolean condition; 62 if (m_type == CommandGene.IntegerClass) { 63 int x = c.execute_int(n, 0, args); 64 if (x < 0 || x >= Board.WIDTH) { 65 throw new IllegalStateException ("x must be 0.." + Board.WIDTH); 66 } 67 int y = c.execute_int(n, 1, args); 68 if (y < 0 || x >= Board.HEIGHT) { 69 throw new IllegalStateException ("y must be 0.." + Board.HEIGHT); 70 } 71 condition = m_board.readField(x, y) == 0; 72 } 73 else { 74 throw new IllegalStateException ("IfIsFree: cannot process type " + m_type); 75 } 76 if (condition) { 77 c.execute_void(n, 2, args); 78 } 79 } 80 81 91 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 92 if (a_chromNum == 0 || a_chromNum == 1) { 93 return m_type; 94 } 95 return CommandGene.VoidClass; 96 } 97 } 98 | Popular Tags |