- Created a GameGoal class to encapsulate the expected computer behavior/target score for a game. The PlayerModel.java function getTargetScore() now returns a GameGoal object.
This commit is contained in:
@@ -6,7 +6,6 @@ import org.apache.log4j.Logger;
|
||||
|
||||
import player.AlphaBetaComPlayer;
|
||||
import player.HumanPlayer;
|
||||
import player.MinimaxComPlayer;
|
||||
import player.Player;
|
||||
import view.BoardPanel;
|
||||
import view.MessagePanel;
|
||||
@@ -16,16 +15,16 @@ public class Referee implements Runnable {
|
||||
|
||||
public static final String COM_TURN = "Waiting for the computer's move.";
|
||||
public static final String GAME_OVER = "Game over!";
|
||||
public static final String PLAYER_TURN = "Waiting for the player's move.";
|
||||
|
||||
public static final Logger LOGGER = Logger.getLogger(Referee.class
|
||||
.getName());
|
||||
|
||||
public static final String PLAYER_TURN = "Waiting for the player's move.";
|
||||
|
||||
private final Board board;
|
||||
private final HumanPlayer humanPlayer = new HumanPlayer();
|
||||
private BoardPanel boardPanel;
|
||||
private final Player computerPlayer;
|
||||
|
||||
private BoardPanel boardPanel;
|
||||
private final HumanPlayer humanPlayer = new HumanPlayer();
|
||||
private MessagePanel messagePanel;
|
||||
private ScorePanel scorePanel;
|
||||
|
||||
@@ -34,6 +33,40 @@ public class Referee implements Runnable {
|
||||
computerPlayer = new AlphaBetaComPlayer();
|
||||
}
|
||||
|
||||
public Player getComputerPlayer() {
|
||||
return computerPlayer;
|
||||
}
|
||||
|
||||
public HumanPlayer getHumanPlayer() {
|
||||
return humanPlayer;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
if (isOver()) {
|
||||
return GAME_OVER;
|
||||
} else if (board.isPlayerTurn()) {
|
||||
return PLAYER_TURN;
|
||||
} else {
|
||||
return COM_TURN;
|
||||
}
|
||||
}
|
||||
|
||||
public int getPlayerScore() {
|
||||
return board.getTurn();
|
||||
}
|
||||
|
||||
public TileColor getTile(int r, int c) {
|
||||
return board.getTile(r, c);
|
||||
}
|
||||
|
||||
public boolean isOver() {
|
||||
return board.isTerminalState();
|
||||
}
|
||||
|
||||
public void playToken(Move move) {
|
||||
board.playTile(move.getCell(), move.getColor());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
int plies = 0;
|
||||
@@ -76,40 +109,6 @@ public class Referee implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
public Player getComputerPlayer() {
|
||||
return computerPlayer;
|
||||
}
|
||||
|
||||
public HumanPlayer getHumanPlayer() {
|
||||
return humanPlayer;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
if (isOver()) {
|
||||
return GAME_OVER;
|
||||
} else if (board.isPlayerTurn()) {
|
||||
return PLAYER_TURN;
|
||||
} else {
|
||||
return COM_TURN;
|
||||
}
|
||||
}
|
||||
|
||||
public int getPlayerScore() {
|
||||
return board.getTurn();
|
||||
}
|
||||
|
||||
public TileColor getTile(int r, int c) {
|
||||
return board.getTile(r, c);
|
||||
}
|
||||
|
||||
public boolean isOver() {
|
||||
return board.isTerminalState();
|
||||
}
|
||||
|
||||
public void playToken(Move move) {
|
||||
board.playTile(move.getCell(), move.getColor());
|
||||
}
|
||||
|
||||
public void setBoardPanel(BoardPanel boardPanel) {
|
||||
this.boardPanel = boardPanel;
|
||||
}
|
||||
|
||||
19
src/model/playerModel/GameGoal.java
Normal file
19
src/model/playerModel/GameGoal.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package model.playerModel;
|
||||
|
||||
public class GameGoal {
|
||||
private final boolean highScoreGame;
|
||||
private final int targetScore;
|
||||
|
||||
public GameGoal(int trgtScr, boolean hghScrGm) {
|
||||
targetScore = trgtScr;
|
||||
highScoreGame = hghScrGm;
|
||||
}
|
||||
|
||||
public int getTargetScore() {
|
||||
return targetScore;
|
||||
}
|
||||
|
||||
public boolean shouldCauseHighScore() {
|
||||
return highScoreGame;
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,6 @@ import model.playerModel.node.SigmoidNode;
|
||||
|
||||
public class PlayerModel {
|
||||
|
||||
public static final int FIRST_SCORE = 50;
|
||||
public static final Random rand = new Random();
|
||||
|
||||
private final SigmoidNode[] hiddenLayer;
|
||||
@@ -18,8 +17,7 @@ public class PlayerModel {
|
||||
// One node for each tile-color combination, plus one for each upcoming
|
||||
// tile-color combination.
|
||||
private final InputNode[] inputNode = new InputNode[(Board.NUM_COLS
|
||||
* Board.NUM_ROWS * (Board.TileColor.values().length - 1))
|
||||
+ (4 * 2)];
|
||||
* Board.NUM_ROWS * (Board.TileColor.values().length - 1))];
|
||||
|
||||
private int nextHighInGames = 0;
|
||||
|
||||
@@ -28,6 +26,10 @@ public class PlayerModel {
|
||||
* Board.NUM_ROWS];
|
||||
|
||||
public PlayerModel() {
|
||||
for (int i = 0; i < highScore.length; i++) {
|
||||
highScore[i] = -1;
|
||||
}
|
||||
|
||||
hiddenLayer = new SigmoidNode[inputNode.length
|
||||
+ ((inputNode.length * 2) / 3)];
|
||||
|
||||
@@ -70,18 +72,19 @@ public class PlayerModel {
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getTargetScore() {
|
||||
public GameGoal getTargetScore() {
|
||||
GameGoal goal;
|
||||
int targetScore;
|
||||
int highScore = getHighScore();
|
||||
|
||||
if (highScoresAchieved == 0) {
|
||||
targetScore = FIRST_SCORE;
|
||||
goal = new GameGoal(0, true);
|
||||
nextHighInGames = 1;
|
||||
}
|
||||
|
||||
else if (nextHighInGames == 0) {
|
||||
// Set next game for high score.
|
||||
nextHighInGames = (int) Math.pow(highScoresAchieved, 2);
|
||||
nextHighInGames = (int) (Math.pow(highScoresAchieved + 1, 2) / 2);
|
||||
|
||||
// Return high score times increase percentage.
|
||||
targetScore = highScore / (2 * (highScoresAchieved + 1));
|
||||
@@ -89,6 +92,8 @@ public class PlayerModel {
|
||||
if (targetScore <= highScore) {
|
||||
targetScore = highScore + 1;
|
||||
}
|
||||
|
||||
goal = new GameGoal(targetScore, true);
|
||||
}
|
||||
|
||||
else {
|
||||
@@ -98,21 +103,29 @@ public class PlayerModel {
|
||||
if (targetScore >= highScore) {
|
||||
targetScore = highScore - 1;
|
||||
}
|
||||
|
||||
goal = new GameGoal(targetScore, false);
|
||||
}
|
||||
|
||||
return targetScore;
|
||||
return goal;
|
||||
}
|
||||
|
||||
public void logGame(int score) {
|
||||
loop: for (int i = 0; i < highScore.length; i++) {
|
||||
if (score > highScore[i]) {
|
||||
for (int j = i; j < highScore.length - 1; j++) {
|
||||
highScore[j + 1] = highScore[j];
|
||||
if (highScore[0] == -1) {
|
||||
highScore[0] = score;
|
||||
}
|
||||
|
||||
else {
|
||||
loop: for (int i = 0; i < highScore.length; i++) {
|
||||
if (score > highScore[i]) {
|
||||
for (int j = i; j < highScore.length - 1; j++) {
|
||||
highScore[j + 1] = highScore[j];
|
||||
}
|
||||
|
||||
highScore[i] = score;
|
||||
|
||||
break loop;
|
||||
}
|
||||
|
||||
highScore[i] = score;
|
||||
|
||||
break loop;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user