001: /*
002: * This file is part of JGAP.
003: *
004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
005: *
006: * For licensing information please see the file license.txt included with JGAP
007: * or have a look at the top of class org.jgap.Chromosome which representatively
008: * includes the JGAP license policy applicable for any file delivered with JGAP.
009: */
010: package examples.gp.anttrail;
011:
012: /**
013: * Holds the map of the ant trail. Important: Clone intentionally not supported
014: * here!
015: *
016: * @author Klaus Meffert
017: * @since 3.01
018: */
019: public class AntMap {
020: /** String containing the CVS revision. Read out via reflection!*/
021: private final static String CVS_REVISION = "$Revision: 1.6 $";
022:
023: // map point descriptions
024: public static final int ERROR = 0;
025:
026: public static final int FOOD = -1;
027:
028: public static final int EMPTY = 1;
029:
030: public static final int TRAIL = 2;
031:
032: public static final int ATE = 3;
033:
034: // orientations
035: public static final int O_UP = 0;
036:
037: public static final int O_LEFT = 1;
038:
039: public static final int O_DOWN = 2;
040:
041: public static final int O_RIGHT = 3;
042:
043: private int m_orientation;
044:
045: private int m_posx;
046:
047: private int m_posy;
048:
049: private int m_foodTaken;
050:
051: /**
052: * Number of moves proceeded.
053: */
054: private int m_moves;
055:
056: /**
057: * Width of map.
058: */
059: private int m_sizex;
060:
061: /**
062: * Height of map.
063: */
064: private int m_sizey;
065:
066: /**
067: * Holder of the trail's map.
068: */
069: private int[][] m_map;
070:
071: /**
072: * For displaying moves as a..za..z etc. (idea from ECJ).
073: * Also see m_moveModUpper
074: */
075: private int m_moveMod;
076:
077: /**
078: * False: use lower case chars with m_moveMod. true: Use Upper case chars.
079: */
080: private boolean m_moveModUpper;
081:
082: /**
083: * Stores the moves done to display them later.
084: */
085: private int[][] m_movementMap;
086:
087: /**
088: * Maximum number of solutions allowed.
089: */
090: private int m_maxMoves;
091:
092: public AntMap(final int[][] a_map, int a_maxMoves) {
093: m_sizex = a_map.length;
094: m_sizey = a_map[0].length;
095: m_map = new int[m_sizex][m_sizey];
096: for (int x = 0; x < m_sizex; x++) {
097: m_map[x] = (int[]) (a_map[x].clone());
098: }
099: m_movementMap = new int[m_sizex][m_sizey];
100: /**@todo speedup possible by using string?*/
101: m_orientation = O_RIGHT;
102: m_posx = 0;
103: m_posy = 0;
104: m_moveMod = 0;
105: m_moveModUpper = false;
106: m_maxMoves = a_maxMoves;
107: storeMove();
108: m_moves = 0;
109: }
110:
111: public int[][] getMap() {
112: return m_map;
113: }
114:
115: public int getFromMap(int a_x, int a_y) {
116: return m_map[a_x][a_y];
117: }
118:
119: // public void setInMap(int a_x, int a_y, int a_value) {
120: // m_map[a_x][a_y] = a_value;
121: // }
122:
123: public int getPosX() {
124: return m_posx;
125: }
126:
127: public int getPosY() {
128: return m_posy;
129: }
130:
131: public void setPosX(int a_value) {
132: m_posx = a_value;
133: storeMove();
134: checkFoodTaken();
135: }
136:
137: public void setPosY(int a_value) {
138: m_posy = a_value;
139: storeMove();
140: checkFoodTaken();
141: }
142:
143: private void storeMove() {
144: char c;
145: if (!m_moveModUpper) {
146: c = (char) (m_moveMod + 'a');
147: if (m_moveMod++ >= 25) {
148: m_moveMod = 0;
149: m_moveModUpper = true;
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: } else {
170: // Do nothing.
171: // -----------
172: }
173: }
174:
175: public int getOrientation() {
176: return m_orientation;
177: }
178:
179: public void setOrientation(int a_orientation) {
180: m_orientation = a_orientation;
181: }
182:
183: public int getFoodTaken() {
184: return m_foodTaken;
185: }
186:
187: public int getMoveCount() {
188: return m_moves;
189: }
190:
191: public void IncrementMoveCounter() {
192: m_moves++;
193: if (m_moves > m_maxMoves) {
194: throw new IllegalStateException(
195: "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: }
|