01: /*
02: * This file is part of JGAP.
03: *
04: * JGAP offers a dual license model containing the LGPL as well as the MPL.
05: *
06: * For licencing information please see the file license.txt included with JGAP
07: * or have a look at the top of class org.jgap.Chromosome which representatively
08: * includes the JGAP license policy applicable for any file delivered with JGAP.
09: */
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 PutStone extends CommandGene {
17: /** String containing the CVS revision. Read out via reflection!*/
18: private final static String CVS_REVISION = "$Revision: 1.2 $";
19:
20: private Board m_board;
21:
22: private int m_color;
23:
24: public PutStone(final GPConfiguration a_conf, Board a_board,
25: int a_color) throws InvalidConfigurationException {
26: this (a_conf, a_board, a_color, 0, null);
27: }
28:
29: public PutStone(final GPConfiguration a_conf, Board a_board,
30: int a_color, int a_subReturnType, int[] a_subChildTypes)
31: throws InvalidConfigurationException {
32: super (a_conf, 2, CommandGene.VoidClass, a_subReturnType,
33: a_subChildTypes);
34: m_board = a_board;
35: m_color = a_color;
36: }
37:
38: public String toString() {
39: return "put_stone(&1, &2)";
40: }
41:
42: public void execute_void(ProgramChromosome c, int n, Object[] args) {
43: check(c);
44: int x = c.execute_int(n, 0, args);
45: int y = c.execute_int(n, 1, args);
46: // Put stone on board.
47: // -------------------
48: boolean gameWon = m_board.putStone(x, y, m_color);
49: // If game won, quit GP-program.
50: // -----------------------------
51: if (gameWon) {
52: throw new GameWonException(m_color, "Game won by color "
53: + m_color);
54: }
55: }
56:
57: protected void check(ProgramChromosome a_program) {
58: if (m_board.getLastColor() == m_color) {
59: throw new IllegalStateException(
60: "Only one stone of a color per round!");
61: }
62: }
63:
64: /**
65: * Determines which type a specific child of this command has.
66: *
67: * @param a_ind ignored here
68: * @param a_chromNum index of child
69: * @return type of the a_chromNum'th child
70: *
71: * @author Klaus Meffert
72: * @since 3.2
73: */
74: public Class getChildType(IGPProgram a_ind, int a_chromNum) {
75: // if (a_chromNum == 0 || a_chromNum == 1) {
76: return CommandGene.IntegerClass;
77: // }
78: }
79: }
|