Skip to content

This repository contains a full-stack implementation of the classic board game Reversi. It consists of a Node.js backend with Socket.IO for real-time communication.

Notifications You must be signed in to change notification settings

calvinchan999/reversi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Reversi Game

This repository contains a full-stack implementation of the classic board game Reversi. It consists of a Node.js backend with Socket.IO for real-time communication.

Future Improvements

  • Add a spectator mode for other users to watch ongoing games.
  • Implement a ranking system for players.
  • Add more AI difficulty levels.
  • Implement MySQL Database to store game results.
  • Create a 3D Reversi game with Node.js, React.js, and Three.js (8x8x6 grid).
  • Develop mobile versions to support Android and iOS platforms.
  • Add support for Helm Chart for K8s deployment.

Client: Reversi Game App (React)

Features

  1. Game Modes:

    • Human vs Human
    • Human vs AI
  2. Multiplayer Functionality:

    • Join rooms using unique room IDs
    • Real-time game updates using Socket.IO
  3. Game Board:

    • 8x8 grid
    • Visual representation of black and white pieces
    • Highlighting of legal moves
  4. Game Logic:

    • Implemented on the server-side (not shown in the provided code)
    • Handles move validation, turn switching, and game ending conditions
  5. Chat System:

    • Real-time chat functionality for players in the same room
  6. Game Information Display:

    • Current player turn
    • Player color assignment
    • Score tracking (number of pieces for each player)
  7. Move History:

    • Navigate through previous game states
    • View the current game state
  8. I18n:

    • Supports multiple languages (English and Traditional Chinese)
  9. Responsive Design:

    • Adapts to different screen sizes (CSS not provided in the given code)

Technical Stack

  • Frontend: React.js
  • State Management: React Hooks (useState, useEffect)
  • Routing: React Router
  • Real-time Communication: Socket.IO client
  • Internationalization: react-i18next

Setup and Installation

  1. Ensure you have Node.js and npm installed on your system.
  2. Clone the repository to your local machine.
  3. Navigate to the project directory and run npm ci to install dependencies.
  4. Start the development server with npm start.
  5. The app should now be running on http://localhost:3000.

Note: This frontend application requires a corresponding backend server running on http://localhost:3001 to handle game logic and Socket.IO connections.

How to Play

  1. Enter a room ID and select a game mode (Human vs Human or Human vs AI).
  2. Click "Join Room" to enter the game.
  3. The game starts when two players join the room (or one player for AI mode).
  4. Click on a highlighted cell to make a move during your turn.
  5. Use the chat feature to communicate with your opponent.
  6. The game ends when no more moves are possible. The player with the most pieces wins.

Server: Reversi Node.js Backend

The server is a Node.js application that handles game logic, real-time communication, and persistent storage using Redis.

Features

  • Socket.IO for real-time game updates
  • Redis for game state persistence
  • Support for both player vs player and player vs AI modes
  • Robust game logic implementation including move validation
  • Player management and turn control
  • Chat functionality
  • Logging with Winston

Dependencies

  • Express
  • Socket.IO
  • ioredis
  • Winston
  • dotenv

Setup

  1. Install dependencies: npm ci
  2. Set up environment variables: Create a .env file in the root directory and add the following:
  3. Start the server: npm run start The server will be running on http://localhost:3001 (or the port specified in your .env file).

Game Logic

The game implements the standard Reversi rules, including:

  • Valid move checking
  • Piece flipping
  • Turn management
  • Win condition checking

Socket.IO Events

  • joinRoom: Join a game room
  • makeMove: Make a move on the board
  • sendMessage: Send a chat message
  • getLegalMoves: Get legal moves for the current player
  • updateBoard: Receive updated board state
  • switchTurn: Notify of turn change
  • gameStart: Notify that the game has started
  • gameOver: Notify of game end and winner
  • skipTurn: Notify when a player's turn is skipped
  • playerDisconnected: Notify when a player disconnects

AI Implementation

The game includes a simple AI opponent that:

  • Chooses moves based on the number of pieces flipped
  • Has a delay before making moves to simulate "thinking"

Data Persistence

Game states are stored in Redis with a TTL of 1 hour.

Error Handling and Logging

The application uses Winston for logging. Logs are written to:

  • error.log for error-level logs
  • combined.log for all logs

In non-production environments, logs are also output to the console.

Development

To run the server in development mode: npm run start

About

This repository contains a full-stack implementation of the classic board game Reversi. It consists of a Node.js backend with Socket.IO for real-time communication.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published