- Finished HighScoreDialog.java. Now the player can play the game repeatedly.

This commit is contained in:
Marshall
2012-04-28 16:24:45 -04:00
parent 24096b50a8
commit 1a4c6d865b
3 changed files with 150 additions and 64 deletions

View File

@@ -1,6 +1,7 @@
package model;
import model.Board.TileColor;
import model.playerModel.PlayerModel;
import org.apache.log4j.Logger;
@@ -8,7 +9,8 @@ import player.AlphaBetaComPlayer;
import player.HumanPlayer;
import player.Player;
import view.BoardPanel;
import view.MessagePanel;
import view.HighScoreDialog;
import view.MainFrame;
import view.ScorePanel;
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.";
private final Board board;
private Board board;
private BoardPanel boardPanel;
private final Player computerPlayer;
private Player computerPlayer;
private final HumanPlayer humanPlayer = new HumanPlayer();
private MessagePanel messagePanel;
private ScorePanel scorePanel;
public Referee() {
board = new Board();
computerPlayer = new AlphaBetaComPlayer();
private final MainFrame mf;
public Referee(MainFrame mnFrm) {
mf = mnFrm;
initGame();
}
public Player getComputerPlayer() {
@@ -69,9 +71,31 @@ public class Referee implements Runnable {
@Override
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;
while (!isOver()) {
scorePanel.updateScore(getPlayerScore());
getScorePanel().updateScore(getPlayerScore());
if (board.isPlayerTurn()) {
boolean wasInterrupted = false;
while (!humanPlayer.isReady()) {
@@ -97,7 +121,7 @@ public class Referee implements Runnable {
playToken(mv);
}
messagePanel.updateMessage(getMessage());
mf.updateMessage(getMessage());
boardPanel.updateIcons();
LOGGER.info("plies: " + plies++);
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;
}
}

View File

@@ -1,6 +1,12 @@
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.JFrame;
import javax.swing.JLabel;
@@ -39,6 +45,8 @@ public class HighScoreDialog extends JDialog {
public HighScoreDialog(JFrame owner, PlayerModel pm) {
super(owner, "Game over!", true);
GridBagLayout gbl = new GridBagLayout();
String text = "<html>";
text += "<b>Game Over!</b>";
text += "<br><br>";
@@ -59,11 +67,60 @@ public class HighScoreDialog extends JDialog {
text += "</ol></html>";
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(newGame);
this.add(exitButton);
this.pack();
this.setResizable(false);
this.setLocationRelativeTo(owner);
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.setVisible(true);
}
}

View File

@@ -13,56 +13,71 @@ public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
private final Referee referee;
public static void main(String[] args) {
MainFrame mainFrame = new MainFrame();
mainFrame.playGame();
}
private BoardPanel bp;
private MessagePanel mp;
private final Referee referee;
private TileSelectionPanel tp;
ScorePanel sp;
public MainFrame() {
super("CS8803 Project 4");
referee = new Referee();
referee = new Referee(this);
init();
setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE);
setVisible(true);
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() {
ScorePanel sp = new ScorePanel(referee);
referee.setScorePanel(sp);
sp = new ScorePanel(referee);
TileSelectionPanel tp = new TileSelectionPanel(referee.getHumanPlayer());
BoardPanel bp = new BoardPanel(referee,tp);
tp = new TileSelectionPanel(referee.getHumanPlayer());
bp = new BoardPanel(referee, tp);
referee.setBoardPanel(bp);
MessagePanel mp = new MessagePanel(referee);
referee.setMessagePanel(mp);
mp = new MessagePanel(referee);
//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();
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();
hWrapperTop.setLayout(new BoxLayout(hWrapperTop,BoxLayout.X_AXIS));
hWrapperTop.setLayout(new BoxLayout(hWrapperTop, BoxLayout.X_AXIS));
JPanel hWrapperMid = new JPanel();
hWrapperMid.setLayout(new BoxLayout(hWrapperMid,BoxLayout.X_AXIS));
hWrapperMid.setLayout(new BoxLayout(hWrapperMid, BoxLayout.X_AXIS));
JPanel hWrapperBottom = new JPanel();
hWrapperBottom.setLayout(new BorderLayout());
//Top horizontal-box wrapper contains message panel, score panel
// Top horizontal-box wrapper contains message panel, score panel
hWrapperTop.add(Box.createHorizontalGlue());
hWrapperTop.add(mp);
hWrapperTop.add(sp);
hWrapperTop.add(Box.createHorizontalGlue());
//Mid horizontal box wrapper contains board panel
// Mid horizontal box wrapper contains board panel
hWrapperMid.add(Box.createHorizontalGlue());
JPanel bpContainer = new JPanel();
@@ -70,25 +85,27 @@ public class MainFrame extends JFrame {
hWrapperMid.add(bpContainer);
hWrapperMid.add(Box.createHorizontalGlue());
//Bottom horizontal box wrapper contains tile panel
hWrapperBottom.add(tp,BorderLayout.NORTH);
// Bottom horizontal box wrapper contains tile panel
hWrapperBottom.add(tp, BorderLayout.NORTH);
//the outter wrapper contains the 3 inner wrappers: top, mid, bottom
// the outter wrapper contains the 3 inner wrappers: top, mid, bottom
vWrapper.add(hWrapperTop);
vWrapper.add(hWrapperMid);
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());
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);
mp.updateMessage("Loading new game...");
pack();
//Finally, update the border to show the initially selected tile color.
// Finally, update the border to show the initially selected tile color.
tp.updateBorders();
}