An interactive game where the user must hit as many moles as possible within a set timeframe. The game uses Java and Java Swing for the graphical user interface (GUI).
- Game UI: A 4x4 grid of holes where moles randomly appear.
- Random Mole Appearance: Moles pop up from random holes every 2 seconds.
- Timeframe Options: Players can select from three game durations:
- 1 minute
- 2 minutes
- 3 minutes
- User Interaction: Players hit the moles using mouse clicks.
- Score Tracking: Each successful hit earns 10 points.
| Timeframe | Mole Speed (sec) | Total Moles | Maximum Points |
|---|---|---|---|
| 1 min | 2 secs | 30 | 300 |
| 2 mins | 2 secs | 60 | 600 |
| 3 mins | 2 secs | 90 | 900 |
- Difficulty Settings:
- Grid size variations: 3x3, 4x4, 5x5
- Adjustable mole speed: 2 secs, 1.5 secs, 1 sec
- 4x4 grid
- 2-minute game duration
- Constant mole speed (2 seconds per appearance)
- Java for game logic
- Java Swing for UI development
- Extract the ZIP file containing the source code.
- Open the folder in Visual Studio Code.
- Make sure you have the Java and Java Extension Pack installed in VS Code.
- Open the
App.javafile — this is the main entry point. - Run the program using the "Run" button in VS Code or via the terminal:
javac App.java java App
- No major bugs found during testing.
classDiagram
direction LR
class App {
+static void main(String[] args)
}
class GameController {
-int timeFrame
-int score
-int totalMoles
-int maxPoints
-boolean isRunning
-Timer gameTimer
-Timer moleTimer
-Grid grid
-GameUI gameUI
-Random random
-int moleSpeed
-static GameController gameInstance
-int currentTime
+GameController()
+void initialize()
+static synchronized GameController getInstance()
-int calculateTotalMoles(int timeInSeconds, int moleSpeed)
-void initTimers()
+void placeMoleRandomly()
+void removeAllMoles()
+void adjustMoleSpeed(int newSpeed)
+void startGame()
+void endGame()
+void resetGame()
+boolean isRunning()
+void updateScore(int points)
+int getScore()
+int getMaxPoints()
+int getTimeRemaining()
+void setTimeFrame(int timeFrame)
}
class GameUI {
-JFrame frame
-JPanel gridPanel
-JPanel elementPanel
-JLabel scoreLabel
-JLabel timeLabel
-JButton startButton
-JComboBox~String~ timeSelector
-GameController gameController
-Grid grid
+GameUI(GameController gameController, Grid grid)
+void initializeUI()
-void setupHolesGrid()
-int getSelectedTimeInSeconds()
-void updateSelectedTime()
+void updateScore(int score)
+void updateTime(int timeRemaining)
+void showGameOver(int finalScore)
+JButton getStartButton()
+JComboBox~String~ getTimeSelector()
}
class Grid {
-int rows
-int columns
-Hole[][] holes
+Grid(int rows, int columns)
+Hole getHole(int row, int col)
+List~Hole~ getAllHoles()
+int getRows()
+int getColumns()
}
class Hole {
-int row
-int column
-boolean hasMole
-boolean isHit
-JButton button
-Mole mole
+Hole(int row, int column)
+void placeMole()
+void removeMole()
+boolean hit()
+boolean hasMole()
+int getRow()
+int getColumn()
+JButton getButton()
}
class Mole {
-boolean isVisible
+Mole()
+void show()
+void hide()
+boolean isVisible()
}
class ResourceManager {
+static ImageIcon loadImage(String imageName)
}
class UIConstants {
+static final Color BACKGROUND_COLOR
+static final Color GRID_BACKGROUND
+static final Color TITLE_COLOR
+static final Color TIME_COLOR
+static final Color SCORE_COLOR
+static final String TITLE_FONT
+static final String TIME_FONT
+static final String SCORE_FONT
+static final int HOLE_SIZE
}
class GameConstants {
+static final int DEFAULT_TIME_FRAME
+static final int DEFAULT_MOLE_SPEED
+static final int POINTS_PER_HIT
+static final int DEFAULT_GRID_ROWS
+static final int DEFAULT_GRID_COLS
+static final String RESOURCE_PATH
+static final String HOLE_IMAGE
+static final String MOLE_IMAGE
}
class GameException {
+GameException(String message)
+GameException(String message, Throwable cause)
+GameException(Throwable cause)
}
App --> GameController : creates
GameController "1" *-- "1" Grid : contains
GameController "1" *-- "1" GameUI : creates and uses
Grid "1" *-- "*" Hole : contains
Hole "0..1" o-- "0..1" Mole : may contain
Hole ..> GameController : references via getInstance
GameUI ..> UIConstants : uses
Hole ..> ResourceManager : uses
GameController ..> GameConstants : uses
GameController ..> GameException : may throw
ResourceManager ..> GameConstants : uses
ResourceManager ..> UIConstants : uses
<<singleton>> GameController