Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions app/src/main/java/atorch/shortestpaths/SolvePuzzle.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand Down Expand Up @@ -53,6 +56,12 @@ public class SolvePuzzle extends AppCompatActivity {
static String paths[][]; // Paths[i] is an array of country names
static boolean path_indicators[]; // When user's guess is consistent with paths[i], path_indicators[i] will be true
static int user_progress; // How far along path user has traveled
static String current_country; // Where the user is now: country_from, then their last correct answer

static TrickyBorders tricky_borders;
static int wrong_guesses_this_step;
static boolean hint_shown_this_step;
private static final int WRONG_GUESSES_BEFORE_HINT = 2;

public static final Random random = new Random();

Expand Down Expand Up @@ -81,9 +90,71 @@ private void setPuzzleStatement(Resources res) {
path_length_statement.setText(path_length_statement_text);
}

private void addCrossingNote(Context context, LinearLayout countries_visited, String from, String to) {
// If going from -> to crosses a border players often don't know about (e.g. Spain -> Morocco),
// explain it on the route, between the arrow and the country we just reached
String note = tricky_borders.crossingNote(from, to);
if (note == null) {
return;
}
TextView note_view = new TextView(context);
note_view.setText(note);
note_view.setTextColor(root_country.getCurrentTextColor());
note_view.setTypeface(null, Typeface.ITALIC);
note_view.setTextSize(TypedValue.COMPLEX_UNIT_PX, root_country.getTextSize() * 0.85f);
note_view.setGravity(Gravity.CENTER_HORIZONTAL);
int padding = getResources().getDimensionPixelSize(R.dimen.activity_vertical_margin_quarter);
note_view.setPadding(padding, padding, padding, padding);
note_view.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
countries_visited.addView(note_view);
}

private boolean everyWayForwardIsTricky() {
// True if every shortest path still consistent with the user's answers leaves
// current_country through a tricky border, i.e. the user can't make progress without it
boolean any_path = false;
for (int i = 0; i < number_of_paths; i++) {
if (!path_indicators[i]) {
continue;
}
any_path = true;
if (!tricky_borders.isTricky(current_country, paths[i][user_progress])) {
return false;
}
}
return any_path;
}

private void showBorderHint(String text) {
TextView border_hint = (TextView) findViewById(R.id.border_hint);
border_hint.setText(text);
border_hint.setVisibility(View.VISIBLE);
}

private void onWrongGuess(Resources res, String user_answer) {
wrong_guesses_this_step++;
String missing_border_note = tricky_borders.missingBorderNote(current_country, user_answer);
if (missing_border_note != null) {
// E.g. Morocco -> Mauritania: explain why they aren't neighbours here
showBorderHint(missing_border_note);
} else if (!hint_shown_this_step && wrong_guesses_this_step >= WRONG_GUESSES_BEFORE_HINT && everyWayForwardIsTricky()) {
showBorderHint(String.format(res.getString(R.string.tricky_border_hint), current_country));
hint_shown_this_step = true;
}
}

private void onCorrectGuess(String user_answer) {
current_country = user_answer;
wrong_guesses_this_step = 0;
hint_shown_this_step = false;
findViewById(R.id.border_hint).setVisibility(View.GONE);
}

private void addCountryVisited(Context context, String user_answer) {
// Called each time user enters a correct answer
LinearLayout countries_visited = (LinearLayout) findViewById(R.id.countries_visited);
addCrossingNote(context, countries_visited, current_country, user_answer);
TextView new_country_visited = new TextView(context);
new_country_visited.setText(user_answer);
new_country_visited.setLayoutParams(root_country.getLayoutParams());
Expand Down Expand Up @@ -124,6 +195,7 @@ private void reachedDestinationCountry(Context context, Resources res) {
// Update the UI when the user has correctly entered the last country before destination,
// which means we've reached the destination (we don't ask the user to input the destination country)
LinearLayout countries_visited = (LinearLayout) findViewById(R.id.countries_visited);
addCrossingNote(context, countries_visited, current_country, country_to);
TextView destination_country = new TextView(context);
destination_country.setText(country_to);
destination_country.setLayoutParams(root_country.getLayoutParams());
Expand Down Expand Up @@ -165,6 +237,9 @@ protected void onCreate(Bundle savedInstanceState) {
final Resources res = getResources();
countries = res.getStringArray(R.array.countries);
congratulations_array = res.getStringArray(R.array.correct_guess_congratulations);
tricky_borders = new TrickyBorders(res);
wrong_guesses_this_step = 0;
hint_shown_this_step = false;

final Context context = getApplicationContext();
MySQLiteHelper mSQLiteHelper = new MySQLiteHelper(context);
Expand All @@ -175,6 +250,7 @@ protected void onCreate(Bundle savedInstanceState) {
country_to_index = c.getInt(c.getColumnIndexOrThrow(MySQLiteHelper.COL_TO));
country_from = countries[country_from_index];
country_to = countries[country_to_index];
current_country = country_from;
path_length = c.getInt(c.getColumnIndexOrThrow(MySQLiteHelper.COL_PATH_LENGTH));
path_length_inner = path_length - 2; // Number of countries the user has to input
c.close();
Expand Down Expand Up @@ -232,6 +308,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
user_progress++;
path_indicators = new_path_indicators;
addCountryVisited(context, user_answer);
onCorrectGuess(user_answer);

// TODO Put this in a fn, and call it for very last country seen
ContentValues content = new ContentValues();
Expand All @@ -258,6 +335,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
}
} else {
Toast.makeText(context, "Sorry, " + user_answer + " won\'t help.", Toast.LENGTH_LONG).show();
onWrongGuess(res, user_answer);
}
country_input.setText("");
}
Expand Down
55 changes: 55 additions & 0 deletions app/src/main/java/atorch/shortestpaths/TrickyBorders.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package atorch.shortestpaths;

import android.content.res.Resources;
import android.util.Log;

import java.util.HashMap;
import java.util.Map;

/**
* Borders that players often don't expect (like Spain and Morocco, via Ceuta and Melilla),
* and pairs that look like neighbours but aren't in the puzzle's map (like Morocco and
* Mauritania). Loaded from res/values/tricky_borders.xml.
*/
final class TrickyBorders {

private static final String TAG = "TrickyBorders";

private final Map<String, String> trickyNotes = new HashMap<>();
private final Map<String, String> missingNotes = new HashMap<>();

TrickyBorders(Resources res) {
load(res.getStringArray(R.array.tricky_borders), trickyNotes);
load(res.getStringArray(R.array.missing_borders), missingNotes);
}

/** True if crossing from a to b (in either direction) is a border players tend to miss. */
boolean isTricky(String a, String b) {
return trickyNotes.containsKey(key(a, b));
}

/** The explanation to show when crossing from a to b, or null if it's an ordinary border. */
String crossingNote(String a, String b) {
return trickyNotes.get(key(a, b));
}

/** The explanation to show when a player tries to go from a to b but the puzzle has no such border, or null. */
String missingBorderNote(String a, String b) {
return missingNotes.get(key(a, b));
}

private static String key(String a, String b) {
return a.compareTo(b) < 0 ? a + "|" + b : b + "|" + a;
}

private static void load(String[] items, Map<String, String> into) {
for (String item : items) {
String[] parts = item.split("\\|", 3);
if (parts.length != 3) {
Log.w(TAG, "Skipping malformed entry: " + item);
continue;
}
into.put(key(parts[0].trim(), parts[1].trim()), parts[2].trim());
}
}
}
11 changes: 10 additions & 1 deletion app/src/main/res/layout/activity_solve_puzzle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@
android:layout_height="wrap_content"
android:layout_width="match_parent" />
</LinearLayout>


<TextView
android:visibility="gone"
android:paddingBottom="@dimen/activity_vertical_margin_half"
android:paddingTop="@dimen/activity_vertical_margin_half"
android:textStyle="italic"
android:id="@+id/border_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<AutoCompleteTextView
android:hint="@string/where_next"
android:id="@+id/autocomplete_country"
Expand Down
34 changes: 34 additions & 0 deletions app/src/main/res/values/tricky_borders.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<!--
Borders that come up in puzzle solutions but that players often don't know about.
Format: "Country A|Country B|Note". The names must match the "countries" array in
strings.xml exactly, in either order. The note appears on the route when the player
crosses that border.
-->
<string-array name="tricky_borders">
<item>Morocco|Spain|Spain and Morocco share a land border: the Spanish cities of Ceuta and Melilla are on the North African coast.</item>
<item>Azerbaijan|Turkey|Turkey borders Azerbaijan through Nakhchivan, a part of Azerbaijan cut off from the rest of the country by Armenia.</item>
<item>Afghanistan|People\'s Republic of China|Afghanistan reaches China through the Wakhan Corridor, a long, narrow strip of land high in the Pamir mountains.</item>
<item>Lithuania|Russia|Lithuania borders Russia at Kaliningrad, a part of Russia on the Baltic coast between Lithuania and Poland.</item>
<item>Norway|Russia|Norway and Russia share a short border in the far north, above the Arctic Circle.</item>
<item>North Korea|Russia|North Korea and Russia share a very short border along the Tumen River, near the Sea of Japan.</item>
<item>Botswana|Zambia|Botswana and Zambia meet at Kazungula on the Zambezi River, where their border is only a few hundred metres long.</item>
<item>Namibia|Zambia|Namibia reaches Zambia through the Caprivi Strip, a long, narrow arm of land in Namibia\'s north-east.</item>
<item>France|Kingdom of the Netherlands|France and the Kingdom of the Netherlands share the Caribbean island of Saint Martin.</item>
<item>Cyprus|United Kingdom|The United Kingdom borders Cyprus through Akrotiri and Dhekelia, two British bases on the island.</item>
</string-array>

<!--
Pairs that look like neighbours but aren't in the puzzle's map. Same format as above.
The note appears when the player guesses one of them while standing in the other.
-->
<string-array name="missing_borders">
<item>Mauritania|Morocco|Morocco and Mauritania don\'t share a border in this puzzle: Western Sahara lies between them.</item>
</string-array>

<!-- Shown after a few wrong guesses when every way forward crosses one of the borders above. -->
<string name="tricky_border_hint">Hint: %s has a neighbour that\'s easy to overlook.</string>

</resources>
Loading