KickJava   Java API By Example, From Geeks To Geeks.

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


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 TransferBoardToMemory
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_startMemoryIndex;
24
25   public TransferBoardToMemory(final GPConfiguration a_conf, Board a_board,
26                                int a_startMemoryIndex)
27       throws InvalidConfigurationException {
28     this(a_conf, a_board, a_startMemoryIndex, 0);
29   }
30
31   public TransferBoardToMemory(final GPConfiguration a_conf, Board a_board,
32                                int a_startMemoryIndex, int a_subReturnType)
33       throws InvalidConfigurationException {
34     super(a_conf, 0, CommandGene.VoidClass, a_subReturnType, null);
35     m_board = a_board;
36     m_startMemoryIndex = a_startMemoryIndex;
37   }
38
39   public String JavaDoc toString() {
40     return "transfer_Board_to_Mem(" + m_startMemoryIndex + ")";
41   }
42
43   /**
44    * @return textual name of this command
45    *
46    * @author Klaus Meffert
47    * @since 3.2
48    */

49   public String JavaDoc getName() {
50     return "Transfer Board to Memory (index " + m_startMemoryIndex + ")";
51   }
52
53   /**
54    * Executes the command.
55    *
56    * @param c ProgramChromosome
57    * @param n ignored here
58    * @param args ignored here
59    *
60    * @author Klaus Meffert
61    * @since 3.2
62    */

63   public void execute_void(ProgramChromosome c, int n, Object JavaDoc[] args) {
64     check(c);
65     int index = m_startMemoryIndex;
66     for (int x = 0; x < Board.WIDTH; x++) {
67       for (int y = 0; y < Board.HEIGHT; y++) {
68         int boardValue = m_board.readField(x+1, y+1);
69         getGPConfiguration().storeIndexedMemory(index++, new Integer JavaDoc(boardValue));
70       }
71     }
72   }
73 }
74
Popular Tags