- 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,18 +6,18 @@ import java.awt.event.MouseWheelEvent;
|
|||||||
import java.awt.event.MouseWheelListener;
|
import java.awt.event.MouseWheelListener;
|
||||||
|
|
||||||
import player.HumanPlayer;
|
import player.HumanPlayer;
|
||||||
|
|
||||||
|
|
||||||
import view.Tile;
|
import view.Tile;
|
||||||
import view.TileSelectionPanel;
|
import view.TileSelectionPanel;
|
||||||
|
|
||||||
public class BoardPanelMouseListener implements MouseListener, MouseWheelListener {
|
public class BoardPanelMouseListener implements MouseListener,
|
||||||
private static int clickNum = 0;
|
MouseWheelListener {
|
||||||
|
// private static int clickNum = 0;
|
||||||
|
|
||||||
private final HumanPlayer humanPlayer;
|
private final HumanPlayer humanPlayer;
|
||||||
private final TileSelectionPanel tsp;
|
private final TileSelectionPanel tsp;
|
||||||
|
|
||||||
public BoardPanelMouseListener(TileSelectionPanel tsp, HumanPlayer humanPlayer) {
|
public BoardPanelMouseListener(TileSelectionPanel tsp,
|
||||||
|
HumanPlayer humanPlayer) {
|
||||||
this.humanPlayer = humanPlayer;
|
this.humanPlayer = humanPlayer;
|
||||||
this.tsp = tsp;
|
this.tsp = tsp;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import org.apache.log4j.Logger;
|
|||||||
|
|
||||||
import player.AlphaBetaComPlayer;
|
import player.AlphaBetaComPlayer;
|
||||||
import player.HumanPlayer;
|
import player.HumanPlayer;
|
||||||
import player.MinimaxComPlayer;
|
|
||||||
import player.Player;
|
import player.Player;
|
||||||
import view.BoardPanel;
|
import view.BoardPanel;
|
||||||
import view.MessagePanel;
|
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 COM_TURN = "Waiting for the computer's move.";
|
||||||
public static final String GAME_OVER = "Game over!";
|
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
|
public static final Logger LOGGER = Logger.getLogger(Referee.class
|
||||||
.getName());
|
.getName());
|
||||||
|
|
||||||
|
public static final String PLAYER_TURN = "Waiting for the player's move.";
|
||||||
|
|
||||||
private final Board board;
|
private final Board board;
|
||||||
private final HumanPlayer humanPlayer = new HumanPlayer();
|
private BoardPanel boardPanel;
|
||||||
private final Player computerPlayer;
|
private final Player computerPlayer;
|
||||||
|
|
||||||
private BoardPanel boardPanel;
|
private final HumanPlayer humanPlayer = new HumanPlayer();
|
||||||
private MessagePanel messagePanel;
|
private MessagePanel messagePanel;
|
||||||
private ScorePanel scorePanel;
|
private ScorePanel scorePanel;
|
||||||
|
|
||||||
@@ -34,6 +33,40 @@ public class Referee implements Runnable {
|
|||||||
computerPlayer = new AlphaBetaComPlayer();
|
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
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
int plies = 0;
|
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) {
|
public void setBoardPanel(BoardPanel boardPanel) {
|
||||||
this.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 class PlayerModel {
|
||||||
|
|
||||||
public static final int FIRST_SCORE = 50;
|
|
||||||
public static final Random rand = new Random();
|
public static final Random rand = new Random();
|
||||||
|
|
||||||
private final SigmoidNode[] hiddenLayer;
|
private final SigmoidNode[] hiddenLayer;
|
||||||
@@ -18,8 +17,7 @@ public class PlayerModel {
|
|||||||
// One node for each tile-color combination, plus one for each upcoming
|
// One node for each tile-color combination, plus one for each upcoming
|
||||||
// tile-color combination.
|
// tile-color combination.
|
||||||
private final InputNode[] inputNode = new InputNode[(Board.NUM_COLS
|
private final InputNode[] inputNode = new InputNode[(Board.NUM_COLS
|
||||||
* Board.NUM_ROWS * (Board.TileColor.values().length - 1))
|
* Board.NUM_ROWS * (Board.TileColor.values().length - 1))];
|
||||||
+ (4 * 2)];
|
|
||||||
|
|
||||||
private int nextHighInGames = 0;
|
private int nextHighInGames = 0;
|
||||||
|
|
||||||
@@ -28,6 +26,10 @@ public class PlayerModel {
|
|||||||
* Board.NUM_ROWS];
|
* Board.NUM_ROWS];
|
||||||
|
|
||||||
public PlayerModel() {
|
public PlayerModel() {
|
||||||
|
for (int i = 0; i < highScore.length; i++) {
|
||||||
|
highScore[i] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
hiddenLayer = new SigmoidNode[inputNode.length
|
hiddenLayer = new SigmoidNode[inputNode.length
|
||||||
+ ((inputNode.length * 2) / 3)];
|
+ ((inputNode.length * 2) / 3)];
|
||||||
|
|
||||||
@@ -70,18 +72,19 @@ public class PlayerModel {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTargetScore() {
|
public GameGoal getTargetScore() {
|
||||||
|
GameGoal goal;
|
||||||
int targetScore;
|
int targetScore;
|
||||||
int highScore = getHighScore();
|
int highScore = getHighScore();
|
||||||
|
|
||||||
if (highScoresAchieved == 0) {
|
if (highScoresAchieved == 0) {
|
||||||
targetScore = FIRST_SCORE;
|
goal = new GameGoal(0, true);
|
||||||
nextHighInGames = 1;
|
nextHighInGames = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (nextHighInGames == 0) {
|
else if (nextHighInGames == 0) {
|
||||||
// Set next game for high score.
|
// 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.
|
// Return high score times increase percentage.
|
||||||
targetScore = highScore / (2 * (highScoresAchieved + 1));
|
targetScore = highScore / (2 * (highScoresAchieved + 1));
|
||||||
@@ -89,6 +92,8 @@ public class PlayerModel {
|
|||||||
if (targetScore <= highScore) {
|
if (targetScore <= highScore) {
|
||||||
targetScore = highScore + 1;
|
targetScore = highScore + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
goal = new GameGoal(targetScore, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
@@ -98,21 +103,29 @@ public class PlayerModel {
|
|||||||
if (targetScore >= highScore) {
|
if (targetScore >= highScore) {
|
||||||
targetScore = highScore - 1;
|
targetScore = highScore - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
goal = new GameGoal(targetScore, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return targetScore;
|
return goal;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void logGame(int score) {
|
public void logGame(int score) {
|
||||||
loop: for (int i = 0; i < highScore.length; i++) {
|
if (highScore[0] == -1) {
|
||||||
if (score > highScore[i]) {
|
highScore[0] = score;
|
||||||
for (int j = i; j < highScore.length - 1; j++) {
|
}
|
||||||
highScore[j + 1] = highScore[j];
|
|
||||||
|
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