From 4ccb3506c19639b410fcd5a50364c834ea153c83 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Thu, 23 Apr 2015 18:42:02 +0200 Subject: Cleanup --- .../main/java/com/camilstaps/rushhour/Board.java | 41 +++++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) (limited to 'app/src/main/java/com/camilstaps/rushhour/Board.java') diff --git a/app/src/main/java/com/camilstaps/rushhour/Board.java b/app/src/main/java/com/camilstaps/rushhour/Board.java index cfd3774..bdfb6d4 100644 --- a/app/src/main/java/com/camilstaps/rushhour/Board.java +++ b/app/src/main/java/com/camilstaps/rushhour/Board.java @@ -19,6 +19,9 @@ public class Board { private DriveListener driveListener; + /** + * Move a car if possible, and call the appropriate listeners + */ private MoveListener moveListener = new MoveListener() { @Override public void onMove(Car car, int offset) { @@ -38,25 +41,53 @@ public class Board { } }; - public Board() { - this(new HashSet()); - } - public Board(Set cars) { for (Car car : cars) { add(car); } } + public Board() { + this(new HashSet()); + } + public void add(Car car) { car.setMoveListener(moveListener); cars.add(car); } + /** + * Add all cars to an existing layout + * RelativeLayout is assumed, although this may work with other Layouts + * @param context + * @param layout + */ public void addToLayout(Context context, ViewGroup layout) { for (Car car : cars) { - layout.addView(car.getImageView(context, (layout.getWidth() - layout.getPaddingLeft() - layout.getPaddingRight()) / DIMENSION)); + layout.addView(car.getImageView( + context, + (layout.getWidth() - layout.getPaddingLeft() - layout.getPaddingRight()) / DIMENSION + )); + } + } + + /** + * True iff the red car can move out without problems + * @return + */ + public boolean isSolved() { + for (int x = DIMENSION - 1; x >= 0; x++) { + for (Car car : cars) { + if (car.occupies(new Coordinate(x, 3))) { + if (car.canMoveHorizontally()) { + return true; + } else { + return false; + } + } + } } + return false; } public void setDriveListener(DriveListener dl) { -- cgit v1.2.3