KickJava   Java API By Example, From Geeks To Geeks.

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


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.gp.*;
13 import org.jgap.*;
14 import org.jgap.gp.impl.*;
15
16 public class PutStone1
17     extends CommandGene {
18   /** String containing the CVS revision. Read out via reflection!*/
19   private final static String JavaDoc CVS_REVISION = "$Revision: 1.1 $";
20
21   private Board m_board;
22
23   private int m_color;
24
25   public PutStone1(final GPConfiguration a_conf, Board a_board, int a_color)
26       throws InvalidConfigurationException {
27     this(a_conf, a_board, a_color, 0, 0);
28   }
29
30   public PutStone1(final GPConfiguration a_conf, Board a_board, int a_color,
31                    int a_subReturnType, int a_subChildType)
32       throws InvalidConfigurationException {
33     super(a_conf, 1, CommandGene.VoidClass, a_subReturnType, a_subChildType);
34     m_board = a_board;
35     m_color = a_color;
36   }
37
38   public String JavaDoc toString() {
39     return "put_stone1(&1)";
40   }
41
42   public void execute_void(ProgramChromosome c, int n, Object JavaDoc[] args) {
43     check(c);
44     int index = c.execute_int(n, 0, args);
45     int x = index / Board.WIDTH;
46     int y = index % Board.HEIGHT;
47     // Put stone on board.
48
// -------------------
49
boolean gameWon = m_board.putStone(x, y, m_color);
50     // If game won, quit GP-program.
51
// -----------------------------
52
if (gameWon) {
53       throw new GameWonException(m_color, "Game won by color " + m_color);
54     }
55   }
56
57   protected void check(ProgramChromosome a_program) {
58     if (m_board.getLastColor() == m_color) {
59       throw new IllegalStateException JavaDoc("Only one stone of a color per round!");
60     }
61   }
62
63   /**
64    * Determines which type a specific child of this command has.
65    *
66    * @param a_ind ignored here
67    * @param a_chromNum index of child
68    * @return type of the a_chromNum'th child
69    *
70    * @author Klaus Meffert
71    * @since 3.2
72    */

73   public Class JavaDoc getChildType(IGPProgram a_ind, int a_chromNum) {
74     return CommandGene.IntegerClass;
75   }
76 }
77
Popular Tags