1 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 19 private final static String CVS_REVISION = "$Revision: 1.2 $"; 20 21 private Board m_board; 22 23 private String m_memoryNameBase; 24 25 private int m_color; 26 27 public CountStones(final GPConfiguration a_conf, Board a_board, int a_color, 28 String 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 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 toString() { 43 return "Count Stones(" + m_color + "," + m_memoryNameBase + ")"; 44 } 45 46 52 public String getName() { 53 return "Count Stones"; 54 } 55 56 public void execute_void(ProgramChromosome c, int n, Object [] args) { 57 check(c); 58 for (int i = 0; i < Board.WIDTH; i++) { 60 readRow(i, m_memoryNameBase); 61 } 62 for (int i = 0; i < Board.HEIGHT; i++) { 64 readCol(i, m_memoryNameBase); 65 } 66 readDia(0, m_memoryNameBase); 68 readDia(1, m_memoryNameBase); 69 } 70 71 private void readRow(int i, String a_baseName) { 72 String 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 a_baseName) { 83 String 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 a_baseName) { 94 String 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 memoryName, int a_count) { 120 getGPConfiguration().storeInMemory(memoryName, new Integer (a_count)); 121 } 122 } 123 | Popular Tags |