KickJava   Java API By Example, From Geeks To Geeks.

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


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 specific color, then
18  * execute X else do nothing.
19  *
20  * @author Klaus Meffert
21  * @since 3.2
22  */

23 public class IfIsFree
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 IfIsFree(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 IfIsFree(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 free(&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 Free";
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("IfIsFree: cannot process type " + m_type);
75     }
76     if (condition) {
77       c.execute_void(n, 2, args);
78     }
79   }
80
81   /**
82    * Determines which type a specific child of this command has.
83    *
84    * @param a_ind ignored here
85    * @param a_chromNum index of child
86    * @return type of the a_chromNum'th child
87    *
88    * @author Klaus Meffert
89    * @since 3.2
90    */

91   public Class JavaDoc getChildType(IGPProgram a_ind, int a_chromNum) {
92     if (a_chromNum == 0 || a_chromNum == 1) {
93       return m_type;
94     }
95     return CommandGene.VoidClass;
96   }
97 }
98
Popular Tags