- Implemented multiple users, including a selection dialog and automatic preference saving and loading.
- Integrated the ANN with the game. The network now predicts a user move, completely ignores it, and trains itself on the players actual move. This integration also included implementing two new functions. The first translates a board state to a boolean array to correspond with input nodes. The second translates a move to a boolean array to correspond with output nodes.
This commit is contained in:
@@ -30,19 +30,38 @@ public class Referee implements Runnable {
|
||||
private final MainFrame mf;
|
||||
private PlayerModel playerModel = null;
|
||||
|
||||
public Referee(MainFrame mnFrm) {
|
||||
if (PlayerModel.TRY_LOAD && PlayerModel.exists()) {
|
||||
playerModel = PlayerModel.load();
|
||||
public Referee(MainFrame mnFrm, String player) {
|
||||
if (PlayerModel.exists(player)) {
|
||||
PlayerModel.getPlayerPath(player);
|
||||
playerModel = PlayerModel.load(PlayerModel.getPlayerPath(player));
|
||||
}
|
||||
|
||||
if (playerModel == null) {
|
||||
playerModel = new PlayerModel();
|
||||
if (getPlayerModel() == null) {
|
||||
playerModel = new PlayerModel(player);
|
||||
}
|
||||
|
||||
mf = mnFrm;
|
||||
initGame();
|
||||
}
|
||||
|
||||
public boolean[] getBoardState() {
|
||||
boolean[] boardState = new boolean[getPlayerModel().getNumInputNodes()];
|
||||
|
||||
int i = 0;
|
||||
for (int r = 0; r < Board.NUM_ROWS; r++) {
|
||||
for (int c = 0; c < Board.NUM_COLS; c++) {
|
||||
boardState[i] = (board.getTile(r, c) == TileColor.BLUE);
|
||||
boardState[i + 1] = (board.getTile(r, c) == TileColor.GREEN);
|
||||
boardState[i + 2] = (board.getTile(r, c) == TileColor.RED);
|
||||
boardState[i + 3] = (board.getTile(r, c) == TileColor.YELLOW);
|
||||
|
||||
i += 4;
|
||||
}
|
||||
}
|
||||
|
||||
return boardState;
|
||||
}
|
||||
|
||||
public Player getComputerPlayer() {
|
||||
return computerPlayer;
|
||||
}
|
||||
@@ -83,13 +102,13 @@ public class Referee implements Runnable {
|
||||
initGame();
|
||||
mf.updateBoard();
|
||||
play();
|
||||
playerModel.logGame(getPlayerScore());
|
||||
getPlayerModel().logGame(getPlayerScore());
|
||||
|
||||
if (!playerModel.save()) {
|
||||
if (!getPlayerModel().save()) {
|
||||
System.err.println("Saving PlayerModel failed.");
|
||||
}
|
||||
|
||||
new HighScoreDialog(mf, playerModel);
|
||||
new HighScoreDialog(mf, getPlayerModel());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,6 +116,28 @@ public class Referee implements Runnable {
|
||||
this.boardPanel = boardPanel;
|
||||
}
|
||||
|
||||
private boolean[] getMoveArray(Move mv) {
|
||||
boolean[] move = new boolean[getPlayerModel().getNumOutputNodes()];
|
||||
|
||||
move[0] = (mv.getColor() == TileColor.BLUE);
|
||||
move[1] = (mv.getColor() == TileColor.GREEN);
|
||||
move[2] = (mv.getColor() == TileColor.RED);
|
||||
move[3] = (mv.getColor() == TileColor.YELLOW);
|
||||
|
||||
int tile = 0;
|
||||
for (int r = 0; r < Board.NUM_ROWS; r++) {
|
||||
for (int c = 0; c < Board.NUM_COLS; c++) {
|
||||
move[tile] = (mv.getCell().r == r && mv.getCell().c == c);
|
||||
}
|
||||
}
|
||||
|
||||
return move;
|
||||
}
|
||||
|
||||
private PlayerModel getPlayerModel() {
|
||||
return playerModel;
|
||||
}
|
||||
|
||||
private ScorePanel getScorePanel() {
|
||||
return mf.getScorePanel();
|
||||
}
|
||||
@@ -126,6 +167,9 @@ public class Referee implements Runnable {
|
||||
Move mv = humanPlayer.getMove(board);
|
||||
if (board.getTile(mv.getCell().r, mv.getCell().c) == TileColor.NONE) {
|
||||
playToken(humanPlayer.getMove(board));
|
||||
|
||||
getPlayerModel().train(getMoveArray(mv));
|
||||
|
||||
} else {
|
||||
humanPlayer.denyMove();
|
||||
}
|
||||
@@ -133,6 +177,13 @@ public class Referee implements Runnable {
|
||||
} else {
|
||||
Move mv = computerPlayer.getMove(board);
|
||||
playToken(mv);
|
||||
|
||||
// TODO
|
||||
// This is the call that gets a prediction of a user's move.
|
||||
// Some changes will probably be necessary to put it in the
|
||||
// right place and also to get the node weights. But... all in
|
||||
// due time.
|
||||
getPlayerModel().getPrediction(getBoardState());
|
||||
}
|
||||
|
||||
mf.updateMessage(getMessage());
|
||||
|
||||
@@ -17,30 +17,27 @@ import model.playerModel.node.SigmoidNode;
|
||||
|
||||
public class PlayerModel implements Serializable {
|
||||
|
||||
public static final String PLAYER_MODEL_PATH = "playerModel.dat"; // Path to
|
||||
// the
|
||||
// stored
|
||||
// player
|
||||
// model.
|
||||
public static final String DATA_FOLDER = "data/";
|
||||
public static final String PLAYER_MODEL_PREFIX = "playerModel";
|
||||
public static final String PLAYER_MODEL_SUFFIX = ".dat";
|
||||
public static final Random rand = new Random(); // Randomizer object.
|
||||
public static final boolean TRY_LOAD = true; // Set to false if any existing
|
||||
// player model should be
|
||||
// discarded and the next
|
||||
// game should begin a new
|
||||
// sequence.
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static boolean exists() {
|
||||
return (new File(PLAYER_MODEL_PATH)).exists();
|
||||
public static boolean exists(String playerName) {
|
||||
return (new File(getPlayerPath(playerName))).exists();
|
||||
}
|
||||
|
||||
public static PlayerModel load() {
|
||||
public static String getPlayerPath(String playerName) {
|
||||
return DATA_FOLDER + PLAYER_MODEL_PREFIX + "_" + playerName
|
||||
+ PLAYER_MODEL_SUFFIX;
|
||||
}
|
||||
|
||||
public static PlayerModel load(String path) {
|
||||
FileInputStream fin = null;
|
||||
ObjectInputStream oin = null;
|
||||
|
||||
try {
|
||||
fin = new FileInputStream(PLAYER_MODEL_PATH);
|
||||
fin = new FileInputStream(path);
|
||||
oin = new ObjectInputStream(fin);
|
||||
PlayerModel pm = (PlayerModel) oin.readObject();
|
||||
oin.close();
|
||||
@@ -54,29 +51,31 @@ public class PlayerModel implements Serializable {
|
||||
private final SigmoidNode[] hiddenLayer;
|
||||
private int highScoresAchieved = 0;
|
||||
|
||||
// One node for each tile-color combination, plus one for each upcoming
|
||||
// tile-color combination.
|
||||
// One node for each tile-color combination.
|
||||
private final InputNode[] inputNode = new InputNode[(Board.NUM_COLS
|
||||
* Board.NUM_ROWS * (Board.TileColor.values().length - 1))];
|
||||
|
||||
private final String name;
|
||||
|
||||
private int nextHighInGames = 0;
|
||||
|
||||
// One node for each tile plus four for the colors to be selected.
|
||||
// outputNode[0] is blue.
|
||||
// outputNode[1] is green.
|
||||
// outputNode[2] is red.
|
||||
// outputNode[3] is yellow.
|
||||
// outputNode[4] through outputNode[n] represent grid spaces. A true means
|
||||
// that the player is predicted to place on that tile.
|
||||
// They should be read from the top-left to bottom-right, across rows.
|
||||
// Ideally, the network should return only one true between 0 and 3 and only
|
||||
// one true between 4 and n, representing one color and the tile in which it
|
||||
// should be placed.
|
||||
/*
|
||||
* One node for each tile plus four for the colors to be selected.
|
||||
* outputNode[0] is blue. outputNode[1] is green. outputNode[2] is red.
|
||||
* outputNode[3] is yellow. outputNode[4] through outputNode[n] represent
|
||||
* grid spaces. A true means that the player is predicted to place on that
|
||||
* tile. They should be read from the top-left to bottom-right, across rows.
|
||||
* Ideally, the network should return only one true between 0 and 3 and only
|
||||
* one true between 4 and n, representing one color and the tile in which it
|
||||
* should be placed. See Referee.getMoveArray().
|
||||
*/
|
||||
private final SigmoidNode[] outputNode = new SigmoidNode[(Board.NUM_COLS * Board.NUM_ROWS) + 4];
|
||||
|
||||
private final ArrayList<GameLog> scores = new ArrayList<GameLog>();
|
||||
|
||||
public PlayerModel() {
|
||||
public PlayerModel(String nme) {
|
||||
name = nme;
|
||||
|
||||
hiddenLayer = new SigmoidNode[inputNode.length
|
||||
+ ((inputNode.length * 2) / 3)];
|
||||
|
||||
@@ -121,6 +120,18 @@ public class PlayerModel implements Serializable {
|
||||
return highScores;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getNumInputNodes() {
|
||||
return inputNode.length;
|
||||
}
|
||||
|
||||
public int getNumOutputNodes() {
|
||||
return outputNode.length;
|
||||
}
|
||||
|
||||
public boolean[] getPrediction(boolean[] input) {
|
||||
if (input.length == inputNode.length) {
|
||||
boolean[] prediction = new boolean[outputNode.length];
|
||||
@@ -187,13 +198,17 @@ public class PlayerModel implements Serializable {
|
||||
public boolean save() {
|
||||
FileOutputStream fout = null;
|
||||
ObjectOutputStream oout = null;
|
||||
String path = getPlayerPath(getName());
|
||||
try {
|
||||
fout = new FileOutputStream(PLAYER_MODEL_PATH);
|
||||
(new File(DATA_FOLDER)).mkdirs();
|
||||
|
||||
fout = new FileOutputStream(path);
|
||||
oout = new ObjectOutputStream(fout);
|
||||
oout.writeObject(this);
|
||||
oout.close();
|
||||
return true;
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user