KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
13  * Holds the map of the ant trail. Important: Clone intentionally not supported
14  * here!
15  *
16  * @author Klaus Meffert
17  * @since 3.01
18  */

19 public class AntMap {
20   /** String containing the CVS revision. Read out via reflection!*/
21   private final static String JavaDoc CVS_REVISION = "$Revision: 1.5 $";
22
23   // map point descriptions
24
public static final int ERROR = 0;
25
26   public static final int FOOD = -1;
27
28   public static final int EMPTY = 1;
29
30   public static final int TRAIL = 2;
31
32   public static final int ATE = 3;
33
34   // orientations
35
public static final int O_UP = 0;
36
37   public static final int O_LEFT = 1;
38
39   public static final int O_DOWN = 2;
40
41   public static final int O_RIGHT = 3;
42
43   private int m_orientation;
44
45   private int m_posx;
46
47   private int m_posy;
48
49   private int m_foodTaken;
50
51   /**
52    * Number of moves proceeded.
53    */

54   private int m_moves;
55
56   /**
57    * Width of map.
58    */

59   private int m_sizex;
60
61   /**
62    * Height of map.
63    */

64   private int m_sizey;
65
66   /**
67    * Holder of the trail's map.
68    */

69   private int[][] m_map;
70
71   /**
72    * For displaying moves as a..za..z etc. (idea from ECJ).
73    * Also see m_moveModUpper
74    */

75   private int m_moveMod;
76
77   /**
78    * False: use lower case chars with m_moveMod. true: Use Upper case chars.
79    */

80   private boolean m_moveModUpper;
81
82   /**
83    * Stores the moves done to display them later.
84    */

85   private int[][] m_movementMap;
86
87   /**
88    * Maximum number of solutions allowed.
89    */

90   private int m_maxMoves;
91
92   public AntMap(final int[][] a_map, int a_maxMoves) {
93     m_sizex = a_map.length;
94     m_sizey = a_map[0].length;
95     m_map = new int[m_sizex][m_sizey];
96     for (int x = 0; x < m_sizex; x++) {
97       m_map[x] = (int[]) (a_map[x].clone());
98     }
99     m_movementMap = new int[m_sizex][m_sizey];/**@todo speedup possible by using string?*/
100     m_orientation = O_RIGHT;
101     m_posx = 0;
102     m_posy = 0;
103     m_moveMod = 0;
104     m_moveModUpper = false;
105     m_maxMoves = a_maxMoves;
106     storeMove();
107     m_moves = 0;
108   }
109
110   public int[][] getMap() {
111     return m_map;
112   }
113
114   public int getFromMap(int a_x, int a_y) {
115     return m_map[a_x][a_y];
116   }
117
118 // public void setInMap(int a_x, int a_y, int a_value) {
119
// m_map[a_x][a_y] = a_value;
120
// }
121

122   public int getPosX() {
123     return m_posx;
124   }
125
126   public int getPosY() {
127     return m_posy;
128   }
129
130   public void setPosX(int a_value) {
131     m_posx = a_value;
132     storeMove();
133     checkFoodTaken();
134   }
135
136   public void setPosY(int a_value) {
137     m_posy = a_value;
138     storeMove();
139     checkFoodTaken();
140   }
141
142   private void storeMove() {
143     char c;
144     if (!m_moveModUpper) {
145       c = (char) (m_moveMod + 'a');
146       if (m_moveMod++ >= 25) {
147         m_moveMod = 0;
148         m_moveModUpper = true;
149       }
150     }
151     else {
152       c = (char) (m_moveMod + 'A');
153       if (m_moveMod++ >= 25) {
154         m_moveMod = 0;
155         m_moveModUpper = false;
156       }
157     }
158     m_movementMap[m_posx][m_posy] = c;
159   }
160
161   public int[][] getMovements() {
162     return m_movementMap;
163   }
164
165   private void checkFoodTaken() {
166     if (m_map[m_posx][m_posy] == AntMap.FOOD) {
167       m_foodTaken++;
168       m_map[m_posx][m_posy] = AntMap.ATE;
169     }
170     else {
171       // Do nothing.
172
// -----------
173
}
174   }
175
176   public int getOrientation() {
177     return m_orientation;
178   }
179
180   public void setOrientation(int a_orientation) {
181     m_orientation = a_orientation;
182   }
183
184   public int getFoodTaken() {
185     return m_foodTaken;
186   }
187
188   public int getMoveCount() {
189     return m_moves;
190   }
191
192   public void IncrementMoveCounter() {
193     m_moves++;
194     if (m_moves > m_maxMoves) {
195       throw new IllegalStateException JavaDoc("Maximum number of moves exceeded");
196     }
197   }
198
199   public int getWidth() {
200     return m_sizex;
201   }
202
203   public int getHeight() {
204     return m_sizey;
205   }
206
207 }
208
Popular Tags