KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > gp > tictactoe > IfIsOccupied


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.tictactoe;
11
12 import org.jgap.*;
13 import org.jgap.gp.*;
14 import org.jgap.gp.impl.*;
15
16 /**
17  * The if-then construct. If-Condition is: if board free at position (X,Y),
18  * then execute Z else do nothing.
19  *
20  * @author Klaus Meffert
21  * @since 3.2
22  */

23 public class IfIsOccupied
24     extends CommandGene {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private final static String JavaDoc CVS_REVISION = "$Revision: 1.1 $";
27
28   private Class JavaDoc m_type;
29
30   private Board m_board;
31
32   public IfIsOccupied(final GPConfiguration a_conf, Board a_board, Class JavaDoc 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 JavaDoc 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 JavaDoc toString() {
46     return "if occupied(&1, &2) then (&3)";
47   }
48
49   /**
50    * @return textual name of this command
51    *
52    * @author Klaus Meffert
53    * @since 3.2
54    */

55   public String JavaDoc getName() {
56     return "If Occupied";
57   }
58
59   public void execute_void(ProgramChromosome c, int n, Object JavaDoc[] 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 JavaDoc("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 JavaDoc("y must be 0.." + Board.HEIGHT);
70       }
71       condition = m_board.readField(x, y) != 0;
72     }
73     else {
74       throw new IllegalStateException JavaDoc("IfOccupied: cannot process type "
75                                       + m_type);
76     }
77     if (condition) {
78       c.execute_void(n, 2, args);
79     }
80   }
81
82   /**
83    * Determines which type a specific child of this command has.
84    *
85    * @param a_ind ignored here
86    * @param a_chromNum index of child
87    * @return type of the a_chromNum'th child
88    *
89    * @author Klaus Meffert
90    * @since 3.2
91    */

92   public Class JavaDoc 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