My Problem
I am attempting to recreate the board game "Stratego" but I am having trouble getting the available spots for each piece to show up. So far I have pieces on teams that take turns moving, and after the pieces move they snap to the spot and it becomes the other teams turn. I would like to make it so that when you are holding a piece and getting ready to drop it, only certain spots are visible. The criteria for these spots to be visible are:- The spot does not currently have a piece on it.
- The spot is one "space" on the board away from the currently moving piece's original location. (not diagonal)
My Code
Code for the spots that the pieces snap to1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | private GreenfootImage invisible = new GreenfootImage("OneWhitePixel.png"); private GreenfootImage visible = new GreenfootImage("Spot.png"); private int touching; /** * Act - do whatever the AvailableSpot wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkTouching(); checkInvisible(); } public void checkTouching() { if(isTouching(RedPiece.class)) { touching = 1; } else if(isTouching(BluePiece.class)) { touching = 2; } else { touching = 0; } } public void checkInvisible() { if(PieceMover.getTurn() && PieceMover.getPieceColor() && touching == 1) { getNeighbours(30, false, AvailableSpot.class).get(0).setImage(visible); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | private static boolean redTurn = true; private static boolean pickedRedPiece; public PieceMover() { } /** * Act - do whatever the PieceMover wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { decideToDrag(); } public void decideToDrag() { if(this instanceof RedPiece && redTurn) { dragPiece(); pickedRedPiece = true; } if(this instanceof BluePiece && !redTurn) { dragPiece(); pickedRedPiece = false; } } public void dragPiece() { if(Greenfoot.mouseDragged(this)) { MouseInfo mouse = Greenfoot.getMouseInfo(); setLocation(mouse.getX(), mouse.getY()); } if(Greenfoot.mouseDragEnded(this)) { Actor choice = getOneIntersectingObject(AvailableSpot.class); if(choice != null) { setLocation(choice.getX(), choice.getY()); redTurn = !redTurn; } } } public static boolean getPieceColor() { return pickedRedPiece; } public static boolean getTurn() { return redTurn; } |
