diff options
author | Camil Staps | 2015-04-23 18:42:02 +0200 |
---|---|---|
committer | Camil Staps | 2015-04-23 18:42:02 +0200 |
commit | 4ccb3506c19639b410fcd5a50364c834ea153c83 (patch) | |
tree | d04e357d970901b06c636d8a8b5cf7a5fac938e7 /app/src/main/java/com/camilstaps/rushhour | |
parent | OnBlock (diff) |
Cleanup
Diffstat (limited to 'app/src/main/java/com/camilstaps/rushhour')
5 files changed, 138 insertions, 33 deletions
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<Car>()); - } - public Board(Set<Car> cars) { for (Car car : cars) { add(car); } } + public Board() { + this(new HashSet<Car>()); + } + 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) { diff --git a/app/src/main/java/com/camilstaps/rushhour/DriveListener.java b/app/src/main/java/com/camilstaps/rushhour/DriveListener.java index bfb1eb3..553c29f 100644 --- a/app/src/main/java/com/camilstaps/rushhour/DriveListener.java +++ b/app/src/main/java/com/camilstaps/rushhour/DriveListener.java @@ -4,6 +4,13 @@ package com.camilstaps.rushhour; * Created by camilstaps on 22-4-15. */ public abstract class DriveListener { + /** + * Called when a car moves + */ public abstract void onDrive(); + + /** + * Called when a car attempted to move, but couldn't + */ public abstract void onBlocked(); } diff --git a/app/src/main/java/com/camilstaps/rushhour/GamePlayActivity.java b/app/src/main/java/com/camilstaps/rushhour/GamePlayActivity.java index 22532f2..d7958c3 100755 --- a/app/src/main/java/com/camilstaps/rushhour/GamePlayActivity.java +++ b/app/src/main/java/com/camilstaps/rushhour/GamePlayActivity.java @@ -20,36 +20,12 @@ public class GamePlayActivity extends Activity { setContentView(R.layout.activity_fullscreen); + setupSoundPool(); + InputStream input = getResources().openRawResource(R.raw.level); BoardLoader loader = new BoardLoader(); final Board board = loader.loadBoard(input); - //final Board board = new Board(); -// board.add(new Car(new Coordinate(0,0), new Coordinate(2,0), Color.YELLOW)); -// board.add(new Car(new Coordinate(3,0), new Coordinate(3,1), Color.rgb(128,223,182))); -// board.add(new Car(new Coordinate(4,0), new Coordinate(4,2), Color.rgb(198, 134,221))); -// board.add(new Car(new Coordinate(0,2), new Coordinate(1,2), Color.RED)); -// board.add(new Car(new Coordinate(5,2), new Coordinate(5,3), Color.rgb(255,165,0))); -// board.add(new Car(new Coordinate(0,3), new Coordinate(0,4), Color.rgb(158,231,246))); -// board.add(new Car(new Coordinate(1,3), new Coordinate(2,3), Color.rgb(245,158,246))); -// board.add(new Car(new Coordinate(3,3), new Coordinate(4,3), Color.rgb(150,126,196))); -// board.add(new Car(new Coordinate(1,4), new Coordinate(2,4), Color.GREEN)); -// board.add(new Car(new Coordinate(3,4), new Coordinate(3,5), Color.BLACK)); -// board.add(new Car(new Coordinate(5,4), new Coordinate(5,5), Color.rgb(219,202,161))); -// board.add(new Car(new Coordinate(0,5), new Coordinate(2,5), Color.rgb(25,195,167))); - - soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0); - soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { - @Override - public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { - if (sampleId == soundBackgroundId) { - soundPool.play(soundBackgroundId, 1, 1, 2, -1, 1); - } - } - }); - soundBackgroundId = soundPool.load(this, R.raw.tune, 2); - soundCarDriveId = soundPool.load(this, R.raw.car_drive, 1); - soundCantMoveId = soundPool.load(this, R.raw.cantmove, 1); final RelativeLayout boardLayout = (RelativeLayout) findViewById(R.id.board); ViewTreeObserver vto = boardLayout.getViewTreeObserver(); @@ -61,6 +37,9 @@ public class GamePlayActivity extends Activity { } }); + /* + * Sounds on move and attempt to move + */ board.setDriveListener(new DriveListener() { @Override public void onDrive() { @@ -73,4 +52,22 @@ public class GamePlayActivity extends Activity { } }); } + + /** + * Load sounds; start background music + */ + protected void setupSoundPool() { + soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0); + soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { + @Override + public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { + if (sampleId == soundBackgroundId) { + soundPool.play(soundBackgroundId, 1, 1, 2, -1, 1); + } + } + }); + soundBackgroundId = soundPool.load(this, R.raw.tune, 2); + soundCarDriveId = soundPool.load(this, R.raw.car_drive, 1); + soundCantMoveId = soundPool.load(this, R.raw.cantmove, 1); + } } diff --git a/app/src/main/java/com/camilstaps/rushhour/HighScore.java b/app/src/main/java/com/camilstaps/rushhour/HighScore.java index ce5e001..9168f02 100644 --- a/app/src/main/java/com/camilstaps/rushhour/HighScore.java +++ b/app/src/main/java/com/camilstaps/rushhour/HighScore.java @@ -1,5 +1,8 @@ package com.camilstaps.rushhour; +import org.json.JSONException; +import org.json.JSONObject; + /** * Created by camilstaps on 23-4-15. * Edited by Halzyn on 23-4-15. @@ -14,6 +17,24 @@ public class HighScore implements Comparable<HighScore> { this.name = name; } + /** + * HighScore from json + * @see #toString() + * @param jsonString + */ + public HighScore(String jsonString) { + int temp_score = -1; + String temp_name = null; + try { + JSONObject json = new JSONObject(jsonString); + temp_score = json.getInt("score"); + temp_name = json.getString("name"); + } catch (JSONException e) { + } + score = temp_score; + name = temp_name; + } + public int getScore() { return score; } @@ -24,11 +45,11 @@ public class HighScore implements Comparable<HighScore> { @Override public int compareTo(HighScore other_score) { - if (other_score.getScore() < score) + if (other_score.score < score) { return -1; } - else if (other_score.getScore() == score) + else if (other_score.score == score) { return 0; } @@ -38,4 +59,19 @@ public class HighScore implements Comparable<HighScore> { } } + /** + * JSON representation + * @see #HighScore(String) + * @return + */ + public String toString() { + JSONObject json = new JSONObject(); + try { + json.put("score", score); + json.put("name", name); + } catch (JSONException ex) { + } + return json.toString(); + } + } diff --git a/app/src/main/java/com/camilstaps/rushhour/HighScoreList.java b/app/src/main/java/com/camilstaps/rushhour/HighScoreList.java index 6afeb23..4d8f829 100644 --- a/app/src/main/java/com/camilstaps/rushhour/HighScoreList.java +++ b/app/src/main/java/com/camilstaps/rushhour/HighScoreList.java @@ -5,25 +5,59 @@ */ package com.camilstaps.rushhour; +import android.content.Context; +import android.preference.PreferenceManager; + import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; /** * * @author Created by halzyn on 23-4-15. */ public class HighScoreList { + + private final String PREFERENCES_KEY = "highscores"; private List<HighScore> list; public HighScoreList (List<HighScore> some_list) { this.list = some_list; } + + /** + * Get highscores from sharedpreferences + * @param context + */ + public HighScoreList(Context context) { + Set<String> jsonList = PreferenceManager.getDefaultSharedPreferences(context).getStringSet(PREFERENCES_KEY, new HashSet<String>()); + for (String json : jsonList) { + list.add(new HighScore(json)); + } + } + + public List<HighScore> getList() { + return list; + } public void addToList(HighScore score) { list.add(score); Collections.sort(list); } + + /** + * Save highscores to sharedpreferences + * @param context + */ + public void save(Context context) { + Set<String> jsonList = new HashSet<>(); + for (HighScore hs : list) { + jsonList.add(hs.toString()); + } + PreferenceManager.getDefaultSharedPreferences(context).edit().putStringSet(PREFERENCES_KEY, jsonList).apply(); + } } |