001: /*
002: * This file is part of JGAP.
003: *
004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
005: *
006: * For licensing information please see the file license.txt included with JGAP
007: * or have a look at the top of class org.jgap.Chromosome which representatively
008: * includes the JGAP license policy applicable for any file delivered with JGAP.
009: */
010: package examples.gp.tictactoe;
011:
012: import org.jgap.gp.*;
013: import org.jgap.*;
014: import org.jgap.gp.impl.*;
015:
016: public class CountStones extends CommandGene {
017: /** String containing the CVS revision. Read out via reflection!*/
018: private final static String CVS_REVISION = "$Revision: 1.3 $";
019:
020: private Board m_board;
021:
022: private String m_memoryNameBase;
023:
024: private int m_color;
025:
026: public CountStones(final GPConfiguration a_conf, Board a_board,
027: int a_color, String a_memoryNameBase)
028: throws InvalidConfigurationException {
029: this (a_conf, a_board, a_color, a_memoryNameBase, 0);
030: }
031:
032: public CountStones(final GPConfiguration a_conf, Board a_board,
033: int a_color, String a_memoryNameBase, int a_subReturnType)
034: throws InvalidConfigurationException {
035: super (a_conf, 0, CommandGene.VoidClass, a_subReturnType, null);
036: m_board = a_board;
037: m_memoryNameBase = a_memoryNameBase;
038: m_color = a_color;
039: }
040:
041: public String toString() {
042: return "Count Stones(" + m_color + "," + m_memoryNameBase + ")";
043: }
044:
045: /**
046: * @return textual name of this command
047: *
048: * @author Klaus Meffert
049: * @since 3.2
050: */
051: public String getName() {
052: return "Count Stones";
053: }
054:
055: public void execute_void(ProgramChromosome c, int n, Object[] args) {
056: check(c);
057: // rows
058: for (int i = 0; i < Board.WIDTH; i++) {
059: readRow(i, m_memoryNameBase);
060: }
061: // columns
062: for (int i = 0; i < Board.HEIGHT; i++) {
063: readCol(i, m_memoryNameBase);
064: }
065: // diagonals
066: readDia(0, m_memoryNameBase);
067: readDia(1, m_memoryNameBase);
068: }
069:
070: private void readRow(int i, String a_baseName) {
071: String memoryName = a_baseName + "r" + i;
072: int count = 0;
073: for (int x = 0; x < Board.WIDTH; x++) {
074: if (m_board.readField(x + 1, i + 1) == m_color) {
075: count++;
076: }
077: }
078: store(memoryName, count);
079: }
080:
081: private void readCol(int i, String a_baseName) {
082: String memoryName = a_baseName + "c" + i;
083: int count = 0;
084: for (int y = 0; y < Board.HEIGHT; y++) {
085: if (m_board.readField(i + 1, y + 1) == m_color) {
086: count++;
087: }
088: }
089: store(memoryName, count);
090: }
091:
092: private void readDia(int index, String a_baseName) {
093: String memoryName = a_baseName + "d" + index;
094: int count = 0;
095: int x;
096: int y;
097: int increment;
098: if (index == 0) {
099: increment = 1;
100: x = 1;
101: y = 1;
102: } else {
103: increment = -1;
104: x = Board.WIDTH;
105: y = 1;
106: }
107: for (int i = 0; i < Board.HEIGHT; i++) {
108: if (m_board.readField(x, y) == m_color) {
109: count++;
110: }
111: y++;
112: x = x + increment;
113: }
114: store(memoryName, count);
115: }
116:
117: private void store(String memoryName, int a_count) {
118: getGPConfiguration().storeInMemory(memoryName,
119: new Integer(a_count));
120: }
121: }
|