Skip to content

Latest commit

 

History

39 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

# Mastermind

Welcome to Mastermind, a game to test your pattern recognition, logical thinking, and problem solving abilities.

## Overview

- The computer selects a random pattern of three (easy), four (normal), or five (hard) different numbers from 0 to 7.
- The player has 10 attempts to guess the combinations.
- The computer provides feedback after each guess.
- Winning condition: Guessing the right combination within 10 attempts.

## Example Run

1. The player selects "Easy" difficulty level.
2. The computer will choose a random solution of length three. In this case "223"
3. The player will guess "124"
4. The computer will return a feedback of "1 correct number and 1 correct location"
5. The player will guess "135"
6. The computer will return a feedback of "2 correct number and 1 correct location"
7. The player will continue to guess a string of numbers within the range of 0 - 7 and of length 3 until they run out of guesses or win.

## Setup Instructions

1. Install Node.js on your machine. Please follow instructions here: https://nodejs.org/en/download.
2. Clone the repository to your local machine.
   - `git clone https://github.com/leejun07/Mastermind.git`
3. Install dependencies
   - Navigate to the root directory of the application
     - `cd Mastermind`
     - `npm install`
   - Navigate to the Client directory
     - `cd client`
     - `npm install`
   - Navigate to the Server directory
     - `cd ../server`
     - `npm install`
4. Run client and server
   - Navigate to root directory of the application
   - `cd ..`
   - `npm run dev`
5. Testing
   - Currently server is only tested.
   - Run server tests by:
     - `cd server`
     - `npm run test`

## Playing the game

- Open your browser go to [http://localhost:3000] to start playing Mastermind.
- Select a difficulty level: Easy, Normal, or Hard.
- Input a set of numbers according to difficulty level.
  **All digits must be within the range of 0 - 7**
  - Easy: Length of 3
  - Normal: Length of 4
  - Hard: Length of 5
- Once finished, please press a difficulty level to start over.

## Technologies

- Mastermind is built in **TypeScript**
- Frontend is built in **React**
- The backend runs in a **Node** environment and built in **Express**, following MVC architecture.
  - Tested with **Jest** and **Supertest**.

## Server Code Structure

Server handles all game logic:

- Dynamically fetching a random sequence from the random.org API, based on user-selected difficulty level.
- Initializing a game with necessary data depending on user-selected difficulty level.
- Algorithmically comparing a user-provided guess against the generated solution.
- Dynamically generating feedback to send back to the user.
- Judging winning or losing conditions and sending the client corresponding data.

Services, controllers, routes, configs, utils, types and tests are located with the `src` directory

- **Routes**
  - `gameRoutes.ts`
    - `/start` handles logic to start the game
    - `/play` handles logic to play the game
- **Controllers**
  - `gameController.ts` handles starting the game depending on user input and the processing of player guesses
- **Services**
  - `ValidationService.ts` handles validating the start request and user input. Will short circuit the request if invalid.
  - `GameCacheService.ts` handles initializing and updating the game cache
  - `GameManagementService.ts` handles the game logic
    - Fetching random solution and parsing the response
    - Checking guess against solution and providing feedback

## Thought Process

Create an application where all the logic is handled in the backend. Each service encapsulates the various aspects of Mastermind. Each service is utilized within the respective controllers to ensure readable and optimal code.

The algorithm developed compares a guess with a solution to determine the number of exact matches and matches (correct digits but not in the correct position). It uses maps to track occurrences of digits in both the solution and the guess, updating counts based on matching positions and digits.

## Future Features

- Enable Multiplayer mode.
- Allow user to host a db to store their scores.
- Set a timer according to difficulty level.
- Chatting between users
- UI improvements

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages