Skip to content

Latest commit

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date

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.

JavaJavaFXBuildLicense

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

LayerChoice
LanguageJava 21
GUIJavaFX 21 (Canvas, animation, MediaPlayer)
BuildMaven (javafx-maven-plugin, bundled mvnw wrapper)
TestingJUnit 5
AIHand-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.

ModeMeaning
PvPTwo human players
PvEHuman vs. AI
EvEAI 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

FamilyGameCore Algorithm
Search / game treeTic-Tac-ToeFull Minimax (unbeatable)
Connect Four, Gomoku, Checkers, Chess, ReversiMinimax + Alpha-Beta
Go 9×9MCTS / UCT + random rollout
2048Expectimax
Search / path & constraintSnakeA* + flood fill
TetrisEl-Tetris heuristic
MazeBFS / Dijkstra / A* (visual comparison)
SudokuBacktracking + MRV
MinesweeperLogic + probability
15-PuzzleIDA* (Manhattan + linear conflict)
WordleInformation entropy
Learning / RLBackgammonTD-Gammon (TD(0) self-play)
PongTabular Q-learning
BreakoutDQN (NN + replay buffer + target net)
CartPoleREINFORCE policy gradient
EvolutionFlappy BirdNeuroevolution (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.

ParadigmGameMethodNetwork
Value-based (tabular)PongQ-learningQ-table
Value-based (deep)BreakoutDQNMLP(5, 32, 32, 3)
Value-based (self-play)BackgammonTD-Gammon / TD(0)MLP(196, 40, 1), sigmoid
Policy-basedCartPoleREINFORCEMLP(4, 64, 64, 2)
EvolutionaryFlappy BirdGenetic algorithmNN(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 for implementing algorithms and reinforcement learning with a hand-written MLP for NCU-CSIE-Intro2CS-Final-Project.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Contributors

Languages