Advertisement
shadowsofme

SurroundPanel

Feb 7th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.20 KB | None | 0 0
  1. package package1;
  2.  
  3. import javax.swing.*;
  4.  
  5. import java.awt.event.*;
  6. import java.awt.*;
  7. import java.util.*;
  8. import java.io.*;
  9.  
  10. import javax.swing.JOptionPane;
  11.  
  12. public class SurroundPanel extends JPanel{
  13.     public enum GameStatus{
  14.         Player1Won, Player2Won, Cats, InProgress
  15.     }
  16.  
  17.     /** board */
  18.     private JButton[][] buttonBoard;
  19.  
  20.     /** game */
  21.     private SurroundGame game;
  22.  
  23.     /** String */
  24.     private String message;
  25.  
  26.     /** JLabels */
  27.     private JLabel messageLabel;
  28.  
  29.     /** board size */
  30.     private final static int BDSIZE = 10;
  31.  
  32.     /** menu items */
  33.     private JMenuBar menus;
  34.     private JMenu fileMenu;
  35.     private JMenuItem quitItem;
  36.     private JMenuItem newGameItem;
  37.  
  38.     /** frames */
  39.     private JFrame theGUI;
  40.  
  41.     /** JPanels */
  42.     private JPanel buttonPanel;
  43.     private JPanel messagePanel;
  44.  
  45.  
  46.     /**********************************************************************
  47.     Main method; creates the window for the program and
  48.     everything it uses.
  49.      *********************************************************************/
  50.     public static void main(String arg[]){
  51.         JFrame frame = new JFrame ("Surround");
  52.         frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  53.  
  54.         frame.getContentPane().add(new SurroundPanel());
  55.  
  56.         frame.pack();
  57.         frame.setVisible(true);
  58.     }
  59.  
  60.     public SurroundPanel(){
  61.         game = new SurroundGame();
  62.  
  63.         buttonBoard = new JButton [game.getRows()][game.getCol()];
  64.  
  65.         //Creates the button listener for the game.
  66.         ButtonListener listener = new ButtonListener();
  67.  
  68.         // Creates the window for the program.
  69.         theGUI = new JFrame("Surround");
  70.         theGUI.setVisible(true);
  71.         theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  72.         theGUI.setPreferredSize (new Dimension(900, 900));
  73.  
  74.         buttonPanel = new JPanel(new GridLayout(game.getRows(), game.getCol()));
  75.         for (int row = 0; row < game.getRows(); row++){
  76.             for (int col = 0; col <  game.getCol(); col++) {
  77.                 buttonBoard[row][col] = new JButton(" ");
  78.                 buttonBoard[row][col].addActionListener(listener);
  79.                 buttonPanel.add(buttonBoard[row][col]);
  80.             }
  81.         }
  82.         buttonPanel.setPreferredSize (new Dimension(800, 800));
  83.  
  84.         messagePanel = new JPanel();
  85.  
  86.         message = game.getMessage();
  87.         messageLabel = new JLabel(message);
  88.         messagePanel.add(messageLabel);
  89.  
  90.         theGUI.add(BorderLayout.SOUTH, buttonPanel);
  91.         theGUI.add(BorderLayout.WEST,messagePanel);
  92.  
  93.         // Sets up the File menus
  94.         setupMenus();
  95.         theGUI.pack();
  96.     }
  97.  
  98.     public class ButtonListener implements ActionListener{
  99.  
  100.         public void refreshButtons() {
  101.             for(int row= 0; row < game.getRows();row++) {
  102.                 for (int col = 0; col < game.getCol();col++) {
  103.                     Cell c = game.getCell(row,col);
  104.                     if (c.getPlayerNumber() == null) {
  105.                         buttonBoard[row][col].setText("");
  106.                     }else {
  107.                         buttonBoard[row][col].setText("" +c.getName());
  108.                     }
  109.                 }
  110.             }
  111.         }
  112.  
  113.         public void actionPerformed(ActionEvent e){
  114.             Object comp = e.getSource();
  115.             if(comp == newGameItem){
  116.                 game.reset();
  117.                 refreshButtons();
  118.                 while (game.currentPlayer() != 1){
  119.                     game.nextPlayer();
  120.                 }
  121.                 JOptionPane.showMessageDialog(null, "Board reset.\nClick anywhere to start"
  122.                         + " a new game.");
  123.             }else if(comp == quitItem){
  124.                 System.exit(1);
  125.             }else{
  126.                 for (int row = 0; row < game.getRows(); row++)
  127.                     for(int col = 0; col < game.getCol(); col++){
  128.                         if(buttonBoard[row][col] == comp){
  129.                             if(game.select(row, col)){ 
  130.                                 game.cellBoard[row][col] = new Cell(game.currentPlayer(), game.getCurrentPlayerName());
  131.                                 game.cellBoard[row][col].setName(game.getCurrentPlayerName());
  132.                                 game.nextPlayer();
  133.                             }else
  134.                                 JOptionPane.showMessageDialog(null, "Pick again.");
  135.                         }
  136.                     }
  137.                 for(int row=0; row<game.getRows(); row++)
  138.                     for (int col=0; col< game.getCol(); col++){
  139.                         Cell c = game.getCell(row,col); //Cell is a class created in step 5
  140.                         if(c.getPlayerNumber() != null){
  141.                             buttonBoard[row][col].setText("" + c.getName());   
  142.                         }else {
  143.                             buttonBoard[row][col].setText("");
  144.                         }
  145.                     }
  146.                 int winner = game.isWinner();
  147.                 if(winner != -1){
  148.                     if (winner != 0){
  149.                         game.addToScore(winner);
  150.                         messageLabel.setText(game.setMessage());
  151.                         JOptionPane.showMessageDialog(null,  "Player " + winner + " Wins!");
  152.                         game.reset();
  153.                         refreshButtons();
  154.                         int dialogResult  = JOptionPane.showConfirmDialog(null, "Will the same player "
  155.                                 + "start again?","Player Select", JOptionPane.YES_NO_OPTION);
  156.                         if (dialogResult  == JOptionPane.YES_OPTION){
  157.                             game.setPlayer(game.getStartPlayer());
  158.                             game.setStartPlayer(game.currentPlayer());
  159.                             JOptionPane.showMessageDialog(null, "Board reset. Player " + game.getStartPlayer()
  160.                                     + " will begin. Click anywhere to start the game.");
  161.                         }else {
  162.                             // Asks the user which player will begin the match.
  163.                             String string4 = JOptionPane.showInputDialog("Which player will begin the match?:"
  164.                                     + "\n(NOTE: Non-numeric characters will be ignored)");
  165.  
  166.                             // Removes any non-numeric values.
  167.                             string4 = string4.replaceAll("[^0-9.]", "");
  168.  
  169.                             // So long as the user entered an int above zero and less than or equal to the set number of players, the
  170.                             // game will have the player who was selected go first. Otherwise, player #1 goes first.
  171.                             if (string4.equals("") == true || Integer.parseInt(string4) <= 0 || Integer.parseInt(string4) > game.getPlayers()){
  172.                                 JOptionPane.showMessageDialog (
  173.                                         null, "Please enter a valid player number for who will begin.\nPlayer "
  174.                                                 + game.getStartPlayer() + " will begin.",
  175.                                                 "Number Error",
  176.                                                 JOptionPane.ERROR_MESSAGE);
  177.                                 game.setPlayer(game.getStartPlayer());
  178.                                 game.setStartPlayer(game.currentPlayer());
  179.                             }else {
  180.                                 game.setPlayer(Integer.parseInt(string4));
  181.                                 game.setStartPlayer(game.currentPlayer());
  182.                                 JOptionPane.showMessageDialog(null, "Board reset. Player " + game.currentPlayer()
  183.                                         + " will begin. Click anywhere to start the game.");
  184.                             }
  185.                         }
  186.                     }else {
  187.                         JOptionPane.showMessageDialog(null,  "Tie game. Resetting board...");
  188.                         game.reset();
  189.                         refreshButtons();
  190.                         int dialogResult  = JOptionPane.showConfirmDialog(null, "Will the same player "
  191.                                 + "start again?","Player Select", JOptionPane.YES_NO_OPTION);
  192.                         if (dialogResult  == JOptionPane.YES_OPTION){
  193.                             game.setPlayer(game.getStartPlayer());
  194.                             game.setStartPlayer(game.currentPlayer());
  195.                         }else {
  196.                             // Asks the user which player will begin the match.
  197.                             String string4 = JOptionPane.showInputDialog("Which player will begin the match?:"
  198.                                     + "\n(NOTE: Non-numeric characters will be ignored)");
  199.  
  200.                             // Removes any non-numeric values.
  201.                             string4 = string4.replaceAll("[^0-9.]", "");
  202.  
  203.                             // So long as the user entered an int above zero and less than or equal to the set number of players, the
  204.                             // game will have the player who was selected go first. Otherwise, player #1 goes first.
  205.                             if (string4.equals("") == true || Integer.parseInt(string4) <= 0 || Integer.parseInt(string4) > game.getPlayers()){
  206.                                 JOptionPane.showMessageDialog (
  207.                                         null, "Please enter a valid player number for who will begin.\nPlayer "
  208.                                                 + game.getStartPlayer() + " will begin.",
  209.                                                 "Number Error",
  210.                                                 JOptionPane.ERROR_MESSAGE);
  211.                                 game.setPlayer(game.getStartPlayer());
  212.                                 game.setStartPlayer(game.currentPlayer());
  213.                             }else {
  214.                                 game.setPlayer(Integer.parseInt(string4));
  215.                                 game.setStartPlayer(game.currentPlayer());
  216.                                 JOptionPane.showMessageDialog(null, "Player " + game.currentPlayer()
  217.                                         + " will begin. Click anywhere to start the game.");
  218.                             }
  219.                         }
  220.                     }
  221.                 }
  222.             }
  223.         }
  224.     }
  225.     private void setupMenus(){
  226.  
  227.         // create menu components
  228.         fileMenu = new JMenu("File");
  229.         quitItem = new JMenuItem("Quit");
  230.         newGameItem = new JMenuItem("New Game");
  231.  
  232.         // assign action listeners
  233.         ButtonListener ml = new ButtonListener();
  234.         quitItem.addActionListener(ml);
  235.         newGameItem.addActionListener(ml);
  236.  
  237.         // display menu components
  238.         fileMenu.add(newGameItem);
  239.         fileMenu.add(quitItem);
  240.         menus = new JMenuBar();
  241.  
  242.         menus.add(fileMenu);
  243.         theGUI.setJMenuBar(menus);
  244.     }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement