KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > samples > solitaire > Configuration


1 package org.objectweb.jac.samples.solitaire;
2
3 import org.objectweb.jac.lib.java.util.Vector;
4 //import java.util.Vector;
5
import org.objectweb.jac.aspects.naming.*;
6
7 public class Configuration {
8    public int nBalls ;
9    public byte[][] game ;
10    public Configuration parent ;
11    public Vector children;
12    static java.util.Hashtable JavaDoc instancesCounts=new java.util.Hashtable JavaDoc();
13    public static int getInstanceCount(Integer JavaDoc level) {
14       Integer JavaDoc count = (Integer JavaDoc)instancesCounts.get(level);
15       if( count == null ) {
16          instancesCounts.put(level,new Integer JavaDoc(0));
17          return 0;
18       }
19       return count.intValue();
20    }
21    public static void incr(Integer JavaDoc level) {
22       Integer JavaDoc count = (Integer JavaDoc)instancesCounts.get(level);
23       if( count == null ) {
24          instancesCounts.put(level,new Integer JavaDoc(1));
25          return;
26       }
27       instancesCounts.put(level,new Integer JavaDoc(count.intValue()+1));
28    }
29
30    public Configuration(int nBalls, byte[][] game, Configuration parent){
31       this.nBalls = nBalls;
32       this.game = game;
33       this.parent = parent;
34       //incr(new Integer(nBalls));
35
}
36    public void printTree() {
37       java.util.Iterator JavaDoc it;
38       it = children.iterator();
39       System.out.println("---- begin");
40       while(it.hasNext()) {
41          Configuration conf = (Configuration)it.next();
42          System.out.println(conf);
43       }
44       System.out.println("---- end");
45       it = children.iterator();
46       while(it.hasNext()) {
47          Configuration conf = (Configuration)it.next();
48          conf.printTree();
49       }
50    }
51
52    public void printGame() {
53       System.out.println( this.toString() );
54    }
55
56    public String JavaDoc toString() {
57       return toString(game);
58    }
59    public String JavaDoc toString(byte[][] game) {
60       
61       if ( game == null ) return "";
62       String JavaDoc result = "\n";
63       for(int i = 2 ; i<9 ;i++) {
64          for(int j = 2 ; j<9 ; j++) {
65             if (game[i][j] == 1)
66                result = result + "O";
67             if (game[i][j] == 0)
68                result = result + ".";
69             if (game[i][j] == 2)
70                result = result + " ";
71          }
72          result = result + "\n";
73       }
74       return result;
75    }
76
77    public void createChildren () {
78       children = new Vector();
79       Configuration newConf=null;
80       for(int i = 2 ; i<9 ;i++) {
81          for(int j = 2 ; j<9 ; j++) {
82             if (game[i][j] == 0) {
83                // System.out.println("vide pour " + i + ":" + j);
84
// System.out.println( this );
85
if (game[i-2][j] == 1 && game[i-1][j] == 1) {
86                   byte[][] copy = new byte[game.length][];
87                   for(int k = 0 ; k < game.length ; k++)
88                      copy[k] = (byte[]) game[k].clone();
89                         
90                   copy[i][j] = 1;
91                   copy[i-2][j] = 0;
92                   copy[i-1][j] = 0;
93                   //NamingAC.forceName("configuration_"+(nBalls-1)+"_"+
94
// getInstanceCount(new Integer(nBalls-1)));
95
newConf=new Configuration(nBalls - 1, copy, this);
96                   children.add( newConf );
97                   newConf.createChildren();
98                }
99                if (game[i+2][j] == 1 && game[i+1][j] == 1) {
100                   byte[][] copy = new byte[game.length][];
101                   for(int k = 0 ; k < game.length ; k++)
102                      copy[k] = (byte[]) game[k].clone();
103                   copy[i][j] = 1;
104                   copy[i+2][j] = 0;
105                   copy[i+1][j] = 0;
106                   //NamingAC.forceName("configuration_"+(nBalls-1)+"_"+
107
// getInstanceCount(new Integer(nBalls-1)));
108
newConf=new Configuration(nBalls - 1, copy, this);
109                   children.add( newConf );
110                   newConf.createChildren();
111                }
112
113                if (game[i][j-2] == 1 && game[i][j-1] == 1) {
114                   byte[][] copy = new byte[game.length][];
115                   for(int k = 0 ; k < game.length ; k++)
116                      copy[k] = (byte[]) game[k].clone();
117                   copy[i][j] = 1;
118                   copy[i][j-2] = 0;
119                   copy[i][j-1] = 0;
120                   //NamingAC.forceName("configuration_"+(nBalls-1)+"_"+
121
// getInstanceCount(new Integer(nBalls-1)));
122
newConf=new Configuration(nBalls - 1, copy, this);
123                   children.add( newConf );
124                   newConf.createChildren();
125                }
126                if (game[i][j+2] == 1 && game[i][j+1] == 1) {
127                   byte[][] copy = new byte[game.length][];
128                   for(int k = 0 ; k < game.length ; k++)
129                      copy[k] = (byte[]) game[k].clone();
130                   copy[i][j] = 1;
131                   copy[i][j+2] = 0;
132                   copy[i][j+1] = 0;
133                   //NamingAC.forceName("configuration_"+(nBalls-1)+"_"+
134
// getInstanceCount(new Integer(nBalls-1)));
135
newConf=new Configuration(nBalls - 1, copy, this);
136                   children.add( newConf );
137                   newConf.createChildren();
138                }
139             }
140          }
141       }
142       // System.out.println( result);
143
// return result;
144
}
145
146 }
147
148
149
150
Popular Tags