- Finished HighScoreDialog.java. Now the player can play the game repeatedly.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package model;
|
package model;
|
||||||
|
|
||||||
import model.Board.TileColor;
|
import model.Board.TileColor;
|
||||||
|
import model.playerModel.PlayerModel;
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
@@ -8,7 +9,8 @@ import player.AlphaBetaComPlayer;
|
|||||||
import player.HumanPlayer;
|
import player.HumanPlayer;
|
||||||
import player.Player;
|
import player.Player;
|
||||||
import view.BoardPanel;
|
import view.BoardPanel;
|
||||||
import view.MessagePanel;
|
import view.HighScoreDialog;
|
||||||
|
import view.MainFrame;
|
||||||
import view.ScorePanel;
|
import view.ScorePanel;
|
||||||
|
|
||||||
public class Referee implements Runnable {
|
public class Referee implements Runnable {
|
||||||
@@ -20,17 +22,17 @@ public class Referee implements Runnable {
|
|||||||
|
|
||||||
public static final String PLAYER_TURN = "Waiting for the player's move.";
|
public static final String PLAYER_TURN = "Waiting for the player's move.";
|
||||||
|
|
||||||
private final Board board;
|
private Board board;
|
||||||
private BoardPanel boardPanel;
|
private BoardPanel boardPanel;
|
||||||
private final Player computerPlayer;
|
private Player computerPlayer;
|
||||||
|
|
||||||
private final HumanPlayer humanPlayer = new HumanPlayer();
|
private final HumanPlayer humanPlayer = new HumanPlayer();
|
||||||
private MessagePanel messagePanel;
|
|
||||||
private ScorePanel scorePanel;
|
|
||||||
|
|
||||||
public Referee() {
|
private final MainFrame mf;
|
||||||
board = new Board();
|
|
||||||
computerPlayer = new AlphaBetaComPlayer();
|
public Referee(MainFrame mnFrm) {
|
||||||
|
mf = mnFrm;
|
||||||
|
initGame();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player getComputerPlayer() {
|
public Player getComputerPlayer() {
|
||||||
@@ -69,9 +71,31 @@ public class Referee implements Runnable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
initGame();
|
||||||
|
mf.updateBoard();
|
||||||
|
play();
|
||||||
|
new HighScoreDialog(mf, new PlayerModel());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBoardPanel(BoardPanel boardPanel) {
|
||||||
|
this.boardPanel = boardPanel;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ScorePanel getScorePanel() {
|
||||||
|
return mf.getScorePanel();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initGame() {
|
||||||
|
board = new Board();
|
||||||
|
computerPlayer = new AlphaBetaComPlayer();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void play() {
|
||||||
int plies = 0;
|
int plies = 0;
|
||||||
while (!isOver()) {
|
while (!isOver()) {
|
||||||
scorePanel.updateScore(getPlayerScore());
|
getScorePanel().updateScore(getPlayerScore());
|
||||||
if (board.isPlayerTurn()) {
|
if (board.isPlayerTurn()) {
|
||||||
boolean wasInterrupted = false;
|
boolean wasInterrupted = false;
|
||||||
while (!humanPlayer.isReady()) {
|
while (!humanPlayer.isReady()) {
|
||||||
@@ -97,7 +121,7 @@ public class Referee implements Runnable {
|
|||||||
playToken(mv);
|
playToken(mv);
|
||||||
}
|
}
|
||||||
|
|
||||||
messagePanel.updateMessage(getMessage());
|
mf.updateMessage(getMessage());
|
||||||
boardPanel.updateIcons();
|
boardPanel.updateIcons();
|
||||||
LOGGER.info("plies: " + plies++);
|
LOGGER.info("plies: " + plies++);
|
||||||
try {
|
try {
|
||||||
@@ -108,16 +132,4 @@ public class Referee implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBoardPanel(BoardPanel boardPanel) {
|
|
||||||
this.boardPanel = boardPanel;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMessagePanel(MessagePanel messagePanel) {
|
|
||||||
this.messagePanel = messagePanel;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setScorePanel(ScorePanel scorePanel) {
|
|
||||||
this.scorePanel = scorePanel;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,12 @@
|
|||||||
package view;
|
package view;
|
||||||
|
|
||||||
import javax.swing.BorderFactory;
|
import java.awt.GridBagConstraints;
|
||||||
|
import java.awt.GridBagLayout;
|
||||||
|
import java.awt.Insets;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
import javax.swing.JButton;
|
||||||
import javax.swing.JDialog;
|
import javax.swing.JDialog;
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
@@ -39,6 +45,8 @@ public class HighScoreDialog extends JDialog {
|
|||||||
public HighScoreDialog(JFrame owner, PlayerModel pm) {
|
public HighScoreDialog(JFrame owner, PlayerModel pm) {
|
||||||
super(owner, "Game over!", true);
|
super(owner, "Game over!", true);
|
||||||
|
|
||||||
|
GridBagLayout gbl = new GridBagLayout();
|
||||||
|
|
||||||
String text = "<html>";
|
String text = "<html>";
|
||||||
text += "<b>Game Over!</b>";
|
text += "<b>Game Over!</b>";
|
||||||
text += "<br><br>";
|
text += "<br><br>";
|
||||||
@@ -59,11 +67,60 @@ public class HighScoreDialog extends JDialog {
|
|||||||
text += "</ol></html>";
|
text += "</ol></html>";
|
||||||
|
|
||||||
JLabel label = new JLabel(text);
|
JLabel label = new JLabel(text);
|
||||||
label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
|
|
||||||
|
|
||||||
|
JButton newGame = new JButton("New Game");
|
||||||
|
newGame.addActionListener(new ActionListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
|
HighScoreDialog.this.setVisible(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
JButton exitButton = new JButton("Exit");
|
||||||
|
exitButton.addActionListener(new ActionListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
GridBagConstraints con = new GridBagConstraints();
|
||||||
|
con.gridheight = 1;
|
||||||
|
con.gridwidth = 6;
|
||||||
|
con.gridx = 0;
|
||||||
|
con.gridy = 0;
|
||||||
|
con.insets = new Insets(20, 20, 5, 20);
|
||||||
|
gbl.setConstraints(label, con);
|
||||||
|
|
||||||
|
con = new GridBagConstraints();
|
||||||
|
con.fill = GridBagConstraints.BOTH;
|
||||||
|
con.gridheight = 1;
|
||||||
|
con.gridwidth = 2;
|
||||||
|
con.gridx = 1;
|
||||||
|
con.gridy = 1;
|
||||||
|
con.insets = new Insets(5, 20, 20, 5);
|
||||||
|
gbl.setConstraints(newGame, con);
|
||||||
|
|
||||||
|
con = new GridBagConstraints();
|
||||||
|
con.fill = GridBagConstraints.BOTH;
|
||||||
|
con.gridheight = 1;
|
||||||
|
con.gridwidth = 2;
|
||||||
|
con.gridx = 3;
|
||||||
|
con.gridy = 1;
|
||||||
|
con.insets = new Insets(5, 5, 20, 20);
|
||||||
|
gbl.setConstraints(exitButton, con);
|
||||||
|
|
||||||
|
this.setLayout(gbl);
|
||||||
this.add(label);
|
this.add(label);
|
||||||
|
this.add(newGame);
|
||||||
|
this.add(exitButton);
|
||||||
|
|
||||||
this.pack();
|
this.pack();
|
||||||
this.setResizable(false);
|
this.setResizable(false);
|
||||||
|
this.setLocationRelativeTo(owner);
|
||||||
|
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
||||||
this.setVisible(true);
|
this.setVisible(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,42 +13,57 @@ public class MainFrame extends JFrame {
|
|||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private final Referee referee;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
MainFrame mainFrame = new MainFrame();
|
MainFrame mainFrame = new MainFrame();
|
||||||
mainFrame.playGame();
|
mainFrame.playGame();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private BoardPanel bp;
|
||||||
|
private MessagePanel mp;
|
||||||
|
private final Referee referee;
|
||||||
|
private TileSelectionPanel tp;
|
||||||
|
ScorePanel sp;
|
||||||
|
|
||||||
public MainFrame() {
|
public MainFrame() {
|
||||||
super("CS8803 Project 4");
|
super("CS8803 Project 4");
|
||||||
referee = new Referee();
|
referee = new Referee(this);
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
|
||||||
setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
setVisible(true);
|
|
||||||
pack();
|
pack();
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
setVisible(true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScorePanel getScorePanel() {
|
||||||
|
return sp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateBoard() {
|
||||||
|
bp.updateIcons();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateMessage(String message) {
|
||||||
|
mp.updateMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void init() {
|
private void init() {
|
||||||
ScorePanel sp = new ScorePanel(referee);
|
sp = new ScorePanel(referee);
|
||||||
referee.setScorePanel(sp);
|
|
||||||
|
|
||||||
TileSelectionPanel tp = new TileSelectionPanel(referee.getHumanPlayer());
|
tp = new TileSelectionPanel(referee.getHumanPlayer());
|
||||||
BoardPanel bp = new BoardPanel(referee,tp);
|
bp = new BoardPanel(referee, tp);
|
||||||
referee.setBoardPanel(bp);
|
referee.setBoardPanel(bp);
|
||||||
|
|
||||||
MessagePanel mp = new MessagePanel(referee);
|
mp = new MessagePanel(referee);
|
||||||
referee.setMessagePanel(mp);
|
|
||||||
|
|
||||||
// The outer wrapper allows the game board to be resized vertically.
|
// The outer wrapper allows the game board to be resized vertically.
|
||||||
JPanel vWrapper = new JPanel();
|
JPanel vWrapper = new JPanel();
|
||||||
vWrapper.setLayout(new BoxLayout(vWrapper, BoxLayout.Y_AXIS));
|
vWrapper.setLayout(new BoxLayout(vWrapper, BoxLayout.Y_AXIS));
|
||||||
|
|
||||||
//The inner wrappers allow each sub-panel to be independently resized horizontally.
|
// The inner wrappers allow each sub-panel to be independently resized
|
||||||
|
// horizontally.
|
||||||
JPanel hWrapperTop = new JPanel();
|
JPanel hWrapperTop = new JPanel();
|
||||||
hWrapperTop.setLayout(new BoxLayout(hWrapperTop, BoxLayout.X_AXIS));
|
hWrapperTop.setLayout(new BoxLayout(hWrapperTop, BoxLayout.X_AXIS));
|
||||||
JPanel hWrapperMid = new JPanel();
|
JPanel hWrapperMid = new JPanel();
|
||||||
@@ -78,11 +93,13 @@ public class MainFrame extends JFrame {
|
|||||||
vWrapper.add(hWrapperMid);
|
vWrapper.add(hWrapperMid);
|
||||||
vWrapper.add(hWrapperBottom);
|
vWrapper.add(hWrapperBottom);
|
||||||
|
|
||||||
//The main JFrame contains a the outer (vertical) wrapper, in the center of a BorderLayout.
|
// The main JFrame contains a the outer (vertical) wrapper, in the
|
||||||
|
// center of a BorderLayout.
|
||||||
setLayout(new BorderLayout());
|
setLayout(new BorderLayout());
|
||||||
add(vWrapper, BorderLayout.CENTER);
|
add(vWrapper, BorderLayout.CENTER);
|
||||||
|
|
||||||
//To ensure correct size, pre-populate the score and message panels with text.
|
// To ensure correct size, pre-populate the score and message panels
|
||||||
|
// with text.
|
||||||
sp.updateScore(0);
|
sp.updateScore(0);
|
||||||
mp.updateMessage("Loading new game...");
|
mp.updateMessage("Loading new game...");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user