Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- public class Game extends JFrame implements ActionListener {
- private Mode gameMode = Mode.STANDARD;
- private Position position = new Position();
- private JButton[] buttons = new JButton[Position.SIZE];
- private JMenuBar menuBar = new JMenuBar();
- private JMenu fileMenu = new JMenu("File");
- private JMenu gameMenu = new JMenu("New Game");
- private JMenuItem restart = new JMenuItem("Restart Game");
- private JMenuItem exit = new JMenuItem("Exit");
- private JMenuItem standard = new JMenuItem("1 vs 1");
- private JMenuItem computer = new JMenuItem("Player vs AI");
- public Game() {
- createMenu();
- addActionListeners();
- setLayout(new GridLayout(Position.DIM, Position.DIM));
- playGame(this.gameMode);
- pack();
- setVisible(true);
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- }
- // Starts a game with the given gameMode. The user can change the gameMode later if they desire.
- private void playGame(Mode gameMode) {
- switch(gameMode) {
- case STANDARD:
- playStandard();
- break;
- case COMPUTER:
- playComputer();
- break;
- }
- }
- // Play a match against another player locally
- private void playStandard() {
- for (int i = 0; i < Position.SIZE; i++) {
- final JButton button = createButton();
- final int idx = i;
- this.buttons[i] = button;
- button.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- playerMove(button, idx);
- drawEndGameText(gameMode);
- }
- });
- }
- }
- // Play a match against the computer
- private void playComputer() {
- for (int i = 0; i < Position.SIZE; i++) {
- final JButton button = createButton();
- final int idx = i;
- this.buttons[i] = button;
- button.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- playerMove(button, idx);
- computerMove();
- drawEndGameText(gameMode);
- }
- });
- }
- }
- // Allows the user to make a move and prepares the game for the next turn
- private void playerMove(JButton button, int idx) {
- if(button.isEnabled()) {
- button.setText(Character.toString(this.position.turn));
- button.setEnabled(false);
- this.position.move(idx);
- }
- }
- // The computer analyzes the best move given the current board and makes it
- private void computerMove() {
- if (!this.position.isGameEnd()) {
- int best = position.bestMove();
- this.buttons[best].setText(Character.toString(this.position.turn));
- this.buttons[best].setEnabled(false);
- this.position.move(best);
- }
- }
- private void drawEndGameText(Mode gameMode) {
- if(this.position.isGameEnd()) {
- String message;
- switch (gameMode) {
- // If two players are playing against each other, show which symbol won
- case STANDARD:
- message = this.position.isWinFor('x') ? "'X' won!" :
- this.position.isWinFor('o') ? "'O' Won!" : "Draw";
- JOptionPane.showMessageDialog(this, message);
- break;
- // If you are playing against the computer, show whether you or it won
- case COMPUTER:
- message = this.position.isWinFor('x') ? "You won!" :
- this.position.isWinFor('o') ? "Computer Won!" : "Draw";
- JOptionPane.showMessageDialog(this, message);
- break;
- }
- }
- }
- // Create a single button that goes on the game board
- private JButton createButton() {
- JButton button = new JButton();
- button.setPreferredSize(new Dimension(100, 100));
- button.setBackground(Color.WHITE);
- button.setOpaque(true);
- button.setFont(new Font(null, Font.PLAIN, 50));
- this.add(button);
- return button;
- }
- // Reset all variables so that the game can start over again
- private void startGame() {
- this.buttons = new JButton[Position.SIZE];
- this.position = new Position();
- this.getContentPane().removeAll();
- playGame(this.gameMode);
- this.repaint();
- this.validate();
- }
- // Handle menu choices
- @Override
- public void actionPerformed(ActionEvent e) {
- if(e.getSource() == this.standard) {
- this.gameMode = Mode.STANDARD;
- } else if(e.getSource() == computer) {
- this.gameMode = Mode.COMPUTER;
- } else if(e.getSource() == this.exit) {
- System.exit(0);
- }
- startGame();
- }
- // Add action listeners to handle menu choices
- private void addActionListeners() {
- this.standard.addActionListener(this);
- this.computer.addActionListener(this);
- this.restart.addActionListener(this);
- this.exit.addActionListener(this);
- }
- // Create the basic game menu
- private void createMenu() {
- this.setJMenuBar(menuBar);
- this.menuBar.add(fileMenu);
- this.fileMenu.add(gameMenu);
- this.fileMenu.add(restart);
- this.fileMenu.add(exit);
- this.gameMenu.add(standard);
- this.gameMenu.add(computer);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement