aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/com/camilstaps/rushhour/Board.java
diff options
context:
space:
mode:
authorCamil Staps2015-04-16 18:23:27 +0200
committerCamil Staps2015-04-16 18:23:27 +0200
commit8b2061b85722163de3b9e5f47534d7869b16239e (patch)
treec82d3bf97ed95ec4e8719146bd6dfaed3d1da8f6 /app/src/main/java/com/camilstaps/rushhour/Board.java
Initial commit
Diffstat (limited to 'app/src/main/java/com/camilstaps/rushhour/Board.java')
-rw-r--r--app/src/main/java/com/camilstaps/rushhour/Board.java55
1 files changed, 55 insertions, 0 deletions
diff --git a/app/src/main/java/com/camilstaps/rushhour/Board.java b/app/src/main/java/com/camilstaps/rushhour/Board.java
new file mode 100644
index 0000000..7ea52de
--- /dev/null
+++ b/app/src/main/java/com/camilstaps/rushhour/Board.java
@@ -0,0 +1,55 @@
+package com.camilstaps.rushhour;
+
+import android.content.Context;
+import android.util.Log;
+import android.widget.RelativeLayout;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Created by camilstaps on 16-4-15.
+ */
+public class Board {
+
+ Set<Car> cars = new HashSet<>();
+
+ public static final int DIMENSION = 6;
+
+ private MoveListener moveListener = new MoveListener() {
+ @Override
+ public void onMove(Car car, int offset) {
+ Coordinate newC = car.wouldMoveTo(offset);
+ if (newC.getY() > DIMENSION - 1 || newC.getX() > DIMENSION - 1 || newC.getX() < 0 || newC.getY() < 0) return;
+ for (Car iter : cars) {
+ if (iter.occupies(newC)) {
+ Log.d("Board", "Can't move");
+ return;
+ }
+ }
+ car.move(offset);
+ }
+ };
+
+ public Board() {
+ this(new HashSet<Car>());
+ }
+
+ public Board(Set<Car> cars) {
+ for (Car car : cars) {
+ add(car);
+ }
+ }
+
+ public void add(Car car) {
+ car.setMoveListener(moveListener);
+ cars.add(car);
+ }
+
+ public void addToLayout(Context context, RelativeLayout layout) {
+ for (Car car : cars) {
+ layout.addView(car.getImageView(context));
+ }
+ }
+
+}