Skip to content

ShiYu0318/AI-Games-Lab-JavaFX

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AI Games Lab

A single-window JavaFX desktop application that packages 20 classic games, each one a visual demonstration of a different AI algorithm — with reinforcement learning (RL) as the central theme.

Java JavaFX Build License

AI-GAMES-LAB

Each game is a thin shell around one AI technique. The goal is not to build many games, but to use games as containers for a full spectrum of algorithms and trainable AI models. Five of the games learn by themselves and form a complete RL spectrum: tabular Q-learning, Deep Q-Network (DQN), TD-Gammon-style value learning, REINFORCE policy gradient, and neuroevolution.

Table of Contents

Features

  • 20 games, 3 tiers: complete-information board games, single-player puzzles, and real-time arcade games.
  • One AI per game: Minimax + Alpha-Beta, MCTS/UCT, Expectimax, A*, IDA*, backtracking, information entropy, and five learning/RL methods.
  • Reinforcement learning focus: watch agents train live (progress bar), then play after training.
  • Three modes: PvP (two humans), PvE (human vs. AI), EvE (AI self-play, also used as live previews in the lobby).
  • Unified architecture: every game is model (pure rules, no JavaFX) + view (JavaFX UI) + ai (algorithm), plugged into a single Game interface.
  • In-app explanations: each game shows its rules and AI technique in floating bubbles.
  • Audio: self-made CC0 8-bit sound effects and CC0 chiptune background music (random track per session, lobby/game playlists, skip & mute).
  • No external ML libraries: all neural networks and RL training loops are hand-written in plain Java.

Tech Stack

Layer Choice
Language Java 21
GUI JavaFX 21 (Canvas, animation, MediaPlayer)
Build Maven (javafx-maven-plugin, bundled mvnw wrapper)
Testing JUnit 5
AI Hand-written, no external ML dependencies

Getting Started

Prerequisites

  • JDK 21
  • No separate Maven install needed (the bundled mvnw wrapper is used).

Build & Run

# Compile
./mvnw -q compile

# Run the game hub (entry point: hw.games.GameHubApp)
./mvnw -q javafx:run

Test

./mvnw -q test

Pure-logic classes (model / ai) do not depend on JavaFX, so they can be run headlessly — this is how the RL agents are verified to actually converge:

javac -d /tmp/out -cp target/classes MyTest.java
java  -cp target/classes:/tmp/out MyTest

Usage

Pick a mode at the top of the lobby, then click a game card to enter it.

Mode Meaning
PvP Two human players
PvE Human vs. AI
EvE AI self-play (watch only; also the lobby card previews)

Controls

  • Board / puzzle games: mouse click to place / select.
  • Arcade / real-time games: arrow keys or WASD (Flappy uses Space / W / ↑).
  • Bottom-right ? shows the game rules, ! shows the AI technique.
  • Music controls (mute / skip track) are in the top-left of the lobby and bottom-right in game.

Game Catalog

Family Game Core Algorithm
Search / game tree Tic-Tac-Toe Full Minimax (unbeatable)
Connect Four, Gomoku, Checkers, Chess, Reversi Minimax + Alpha-Beta
Go 9×9 MCTS / UCT + random rollout
2048 Expectimax
Search / path & constraint Snake A* + flood fill
Tetris El-Tetris heuristic
Maze BFS / Dijkstra / A* (visual comparison)
Sudoku Backtracking + MRV
Minesweeper Logic + probability
15-Puzzle IDA* (Manhattan + linear conflict)
Wordle Information entropy
Learning / RL Backgammon TD-Gammon (TD(0) self-play)
Pong Tabular Q-learning
Breakout DQN (NN + replay buffer + target net)
CartPole REINFORCE policy gradient
Evolution Flappy Bird Neuroevolution (GA)

Reinforcement Learning

The five self-learning games form an RL spectrum. All training runs in a background thread with a progress bar; the agent plays only after training finishes.

Paradigm Game Method Network
Value-based (tabular) Pong Q-learning Q-table
Value-based (deep) Breakout DQN MLP(5, 32, 32, 3)
Value-based (self-play) Backgammon TD-Gammon / TD(0) MLP(196, 40, 1), sigmoid
Policy-based CartPole REINFORCE MLP(4, 64, 64, 2)
Evolutionary Flappy Bird Genetic algorithm NN(3, 6, 1)

DQN and REINFORCE share the same hand-written MLP (ReLU hidden layers, linear output, He initialization, MSE backprop). The trick is in how the training target is set: DQN sets it to the Bellman target r + γ·max Q', while REINFORCE sets it to logits + advantage·(onehot − softmax) so a single MSE step equals one policy-gradient ascent step.

Project Structure

HW/
├── pom.xml                       # Maven build (JavaFX, JUnit deps)
├── mvnw / mvnw.cmd               # Maven wrapper
├── docs/                         # Reports and technical notes
└── src/
    ├── main/java/
    │   ├── module-info.java
    │   └── hw/games/
    │       ├── Game.java          # Game interface
    │       ├── Mode.java          # PVP / PVE / EVE + aiControls()
    │       ├── HubView.java       # Lobby
    │       ├── GameHubApp.java    # Entry point (registers all games)
    │       ├── GameFrame.java     # In-game frame (back / rules / AI bubbles)
    │       ├── ui/                # GameStyles, Sound, Icons
    │       └── <game>/            # one package per game, e.g. cartpole/
    │           ├── XxxGame.java   # implements Game
    │           ├── model/         # pure rules (no JavaFX)
    │           ├── view/          # JavaFX view
    │           └── ai/            # algorithm / agent
    ├── main/resources/sounds/    # CC0 sfx (.wav) + bgm (.mp3) + CREDITS.txt
    └── test/java/hw/games/        # JUnit tests (model / ai)

Architecture

  • Core (4 files): Game (interface), Mode (enum), HubView (lobby), GameHubApp (entry point).
  • Per game: XxxGame (thin adapter) wires together model / view / ai.
  • Separation of concerns: model holds rules and never imports JavaFX, so AI can copy game state and simulate at high speed in the background. view extends BorderPane with a fixed constructor (Runnable onBack, Mode mode). ai consumes a model state and returns a move (or exposes train() / act()).
  • Adding a game is an Open/Closed change: implement Game (plus model / view / ai) and add one line to the games list in GameHubApp.

Testing

  • JUnit pure-logic tests for TicTacToe, Go, Chess, 15-Puzzle, Sudoku, and Wordle.
  • RL agents are validated headlessly (e.g., CartPole reaches the 500-step cap after about 300 training episodes).

License

Code is released under the MIT License. Audio assets are CC0 (public domain).

About

A JavaFX-based game platform to develop algorithms and reinforcement learning for NCU-CSIE-Intro2CS-Final-Project.

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages