KickJava   Java API By Example, From Geeks To Geeks.

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


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 CountStones
17     extends CommandGene {
18   /** String containing the CVS revision. Read out via reflection!*/
19   private final static String JavaDoc CVS_REVISION = "$Revision: 1.2 $";
20
21   private Board m_board;
22
23   private String JavaDoc m_memoryNameBase;
24
25   private int m_color;
26
27   public CountStones(final GPConfiguration a_conf, Board a_board, int a_color,
28                      String JavaDoc a_memoryNameBase)
29       throws InvalidConfigurationException {
30     this(a_conf, a_board, a_color, a_memoryNameBase, 0);
31   }
32
33   public CountStones(final GPConfiguration a_conf, Board a_board, int a_color,
34                      String JavaDoc a_memoryNameBase, int a_subReturnType)
35       throws InvalidConfigurationException {
36     super(a_conf, 0, CommandGene.VoidClass, a_subReturnType, null);
37     m_board = a_board;
38     m_memoryNameBase = a_memoryNameBase;
39     m_color = a_color;
40   }
41
42   public String JavaDoc toString() {
43     return "Count Stones(" + m_color + "," + m_memoryNameBase + ")";
44   }
45
46   /**
47    * @return textual name of this command
48    *
49    * @author Klaus Meffert
50    * @since 3.2
51    */

52   public String JavaDoc getName() {
53     return "Count Stones";
54   }
55
56   public void execute_void(ProgramChromosome c, int n, Object JavaDoc[] args) {
57     check(c);
58     // rows
59
for (int i = 0; i < Board.WIDTH; i++) {
60       readRow(i, m_memoryNameBase);
61     }
62     // columns
63
for (int i = 0; i < Board.HEIGHT; i++) {
64       readCol(i, m_memoryNameBase);
65     }
66     // diagonals
67
readDia(0, m_memoryNameBase);
68     readDia(1, m_memoryNameBase);
69   }
70
71   private void readRow(int i, String JavaDoc a_baseName) {
72     String JavaDoc memoryName = a_baseName + "r" + i;
73     int count = 0;
74     for (int x = 0; x < Board.WIDTH; x++) {
75       if (m_board.readField(x + 1, i + 1) == m_color) {
76         count++;
77       }
78     }
79     store(memoryName, count);
80   }
81
82   private void readCol(int i, String JavaDoc a_baseName) {
83     String JavaDoc memoryName = a_baseName + "c" + i;
84     int count = 0;
85     for (int y = 0; y < Board.HEIGHT; y++) {
86       if (m_board.readField(i + 1, y + 1) == m_color) {
87         count++;
88       }
89     }
90     store(memoryName, count);
91   }
92
93   private void readDia(int index, String JavaDoc a_baseName) {
94     String JavaDoc memoryName = a_baseName + "d" + index;
95     int count = 0;
96     int x;
97     int y;
98     int increment;
99     if (index == 0) {
100       increment = 1;
101       x = 1;
102       y = 1;
103     }
104     else {
105       increment = -1;
106       x = Board.WIDTH;
107       y = 1;
108     }
109     for (int i = 0; i < Board.HEIGHT; i++) {
110       if (m_board.readField(x, y) == m_color) {
111         count++;
112       }
113       y++;
114       x = x + increment;
115     }
116     store(memoryName, count);
117   }
118
119   private void store(String JavaDoc memoryName, int a_count) {
120     getGPConfiguration().storeInMemory(memoryName, new Integer JavaDoc(a_count));
121   }
122 }
123
Popular Tags