From e7924dc6cc120ad0369cf525b0dded6532a43793 Mon Sep 17 00:00:00 2001 From: Akiva_Miller Date: Mon, 23 Jan 2023 17:17:10 -0500 Subject: [PATCH] Added restart option --- src/com/shonejin/minesweeper/game/Board.java | 8 +++ src/com/shonejin/minesweeper/game/Game.java | 54 +++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/com/shonejin/minesweeper/game/Board.java b/src/com/shonejin/minesweeper/game/Board.java index d2b640e..9d47bbd 100644 --- a/src/com/shonejin/minesweeper/game/Board.java +++ b/src/com/shonejin/minesweeper/game/Board.java @@ -174,4 +174,12 @@ else if (states[row][col] == CellStates.FLAGGED) public GameStates getGameState() { return gameState; } + // Reset the game state if necessary + public void resetGameState() { + this.gameState = GameStates.ONGOING; + } + // Getter for NMines + public int getNMines() { + return this.NMines; + } } diff --git a/src/com/shonejin/minesweeper/game/Game.java b/src/com/shonejin/minesweeper/game/Game.java index e4ae635..7a33a21 100644 --- a/src/com/shonejin/minesweeper/game/Game.java +++ b/src/com/shonejin/minesweeper/game/Game.java @@ -1,8 +1,18 @@ package com.shonejin.minesweeper.game; +import java.awt.BorderLayout; import java.awt.Graphics; +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.WindowEvent; import java.awt.image.BufferStrategy; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.SwingConstants; + import com.shonejin.minesweeper.game.states.CellStates; import com.shonejin.minesweeper.game.states.GameStates; import com.shonejin.minesweeper.gfx.Assets; @@ -44,6 +54,7 @@ public Game(String title, int N, int NMines) { // called when mouse clicks happen (mouse release) public void onClick(boolean isLeft, int x, int y) { + int NMines = board.getNMines(); if (finished) return; @@ -62,8 +73,49 @@ public void onClick(boolean isLeft, int x, int y) { // when game ends if (result != GameStates.ONGOING) { + // Create a new frame to enable restart + JFrame frame = new JFrame(result == GameStates.LOST ? "!!!!! You Lose !!!!!" : "!!!!! You Won !!!!!"); //Sets title depending on win or loss + frame.setSize(500, 150); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setResizable(true); + frame.setLocationRelativeTo(display.getFrame()); + + // Ask user if would like to play again + JLabel message = new JLabel("Game Lost! Would you like to play again?", SwingConstants.CENTER); + + // Create yes or no buttons and panel + JPanel buttonPanel = new JPanel(); + buttonPanel.setLayout(new GridLayout(1,2)); + JButton yes = new JButton("Yes"); + JButton no = new JButton("No"); + + // If the user selects yes it creates a new game and starts it + yes.addActionListener((ActionEvent e) -> { + Game game = new Game("Minesweeper", N, NMines); + game.start(); + + display.getFrame().setVisible(false); + frame.setVisible(false); + }); + + // If the user selects no it will close the windows and print "Game ended!" to the console + no.addActionListener((ActionEvent e) -> { + System.out.println("Game ended!"); + frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); + }); + + buttonPanel.add(yes); + buttonPanel.add(no); + + // Create the panel to store the message + JPanel popUp = new JPanel(); + popUp.setLayout(new BorderLayout(20,20)); + popUp.add(message, BorderLayout.CENTER); + popUp.add(buttonPanel, BorderLayout.SOUTH); + + frame.add(popUp); + frame.setVisible(true); finished = true; - System.out.println("Game ended!"); String msg = (result == GameStates.LOST ? "!!!!! You Lose !!!!!" : "!!!!! You Won !!!!!"); display.getFrame().setTitle(msg); }