- Reorganized packages so that the computer agents are inside the model package.
- Added support for the player model inside Referee.java; high scores should now persist over a single execution of the program. - Refactored PlayerModel.java to support game logging. All games are now logged so that we can track overall progress. - Added scaffolding to allow saving and importing of PlayerModel.java. It is not yet functional, but it will be with two function implementations and then the appropriate calls.
This commit is contained in:
32
src/model/playerModel/GameLog.java
Normal file
32
src/model/playerModel/GameLog.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package model.playerModel;
|
||||
|
||||
public class GameLog implements Comparable<GameLog> {
|
||||
public static boolean SORT_BY_SCORE = true;
|
||||
private final int gameNum;
|
||||
|
||||
private final int score;
|
||||
|
||||
public GameLog(int scr, int gmNm) {
|
||||
score = scr;
|
||||
gameNum = gmNm;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(GameLog o) {
|
||||
if (SORT_BY_SCORE) {
|
||||
return o.getScore() - getScore();
|
||||
}
|
||||
|
||||
else {
|
||||
return getGameNum() - o.getGameNum();
|
||||
}
|
||||
}
|
||||
|
||||
public int getGameNum() {
|
||||
return gameNum;
|
||||
}
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,44 @@
|
||||
package model.playerModel;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Random;
|
||||
|
||||
import model.Board;
|
||||
import model.playerModel.node.InputNode;
|
||||
import model.playerModel.node.SigmoidNode;
|
||||
|
||||
public class PlayerModel {
|
||||
public class PlayerModel implements Serializable {
|
||||
|
||||
public static final Random rand = new Random();
|
||||
public static final String PLAYER_MODEL_PATH = "playerModel.dat"; // Path to
|
||||
// the
|
||||
// stored
|
||||
// player
|
||||
// model.
|
||||
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 PlayerModel load() {
|
||||
// TODO Import the PlayerModel from a file.
|
||||
return new PlayerModel();
|
||||
}
|
||||
|
||||
private int GAME_COUNTER = 0;
|
||||
private final SigmoidNode[] hiddenLayer;
|
||||
private final int[] highScore = new int[10];
|
||||
|
||||
private int highScoresAchieved = 0;
|
||||
|
||||
// One node for each tile-color combination, plus one for each upcoming
|
||||
// tile-color combination.
|
||||
private final InputNode[] inputNode = new InputNode[(Board.NUM_COLS
|
||||
@@ -34,11 +59,9 @@ public class PlayerModel {
|
||||
// should be placed.
|
||||
private final SigmoidNode[] outputNode = new SigmoidNode[(Board.NUM_COLS * Board.NUM_ROWS) + 4];
|
||||
|
||||
public PlayerModel() {
|
||||
for (int i = 0; i < highScore.length; i++) {
|
||||
highScore[i] = -1;
|
||||
}
|
||||
private final ArrayList<GameLog> scores = new ArrayList<GameLog>();
|
||||
|
||||
public PlayerModel() {
|
||||
hiddenLayer = new SigmoidNode[inputNode.length
|
||||
+ ((inputNode.length * 2) / 3)];
|
||||
|
||||
@@ -64,7 +87,23 @@ public class PlayerModel {
|
||||
}
|
||||
|
||||
public int[] getHighScores() {
|
||||
return highScore;
|
||||
GameLog.SORT_BY_SCORE = true;
|
||||
|
||||
Collections.sort(scores);
|
||||
|
||||
int[] highScores = new int[10];
|
||||
|
||||
for (int i = 0; i < highScores.length; i++) {
|
||||
if (i >= scores.size()) {
|
||||
highScores[i] = -1;
|
||||
}
|
||||
|
||||
else {
|
||||
highScores[i] = scores.get(i).getScore();
|
||||
}
|
||||
}
|
||||
|
||||
return highScores;
|
||||
}
|
||||
|
||||
public boolean[] getPrediction(boolean[] input) {
|
||||
@@ -124,25 +163,15 @@ public class PlayerModel {
|
||||
}
|
||||
|
||||
public void logGame(int score) {
|
||||
if (highScore[0] == -1) {
|
||||
highScore[0] = score;
|
||||
}
|
||||
|
||||
else {
|
||||
loop: for (int i = 0; i < highScore.length; i++) {
|
||||
if (score > highScore[i]) {
|
||||
for (int j = highScore.length - 2; j >= i; j--) {
|
||||
highScore[j + 1] = highScore[j];
|
||||
}
|
||||
|
||||
highScore[i] = score;
|
||||
break loop;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
highScoresAchieved += (score == highScore[0]) ? 1 : 0;
|
||||
scores.add(new GameLog(score, GAME_COUNTER));
|
||||
GAME_COUNTER++;
|
||||
nextHighInGames--;
|
||||
highScoresAchieved += (score == getHighScore()) ? 1 : 0;
|
||||
}
|
||||
|
||||
public boolean save() {
|
||||
// TODO Implement saving.
|
||||
return false;
|
||||
}
|
||||
|
||||
public void train(boolean[] example) {
|
||||
@@ -154,6 +183,6 @@ public class PlayerModel {
|
||||
}
|
||||
|
||||
private int getHighScore() {
|
||||
return highScore[0];
|
||||
return getHighScores()[0];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user