Advertisement
Guest User

mathias

a guest
May 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.89 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.MouseAdapter;
  8. import java.awt.event.MouseEvent;
  9.  
  10. public class Game extends JFrame implements ActionListener {
  11.     private Mode gameMode = Mode.STANDARD;
  12.     private Position position = new Position();
  13.  
  14.     private JButton[] buttons = new JButton[Position.SIZE];
  15.     private JMenuBar menuBar = new JMenuBar();
  16.     private JMenu fileMenu = new JMenu("File");
  17.     private JMenu gameMenu = new JMenu("New Game");
  18.     private JMenuItem restart = new JMenuItem("Restart Game");
  19.     private JMenuItem exit = new JMenuItem("Exit");
  20.     private JMenuItem standard = new JMenuItem("1 vs 1");
  21.     private JMenuItem computer = new JMenuItem("Player vs AI");
  22.  
  23.     public Game() {
  24.         createMenu();
  25.         addActionListeners();
  26.  
  27.         setLayout(new GridLayout(Position.DIM, Position.DIM));
  28.  
  29.         playGame(this.gameMode);
  30.  
  31.         pack();
  32.         setVisible(true);
  33.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  34.     }
  35.  
  36.     // Starts a game with the given gameMode. The user can change the gameMode later if they desire.
  37.     private void playGame(Mode gameMode) {
  38.         switch(gameMode) {
  39.             case STANDARD:
  40.                 playStandard();
  41.                 break;
  42.             case COMPUTER:
  43.                 playComputer();
  44.                 break;
  45.         }
  46.     }
  47.  
  48.     // Play a match against another player locally
  49.     private void playStandard() {
  50.         for (int i = 0; i < Position.SIZE; i++) {
  51.             final JButton button = createButton();
  52.             final int idx = i;
  53.             this.buttons[i] = button;
  54.             button.addMouseListener(new MouseAdapter() {
  55.                 @Override
  56.                 public void mouseClicked(MouseEvent e) {
  57.                     playerMove(button, idx);
  58.                     drawEndGameText(gameMode);
  59.                 }
  60.             });
  61.         }
  62.     }
  63.  
  64.     // Play a match against the computer
  65.     private void playComputer() {
  66.         for (int i = 0; i < Position.SIZE; i++) {
  67.             final JButton button = createButton();
  68.             final int idx = i;
  69.             this.buttons[i] = button;
  70.             button.addMouseListener(new MouseAdapter() {
  71.                 @Override
  72.                 public void mouseClicked(MouseEvent e) {
  73.                     playerMove(button, idx);
  74.                     computerMove();
  75.                     drawEndGameText(gameMode);
  76.                 }
  77.             });
  78.         }
  79.     }
  80.  
  81.     // Allows the user to make a move and prepares the game for the next turn
  82.     private void playerMove(JButton button, int idx) {
  83.         if(button.isEnabled()) {
  84.             button.setText(Character.toString(this.position.turn));
  85.             button.setEnabled(false);
  86.             this.position.move(idx);
  87.         }
  88.     }
  89.  
  90.     // The computer analyzes the best move given the current board and makes it
  91.     private void computerMove() {
  92.         if (!this.position.isGameEnd()) {
  93.             int best = position.bestMove();
  94.             this.buttons[best].setText(Character.toString(this.position.turn));
  95.             this.buttons[best].setEnabled(false);
  96.             this.position.move(best);
  97.         }
  98.     }
  99.  
  100.     private void drawEndGameText(Mode gameMode) {
  101.         if(this.position.isGameEnd()) {
  102.             String message;
  103.             switch (gameMode) {
  104.  
  105.                 // If two players are playing against each other, show which symbol won
  106.                 case STANDARD:
  107.                     message = this.position.isWinFor('x') ? "'X' won!" :
  108.                             this.position.isWinFor('o') ? "'O' Won!" : "Draw";
  109.                     JOptionPane.showMessageDialog(this, message);
  110.                     break;
  111.  
  112.                 // If you are playing against the computer, show whether you or it won
  113.                 case COMPUTER:
  114.                     message = this.position.isWinFor('x') ? "You won!" :
  115.                             this.position.isWinFor('o') ? "Computer Won!" : "Draw";
  116.                     JOptionPane.showMessageDialog(this, message);
  117.                     break;
  118.             }
  119.         }
  120.     }
  121.  
  122.     // Create a single button that goes on the game board
  123.     private JButton createButton() {
  124.         JButton button = new JButton();
  125.         button.setPreferredSize(new Dimension(100, 100));
  126.         button.setBackground(Color.WHITE);
  127.         button.setOpaque(true);
  128.         button.setFont(new Font(null, Font.PLAIN, 50));
  129.         this.add(button);
  130.         return button;
  131.     }
  132.  
  133.     // Reset all variables so that the game can start over again
  134.     private void startGame() {
  135.         this.buttons = new JButton[Position.SIZE];
  136.         this.position = new Position();
  137.         this.getContentPane().removeAll();
  138.         playGame(this.gameMode);
  139.         this.repaint();
  140.         this.validate();
  141.     }
  142.  
  143.     // Handle menu choices
  144.     @Override
  145.     public void actionPerformed(ActionEvent e) {
  146.         if(e.getSource() == this.standard) {
  147.             this.gameMode = Mode.STANDARD;
  148.         } else if(e.getSource() == computer) {
  149.             this.gameMode = Mode.COMPUTER;
  150.         } else if(e.getSource() == this.exit) {
  151.             System.exit(0);
  152.         }
  153.         startGame();
  154.     }
  155.  
  156.     // Add action listeners to handle menu choices
  157.     private void addActionListeners() {
  158.         this.standard.addActionListener(this);
  159.         this.computer.addActionListener(this);
  160.         this.restart.addActionListener(this);
  161.         this.exit.addActionListener(this);
  162.     }
  163.  
  164.     // Create the basic game menu
  165.     private void createMenu() {
  166.         this.setJMenuBar(menuBar);
  167.         this.menuBar.add(fileMenu);
  168.         this.fileMenu.add(gameMenu);
  169.         this.fileMenu.add(restart);
  170.         this.fileMenu.add(exit);
  171.         this.gameMenu.add(standard);
  172.         this.gameMenu.add(computer);
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement