diff options
-rw-r--r-- | app/src/main/AndroidManifest.xml | 11 | ||||
-rw-r--r-- | app/src/main/java/com/camilstaps/rushhour/Board.java | 21 | ||||
-rw-r--r-- | app/src/main/java/com/camilstaps/rushhour/FinishedActivity.java | 101 | ||||
-rwxr-xr-x | app/src/main/java/com/camilstaps/rushhour/GamePlayActivity.java | 12 | ||||
-rw-r--r-- | app/src/main/java/com/camilstaps/rushhour/HighScore.java | 4 | ||||
-rw-r--r-- | app/src/main/java/com/camilstaps/rushhour/HighScoreList.java | 19 | ||||
-rw-r--r-- | app/src/main/res/layout/activity_finished.xml | 22 | ||||
-rw-r--r-- | app/src/main/res/layout/highscore_item.xml | 17 | ||||
-rw-r--r-- | app/src/main/res/values/strings.xml | 4 |
9 files changed, 198 insertions, 13 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index dacd771..3ff025c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,7 +6,7 @@ android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" - android:theme="@style/AppTheme"> + android:theme="@style/AppTheme" > <activity android:name=".GamePlayActivity" android:configChanges="orientation|keyboardHidden|screenSize" @@ -16,14 +16,19 @@ </activity> <activity android:name=".MainActivity" - android:label="@string/title_activity_main" android:configChanges="orientation|keyboardHidden|screenSize" - android:screenOrientation="portrait"> + android:label="@string/title_activity_main" + android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> + <activity + android:name=".FinishedActivity" + android:label="@string/title_activity_finished" > + </activity> </application> </manifest> diff --git a/app/src/main/java/com/camilstaps/rushhour/Board.java b/app/src/main/java/com/camilstaps/rushhour/Board.java index bdfb6d4..7f1f842 100644 --- a/app/src/main/java/com/camilstaps/rushhour/Board.java +++ b/app/src/main/java/com/camilstaps/rushhour/Board.java @@ -1,9 +1,11 @@ package com.camilstaps.rushhour; import android.content.Context; +import android.content.Intent; import android.util.Log; import android.view.ViewGroup; import android.widget.RelativeLayout; +import android.widget.Toast; import java.util.HashSet; import java.util.Set; @@ -18,6 +20,9 @@ public class Board { public static final int DIMENSION = 6; private DriveListener driveListener; + private SolveListener solveListener; + + private int score; /** * Move a car if possible, and call the appropriate listeners @@ -37,6 +42,10 @@ public class Board { } } car.move(offset); + score++; + if (isSolved() && solveListener != null) { + solveListener.onSolve(score); + } driveListener.onDrive(); } }; @@ -76,9 +85,9 @@ public class Board { * @return */ public boolean isSolved() { - for (int x = DIMENSION - 1; x >= 0; x++) { + for (int x = DIMENSION - 1; x >= 0; x--) { for (Car car : cars) { - if (car.occupies(new Coordinate(x, 3))) { + if (car.occupies(new Coordinate(x, 2))) { if (car.canMoveHorizontally()) { return true; } else { @@ -94,4 +103,12 @@ public class Board { driveListener = dl; } + public interface SolveListener { + public void onSolve(int score); + } + + public void setSolveListener(SolveListener sl) { + solveListener = sl; + } + } diff --git a/app/src/main/java/com/camilstaps/rushhour/FinishedActivity.java b/app/src/main/java/com/camilstaps/rushhour/FinishedActivity.java new file mode 100644 index 0000000..9c092a3 --- /dev/null +++ b/app/src/main/java/com/camilstaps/rushhour/FinishedActivity.java @@ -0,0 +1,101 @@ +package com.camilstaps.rushhour; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.support.v7.app.ActionBarActivity; +import android.os.Bundle; +import android.text.Editable; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.ListView; +import android.widget.TextView; + +import com.camilstaps.rushhour.R; + +import java.util.ArrayList; + +public class FinishedActivity extends ActionBarActivity { + + Context context; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_finished); + + context = this; + + final HighScoreList list = new HighScoreList(this); + + final int score = getIntent().getIntExtra("score", Integer.MAX_VALUE); + final EditText input = new EditText(this); + new AlertDialog.Builder(this) + .setTitle("Enter name") + .setView(input) + .setPositiveButton("Ok", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + Editable value = input.getText(); + list.addToList(new HighScore(score, value.toString())); + list.save(context); + } + }) + .show(); + + ListView highscoresListView = (ListView) findViewById(R.id.highscoresListView); + + HighScoreAdapter arrayAdapter = new HighScoreAdapter(this, R.layout.highscore_item, list.getList()); + + highscoresListView.setAdapter(arrayAdapter); + } + + private class HighScoreAdapter extends ArrayAdapter<HighScore> { + Context context; + int layoutResourceId; + ArrayList<HighScore> data; + + public HighScoreAdapter(Context context, int layoutResourceId, ArrayList<HighScore> data) { + super(context, layoutResourceId, data); + this.layoutResourceId = layoutResourceId; + this.context = context; + this.data = data; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + View row = convertView; + HighScoreHolder holder; + + if(row == null) { + LayoutInflater inflater = ((Activity)context).getLayoutInflater(); + row = inflater.inflate(layoutResourceId, parent, false); + + holder = new HighScoreHolder(); + holder.score = (TextView)row.findViewById(R.id.score); + holder.name = (TextView)row.findViewById(R.id.name); + + row.setTag(holder); + } else { + holder = (HighScoreHolder)row.getTag(); + } + + HighScore highscore = data.get(position); + holder.name.setText(highscore.getName()); + holder.score.setText(Integer.toString(highscore.getScore())); + + return row; + } + + private class HighScoreHolder { + TextView score, name; + } + } + +} diff --git a/app/src/main/java/com/camilstaps/rushhour/GamePlayActivity.java b/app/src/main/java/com/camilstaps/rushhour/GamePlayActivity.java index d7958c3..dd3bafe 100755 --- a/app/src/main/java/com/camilstaps/rushhour/GamePlayActivity.java +++ b/app/src/main/java/com/camilstaps/rushhour/GamePlayActivity.java @@ -1,6 +1,7 @@ package com.camilstaps.rushhour; import android.app.Activity; +import android.content.Intent; import android.media.AudioManager; import android.media.SoundPool; import android.os.Bundle; @@ -9,7 +10,7 @@ import android.widget.RelativeLayout; import java.io.InputStream; -public class GamePlayActivity extends Activity { +public class GamePlayActivity extends Activity implements Board.SolveListener { private SoundPool soundPool; private int soundBackgroundId, soundCarDriveId, soundCantMoveId; @@ -51,6 +52,8 @@ public class GamePlayActivity extends Activity { soundPool.play(soundCantMoveId, 1, 1, 1, 0, 1); } }); + + board.setSolveListener(this); } /** @@ -70,4 +73,11 @@ public class GamePlayActivity extends Activity { soundCarDriveId = soundPool.load(this, R.raw.car_drive, 1); soundCantMoveId = soundPool.load(this, R.raw.cantmove, 1); } + + @Override + public void onSolve(int score) { + Intent intent = new Intent(this, FinishedActivity.class); + intent.putExtra("score", score); + startActivity(intent); + } } diff --git a/app/src/main/java/com/camilstaps/rushhour/HighScore.java b/app/src/main/java/com/camilstaps/rushhour/HighScore.java index 9168f02..5bff298 100644 --- a/app/src/main/java/com/camilstaps/rushhour/HighScore.java +++ b/app/src/main/java/com/camilstaps/rushhour/HighScore.java @@ -47,7 +47,7 @@ public class HighScore implements Comparable<HighScore> { public int compareTo(HighScore other_score) { if (other_score.score < score) { - return -1; + return 1; } else if (other_score.score == score) { @@ -55,7 +55,7 @@ public class HighScore implements Comparable<HighScore> { } else { - return 1; + return -1; } } diff --git a/app/src/main/java/com/camilstaps/rushhour/HighScoreList.java b/app/src/main/java/com/camilstaps/rushhour/HighScoreList.java index 4d8f829..f84b30f 100644 --- a/app/src/main/java/com/camilstaps/rushhour/HighScoreList.java +++ b/app/src/main/java/com/camilstaps/rushhour/HighScoreList.java @@ -8,6 +8,7 @@ package com.camilstaps.rushhour; import android.content.Context; import android.preference.PreferenceManager; +import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -21,9 +22,9 @@ public class HighScoreList { private final String PREFERENCES_KEY = "highscores"; - private List<HighScore> list; + private ArrayList<HighScore> list; - public HighScoreList (List<HighScore> some_list) { + public HighScoreList (ArrayList<HighScore> some_list) { this.list = some_list; } @@ -32,13 +33,17 @@ public class HighScoreList { * @param context */ public HighScoreList(Context context) { - Set<String> jsonList = PreferenceManager.getDefaultSharedPreferences(context).getStringSet(PREFERENCES_KEY, new HashSet<String>()); + Set<String> jsonList = PreferenceManager + .getDefaultSharedPreferences(context) + .getStringSet(PREFERENCES_KEY, new HashSet<String>()); + list = new ArrayList<>(); for (String json : jsonList) { list.add(new HighScore(json)); } + Collections.sort(list); } - public List<HighScore> getList() { + public ArrayList<HighScore> getList() { return list; } @@ -57,7 +62,11 @@ public class HighScoreList { for (HighScore hs : list) { jsonList.add(hs.toString()); } - PreferenceManager.getDefaultSharedPreferences(context).edit().putStringSet(PREFERENCES_KEY, jsonList).apply(); + PreferenceManager + .getDefaultSharedPreferences(context) + .edit() + .putStringSet(PREFERENCES_KEY, jsonList) + .apply(); } } diff --git a/app/src/main/res/layout/activity_finished.xml b/app/src/main/res/layout/activity_finished.xml new file mode 100644 index 0000000..e9ae900 --- /dev/null +++ b/app/src/main/res/layout/activity_finished.xml @@ -0,0 +1,22 @@ +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" + android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" + android:paddingRight="@dimen/activity_horizontal_margin" + android:paddingTop="@dimen/activity_vertical_margin" + android:paddingBottom="@dimen/activity_vertical_margin" + tools:context="com.camilstaps.rushhour.FinishedActivity"> + + <TextView android:text="Gefeliciteerd!" android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:id="@+id/textView" + android:textSize="40sp"/> + + <ListView + android:layout_width="match_parent" + android:layout_height="match_parent" + android:id="@+id/highscoresListView" + android:layout_below="@+id/textView" + android:layout_alignParentLeft="true" + android:layout_alignParentStart="true" /> + +</RelativeLayout> diff --git a/app/src/main/res/layout/highscore_item.xml b/app/src/main/res/layout/highscore_item.xml new file mode 100644 index 0000000..3c6faf0 --- /dev/null +++ b/app/src/main/res/layout/highscore_item.xml @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical" > + + <TextView android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:id="@+id/name"/> + + <TextView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:id="@+id/score"/> + + +</LinearLayout>
\ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c00a584..c46740f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2,4 +2,8 @@ <string name="app_name">Rush Hour</string> <string name="title_activity_main">@string/app_name</string> <string name="action_start">Start</string> + <string name="title_activity_finished">FinishedActivity</string> + + <string name="hello_world">Hello world!</string> + <string name="action_settings">Settings</string> </resources> |