1 package org.objectweb.jac.samples.solitaire; 2 3 import org.objectweb.jac.lib.java.util.Vector; 4 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 instancesCounts=new java.util.Hashtable (); 13 public static int getInstanceCount(Integer level) { 14 Integer count = (Integer )instancesCounts.get(level); 15 if( count == null ) { 16 instancesCounts.put(level,new Integer (0)); 17 return 0; 18 } 19 return count.intValue(); 20 } 21 public static void incr(Integer level) { 22 Integer count = (Integer )instancesCounts.get(level); 23 if( count == null ) { 24 instancesCounts.put(level,new Integer (1)); 25 return; 26 } 27 instancesCounts.put(level,new Integer (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 } 36 public void printTree() { 37 java.util.Iterator 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 toString() { 57 return toString(game); 58 } 59 public String toString(byte[][] game) { 60 61 if ( game == null ) return ""; 62 String 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 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 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 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 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 newConf=new Configuration(nBalls - 1, copy, this); 136 children.add( newConf ); 137 newConf.createChildren(); 138 } 139 } 140 } 141 } 142 } 145 146 } 147 148 149 150 | Popular Tags |