- Implemented saving and loading of a player model. High scores are now persistent, though only for one user.
- Created a JUnit test class to test the serialization. Should be useful for later tests.
This commit is contained in:
@@ -28,14 +28,14 @@ public class Referee implements Runnable {
|
|||||||
|
|
||||||
private final HumanPlayer humanPlayer = new HumanPlayer();
|
private final HumanPlayer humanPlayer = new HumanPlayer();
|
||||||
private final MainFrame mf;
|
private final MainFrame mf;
|
||||||
private final PlayerModel playerModel;
|
private PlayerModel playerModel = null;
|
||||||
|
|
||||||
public Referee(MainFrame mnFrm) {
|
public Referee(MainFrame mnFrm) {
|
||||||
if (PlayerModel.TRY_LOAD && PlayerModel.exists()) {
|
if (PlayerModel.TRY_LOAD && PlayerModel.exists()) {
|
||||||
playerModel = PlayerModel.load();
|
playerModel = PlayerModel.load();
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
if (playerModel == null) {
|
||||||
playerModel = new PlayerModel();
|
playerModel = new PlayerModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,6 +84,11 @@ public class Referee implements Runnable {
|
|||||||
mf.updateBoard();
|
mf.updateBoard();
|
||||||
play();
|
play();
|
||||||
playerModel.logGame(getPlayerScore());
|
playerModel.logGame(getPlayerScore());
|
||||||
|
|
||||||
|
if (!playerModel.save()) {
|
||||||
|
System.err.println("Saving PlayerModel failed.");
|
||||||
|
}
|
||||||
|
|
||||||
new HighScoreDialog(mf, playerModel);
|
new HighScoreDialog(mf, playerModel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
package model.playerModel;
|
package model.playerModel;
|
||||||
|
|
||||||
public class GameLog implements Comparable<GameLog> {
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class GameLog implements Comparable<GameLog>, Serializable {
|
||||||
public static boolean SORT_BY_SCORE = true;
|
public static boolean SORT_BY_SCORE = true;
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
private final int gameNum;
|
private final int gameNum;
|
||||||
|
|
||||||
private final int score;
|
private final int score;
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package model.playerModel;
|
package model.playerModel;
|
||||||
|
|
||||||
public interface Node {
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public interface Node extends Serializable {
|
||||||
|
|
||||||
boolean axon();
|
boolean axon();
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
package model.playerModel;
|
package model.playerModel;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -31,8 +36,18 @@ public class PlayerModel implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static PlayerModel load() {
|
public static PlayerModel load() {
|
||||||
// TODO Import the PlayerModel from a file.
|
FileInputStream fin = null;
|
||||||
return new PlayerModel();
|
ObjectInputStream oin = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
fin = new FileInputStream(PLAYER_MODEL_PATH);
|
||||||
|
oin = new ObjectInputStream(fin);
|
||||||
|
PlayerModel pm = (PlayerModel) oin.readObject();
|
||||||
|
oin.close();
|
||||||
|
return pm;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int GAME_COUNTER = 0;
|
private int GAME_COUNTER = 0;
|
||||||
@@ -170,9 +185,18 @@ public class PlayerModel implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean save() {
|
public boolean save() {
|
||||||
// TODO Implement saving.
|
FileOutputStream fout = null;
|
||||||
|
ObjectOutputStream oout = null;
|
||||||
|
try {
|
||||||
|
fout = new FileOutputStream(PLAYER_MODEL_PATH);
|
||||||
|
oout = new ObjectOutputStream(fout);
|
||||||
|
oout.writeObject(this);
|
||||||
|
oout.close();
|
||||||
|
return true;
|
||||||
|
} catch (IOException ex) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void train(boolean[] example) {
|
public void train(boolean[] example) {
|
||||||
if (example.length == outputNode.length) {
|
if (example.length == outputNode.length) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import model.playerModel.Node;
|
|||||||
|
|
||||||
public class InputNode implements Node {
|
public class InputNode implements Node {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
private boolean dendrite = false;
|
private boolean dendrite = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import model.playerModel.Node;
|
|||||||
|
|
||||||
public class SigmoidNode implements Node {
|
public class SigmoidNode implements Node {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
// Training rate.
|
// Training rate.
|
||||||
private final double A = .15;
|
private final double A = .15;
|
||||||
|
|
||||||
|
|||||||
17
test/puzzleTests/AllTests.java
Normal file
17
test/puzzleTests/AllTests.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package puzzleTests;
|
||||||
|
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
public class AllTests {
|
||||||
|
|
||||||
|
public static Test suite() {
|
||||||
|
TestSuite suite = new TestSuite(
|
||||||
|
"Tests for edu.gatech.cc.arktos.testcases");
|
||||||
|
// $JUnit-BEGIN$
|
||||||
|
suite.addTestSuite(Tests.class);
|
||||||
|
// $JUnit-END$
|
||||||
|
return suite;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
118
test/puzzleTests/Tests.java
Normal file
118
test/puzzleTests/Tests.java
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
package puzzleTests;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import model.playerModel.PlayerModel;
|
||||||
|
|
||||||
|
public class Tests extends TestCase {
|
||||||
|
|
||||||
|
public static PlayerModel getFakePlayerModel() {
|
||||||
|
PlayerModel pm = new PlayerModel();
|
||||||
|
|
||||||
|
pm.logGame(25);
|
||||||
|
pm.logGame(30);
|
||||||
|
pm.logGame(26);
|
||||||
|
pm.logGame(34);
|
||||||
|
pm.logGame(22);
|
||||||
|
pm.logGame(30);
|
||||||
|
pm.logGame(60);
|
||||||
|
|
||||||
|
return pm;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testHighScores() {
|
||||||
|
PlayerModel pm = getFakePlayerModel();
|
||||||
|
int[] highScores = pm.getHighScores();
|
||||||
|
|
||||||
|
assertEquals(highScores.length, 10);
|
||||||
|
assertEquals(highScores[0], 60);
|
||||||
|
assertEquals(highScores[1], 34);
|
||||||
|
assertEquals(highScores[2], 30);
|
||||||
|
assertEquals(highScores[3], 30);
|
||||||
|
assertEquals(highScores[4], 26);
|
||||||
|
assertEquals(highScores[5], 25);
|
||||||
|
assertEquals(highScores[6], 22);
|
||||||
|
assertEquals(highScores[7], -1);
|
||||||
|
assertEquals(highScores[8], -1);
|
||||||
|
assertEquals(highScores[9], -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSerializationInput() {
|
||||||
|
PlayerModel pm = getFakePlayerModel();
|
||||||
|
|
||||||
|
FileOutputStream fout = null;
|
||||||
|
ObjectOutputStream oout = null;
|
||||||
|
|
||||||
|
FileInputStream fin = null;
|
||||||
|
ObjectInputStream oin = null;
|
||||||
|
try {
|
||||||
|
fout = new FileOutputStream("test/PlayerModel.dat");
|
||||||
|
oout = new ObjectOutputStream(fout);
|
||||||
|
oout.writeObject(pm);
|
||||||
|
oout.close();
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
fail("Exception");
|
||||||
|
}
|
||||||
|
|
||||||
|
pm = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
fin = new FileInputStream("test/PlayerModel.dat");
|
||||||
|
|
||||||
|
oin = new ObjectInputStream(fin);
|
||||||
|
pm = (PlayerModel) oin.readObject();
|
||||||
|
|
||||||
|
oin.close();
|
||||||
|
|
||||||
|
int[] highScores = pm.getHighScores();
|
||||||
|
|
||||||
|
assertEquals(highScores.length, 10);
|
||||||
|
assertEquals(highScores[0], 60);
|
||||||
|
assertEquals(highScores[1], 34);
|
||||||
|
assertEquals(highScores[2], 30);
|
||||||
|
assertEquals(highScores[3], 30);
|
||||||
|
assertEquals(highScores[4], 26);
|
||||||
|
assertEquals(highScores[5], 25);
|
||||||
|
assertEquals(highScores[6], 22);
|
||||||
|
assertEquals(highScores[7], -1);
|
||||||
|
assertEquals(highScores[8], -1);
|
||||||
|
assertEquals(highScores[9], -1);
|
||||||
|
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
fail("Exception");
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
fail("Exception");
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
fail("Exception");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSerializationOutput() {
|
||||||
|
PlayerModel pm = getFakePlayerModel();
|
||||||
|
|
||||||
|
FileOutputStream fout = null;
|
||||||
|
ObjectOutputStream oout = null;
|
||||||
|
try {
|
||||||
|
fout = new FileOutputStream("test/PlayerModel.dat");
|
||||||
|
oout = new ObjectOutputStream(fout);
|
||||||
|
oout.writeObject(pm);
|
||||||
|
oout.close();
|
||||||
|
|
||||||
|
assertTrue((new File("test/playerModel.dat")).exists());
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
fail("Exception");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user