From 0cfe26faf1bdc73864cfbf54b5d60da15a2ee043 Mon Sep 17 00:00:00 2001 From: Marshall Date: Thu, 26 Apr 2012 12:08:17 -0400 Subject: [PATCH] - 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. --- src/controller/BoardPanelMouseListener.java | 14 ++-- src/model/Referee.java | 77 ++++++++++----------- src/model/playerModel/GameGoal.java | 19 +++++ src/model/playerModel/PlayerModel.java | 43 ++++++++---- 4 files changed, 92 insertions(+), 61 deletions(-) create mode 100644 src/model/playerModel/GameGoal.java diff --git a/src/controller/BoardPanelMouseListener.java b/src/controller/BoardPanelMouseListener.java index 86db065..b758464 100644 --- a/src/controller/BoardPanelMouseListener.java +++ b/src/controller/BoardPanelMouseListener.java @@ -6,18 +6,18 @@ import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import player.HumanPlayer; - - import view.Tile; import view.TileSelectionPanel; -public class BoardPanelMouseListener implements MouseListener, MouseWheelListener { - private static int clickNum = 0; - +public class BoardPanelMouseListener implements MouseListener, + MouseWheelListener { + // private static int clickNum = 0; + private final HumanPlayer humanPlayer; private final TileSelectionPanel tsp; - - public BoardPanelMouseListener(TileSelectionPanel tsp, HumanPlayer humanPlayer) { + + public BoardPanelMouseListener(TileSelectionPanel tsp, + HumanPlayer humanPlayer) { this.humanPlayer = humanPlayer; this.tsp = tsp; } diff --git a/src/model/Referee.java b/src/model/Referee.java index 7c1f945..d917317 100644 --- a/src/model/Referee.java +++ b/src/model/Referee.java @@ -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; } diff --git a/src/model/playerModel/GameGoal.java b/src/model/playerModel/GameGoal.java new file mode 100644 index 0000000..eaeafd6 --- /dev/null +++ b/src/model/playerModel/GameGoal.java @@ -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; + } +} diff --git a/src/model/playerModel/PlayerModel.java b/src/model/playerModel/PlayerModel.java index e23b5ee..b6c1da5 100644 --- a/src/model/playerModel/PlayerModel.java +++ b/src/model/playerModel/PlayerModel.java @@ -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; } }