package game.cc.gui;
// deleted import list to save space
public class GUI implements ActionListener {
Player Player = new Player();
Board Board = new Board();
Options Options = new Options();
Save_Game Save_Game = new Save_Game();
Variables Variables = new Variables();
// JButton backButton = new JButton("Back");
ImageIcon pretzel = new ImageIcon("pretzel.gif");
ImageIcon cheese = new ImageIcon("game/cheese.gif");
boolean gameInProgress = false;
String player1Name;
String player2Name;
int windowSize;
JFrame frame;
JPanel gameMain = new JPanel(), buttonHolder = new JPanel(),
pnlNorth = new JPanel(), pnlSouth = new JPanel(),
pnlTop = new JPanel(), pnlBottom = new JPanel(),
gameSetup = new JPanel();
JMenuBar menuBar;
JMenu file, help;
JMenuItem helpMenu, aboutGame, newGame, saveGame, loadGame, options, exit;
String message;
JTextArea score, playerTurn, helpMenuText, aboutTxt;
public void GUI() {
System.out.println("checking image");
System.out.println(cheese);
System.out.println(pretzel);
// design game window frame
frame = new JFrame("Cheese & Pretzels" + Constants.VERSION);
frame.setLayout(new BorderLayout());
frame.setSize(Variables.getX(), Variables.getY());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Setting Panel layouts and properties
buttonHolder.setLayout(new GridLayout(2, 1, 2, 10));
pnlNorth.setLayout(new FlowLayout(FlowLayout.CENTER));
pnlSouth.setLayout(new FlowLayout(FlowLayout.CENTER));
pnlNorth.setBackground(new Color(Variables.getColor() - 20, Variables
.getColor() - 20, Variables.getColor() - 20));
pnlSouth.setBackground(new Color(Variables.getColor(), Variables
.getColor(), Variables.getColor()));
pnlTop.setBackground(new Color(Variables.getColor(), Variables
.getColor(), Variables.getColor()));
pnlBottom.setBackground(new Color(Variables.getColor(), Variables
.getColor(), Variables.getColor()));
pnlTop.setLayout(new FlowLayout(FlowLayout.CENTER));
pnlBottom.setLayout(new FlowLayout(FlowLayout.CENTER));
buttonHolder.setBackground(Color.blue);
// gameSetup.add(backButton);
// backButton.addActionListener(this);
buttonHolder.setLayout(new GridLayout(Variables.getBoardSize(),
Variables.getBoardSize(), 2, 2));
buttonHolder.setBackground(Color.black);
frame.add(gameMain);
frame.add(buttonHolder);
menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
file = new JMenu("File");
menuBar.add(file);
file.addActionListener(this);
help = new JMenu("Help");
menuBar.add(help);
help.addActionListener(this);
helpMenu = new JMenuItem("Cheese and Pretzels Help");
help.add(helpMenu);
helpMenu.addActionListener(this);
aboutGame = new JMenuItem("About Cheese and Pretzels");
help.add(aboutGame);
aboutGame.addActionListener(this);
newGame = new JMenuItem("New Game");
file.add(newGame);
newGame.addActionListener(this);
saveGame = new JMenuItem("Save Game");
file.add(saveGame);
saveGame.addActionListener(this);
loadGame = new JMenuItem("Load Game");
file.add(loadGame);
loadGame.addActionListener(this);
options = new JMenuItem("Options");
file.add(options);
options.addActionListener(this);
exit = new JMenuItem("Exit");
file.add(exit);
exit.addActionListener(this);
setName(); // call set name method to assist in creating interface
score = new JTextArea(player1Name + " Score : "
+ Player.getPlayerScore(1) + " " + player2Name
+ " Score: " + Player.getPlayerScore(2));
score.setBackground(Color.GRAY);
playerTurn = new JTextArea("Your turn, Player "
+ Player.getPlayerTurn());
playerTurn.setEditable(false);
;
score.setEditable(false);
pnlSouth.add(score);
pnlNorth.add(pnlTop);
pnlNorth.add(pnlBottom);
// pnlSouth.add(backButton);
for (int i = 0; i < Variables.getBoardSize() * Variables.getBoardSize(); i++) {
Variables.getButtons()[i] = new JButton();
Variables.getButtons()[i].setText("");
Variables.getButtons()[i].setBackground(new Color(Variables
.getButtonColor(), Variables.getButtonColor(), Variables
.getButtonColor()));
Variables.getButtons()[i].addActionListener(this);
buttonHolder.add(Variables.getButtons()[i]);
}
// Adding to window and Showing window
frame.add(pnlNorth, BorderLayout.NORTH);
frame.add(pnlSouth, BorderLayout.SOUTH);
frame.setVisible(true);
for (int i = 0; i < 49; i++) { // set all buttons unused
Variables.getButtonUsed()[i] = false;
}
}
public void actionPerformed(ActionEvent click) {
Object source = click.getSource();
for (int i = 1; i <= Variables.getBoardSize()
* Variables.getBoardSize(); i++) { // place
// x or
// o on
// button based on
// player turn
if (source == Variables.getButtons()[i]
&& !Variables.getButtonUsed()[i]) {
Variables.getButtonUsed()[i] = true;
if (Player.getPlayerTurn() == 1) {
Variables.getButtons()[i].setText("X");
// buttons[i].setIcon(pretzel);
}
else
Variables.getButtons()[i].setText("O");
// buttons[i].setIcon(cheese);
Variables.getButtons()[i].setEnabled(false);
buttonHolder.requestFocusInWindow();
Player.changePlayer(Player.getPlayerTurn());
} // end if
} // end for
// Board.checkBoard(gameBoard, playerNumber); // update new check
// board
// method
if (source == newGame) {
pnlSouth.setLayout(new GridLayout(2, 1, 2, 5));
pnlTop.add(gameMain);
pnlSouth.add(pnlTop);
pnlSouth.add(pnlBottom);
// pnlBottom.add(backButton);
for (int i = 0; i < Variables.getBoardSize()
* Variables.getBoardSize(); i++) {
Variables.getButtons()[i].setText("");
Variables.getButtonUsed()[i] = false;
Variables.getButtons()[i].setEnabled(true);
}
// need to decide whether to set this here or in options pane
}/**
* else if (source == twoPlayerGame) { if (gameInProgress) { int option
* = JOptionPane.showConfirmDialog(null, "If you start a new game," +
* "your current game will be lost..." + "\n" +
* "Are you sure you want to continue?", "Quit Game?",
* JOptionPane.YES_NO_OPTION); if (option == JOptionPane.YES_OPTION) {
* gameInProgress = false; } } if (!gameInProgress) { clearBoard();
* Player.getPlayerTurn(); for (int i = 1; i < 10; i++) {
* buttons[i].setText(""); buttons[i].setEnabled(true); } // win =
* false; // showGame();
*
* } } else if (source == onePlayerGame) {
* JOptionPane.showMessageDialog(null, "Coming Soon!!");
*
* }
*/
else if (source == exit) { // replace exit action option with this.
int option = JOptionPane.showConfirmDialog(null,
"Do you want to save your game?", "Exit Game",
JOptionPane.YES_NO_OPTION);
if (option == JOptionPane.NO_OPTION)
System.exit(0);
else
try {
Save_Game.save();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // correct to call the save game class.
} else if (source == helpMenu) {
clearPanelSouth();
message = "Instructions:\n\n"
+ "Your goal is to be the first player to fill a straight line with your pieces,\n"
+ " horizontally, diagonally, or vertically.";
helpMenuText = new JTextArea(message);
helpMenuText.setBackground(new Color(Variables.getColor(),
Variables.getColor(), Variables.getColor()));
helpMenuText.setEditable(false);
helpMenuText.setText(message);
pnlSouth.setLayout(new GridLayout(2, 1, 2, 5));
pnlTop.add(helpMenuText);
// pnlBottom.add(backButton);
pnlSouth.add(pnlTop);
pnlSouth.add(pnlBottom);
} else if (source == aboutGame) {
clearPanelSouth();
message = "About:\n\n" + "Title: Cheese & Pretzels\n"
+ "Author: Joanna Alcausin\n" + "Version: "
+ Constants.VERSION + "\n";
aboutTxt = new JTextArea(message);
aboutTxt.setBackground(new Color(Variables.getColor(), Variables
.getColor(), Variables.getColor()));
aboutTxt.setEditable(false);
aboutTxt.setText(message);
pnlSouth.setLayout(new GridLayout(2, 1, 2, 5));
pnlTop.add(aboutTxt);
// pnlBottom.add(backButton);
pnlSouth.add(pnlTop);
pnlSouth.add(pnlBottom);
}
// else if (source == backButton) {
// clearPanelSouth();
// pnlSouth.setLayout(new FlowLayout(FlowLayout.CENTER));
// pnlNorth.setVisible(true);
// }
else if (source == options) {
frame.remove(buttonHolder);
frame.add(Options.getPanel());
}
// else if (source == Options.close) {
// }
pnlSouth.setVisible(false);
pnlSouth.setVisible(true);
}
/**
* allows player to set the names of self and opponent upon opening new game
*
*/
public void setName() {
this.player1Name = JOptionPane.showInputDialog(null,
"Enter Name of player 1: ", "", 1);
this.player2Name = JOptionPane.showInputDialog(null,
"Enter Name of player 2: ", "", 1);
}
public void closeWindow() {
frame.remove(Options);
frame.add(gameMain);
frame.add(buttonHolder);
}
public void clearBoard() {
for (int i = 0; i < Variables.getBoardSize() * Variables.getBoardSize(); i++) {
Variables.getButtons()[i].setText("");
Variables.getButtonUsed()[i] = false;
}
}
public void clearPanelSouth() {
pnlSouth.removeAll();
}
}