- 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; 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;
}
} }

View File

@@ -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);
} }
} }

View File

@@ -13,85 +13,102 @@ 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);
tp = new TileSelectionPanel(referee.getHumanPlayer());
TileSelectionPanel tp = new TileSelectionPanel(referee.getHumanPlayer()); bp = new BoardPanel(referee, tp);
BoardPanel 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();
hWrapperMid.setLayout(new BoxLayout(hWrapperMid,BoxLayout.X_AXIS)); hWrapperMid.setLayout(new BoxLayout(hWrapperMid, BoxLayout.X_AXIS));
JPanel hWrapperBottom = new JPanel(); JPanel hWrapperBottom = new JPanel();
hWrapperBottom.setLayout(new BorderLayout()); 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(Box.createHorizontalGlue());
hWrapperTop.add(mp); hWrapperTop.add(mp);
hWrapperTop.add(sp); hWrapperTop.add(sp);
hWrapperTop.add(Box.createHorizontalGlue()); hWrapperTop.add(Box.createHorizontalGlue());
//Mid horizontal box wrapper contains board panel // Mid horizontal box wrapper contains board panel
hWrapperMid.add(Box.createHorizontalGlue()); hWrapperMid.add(Box.createHorizontalGlue());
JPanel bpContainer = new JPanel(); JPanel bpContainer = new JPanel();
bpContainer.add(bp); bpContainer.add(bp);
hWrapperMid.add(bpContainer); hWrapperMid.add(bpContainer);
hWrapperMid.add(Box.createHorizontalGlue()); hWrapperMid.add(Box.createHorizontalGlue());
//Bottom horizontal box wrapper contains tile panel // Bottom horizontal box wrapper contains tile panel
hWrapperBottom.add(tp,BorderLayout.NORTH); 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(hWrapperTop);
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...");
pack(); 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(); tp.updateBorders();
} }
private void playGame() { private void playGame() {
new Thread(referee).start(); new Thread(referee).start();
} }