Skip to content

Mastermind is a code-breaking game for two players. One player becomes the codemaker , the other the codebreaker . The codemaker chooses a pattern of four color code pegs (duplicates allowed) and the codebreaker tries to guess it, in both order and color. Each guess is made by placing a row of color code pegs on the decoding board. Once placed, …

Notifications You must be signed in to change notification settings

sebastianxcf/mastermind-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mastermind REST-API

Introduction

Mastermind is a code-breaking game for two players. One player becomes the codemaker , the other the codebreaker . The codemaker chooses a pattern of four color code pegs (duplicates allowed) and the codebreaker tries to guess it, in both order and color. Each guess is made by placing a row of color code pegs on the decoding board. Once placed, the codemaker provides feedback by placing from zero to four key pegs in the small holes of the row with the guess. A black key peg (small red in the image) is placed for each code peg from the guess which is correct in both color and position. A white key peg indicates the existence of a correct color code peg placed in the wrong position.

Example: Given a code [RED, BLUE, GREEN, RED] when the codebreaker gives a code with [RED, GREEN, RED, YELLOW] the feedback will be: 1 black, 2 whites. For more information about the game: https://en.wikipedia.org/wiki/Mastermind_(board_game)

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

Java8

Docker (not mandatory) https://docs.docker.com/install/linux/docker-ce/ubuntu/

Configuration

Application-prod.properties for prod configuration. Application-test.properties for test configuration.

The database is a H2 in memory database,to browse the database go http://localhost:{server.port}/h2

Credentials are in the application.propertie files

Installing

In order to deploy a new version of dev follow the next steps.

If you like to use docker, you can build a Docker image

mvn -U clean install docker:build

display the deployed images, you will find a mastermind image

docker images

Then just run the docker image

docker run --rm -p 8080:8080 mastermind

Otherwise you can run just the jar

mvn -U clean install spring-boot:run

In order to verify the installation, make a post request using Swagger http://localhost:8080/swagger-ui.html#/

find the endpoint /mastermind/new/{codeSize}

Running the tests

In order to execute the Junit test please run : mvn test

This will generate the coverage results at : target/site/jacoco/index.html You can see the % of coverage of all the classes. Note that the GameServiceImpl is the core class and has a overage of 97%

Deployment

In order to deploy a production version please run with active-profile = prod

docker run -e SPRING_PROFILES_ACTIVE=prod --rm -p 8080:8080 mastermind  --rm -p 8080:8080 mastermind 

Game play

Create new game

Please use swagger or postman

http://localhost:8080/swagger-ui.html#/

find the endpoint /mastermind/new/{codeSize} As a input just put a code size

As a result you will get the autogeneraed game id

{
  "data": {
    "gameId": 1
  }
}

New guess attempt

In order to play you have to hit the POST endpoint /mastermind/guess/{gameId}

Process the guess for a given gameId.

input json structure

gameId as a variable

Request Body

Posible colors:
BROWN, BLACK, BLUE, GREEN, ORANGE, PINK, PURPLE, RED, SILVER, WHITE, YELLOW

[
  {
    "color": "BROWN"
  },
  {
    "color": "YELLOW"
  },
  {
    "color": "GREEN"
  },
  {
    "color": "PINK"
  }
  
]

Response Body

{
  "data": {
    "guess": [
      {
        "color": "BROWN"
      },
      {
        "color": "SILVER"
      },
      {
        "color": "GREEN"
      },
      {
        "color": "PINK"
      }
    ],
    "onlyColorGuess": [
      {
        "color": "WHITE"
      }
    ],
    "positionColorGuess": [
      {
        "color": "BLACK"
      }
    ],
    "hasWon": false
  }
}

onlyColorGuess

Only hit the color but not position get one a white peg

positionColorGuess

Hit position and color of the ball get one black peg

Get guess history

In order to get hte history you have to hit the GET endpoint

/mastermind/history/{gameId}

gameId as a variable

Response Body

{
  "data": [
    {
      "pegs": [
        {
          "color": "BROWN"
        },
        {
          "color": "YELLOW"
        },
        {
          "color": "GREEN"
        },
        {
          "color": "PINK"
        }
      ],
      "game": 1,
      "numberBlack": 0,
      "numberWhite": 1
    },
    {
      "pegs": [
        {
          "color": "BROWN"
        },
        {
          "color": "SILVER"
        },
        {
          "color": "GREEN"
        },
        {
          "color": "PINK"
        }
      ],
      "game": 1,
      "numberBlack": 1,
      "numberWhite": 1
    }
  ]
}

Git

For new features please follow git flow.

For every now feature >

  1. Create a new feature from development git flow feature start feature_name
  2. Make your changes git flow feature finish feature_name Finish feature - > Will auto merge on development
  3. Push development

For UI in git client in linux I recommend gitkraken

Tech stack

Spring Boot with spring mvc

Spring Boot is an awesome framework to quickly build an Rest based Application. For persistence I used Spring data using h2 in memory database.

Notes

This implementation is a little variation of the original Mastermind game, with the following modifications:

There is no limit to the number of guesses You can play differents games at the same time by switching the game id.

Assumptions

In the scenario that we have in the guess more Pegs of correct color that in the code,it will count only one for each Peg in the code.

In the case of two pegs of same colors (one of them correct position) will prioritize the one that is also in the same position.

In the case of have in the guess a hit on color with two same color in the code, will count only one as correct.

Authors

License

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

About

Mastermind is a code-breaking game for two players. One player becomes the codemaker , the other the codebreaker . The codemaker chooses a pattern of four color code pegs (duplicates allowed) and the codebreaker tries to guess it, in both order and color. Each guess is made by placing a row of color code pegs on the decoding board. Once placed, …

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published