Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package package1;
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.*;
- import java.util.*;
- import java.io.*;
- import javax.swing.JOptionPane;
- public class SurroundPanel extends JPanel{
- public enum GameStatus{
- Player1Won, Player2Won, Cats, InProgress
- }
- /** board */
- private JButton[][] buttonBoard;
- /** game */
- private SurroundGame game;
- /** String */
- private String message;
- /** JLabels */
- private JLabel messageLabel;
- /** board size */
- private final static int BDSIZE = 10;
- /** menu items */
- private JMenuBar menus;
- private JMenu fileMenu;
- private JMenuItem quitItem;
- private JMenuItem newGameItem;
- /** frames */
- private JFrame theGUI;
- /** JPanels */
- private JPanel buttonPanel;
- private JPanel messagePanel;
- /**********************************************************************
- Main method; creates the window for the program and
- everything it uses.
- *********************************************************************/
- public static void main(String arg[]){
- JFrame frame = new JFrame ("Surround");
- frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().add(new SurroundPanel());
- frame.pack();
- frame.setVisible(true);
- }
- public SurroundPanel(){
- game = new SurroundGame();
- buttonBoard = new JButton [game.getRows()][game.getCol()];
- //Creates the button listener for the game.
- ButtonListener listener = new ButtonListener();
- // Creates the window for the program.
- theGUI = new JFrame("Surround");
- theGUI.setVisible(true);
- theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- theGUI.setPreferredSize (new Dimension(900, 900));
- buttonPanel = new JPanel(new GridLayout(game.getRows(), game.getCol()));
- for (int row = 0; row < game.getRows(); row++){
- for (int col = 0; col < game.getCol(); col++) {
- buttonBoard[row][col] = new JButton(" ");
- buttonBoard[row][col].addActionListener(listener);
- buttonPanel.add(buttonBoard[row][col]);
- }
- }
- buttonPanel.setPreferredSize (new Dimension(800, 800));
- messagePanel = new JPanel();
- message = game.getMessage();
- messageLabel = new JLabel(message);
- messagePanel.add(messageLabel);
- theGUI.add(BorderLayout.SOUTH, buttonPanel);
- theGUI.add(BorderLayout.WEST,messagePanel);
- // Sets up the File menus
- setupMenus();
- theGUI.pack();
- }
- public class ButtonListener implements ActionListener{
- public void refreshButtons() {
- for(int row= 0; row < game.getRows();row++) {
- for (int col = 0; col < game.getCol();col++) {
- Cell c = game.getCell(row,col);
- if (c.getPlayerNumber() == null) {
- buttonBoard[row][col].setText("");
- }else {
- buttonBoard[row][col].setText("" +c.getName());
- }
- }
- }
- }
- public void actionPerformed(ActionEvent e){
- Object comp = e.getSource();
- if(comp == newGameItem){
- game.reset();
- refreshButtons();
- while (game.currentPlayer() != 1){
- game.nextPlayer();
- }
- JOptionPane.showMessageDialog(null, "Board reset.\nClick anywhere to start"
- + " a new game.");
- }else if(comp == quitItem){
- System.exit(1);
- }else{
- for (int row = 0; row < game.getRows(); row++)
- for(int col = 0; col < game.getCol(); col++){
- if(buttonBoard[row][col] == comp){
- if(game.select(row, col)){
- game.cellBoard[row][col] = new Cell(game.currentPlayer(), game.getCurrentPlayerName());
- game.cellBoard[row][col].setName(game.getCurrentPlayerName());
- game.nextPlayer();
- }else
- JOptionPane.showMessageDialog(null, "Pick again.");
- }
- }
- for(int row=0; row<game.getRows(); row++)
- for (int col=0; col< game.getCol(); col++){
- Cell c = game.getCell(row,col); //Cell is a class created in step 5
- if(c.getPlayerNumber() != null){
- buttonBoard[row][col].setText("" + c.getName());
- }else {
- buttonBoard[row][col].setText("");
- }
- }
- int winner = game.isWinner();
- if(winner != -1){
- if (winner != 0){
- game.addToScore(winner);
- messageLabel.setText(game.setMessage());
- JOptionPane.showMessageDialog(null, "Player " + winner + " Wins!");
- game.reset();
- refreshButtons();
- int dialogResult = JOptionPane.showConfirmDialog(null, "Will the same player "
- + "start again?","Player Select", JOptionPane.YES_NO_OPTION);
- if (dialogResult == JOptionPane.YES_OPTION){
- game.setPlayer(game.getStartPlayer());
- game.setStartPlayer(game.currentPlayer());
- JOptionPane.showMessageDialog(null, "Board reset. Player " + game.getStartPlayer()
- + " will begin. Click anywhere to start the game.");
- }else {
- // Asks the user which player will begin the match.
- String string4 = JOptionPane.showInputDialog("Which player will begin the match?:"
- + "\n(NOTE: Non-numeric characters will be ignored)");
- // Removes any non-numeric values.
- string4 = string4.replaceAll("[^0-9.]", "");
- // So long as the user entered an int above zero and less than or equal to the set number of players, the
- // game will have the player who was selected go first. Otherwise, player #1 goes first.
- if (string4.equals("") == true || Integer.parseInt(string4) <= 0 || Integer.parseInt(string4) > game.getPlayers()){
- JOptionPane.showMessageDialog (
- null, "Please enter a valid player number for who will begin.\nPlayer "
- + game.getStartPlayer() + " will begin.",
- "Number Error",
- JOptionPane.ERROR_MESSAGE);
- game.setPlayer(game.getStartPlayer());
- game.setStartPlayer(game.currentPlayer());
- }else {
- game.setPlayer(Integer.parseInt(string4));
- game.setStartPlayer(game.currentPlayer());
- JOptionPane.showMessageDialog(null, "Board reset. Player " + game.currentPlayer()
- + " will begin. Click anywhere to start the game.");
- }
- }
- }else {
- JOptionPane.showMessageDialog(null, "Tie game. Resetting board...");
- game.reset();
- refreshButtons();
- int dialogResult = JOptionPane.showConfirmDialog(null, "Will the same player "
- + "start again?","Player Select", JOptionPane.YES_NO_OPTION);
- if (dialogResult == JOptionPane.YES_OPTION){
- game.setPlayer(game.getStartPlayer());
- game.setStartPlayer(game.currentPlayer());
- }else {
- // Asks the user which player will begin the match.
- String string4 = JOptionPane.showInputDialog("Which player will begin the match?:"
- + "\n(NOTE: Non-numeric characters will be ignored)");
- // Removes any non-numeric values.
- string4 = string4.replaceAll("[^0-9.]", "");
- // So long as the user entered an int above zero and less than or equal to the set number of players, the
- // game will have the player who was selected go first. Otherwise, player #1 goes first.
- if (string4.equals("") == true || Integer.parseInt(string4) <= 0 || Integer.parseInt(string4) > game.getPlayers()){
- JOptionPane.showMessageDialog (
- null, "Please enter a valid player number for who will begin.\nPlayer "
- + game.getStartPlayer() + " will begin.",
- "Number Error",
- JOptionPane.ERROR_MESSAGE);
- game.setPlayer(game.getStartPlayer());
- game.setStartPlayer(game.currentPlayer());
- }else {
- game.setPlayer(Integer.parseInt(string4));
- game.setStartPlayer(game.currentPlayer());
- JOptionPane.showMessageDialog(null, "Player " + game.currentPlayer()
- + " will begin. Click anywhere to start the game.");
- }
- }
- }
- }
- }
- }
- }
- private void setupMenus(){
- // create menu components
- fileMenu = new JMenu("File");
- quitItem = new JMenuItem("Quit");
- newGameItem = new JMenuItem("New Game");
- // assign action listeners
- ButtonListener ml = new ButtonListener();
- quitItem.addActionListener(ml);
- newGameItem.addActionListener(ml);
- // display menu components
- fileMenu.add(newGameItem);
- fileMenu.add(quitItem);
- menus = new JMenuBar();
- menus.add(fileMenu);
- theGUI.setJMenuBar(menus);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement