Skip to content

FedeMolteni/MineSweeper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

137 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minesweeper

Overview

This project implements a Java-based Minesweeper game using the MVC pattern in the frontend and a layered architecture in the backend. The application supports multi-language user interfaces and persistent user preferences.


Modules

  • frontend: Handles the graphical interface and user interactions.
  • backend: Manages core game logic, persistence, and application preferences.

Note: Since the project is divided into two separate modules (frontend and backend), you need to import them manually when using IntelliJ IDEA. To do this: 1. Go to File → Project Structure…. 2. Under the Modules section, click the ”+” button and choose Import Module. 3. Select the frontend or backend directory and confirm. 4. Repeat the process for the other module. 5. Make sure both modules are properly configured and recognized by the main project.


Backend

Key Packages

  1. presentation

    • Service Layer: Bridges frontend and backend logic.
    • Important classes:
      • GameServiceImpl: central class for game actions like move, flag, and saving/loading games.
      • PreferencesServiceImpl: manages reading and writing user preferences.
      • TranslationsBusinessInterface: defines translation operations.
      • GameObserver: To overcome the module separation, it has been introduced a new interface implemented by all views: GameObserver. This interface is supposed to be used like the old View interface for the backend. In this way the GameModel has an elegant and safe way to tell views to update themselves, without having to rely on concrete implementations.
  2. business

    • Application Logic Layer: Models and interfaces for preferences and translation logic.
    • PreferencesModel: manages user preferences, including language and mine count.
    • TranslationsModel: loads and manages translations based on selected locale.
  3. dataaccess

    • Persistence Access Layer: Reads/writes configuration data from property files.
    • PreferencesPropertiesDataAccess: saves preferences in the user’s home directory (preferences.properties).
    • TranslationsPropertiesDataAccess: loads translations from resources/i18n.
  4. dataPersistence

    • Game Save/Load Logic:
      • GamePersistence: serializes and deserializes GameModel to/from JSON using Jackson.
  5. model

    • Domain Entities:
      • GameModel: represents the core game state, handles game rules, mine placement, and game progression.
      • CellModel: represents each cell in the grid.
  6. event

    • Defines event structures like GameAction, which captures moves and game state transitions.

Frontend

The frontend uses JavaFX for UI and follows MVC principles.

Main Components

  • MainFx: application entry point, initializes the UI and wires controllers and views.
  • controller: event-driven logic (e.g., GameController, ExitController).
  • view:
    • GameBoardViewFxml, MenuBarViewFxml, PreferencesViewFxml: handle game board, menu, and preferences dialog respectively.
    • UserFeedbackViewFxml: shows translated feedback messages.
    • implements GameObserver: simple mechanism to allow the backend to have a way to update every view when required.

Configurations

Where Preferences Are Read/Written

  • File: preferences.properties
  • Managed by: PreferencesPropertiesDataAccess
  • Location:
    • macOS: ~/Library/Application Support/minesweeper/preferences.properties
    • Windows: %APPDATA%\minesweeper\preferences.properties
    • Linux: ~/.config/minesweeper/preferences.properties

How to Change Language

  • Preferences file property: language-tag
  • Supported values (see supported-languages.properties):
    • "en-US", "it-CH", etc.
  • Change dynamically through PreferencesViewFxml or directly editing preferences.properties

Translation Files

Located in: src/main/resources/i18n/

  • labels_en.properties
  • labels_it.properties
  • supported-languages.properties

Mine Count Preferences

  • property key: mines
  • Cold-loaded during backend model setup
  • Accessed via: PreferencesReader.getInitialNumberOfMines()
  • Used by: GameModel.getInstance() during singleton initialization

How the Language Setting Works

  1. PreferencesController reads the saved language tag.
  2. TranslationsModel loads the corresponding ResourceBundle.
  3. UI elements call TranslationsController.translate(key) to retrieve the translated string.

How Game Logic Works

  1. App starts via MainFx, calling GameService.getGameModel()
  2. GameModel.getInstance() initializes state, including reading mine count from PreferencesReader
  3. GameServiceImpl exposes operations like:
    • newGame(), move(row, col), flag(row, col)
  4. Views observe the model (GameObserver) and refresh on updates
  5. Game state is saved/loaded using GamePersistence

Notes

  • The system uses singleton patterns in most service/model classes.
  • Preferences are saved immediately when changed in the preferences UI.
  • Game state is stored in JSON (game_save.json) using Jackson, placed in the same directory as the .jar file.

About

Java Minesweeper game with JavaFX frontend (MVC) and layered backend, featuring multi-language support and JSON game save/load.

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages