Skip to content

Commit

Permalink
Fix #63 by saving state of Ultraghost when screen rotates.
Browse files Browse the repository at this point in the history
Add a bunch of serialization code and some tests.
  • Loading branch information
donkirkby committed Sep 5, 2015
1 parent 3814f02 commit 4ab32ad
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 49 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Add new words to a student's vocabulary and make the words stick (grab them).

Word Challenges
===============
This app will eventually have several word challenges like anagrams, word
ladders, and Quizl. The current challenges are listed below.
Students will be able to complete these challenges alone or with a timer,
against other students, or against the computer.
Grab a few new words for your vocabulary.

Word Games
==========
This app and web site will eventually have several word games like
anagrams, word ladders, and Quizl. The current games are listed below.
Players will be able to complete these challenges alone or with a timer,
against other players, or against the computer.

Ultraghost
----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,35 @@ public void onGlobalLayout() {
if (intent.getBooleanExtra(INTENT_EXTRA_IS_HYPERGHOST, true)) {
controller.getMatch().setHyperghost(true);
}
controller.start();
start(savedInstanceState);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("match", match);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
start(savedInstanceState);
}

private void start(Bundle savedInstanceState) {
if (savedInstanceState == null) {
controller.start();
}
else {
Match savedMatch = (Match) savedInstanceState.getSerializable("match");
controller.clearStudents();
for (Student student : savedMatch.getStudents()) {
controller.addStudent(student);
student.setListener(this);
}
match = savedMatch;
controller.restart();
}
}

@Override
Expand Down Expand Up @@ -316,7 +344,7 @@ public Match getMatch() {
}

private void focusButton(Button target) {
boolean isFinished = match.getWinner() != null;
boolean isFinished = match != null && match.getWinner() != null;
for (Button button : focusButtons) {
button.setVisibility(
button == target && !isFinished
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
import com.github.donkirkby.vograbulary.VograbularyPreferences;

public class ComputerStudent extends Student {
private static final long serialVersionUID = 2114858077675128651L;
private int searchBatchSize = 1;
private int maxSearchBatchCount = Integer.MAX_VALUE;
private int searchBatchCount;
private int searchedWordsCount;
private Puzzle currentPuzzle;
private Puzzle searchPuzzle; // used to search for the best solution.
private Iterator<String> itr;
private VograbularyPreferences preferences;
private int vocabularySize;
private transient int searchBatchCount;
private transient int searchedWordsCount;
private transient Puzzle currentPuzzle;
private transient Puzzle searchPuzzle; // used to search for the best solution.
private transient Iterator<String> itr;

public ComputerStudent(VograbularyPreferences preferences) {
super("Computer");
this.preferences = preferences;
vocabularySize = preferences.getComputerStudentVocabularySize();
}

public void setSearchBatchSize(int searchBatchSize) {
Expand All @@ -29,9 +30,7 @@ public int getSearchBatchSize() {

public void setMaxSearchBatchCount(int maxSearchBatchCount) {
this.maxSearchBatchCount = maxSearchBatchCount;
searchBatchSize =
preferences.getComputerStudentVocabularySize()
/ maxSearchBatchCount;
searchBatchSize = vocabularySize / maxSearchBatchCount;
}

@Override
Expand All @@ -53,7 +52,6 @@ public void startSolving(Puzzle puzzle) {
public boolean runSearchBatch() {
checkCurrentPuzzle();
searchBatchCount++;
int vocabularySize = preferences.getComputerStudentVocabularySize();
int wordCount = Math.min(
searchBatchSize,
vocabularySize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,25 @@ public void askForResponse() {
}

public void start() {
Puzzle puzzle = getMatch().createPuzzle(wordList);
getMatch().createPuzzle(wordList);
restart();
}

public void restart() {
Puzzle puzzle = getMatch().getPuzzle();
watchPuzzle(puzzle);
screen.refreshPuzzle();
for (Student student : students) {
student.startSolving(puzzle);
}
scoreTask = new ScoreTask();
scheduler.scheduleRepeating(scoreTask, SCORE_MILLISECONDS);
searchTask = new SearchTask(scheduler);
scheduler.scheduleRepeating(searchTask, SEARCH_MILLISECONDS);
if (scoreTask == null) {
scoreTask = new ScoreTask();
scheduler.scheduleRepeating(scoreTask, SCORE_MILLISECONDS);
searchTask = new SearchTask(scheduler);
scheduler.scheduleRepeating(searchTask, SEARCH_MILLISECONDS);
}
}

/**
* Tell the controller to watch for changes in the current puzzle.
*/
Expand All @@ -139,6 +146,7 @@ public void watchPuzzle(final Puzzle puzzle) {
public void completed() {
Puzzle puzzle = getMatch().getPuzzle();
scheduler.cancel(scoreTask);
scoreTask = null;
puzzle.getOwner().addScore(puzzle.getScore());
String hint = puzzle.findNextBetter();
puzzle.setHint(hint == null ? "Perfect!" : "hint: " + hint);
Expand Down
11 changes: 9 additions & 2 deletions core/src/com/github/donkirkby/vograbulary/ultraghost/Match.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.github.donkirkby.vograbulary.ultraghost;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;


public class Match {
public class Match implements Serializable {
private static final long serialVersionUID = 3147466969252314807L;
private boolean isHyperghost;
private UltraghostRandom random = new UltraghostRandom();
private transient UltraghostRandom random = new UltraghostRandom();
private int matchScore;
private Student[] students;
private int studentIndex = Integer.MIN_VALUE;
Expand All @@ -18,6 +20,11 @@ public Match(int matchScore, Student... students) {
this.students = students;
}

/** Get the score required to win the match. */
public int getMatchScore() {
return matchScore;
}

private void checkStudentOrder() {
if (studentIndex < 0) {
for (int i = 0; i < students.length - 1; i++) {
Expand Down
19 changes: 16 additions & 3 deletions core/src/com/github/donkirkby/vograbulary/ultraghost/Puzzle.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.github.donkirkby.vograbulary.ultraghost;

import java.io.Serializable;
import java.util.ArrayList;

public class Puzzle {
public class Puzzle implements Serializable {
private static final long serialVersionUID = -3638349068583271443L;

public interface Listener {
/**
* This is called whenever one of the fields is changed.
Expand All @@ -25,10 +28,10 @@ public interface Listener {
private String response;
private String hint;
private Student owner;
private WordList wordList;
private transient WordList wordList;
private int minimumWordLength = 4;
private String previousWord;
private ArrayList<Listener> listeners =
private transient ArrayList<Listener> listeners =
new ArrayList<Listener>();
private boolean isComplete;
private boolean isTimedOut;
Expand Down Expand Up @@ -69,6 +72,16 @@ public boolean contains(String word) {
public String getLetters() {
return letters;
}

/** Get the word list used by this puzzle. */
public WordList getWordList() {
return wordList;
}

/** Set the word list used by this puzzle. */
public void setWordList(WordList wordList) {
this.wordList = wordList;
}

/**
* Get a display version of the three letters, plus any previous word.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.github.donkirkby.vograbulary.ultraghost;

public class Student {
import java.io.Serializable;

public class Student implements Serializable {
private static final long serialVersionUID = -3342014979878536752L;
public interface StudentListener {
void askForSolution();

Expand All @@ -9,12 +12,12 @@ public interface StudentListener {
void showThinking();
}

private StudentListener listener;
private transient StudentListener listener;
private String name;
private int score;
private int scoreCount;
private Match match;
private WordList wordList;
private transient WordList wordList;

public Student(String name) {
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.donkirkby.vograbulary;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class SerializableTools {
public static <T extends Serializable> byte[] serialize(T obj)
throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
objectStream.writeObject(obj);
objectStream.close();
return byteStream.toByteArray();
}

public static <T extends Serializable> T deserialize(
byte[] bytes,
Class<T> clazz)
throws IOException, ClassNotFoundException {
ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
ObjectInputStream objectStream = new ObjectInputStream(byteStream);
Object obj = objectStream.readObject();
return clazz.cast(obj);
}
}
Loading

0 comments on commit 4ab32ad

Please sign in to comment.