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 IfIsOccupied 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 IfIsOccupied(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 IfIsOccupied(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 occupied(&1, &2) then (&3)"; 47 } 48 49 55 public String getName() { 56 return "If Occupied"; 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 ("IfOccupied: cannot process type " 75 + m_type); 76 } 77 if (condition) { 78 c.execute_void(n, 2, args); 79 } 80 } 81 82 92 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 93 if (a_chromNum == 0 || a_chromNum == 1) { 94 return m_type; 95 } 96 return CommandGene.VoidClass; 97 } 98 } 99 | Popular Tags |