Skip to content

andreypUP/minesweeper-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minesweeper Game - Java Swing Implementation

image

A classic Minesweeper game built with Java Swing.


📌 Overview

Minesweeper is a classic single-player puzzle game where the player's goal is to uncover all cells except those containing mines. This project is a Java Swing implementation of the game, featuring:

  • Customizable board sizes and mine counts.
  • Flagging mechanics to mark suspected mines.
  • Recursive cell uncovering for empty areas.
  • Win/loss conditions with visual feedback.

🎮 Game Mechanics

Objective

  • Uncover all non-mine cells without triggering any mines.
  • Use numbered clues to deduce mine locations.

Rules

  • Left-click: Reveal a cell.
    • If the cell is empty, all adjacent empty cells are automatically revealed.
    • If the cell contains a mine, the game ends.
  • Right-click: Toggle a flag on a cell (to mark suspected mines).
  • Winning: All non-mine cells are uncovered.
  • Losing: A mine is uncovered.

🛠️ Implementation Details

Technologies Used

  • Java Swing: For GUI components (JFrame, JPanel, JLabel).
  • Event-Driven Programming: Handles mouse clicks and game state updates.
  • Object-Oriented Design: Modular architecture for maintainability.

Key Classes

Class Responsibility
Board Manages game state, mine generation, cell uncovering, and rendering.
MinesAdapter Listens for mouse events (clicks, right-clicks) and triggers game actions.
Minesweeper Sets up the game window (title, icon, layout) and initializes the Board.
Main Entry point to launch the game.

Features

  • Safe First Click: Mines are generated after the first click to ensure the player never loses immediately.
  • Recursive Uncovering: Empty cells and their neighbors are revealed automatically.
  • Dynamic Rendering: Cells update visually to reflect their state (covered, flagged, revealed).

🖼️ Screenshots

Board Size Mines Gameplay State
16x16 100 image
32x32 300 image
45x45 500 image

🚀 How to Run

  1. Prerequisites: Java JDK installed.
  2. Clone the repository:
    git clone https://github.com/andreypUP/minesweeper-java.git
    
    
    javac Main.java
    java Main
    

🧩 Challenges & Solutions

Challenge Solution
Mine Generation Bug Implemented a click counter to generate mines only after the first click.
Board Dimensions Used dynamic formulas to calculate cell positions based on rows/columns.
Neighbor-Checking Logic Applied indirect recursion to reveal all adjacent empty cells.

🔮 Future Enhancements

  • Timer: Track and display game completion time.

  • High Scores: Integrate a database to save top scores.

  • UI Improvements: Modernize the interface with animations and sound effects.

Code Refactoring: Use POJO and design patterns for better scalability.

For detailed technical explanations, see Technical Report file.

📚 References & Credits

Developers:

About

Java Minesweeper Game

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages