# HG changeset patch # User Matti Hamalainen # Date 1298347135 -7200 # Node ID 951a4d669af029c232e41a69817ebecb6d1d697a # Parent a5fd4f74a767d1ff90fe73325c6d54a2011a7b43 Initially working path solving algorithm. diff -r a5fd4f74a767 -r 951a4d669af0 game/Engine.java --- a/game/Engine.java Sat Feb 19 03:05:13 2011 +0200 +++ b/game/Engine.java Tue Feb 22 05:58:55 2011 +0200 @@ -17,25 +17,26 @@ import game.*; import javax.sound.sampled.*; + class PathInfo { - public int in, inX, inY, out, outX, outY; - - public PathInfo(int in, int inX, int inY, int out, int outX, int outY) + public int inPoint, inX, inY, outPoint, outX, outY; + + public PathInfo(int inPoint, int inX, int inY, int outPoint, int outX, int outY) { - this.in = in; + this.inPoint = inPoint; this.inX = inX; this.inY = inY; - this.out = out; + this.outPoint = outPoint; this.outX = outX; this.outY = outY; } public void print() { - System.out.print("PathInfo: inP="+in+", inX="+inX+", inY="+inY+"\n"); - System.out.print(" outP="+out+", outX="+outX+", outY="+outY+"\n\n"); + System.out.print("PathInfo: inP="+inPoint+", inX="+inX+", inY="+inY+"\n"); + System.out.print(" outP="+outPoint+", outX="+outX+", outY="+outY+"\n\n"); } } @@ -87,35 +88,74 @@ { } - public boolean hit(float x, float y) + public boolean contains(float x, float y) { return false; } + + public void clicked() + { + } } class IDMButton { - BufferedImage imgUp, imgPressed; - + enum State { FOCUSED, PRESSED, NORMAL } + State state; + BufferedImage imgUp, imgPressed, img; + Point pos; + public IDMButton(float x, float y, String text) { + try + { + ResourceLoader res = new ResourceLoader("graphics/button1_up.png"); + imgUp = ImageIO.read(res.getStream()); + + res = new ResourceLoader("graphics/button1_down.png"); + imgPressed = ImageIO.read(res.getStream()); + } + catch (IOException e) + { + System.out.print(e.getMessage()); + } + + setState(State.NORMAL); } + private void setState(State newState) + { + state = newState; + if (state == State.PRESSED) + img = imgPressed; + else + img = imgUp; + } + public void paint(Graphics2D g) { + g.drawImage(img, pos.x, pos.y, null); } - - public boolean hit(float x, float y) + + public boolean contains(float x, float y) { - return false; + return (x >= pos.x && y >= pos.y && + x < pos.x + img.getWidth() && + y < pos.y + img.getHeight()); + } + + public void clicked() + { } } + class GameBoard { public static final int boardSize = 9; public static final int boardMiddle = 4; Piece[][] board; + public boolean flagGameOver; Piece currPiece; @@ -128,8 +168,8 @@ board[boardMiddle][boardMiddle] = new Piece(PieceType.START); currX = boardMiddle; - currY = boardMiddle - 1; - currPoint = 5; + currY = boardMiddle; + currPoint = 0; pieceFinishTurn(); @@ -175,105 +215,88 @@ currPiece.rotate(dir); } - public PathInfo resolvePath(int startX, int startY, int startPoint, boolean mark) + private void pieceMoveTo(int point) { - int x = startX, y = startY; - int point = startPoint; - Piece curr = board[x][y]; + switch (point) + { + case 0: currY--; break; + case 1: currY--; break; + + case 2: currX++; break; + case 3: currX++; break; + + case 4: currY++; break; + case 5: currY++; break; + + case 6: currX--; break; + case 7: currX--; break; + } + } - do + public void pieceFinishTurn() + { + while (true) { - if (x >= 0 && x < boardSize && y >= 0 && y < boardSize) + System.out.print("x="+currX+", y="+currY+", p="+currPoint+"\n"); + if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize) { - curr = board[x][y]; + Piece curr = board[currX][currY]; + if (curr == null) - break; - + { + currPiece = new Piece(PieceType.ACTIVE); + board[currX][currY] = currPiece; + return; + } + else if (curr.getType() == PieceType.START) { - // Hit center starting piece - return null; + if (currPiece != null) + { + // Hit center starting piece + currPiece.setType(PieceType.LOCKED); + currPiece.setConnectionState(currPiece.getRotatedPoint(currPoint), true); + + flagGameOver = true; + System.out.print("GameOver!\n"); + break; + } + else + { + System.out.print("first piece\n"); + pieceMoveTo(currPoint); + currPiece = new Piece(PieceType.ACTIVE); + board[currX][currY] = currPiece; + return; + } } else { // Mark, if needed - if (mark) - { - curr.setType(PieceType.LOCKED); - curr.setConnectionState(curr.getRotatedPoint(point), true); - } + curr.setType(PieceType.LOCKED); + System.out.print("M1 = "+currPoint+", rotated="+curr.getRotatedPoint(currPoint)+"\n"); + currPoint = curr.getRotatedPoint(curr.getMatchingPoint(currPoint)); + curr.setConnectionState(currPoint, true); - // Get next piece - point = curr.getConnection(curr.getRotatedPoint(point)); - switch (point) - { - case 0: y--; point = 5; break; - case 1: y--; point = 4; break; - - case 2: x++; point = 7; break; - case 3: x++; point = 6; break; - - case 4: y++; point = 1; break; - case 5: y++; point = 0; break; - - case 6: x--; point = 3; break; - case 7: x--; point = 2; break; - } + System.out.print("matchingrotated="+currPoint+"\n"); + + // Move to next position + currPoint = curr.getConnection(currPoint); + System.out.print("conn="+currPoint+"\n"); + currPoint = curr.getAntiRotatedPoint(currPoint); + pieceMoveTo(currPoint); } } else { // Outside of the board - return null; - } - } - while (curr != null); - - return new PathInfo(startPoint, startX, startY, point, x, y); - } - - public void pieceFinishTurn() - { - // Do we have a piece? - if (currPiece != null) - { - // Yes, start resolving path to next piece placement - PathInfo i = resolvePath(currX, currY, currPoint, true); - - if (i != null) - { - currX = i.outX; - currY = i.outY; - currPoint = i.out; + flagGameOver = true; + System.out.print("GameOver!\n"); + break; } } - // Create a new piece - currPiece = new Piece(PieceType.ACTIVE); - - // Find a place for it - if (isEmpty(currX, currY)) - { - // Current position is empty, use it - board[currX][currY] = currPiece; - } - else - { - // Resolve path - PathInfo i = resolvePath(currX, currY, currPoint, true); - if (i != null) - { - // Path found, place the piece - board[currX][currY] = currPiece; - } - else - { - // Path ended up center/gameboard walls - it's game over, man - System.out.print("pieceFinishTurn(): Game Over!\n"); - flagGameOver = true; - } - } - } + } } @@ -324,7 +347,7 @@ System.out.print("Could not initialize fonts.\n"); } - musa = smgr.getSound("sounds/gamemusic.wav"); +// musa = smgr.getSound("sounds/gamemusic.wav"); // placed = smgr.getSound("sounds/placed.wav"); } catch (IOException e) diff -r a5fd4f74a767 -r 951a4d669af0 game/Piece.java --- a/game/Piece.java Sat Feb 19 03:05:13 2011 +0200 +++ b/game/Piece.java Tue Feb 22 05:58:55 2011 +0200 @@ -91,10 +91,37 @@ public int getRotatedPoint(int in) { + int point = (in - (currRotation * 2)) % 8; + if (point < 0) point = 8 + point; + return point; + } + + public int getAntiRotatedPoint(int in) + { int point = (in + (currRotation * 2)) % 8; if (point < 0) point = 8 + point; + System.out.print("getAntiRotatedPoint("+ in +"): rot="+currRotation+", point="+point+"\n"); return point; } + + public int getMatchingPoint(int point) + { + switch (point) + { + case 0: return 5; + case 1: return 4; + + case 2: return 7; + case 3: return 6; + + case 4: return 1; + case 5: return 0; + + case 6: return 3; + case 7: return 2; + } + return -1; + } public void setConnectionState(int point, boolean state) {