KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > gp > anttrail > Move


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package examples.gp.anttrail;
11
12 import org.jgap.gp.impl.*;
13 import org.jgap.*;
14
15 /**
16  * Move the ant. Allows to specify how many times in a row a move will be
17  * executed. The classic ant problem uses 1 as parameter value.
18  *
19  * @author Klaus Meffert
20  * @since 3.01
21  */

22 public class Move
23     extends AntCommand {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.3 $";
26
27   private int m_moves;
28
29   /**
30    * Standard constructor for classic ant problem.
31    *
32    * @param a_conf the configuration to use
33    * @throws InvalidConfigurationException
34    *
35    * @author Klaus Meffert
36    * @since 3.01
37    */

38   public Move(final GPConfiguration a_conf)
39       throws InvalidConfigurationException {
40     this(a_conf, 1);
41   }
42
43   /**
44    * Allows to move more than one time in a row.
45    *
46    * @param a_conf the configuration to use
47    * @param a_moves number of moves to execute in a row
48    *
49    * @throws InvalidConfigurationException
50    *
51    * @author Klaus Meffert
52    * @since 3.01
53    */

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 JavaDoc[] 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 JavaDoc("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 JavaDoc("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 JavaDoc("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 JavaDoc("y smaller zero");
92           }
93           map.setPosY(y);
94           break;
95         default:
96           throw new IllegalStateException JavaDoc("Illegal orientation");
97       }
98       map.IncrementMoveCounter();
99     }
100   }
101
102   public String JavaDoc toString() {
103     if (m_moves == 1) {
104       return "move";
105     }
106     else {
107       return "move" + m_moves;
108     }
109   }
110 }
111
Popular Tags