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 toprivate 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); } }
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; }