- 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());
|
||||
|
||||
Reference in New Issue
Block a user