comparison game/Engine.java @ 42:951a4d669af0

Initially working path solving algorithm.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 22 Feb 2011 05:58:55 +0200
parents a5fd4f74a767
children 9664bbb9d613
comparison
equal deleted inserted replaced
41:a5fd4f74a767 42:951a4d669af0
15 import java.util.*; 15 import java.util.*;
16 import java.io.*; 16 import java.io.*;
17 import game.*; 17 import game.*;
18 import javax.sound.sampled.*; 18 import javax.sound.sampled.*;
19 19
20
20 class PathInfo 21 class PathInfo
21 { 22 {
22 public int in, inX, inY, out, outX, outY; 23 public int inPoint, inX, inY, outPoint, outX, outY;
23 24
24 public PathInfo(int in, int inX, int inY, int out, int outX, int outY) 25 public PathInfo(int inPoint, int inX, int inY, int outPoint, int outX, int outY)
25 { 26 {
26 this.in = in; 27 this.inPoint = inPoint;
27 this.inX = inX; 28 this.inX = inX;
28 this.inY = inY; 29 this.inY = inY;
29 30
30 this.out = out; 31 this.outPoint = outPoint;
31 this.outX = outX; 32 this.outX = outX;
32 this.outY = outY; 33 this.outY = outY;
33 } 34 }
34 35
35 public void print() 36 public void print()
36 { 37 {
37 System.out.print("PathInfo: inP="+in+", inX="+inX+", inY="+inY+"\n"); 38 System.out.print("PathInfo: inP="+inPoint+", inX="+inX+", inY="+inY+"\n");
38 System.out.print(" outP="+out+", outX="+outX+", outY="+outY+"\n\n"); 39 System.out.print(" outP="+outPoint+", outX="+outX+", outY="+outY+"\n\n");
39 } 40 }
40 } 41 }
41 42
42 /* 43 /*
43 class AnimatedElement 44 class AnimatedElement
85 86
86 public void paint(Graphics2D g) 87 public void paint(Graphics2D g)
87 { 88 {
88 } 89 }
89 90
90 public boolean hit(float x, float y) 91 public boolean contains(float x, float y)
91 { 92 {
92 return false; 93 return false;
94 }
95
96 public void clicked()
97 {
93 } 98 }
94 } 99 }
95 100
96 class IDMButton 101 class IDMButton
97 { 102 {
98 BufferedImage imgUp, imgPressed; 103 enum State { FOCUSED, PRESSED, NORMAL }
99 104 State state;
105 BufferedImage imgUp, imgPressed, img;
106 Point pos;
107
100 public IDMButton(float x, float y, String text) 108 public IDMButton(float x, float y, String text)
101 { 109 {
102 } 110 try
103 111 {
112 ResourceLoader res = new ResourceLoader("graphics/button1_up.png");
113 imgUp = ImageIO.read(res.getStream());
114
115 res = new ResourceLoader("graphics/button1_down.png");
116 imgPressed = ImageIO.read(res.getStream());
117 }
118 catch (IOException e)
119 {
120 System.out.print(e.getMessage());
121 }
122
123 setState(State.NORMAL);
124 }
125
126 private void setState(State newState)
127 {
128 state = newState;
129 if (state == State.PRESSED)
130 img = imgPressed;
131 else
132 img = imgUp;
133 }
134
104 public void paint(Graphics2D g) 135 public void paint(Graphics2D g)
105 { 136 {
106 } 137 g.drawImage(img, pos.x, pos.y, null);
107 138 }
108 public boolean hit(float x, float y) 139
109 { 140 public boolean contains(float x, float y)
110 return false; 141 {
142 return (x >= pos.x && y >= pos.y &&
143 x < pos.x + img.getWidth() &&
144 y < pos.y + img.getHeight());
145 }
146
147 public void clicked()
148 {
111 } 149 }
112 } 150 }
151
113 152
114 class GameBoard 153 class GameBoard
115 { 154 {
116 public static final int boardSize = 9; 155 public static final int boardSize = 9;
117 public static final int boardMiddle = 4; 156 public static final int boardMiddle = 4;
118 Piece[][] board; 157 Piece[][] board;
158
119 public boolean flagGameOver; 159 public boolean flagGameOver;
120 160
121 Piece currPiece; 161 Piece currPiece;
122 int currX, currY, currPoint; 162 int currX, currY, currPoint;
123 163
126 board = new Piece[boardSize][boardSize]; 166 board = new Piece[boardSize][boardSize];
127 167
128 board[boardMiddle][boardMiddle] = new Piece(PieceType.START); 168 board[boardMiddle][boardMiddle] = new Piece(PieceType.START);
129 169
130 currX = boardMiddle; 170 currX = boardMiddle;
131 currY = boardMiddle - 1; 171 currY = boardMiddle;
132 currPoint = 5; 172 currPoint = 0;
133 173
134 pieceFinishTurn(); 174 pieceFinishTurn();
135 175
136 flagGameOver = false; 176 flagGameOver = false;
137 } 177 }
173 { 213 {
174 if (currPiece != null) 214 if (currPiece != null)
175 currPiece.rotate(dir); 215 currPiece.rotate(dir);
176 } 216 }
177 217
178 public PathInfo resolvePath(int startX, int startY, int startPoint, boolean mark) 218 private void pieceMoveTo(int point)
179 { 219 {
180 int x = startX, y = startY; 220 switch (point)
181 int point = startPoint; 221 {
182 Piece curr = board[x][y]; 222 case 0: currY--; break;
183 223 case 1: currY--; break;
184 do 224
185 { 225 case 2: currX++; break;
186 if (x >= 0 && x < boardSize && y >= 0 && y < boardSize) 226 case 3: currX++; break;
227
228 case 4: currY++; break;
229 case 5: currY++; break;
230
231 case 6: currX--; break;
232 case 7: currX--; break;
233 }
234 }
235
236 public void pieceFinishTurn()
237 {
238 while (true)
239 {
240 System.out.print("x="+currX+", y="+currY+", p="+currPoint+"\n");
241 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize)
187 { 242 {
188 curr = board[x][y]; 243 Piece curr = board[currX][currY];
244
189 if (curr == null) 245 if (curr == null)
190 break; 246 {
191 247 currPiece = new Piece(PieceType.ACTIVE);
248 board[currX][currY] = currPiece;
249 return;
250 }
251 else
192 if (curr.getType() == PieceType.START) 252 if (curr.getType() == PieceType.START)
193 { 253 {
194 // Hit center starting piece 254 if (currPiece != null)
195 return null; 255 {
256 // Hit center starting piece
257 currPiece.setType(PieceType.LOCKED);
258 currPiece.setConnectionState(currPiece.getRotatedPoint(currPoint), true);
259
260 flagGameOver = true;
261 System.out.print("GameOver!\n");
262 break;
263 }
264 else
265 {
266 System.out.print("first piece\n");
267 pieceMoveTo(currPoint);
268 currPiece = new Piece(PieceType.ACTIVE);
269 board[currX][currY] = currPiece;
270 return;
271 }
196 } 272 }
197 else 273 else
198 { 274 {
199 // Mark, if needed 275 // Mark, if needed
200 if (mark) 276 curr.setType(PieceType.LOCKED);
201 { 277 System.out.print("M1 = "+currPoint+", rotated="+curr.getRotatedPoint(currPoint)+"\n");
202 curr.setType(PieceType.LOCKED); 278 currPoint = curr.getRotatedPoint(curr.getMatchingPoint(currPoint));
203 curr.setConnectionState(curr.getRotatedPoint(point), true); 279 curr.setConnectionState(currPoint, true);
204 }
205 280
206 // Get next piece 281 System.out.print("matchingrotated="+currPoint+"\n");
207 point = curr.getConnection(curr.getRotatedPoint(point)); 282
208 switch (point) 283 // Move to next position
209 { 284 currPoint = curr.getConnection(currPoint);
210 case 0: y--; point = 5; break; 285 System.out.print("conn="+currPoint+"\n");
211 case 1: y--; point = 4; break; 286 currPoint = curr.getAntiRotatedPoint(currPoint);
212 287 pieceMoveTo(currPoint);
213 case 2: x++; point = 7; break;
214 case 3: x++; point = 6; break;
215
216 case 4: y++; point = 1; break;
217 case 5: y++; point = 0; break;
218
219 case 6: x--; point = 3; break;
220 case 7: x--; point = 2; break;
221 }
222 } 288 }
223 } 289 }
224 else 290 else
225 { 291 {
226 // Outside of the board 292 // Outside of the board
227 return null;
228 }
229 }
230 while (curr != null);
231
232 return new PathInfo(startPoint, startX, startY, point, x, y);
233 }
234
235 public void pieceFinishTurn()
236 {
237 // Do we have a piece?
238 if (currPiece != null)
239 {
240 // Yes, start resolving path to next piece placement
241 PathInfo i = resolvePath(currX, currY, currPoint, true);
242
243 if (i != null)
244 {
245 currX = i.outX;
246 currY = i.outY;
247 currPoint = i.out;
248 }
249 }
250
251 // Create a new piece
252 currPiece = new Piece(PieceType.ACTIVE);
253
254 // Find a place for it
255 if (isEmpty(currX, currY))
256 {
257 // Current position is empty, use it
258 board[currX][currY] = currPiece;
259 }
260 else
261 {
262 // Resolve path
263 PathInfo i = resolvePath(currX, currY, currPoint, true);
264 if (i != null)
265 {
266 // Path found, place the piece
267 board[currX][currY] = currPiece;
268 }
269 else
270 {
271 // Path ended up center/gameboard walls - it's game over, man
272 System.out.print("pieceFinishTurn(): Game Over!\n");
273 flagGameOver = true; 293 flagGameOver = true;
274 } 294 System.out.print("GameOver!\n");
275 } 295 break;
276 } 296 }
297 }
298
299 }
277 } 300 }
278 301
279 302
280 public class Engine extends JPanel 303 public class Engine extends JPanel
281 implements Runnable, KeyListener, MouseListener 304 implements Runnable, KeyListener, MouseListener
322 catch (FontFormatException e) 345 catch (FontFormatException e)
323 { 346 {
324 System.out.print("Could not initialize fonts.\n"); 347 System.out.print("Could not initialize fonts.\n");
325 } 348 }
326 349
327 musa = smgr.getSound("sounds/gamemusic.wav"); 350 // musa = smgr.getSound("sounds/gamemusic.wav");
328 // placed = smgr.getSound("sounds/placed.wav"); 351 // placed = smgr.getSound("sounds/placed.wav");
329 } 352 }
330 catch (IOException e) 353 catch (IOException e)
331 { 354 {
332 JOptionPane.showMessageDialog(null, 355 JOptionPane.showMessageDialog(null,