Skip to content

FourFury is a real-time Connect4 game with modes for human play, AI opponents, and online multiplayer. It has a responsive Next.js frontend and a FastAPI backend using MongoDB and Redis for performance and real-time features.

License

Notifications You must be signed in to change notification settings

hackerSa3edy/FourFury

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

61 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

FourFury Logo

FourFury

A modern, real-time Connect Four game featuring AI opponents and online multiplayer

FastAPI Next.js Python TypeScript MongoDB Redis Docker

License: MIT PRs Welcome

๐ŸŽฎ Project Preview

Light Mode
Light Mode
Dark Mode
Dark Mode

๐Ÿ“‹ Table of Contents

๐Ÿ“ Overview

FourFury is a sophisticated implementation of the classic Connect Four game, built with modern web technologies. It offers multiple game modes including AI opponents with adjustable difficulty levels and real-time online multiplayer matches.

The project consists of:

  • A robust FastAPI backend with Socket.IO for real-time communication
  • A responsive Next.js frontend with smooth animations and intuitive controls
  • Redis-powered caching and real-time features
  • MongoDB for persistent data storage

For detailed technical documentation:

๐ŸŽฅ Live Demo

FourFury Demo

โญ Features

Core Gameplay

  • Multiple game modes:
    • Player vs AI with adjustable difficulty (1-5)
    • Local multiplayer
    • Online multiplayer with real-time updates
  • Smart move validation and win detection
  • Responsive design works on all devices
  • Beautiful animations and visual effects
  • Dark/Light theme support

Online Features

  • Anonymous play with persistent sessions
  • Real-time game state synchronization
  • Player presence tracking
  • Automatic forfeit on disconnect
  • Rematch system
  • Game state preservation on refresh

Technical Highlights

  • Efficient game state management
  • Real-time WebSocket communication
  • Redis-powered caching system
  • Scalable MongoDB backend
  • Modern React frontend with Next.js
  • Type-safe with TypeScript and Python type hints

๐Ÿ› ๏ธ Technology Stack


Next.js

TypeScript

TailwindCSS

Socket.IO

FastAPI

Python

Redis

MongoDB

For detailed technical specifications and setup instructions, please refer to the respective frontend and backend README files.

๐Ÿš€ Getting Started

๐Ÿ“‹ Prerequisites

Docker Installation

  • Docker Engine 24.0.0+
  • Docker Compose V2 2.20.0+

Development Requirements

  • For Frontend:
    • Node.js 20.0.0+
    • npm 10.0.0+ or Yarn 1.22.0+
  • For Backend:
    • Python 3.10+
    • Poetry package manager
  • MongoDB 7.0+
  • Redis 7.2+

โš™๏ธ Installation

Using Docker Compose (Recommended)

  1. Clone the repository:

    git clone https://github.com/hackerSa3edy/FourFury.git
    cd FourFury
  2. Configure environment variables:

    # For backend
    # Modify backend/.env.docker if needed (default values should work)
    
    # For frontend
    # Modify frontend/.env.docker if needed (default values should work)
  3. Build and start the containers:

    docker compose up --build
  4. Access the application:

Manual Installation

  1. Clone the repository as shown above

  2. Setup Frontend:

    cd frontend
    npm install
    cp .example.env.local .env.local
    # Modify .env.local with your configuration
    npm run build
    npm start
  3. Setup Backend:

    cd backend
    poetry install
    cp .env.example .env
    # Modify .env with your configuration
    poetry run uvicorn src.fourfury.run:app --reload
  4. Setup MongoDB and Redis locally

๐Ÿ’ป Usage

Docker Compose Services

Our docker-compose.yml sets up a complete development environment with five services:

  1. nginx (Port 80):

    • Reverse proxy handling routing between frontend and backend
    • Load balancing and SSL termination (if configured)
    • Routes /api/* to backend
    • Routes /socket.io/* to backend WebSocket
    • Routes all other traffic to frontend
    • Handles SSL termination (in production)
  2. frontend (Next.js):

    • Serves the React application
    • Automatically rebuilds on code changes
    • Connects to backend through nginx proxy
  3. backend (FastAPI):

    • Handles API requests and WebSocket connections
    • Auto-reloads during development
    • Configured through .env.docker
  4. db (MongoDB):

    • Persistent game data storage
    • Volume mounted for data persistence
    • Accessible to backend service
  5. redis (Redis):

    • Handles caching and real-time features
    • Configured for keyspace notifications
    • Persists data through mounted volume

Common Operations

  • Rebuild single service:

    docker compose up -d --build [service_name]
  • Reset data (clear volumes):

    docker compose down -v
  • Check service status:

    docker compose ps

Troubleshooting

  1. If services fail to start:

    docker compose down
    docker compose up --build
  2. Check service logs:

    docker compose logs [service_name]
  3. Verify network connectivity:

    docker compose exec [service_name] ping [other_service]

๐Ÿ—๏ธ Architecture

System Overview

graph TB
    Client[Browser Client]
    Nginx[Nginx Reverse Proxy]
    Frontend[Next.js Frontend]
    Backend[FastAPI Backend]
    Redis[Redis Cache]
    MongoDB[MongoDB Database]

    Client --> Nginx
    Nginx --> Frontend
    Nginx --> Backend
    Backend <--> Redis
    Backend <--> MongoDB
    Client <-.->|WebSocket| Nginx
    Nginx <-.->|WebSocket| Backend
Loading

Repo Structure

FourFury
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ backend
โ”‚   โ”œโ”€โ”€ .dockerignore
โ”‚   โ”œโ”€โ”€ .env.docker
โ”‚   โ”œโ”€โ”€ .env.example
โ”‚   โ”œโ”€โ”€ .pre-commit-config.yaml
โ”‚   โ”œโ”€โ”€ Dockerfile
โ”‚   โ”œโ”€โ”€ Makefile
โ”‚   โ”œโ”€โ”€ README.md
โ”‚   โ”œโ”€โ”€ docs
โ”‚   โ”œโ”€โ”€ poetry.lock
โ”‚   โ”œโ”€โ”€ poetry.toml
โ”‚   โ”œโ”€โ”€ pyproject.toml
โ”‚   โ”œโ”€โ”€ src
โ”‚   โ””โ”€โ”€ tests
โ”œโ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ frontend
โ”‚   โ”œโ”€โ”€ .env.docker
โ”‚   โ”œโ”€โ”€ .example.env.local
โ”‚   โ”œโ”€โ”€ .gitignore
โ”‚   โ”œโ”€โ”€ Dockerfile
โ”‚   โ”œโ”€โ”€ README.md
โ”‚   โ”œโ”€โ”€ eslint.config.mjs
โ”‚   โ”œโ”€โ”€ next.config.ts
โ”‚   โ”œโ”€โ”€ node_modules
โ”‚   โ”œโ”€โ”€ package-lock.json
โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ”œโ”€โ”€ postcss.config.mjs
โ”‚   โ”œโ”€โ”€ public
โ”‚   โ”œโ”€โ”€ src
โ”‚   โ”œโ”€โ”€ tailwind.config.ts
โ”‚   โ””โ”€โ”€ tsconfig.json
โ”œโ”€โ”€ media
โ”‚   โ”œโ”€โ”€ Connect4_landing-darkMode.png
โ”‚   โ””โ”€โ”€ Connect4_landing-dayMode.png
โ””โ”€โ”€ nginx
    โ””โ”€โ”€ nginx.conf

๐Ÿ›ฃ๏ธ Development Journey

Our journey in developing FourFury has been both challenging and rewarding. This section outlines our key milestones, challenges, and learnings.

๐Ÿ† Achievements

  • Successfully implemented a real-time multiplayer system using Socket.IO
  • Created an efficient AI opponent with multiple difficulty levels
  • Developed a responsive UI that works seamlessly across all devices
  • Achieved sub-100ms move latency in online gameplay
  • Built a scalable architecture capable of handling multiple concurrent games
  • Implemented a robust game state management system
  • Created an efficient winner detection algorithm
  • Successfully integrated MongoDB for persistent storage
  • Achieved 95%+ test coverage for core game logic
  • Implemented efficient board serialization for state management

๐Ÿ’ช Challenges

  • Optimizing winner detection

    • Initial naive approach was too CPU-intensive
    • Solved by checking only affected rows/columns/diagonals
    • Implemented direction-based scanning for efficiency
    • Added caching for frequently checked positions
  • Implementing efficient board representation

    • Balanced memory usage vs computation speed
    • Optimized array operations for move validation
    • Implemented efficient serialization for WebSocket transmission
    • Created compact state representation for persistence
  • Real-time game synchronization

    • Handling network latency and disconnections
    • Managing race conditions in move validation
    • Implementing reliable state recovery
    • Ensuring consistent game state across players
  • AI implementation challenges

    • Balancing AI difficulty levels
    • Optimizing minimax algorithm performance
    • Implementing efficient board evaluation
    • Managing computation time constraints

๐Ÿ“š Lessons Learned

  • Technical Insights

    • WebSocket management requires careful error handling
    • State management is crucial for real-time applications
    • Efficient data structures significantly impact performance
    • Test-driven development is essential for game logic
    • Type safety prevents many runtime errors
  • Architecture Decisions

    • Microservices architecture provides better scalability
    • Redis is excellent for real-time features
    • MongoDB's flexibility suits game state storage
    • Docker simplifies deployment and testing
  • Development Process

    • Early performance optimization is sometimes necessary
    • Comprehensive testing saves debugging time
    • Documentation is crucial for complex game logic
    • Regular refactoring keeps code maintainable
  • User Experience

    • Responsive design requires careful planning
    • Visual feedback is essential for game interactions
    • Accessibility cannot be an afterthought
    • Performance impacts user engagement

๐Ÿ”ฎ Future Enhancements

  • Implement bitboard representation for faster win detection
  • Optimize winner detection to check only around last move
  • Add Google sign-in integration
  • Implement:
    • Playing history
    • Leaderboard
    • Scoring system for both online and AI modes
    • User profiles
    • Data persistence for signed-in users

๐Ÿ‘ฅ Team

๐Ÿ‘จโ€๐Ÿ’ป Lead Developer

Abdelrahman Mohamed

  • Role: Full Stack Developer
  • Contributions:
    • System Architecture Design
    • Frontend Development (Next.js/React)
    • Backend Development (FastAPI/Python)
    • Real-time WebSocket Implementation
    • Docker Configuration
    • DevOps & Deployment
  • Contact:

ALX Logo


Made with โค๏ธ as part of the ALX Software Engineering Program

GitHub LinkedIn

About

FourFury is a real-time Connect4 game with modes for human play, AI opponents, and online multiplayer. It has a responsive Next.js frontend and a FastAPI backend using MongoDB and Redis for performance and real-time features.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published