E:\Src\java\netBeans\Puzzle\JTurm\src\jturm\Puzzle.java
  1 package jturm;
  2 
  3 /*
  4  * To change this template, choose Tools | Templates and open the template in
  5  * the editor.
  6  */
  7 /**
  8  *
  9  * @author Rolf
 10  */
 11 public class Puzzle {
 12 
 13     int height = 0;
 14     short p[] = null;
 15     char c = '?';
 16 
 17     //--------------------------------------------------------------------------
 18     public Puzzle(int height) {
 19         this.height = height;
 20         p = new short[height];           // basic Puzzle
 21         for (int x = 0; x < height; x++) {
 22             p[x] = 0;
 23         }
 24     }
 25     //--------------------------------------------------------------------------
 26 
 27     private short val(short v) {
 28         switch (v) {
 29             case 0:
 30                 return 1;
 31             case 1:
 32                 return 2;
 33             case 2:
 34                 return 4;
 35             case 3:
 36                 return 8;
 37             case 4:
 38                 return 16;
 39             case 5:
 40                 return 32;
 41             case 6:
 42                 return 64;
 43             case 7:
 44                 return 128;
 45             case 8:
 46                 return 256;
 47             case 9:
 48                 return 512;
 49             case 10:
 50                 return 1024;
 51             case 11:
 52                 return 2048;
 53             case 12:
 54                 return 4096;
 55         }
 56         return 0;
 57     }
 58     //--------------------------------------------------------------------------
 59 
 60     public void init(char c, int v[]) {
 61         this.c = c;
 62         for (int i = 0; i < v.length; i++) {
 63             short h = (short) (v[i] / 13);      // array Höhe 0,1,2,3,4
 64             short n = (short) (v[i] % 13);      // array value
 65 
 66             if (h < height) {
 67                 p[h] += val(n);
 68             } else {
 69                 // this is an impossible Puzzle for the container
 70                 for (int x = 0; x < height; x++) {
 71                     p[x] = 0;
 72                 }
 73                 this.c = '?';
 74                 return;
 75             }
 76         }
 77 
 78     }
 79     //--------------------------------------------------------------------------
 80 
 81     public int getFirstFree() {
 82         for (int x = 0; x < height; x++) {
 83             for (short i = 0; i <= 12; i++) {
 84                 if ((p[x] & val(i)) == 0) {
 85                     return i + x * 13;
 86                 }
 87             }
 88         }
 89         return (short) -1;     // ERROR CASE / Solution
 90     }
 91 
 92     //--------------------------------------------------------------------------
 93     public int getLastBusy() {
 94         for (int x = height - 1; x >= 0; x--) {
 95             for (short i = 12; i >= 0; i--) {
 96                 if ((p[x] & val(i)) > 0) {
 97                     return i + x * 13;
 98                 }
 99             }
100         }
101         return (short) -1;     // ERROR CASE
102     }
103 
104     //--------------------------------------------------------------------------
105     public int getFirstBusy() {
106         for (int x = 0; x < height; x++) {
107             for (short i = 0; i <= 12; i++) {
108                 if ((p[x] & val(i)) > 0) {
109                     return i + x * 13;
110                 }
111             }
112         }
113         return (short) -1;     // ERROR CASE
114     }
115 
116     //--------------------------------------------------------------------------
117     public short invert(int x, int width) {
118         short s = 0;
119         boolean b[] = new boolean[width + 1];
120         for (short i = 1; i <= width; i++) {
121             b[i] = (p[x] & val(i)) > 0;
122         }
123         for (x = 1; x <= width; x++) {
124             if (b[x]) {
125                 s += val((short) (width - x + 1));
126             }
127         }
128         return s;
129     }
130     //--------------------------------------------------------------------------
131 
132     public char[][] fill(char[][] s) {
133         for (int x = height - 1; x >= 0; x--) {
134             for (short b = 0; b < 13; b++) {
135                 if ((p[x] & val(b)) != 0) {
136                     s[x][b] = c;
137                 }
138             }
139         }
140         return s;
141     }
142 
143     //--------------------------------------------------------------------------
144     public void disp() {
145         for (int x = height - 1; x >= 0; x--) {
146             for (short b = 0; b < 13; b++) {
147                 if ((p[x] & val(b)) == 0) {
148                     System.out.print(".");
149                 } else {
150                     System.out.print(c);
151                 }
152                 if (b == 0) {
153                     System.out.print("\t");
154                 }
155             }
156             System.out.println();
157         }
158         System.out.println();
159     }
160     //--------------------------------------------------------------------------
161 
162     public boolean add(Puzzle a) {
163         // try to add a puzzle to current Puzzle definition and return success
164 
165         //
166         //  p   a result 
167         //  0   0   0 
168         //  0   1   1  
169         //  1   0   1 
170         //  1   1   1       (ERROR)
171 
172         for (int x = 0; x < height; x++) {
173             if ((p[x] & a.p[x]) != 0) {
174                 return false;
175             }
176         }
177         for (int x = 0; x < height; x++) {
178             p[x] = (short) (p[x] | a.p[x]);         // logical OR
179         }
180         return true;
181     }
182     //--------------------------------------------------------------------------
183 
184     public boolean rem(Puzzle a) {
185         // try to remove a puzzle from current Puzzle definition
186         //
187         //  p   a result 
188         //  0   0   0 
189         //  0   1   1 (ERROR) 
190         //  1   0   1 
191         //  1   1   0
192 
193 
194         for (int x = 0; x < height; x++) {
195             p[x] = (short) (p[x] ^ a.p[x]);         // logical EXOR
196         }
197         return true;
198     }
199 }
200