42-ocreet is an interactive web simulation written entirely in OCaml using the Ocsigen framework.
In this browser game, you help a population of creatures (Creets) survive a deadly virus spreading from a toxic river.
Available on www.four2-ocreet.onrender.com
Note: wait ~2 minutes before the game compiles and gets ready for playing
A 42 School project exploring client-side web programming with static typing, monadic concurrency, and DOM interaction in OCaml.
- Creets move randomly in a rectangular space bordered by:
- ๐งช A toxic river (top) that infects on contact
- ๐ฅ A hospital (bottom) where players can drag sick Creets to heal them
- Infected Creets:
- Change color and move slower (15% speed reduction)
- Can infect others with 2% chance on contact
- Die after configurable time if not healed
- Special infected states:
- Berserk (10% chance): Grow 4x larger, more dangerous
- Insane (10% chance): Shrink 15%, chase healthy Creets
- Player controls:
- Drag and drop Creets with mouse to keep them safe
- Grabbed Creets are temporarily invulnerable
- Only player-delivered Creets get healed at hospital
- Game mechanics:
- Creets reproduce as long as healthy ones exist
- Difficulty increases over time (speed, spawn rate)
- Game Over when all Creets are dead
- Configurable parameters via sliders (creature size, speed, spawn rate, etc.)
- OCaml 4.14.1
- Ocsigen โ Full-stack framework for typed web applications
Eliomโ Library for client/server web applications with TyXML for type-safe HTML generationJs_of_ocamlโ OCaml to JavaScript compilerLwtโ Monadic concurrency library
- Quadtree optimization for collision detection (bonus feature)
- Materialize CSS โ Modern UI framework
For a sandboxed, dependency-free environment, you can run the entire project inside Docker.
This command builds the container image with all necessary dependencies.
sudo docker build -t 42-ocreet .This command starts the application. It will be accessible in your browser at http://localhost:8080.
sudo docker run -it -p 8080:8080 --rm --name ocreet-dev 42-ocreetFollow these steps for a manual setup from your terminal.
This script sets up your entire OCaml environment, including OPAM, the correct OCaml version (4.14.1), and all required packages. You only need to run this once.
chmod +x ./scripts/install_ocaml_4.14.sh
./scripts/install_ocaml_4.14.shYou may be prompted for your sudo password to install system packages.
After installation, switch to the project's OCaml environment:
opam switch ocreet-4.14.1
eval $(opam env)This command compiles the source code and starts the development server.
chmod +x ./scripts/dev.sh
./scripts/dev.shThe server will then be available at http://localhost:8080.
Here are some useful commands to manage your Docker images and containers.
# List running containers
sudo docker ps
# List all containers (including stopped)
sudo docker ps -a
# Stop the running container
sudo docker stop ocreet-dev
# List all Docker images
sudo docker images
# Remove the project's Docker image
sudo docker rmi 42-ocreet
# Clean up all unused containers, networks, and images (use with caution)
sudo docker system prune -a# Clean build artifacts (keep source)
./scripts/clean.sh
# Rebuild after changes
./scripts/clean.sh && ./scripts/build.sh
# Only build (without running)
./scripts/build.sh
# Only run (if already built)
./scripts/run.sh42-ocreet/
โโโ src/
โ โโโ app/ # OCaml source files
โ โ โโโ h42n42.eliom # Main application entry & game logic
โ โ โโโ creature.eliom # Creet logic & behavior
โ โ โโโ creatureType.eliom # Type definitions
โ โ โโโ creatureUtils.eliom # Creature utility functions
โ โ โโโ page.eliom # UI and DOM generation (using TyXML)
โ โ โโโ dragging.eliom # Mouse interaction & drag/drop
โ โ โโโ quadtree.eliom # Collision detection optimization
โ โ โโโ mainUtils.eliom # Main utility functions
โ โ โโโ config.eliom # Configuration management
โ โ โโโ utils.eliom # General utilities
โ โ โโโ h42n42.conf.in # Server configuration template
โ โโโ css/ # Stylesheets
โ โ โโโ h42n42.css # Main game styles
โ โ โโโ materialize.min.css # Materialize CSS framework
โ โโโ js/ # External JavaScript libraries
โ โ โโโ jquery.min.js # jQuery library
โ โ โโโ materialize.min.js # Materialize JS
โ โ โโโ utils.js # Custom JavaScript utilities
โ โโโ images/ # Game graphics
โ โโโ creature_healthy.png
โ โโโ creature_sick.png
โ โโโ creature_berserk.png
โ โโโ creature_insane.png
โ โโโ hospital.png
โ โโโ river.png
โ โโโ sun-moon.svg
โโโ scripts/ # Build & run scripts
โ โโโ install_ocaml_4.14.sh # OCaml environment setup
โ โโโ build.sh # Compile project
โ โโโ run.sh # Start server
โ โโโ dev.sh # Build and run in one command
โ โโโ clean.sh # Clean build files
โ โโโ uninstall_ocaml.sh # Remove OCaml environment
โ โโโ README.md # Scripts documentation
โโโ docs/ # Project documentation
โ โโโ subject.txt # Original project requirements
โโโ Dockerfile # Docker configuration
โโโ .dockerignore # Docker ignore rules
โโโ .gitignore # Git ignore rules
โโโ README.md # This file
This project demonstrates:
- Client-side OCaml: Run OCaml code in the browser via Js_of_ocaml
- Type-safe web development: Shared types between client & server using Eliom
- Monadic concurrency: Lwt for asynchronous programming with cooperative threads
- DOM manipulation: Direct browser interaction from OCaml
- Event handling: Mouse events with Lwt_js_events
- Game development: Real-time simulation with collision detection
- Static HTML validation: Type-safe HTML generation with TyXML (included in Eliom)
- Performance optimization: Quadtree for efficient collision detection
- Random creature movement with realistic bouncing
- Toxic river that infects creatures on contact
- Hospital for healing sick creatures
- Drag and drop mechanics with mouse
- Invulnerability while being dragged
- 15% speed reduction for infected creatures
- 2% infection chance on contact
- Berserk state (10% chance, 4x size increase)
- Insane state (10% chance, 15% size reduction, chases healthy creatures)
- Difficulty progression over time
- Game over when all creatures are infected
- Lwt threads for each creature
- DOM elements for creatures
- Mouse event handling
- Quadtree optimization for collision detection
- Configurable parameters via sliders
- Modern UI with Materialize CSS
- Theme toggle (light/dark mode)
- Responsive design for different screen sizes
Build errors?
- Make sure you're using OCaml 4.14.1:
ocaml --version - Switch to correct environment:
opam switch ocreet-4.14.1 && eval $(opam env) - Check installed packages:
opam list | grep -E "(eliom|js_of_ocaml|lwt)" - Try cleaning first:
./scripts/clean.sh
Server won't start?
- Check port 8080 is free:
lsof -i :8080 - Verify build completed: look for
src/app/h42n42.cmaandsrc/app/static/h42n42.js - Check server logs for errors
Game not loading?
- Check browser console for JavaScript errors
- Ensure ocsigenserver is running: should see "Starting H42N42 server" message
- Verify all static files are copied: check
src/app/static/directory
Wrong OCaml version?
# Check current version
ocaml --version
# Switch to correct version
opam switch ocreet-4.14.1
eval $(opam env)Made with โค๏ธ for 42 School