1 10 package examples.gp.anttrail; 11 12 import org.jgap.gp.impl.*; 13 import org.jgap.*; 14 15 22 public class Move 23 extends AntCommand { 24 25 private final static String CVS_REVISION = "$Revision: 1.3 $"; 26 27 private int m_moves; 28 29 38 public Move(final GPConfiguration a_conf) 39 throws InvalidConfigurationException { 40 this(a_conf, 1); 41 } 42 43 54 public Move(final GPConfiguration a_conf, int a_moves) 55 throws InvalidConfigurationException { 56 super(a_conf); 57 m_moves = a_moves; 58 } 59 60 public void execute_void(ProgramChromosome a_chrom, int a_n, Object [] a_args) { 61 AntMap map = getMap(a_chrom); 62 int x = map.getPosX(); 63 int y = map.getPosY(); 64 int orient = map.getOrientation(); 65 for (int i = 0; i < m_moves; i++) { 66 switch (orient) { 67 case AntMap.O_DOWN: 68 y++; 69 if (y >= map.getHeight()) { 70 throw new IllegalStateException ("y bigger than height"); 71 } 72 map.setPosY(y); 73 break; 74 case AntMap.O_LEFT: 75 x--; 76 if (x < 0) { 77 throw new IllegalStateException ("x smaller zero"); 78 } 79 map.setPosX(x); 80 break; 81 case AntMap.O_RIGHT: 82 x++; 83 if (x >= map.getWidth()) { 84 throw new IllegalStateException ("x bigger than width"); 85 } 86 map.setPosX(x); 87 break; 88 case AntMap.O_UP: 89 y--; 90 if (y < 0) { 91 throw new IllegalStateException ("y smaller zero"); 92 } 93 map.setPosY(y); 94 break; 95 default: 96 throw new IllegalStateException ("Illegal orientation"); 97 } 98 map.IncrementMoveCounter(); 99 } 100 } 101 102 public String toString() { 103 if (m_moves == 1) { 104 return "move"; 105 } 106 else { 107 return "move" + m_moves; 108 } 109 } 110 } 111 | Popular Tags |