Skip to content

calimero-network/battleships

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

17 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Battleship Game on Calimero

A complete implementation of the classic Battleship game built on the Calimero SDK, featuring a modern React frontend and a well-architected Rust backend with comprehensive documentation.

๐ŸŽฎ Game Features

  • Classic Battleship Gameplay: 10x10 grid with standard ship fleet (1x5, 1x4, 2x3, 1x2)
  • Real-time Multiplayer: Turn-based gameplay with shot proposals and acknowledgments
  • Private Ship Placement: Ships are stored privately until hit
  • Modern UI: Clean, intuitive React interface with integrated shot selection
  • Comprehensive Validation: Strategy pattern-based validation system
  • Event-Driven Architecture: Complete audit trail of all game actions

๐Ÿ—๏ธ Architecture

Frontend (app/)

  • React + TypeScript: Modern frontend with hooks and functional components
  • Calimero Integration: Seamless connection to the Calimero blockchain
  • Responsive Design: Clean, intuitive user interface
  • Real-time Updates: Live game state synchronization

Backend (logic/)

  • Rust + Calimero SDK: High-performance blockchain-based game logic
  • Domain-Driven Design: Well-organized modules with clear separation of concerns
  • Validation Strategy Pattern: Extensible validation system
  • Comprehensive Documentation: Full Rust documentation with examples

๐Ÿ“ Project Structure

battleship/
โ”œโ”€โ”€ app/                    # React frontend
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ pages/         # Game pages (home, login, match, play)
โ”‚   โ”‚   โ”œโ”€โ”€ features/      # Feature modules
โ”‚   โ”‚   โ””โ”€โ”€ api/           # Calimero API client
โ”‚   โ””โ”€โ”€ package.json
โ”œโ”€โ”€ logic/                  # Rust backend
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ board.rs       # Board and coordinate types
โ”‚   โ”‚   โ”œโ”€โ”€ ships.rs       # Ship and fleet management
โ”‚   โ”‚   โ”œโ”€โ”€ players.rs     # Player management and private boards
โ”‚   โ”‚   โ”œโ”€โ”€ game.rs        # Core game logic and match management
โ”‚   โ”‚   โ”œโ”€โ”€ events.rs      # Domain events
โ”‚   โ”‚   โ”œโ”€โ”€ validation.rs  # Validation strategy pattern
โ”‚   โ”‚   โ””โ”€โ”€ lib.rs         # Main application logic
โ”‚   โ””โ”€โ”€ Cargo.toml
โ”œโ”€โ”€ data/                   # Calimero node data
โ””โ”€โ”€ scripts/               # Build and deployment scripts

๐Ÿš€ Quick Start

Prerequisites

  • Node.js 16+ and npm/pnpm
  • Rust 1.70+
  • Calimero SDK
  • Git

Installation

  1. Clone the repository

    git clone <repository-url>
    cd battleship
  2. Install frontend dependencies (via root scripts)

    pnpm run app:install
  3. Build the backend (WASM)

    pnpm run logic:build
  4. Start the development servers (frontend + WASM watcher)

    pnpm run app:dev

๐ŸŽฏ Game Rules

Ship Placement

  • Fleet Composition: 1x5 (carrier), 1x4 (battleship), 2x3 (cruiser, submarine), 1x2 (destroyer)
  • Placement Rules: Ships must be straight, contiguous, and non-adjacent
  • Coordinate Format: "x1,y1;x2,y2;..." for ship coordinates

Gameplay

  • Turn-based: Players alternate taking shots
  • Shot Process: Propose shot โ†’ Target acknowledges โ†’ Shot resolved
  • Win Condition: First player to sink all opponent ships wins

๐Ÿ”ง Development

Backend Development

The Rust backend follows Domain-Driven Design principles:

  • board: Board representation and coordinate management
  • ships: Ship definitions and fleet validation
  • players: Player management and private data storage
  • game: Core game logic and match management
  • events: Domain events for decoupling
  • validation: Strategy pattern-based validation system

Building and Testing

# Build WASM (release profile used by the app)
pnpm run logic:build

# Optional: clean build artifacts
pnpm run logic:clean

# Optional: continuously watch and sync WASM into app on changes
pnpm run logic:watch

# Generate ABI client for the frontend from the latest ABI
pnpm run app:generate-client

# Low-level Rust workflows (if you need them)
cd logic
cargo check          # Check for compilation errors
cargo test           # Run tests
cargo doc --open     # Generate and view documentation

Frontend Development

The React frontend provides a modern, intuitive interface:

  • Integrated Shot Selection: Click directly on the "Your Shots" board
  • Real-time Updates: Live game state synchronization
  • Responsive Design: Works on desktop and mobile devices
  • Error Handling: Comprehensive error messages and validation

Development Commands

# Start the app + WASM res watcher together (recommended)
pnpm run app:dev

# Build production frontend
pnpm run app:build

# Preview the production build locally
pnpm run app:preview

๐Ÿ“š Documentation

API Documentation

Generate comprehensive API documentation:

cd logic
cargo doc --open

Code Documentation

  • Rust Documentation: Complete API reference with examples
  • TypeScript Types: Well-defined interfaces and types
  • README Files: Module-specific documentation

๐Ÿงช Testing

Backend Testing

cd logic
cargo test

Frontend Testing

cd app
npm test

๐Ÿš€ Deployment

Calimero Deployment

  1. Bootstrap local Calimero network with Merobox and run workflow

       # Using pipx (recommended)
       pipx install merobox
    
       # Or on macOS with Homebrew
       brew install calimero-network/tap/merobox
    
       # Run workflow
       merobox bootstrap run workflow.yml
  2. Build the WASM

    pnpm run logic:build
  3. Sync the built WASM into the app

    pnpm run logic:sync
  4. Deploy to Calimero

    • Follow Calimero deployment guidelines
    • Upload the generated WASM file (logic/target/wasm32-unknown-unknown/app-release/kv_store.wasm)
    • Configure the frontend to connect to your Calimero node

Frontend Deployment

pnpm run app:build
# Deploy the app/dist (or app/build) directory to your hosting service

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • Calimero SDK: For providing the blockchain infrastructure
  • React Team: For the excellent frontend framework
  • Rust Community: For the amazing language and ecosystem

๐Ÿ“ž Support

If you have any questions or need help:

  1. Check the documentation
  2. Open an issue
  3. Join our community discussions

Happy Gaming! ๐ŸŽฎโš“

About

Example demo of the battleships game built on Calimero

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors