Class: GameForMachines

mastermind-game~ GameForMachines

An interface for implementing strategies that play the game. Sub-classes must implement .guess() in order to play the game!

For example:

const {GameForMachines} = require('mastermind-game')

class YourStrategy extends GameForMachines {
  // guesses randomly, regardless of past guesses
  guess (history) {
    let guess = []
    for (var i = 0; i < this.secretLength; i++) {
      guess.push(Math.floor(Math.random() * this.numChoices))
    }
    return guess
  }
}

new GameForMachines()

Implements:
  • Game

Methods


<abstract> guess(history)

The abstract method that sub-classes must implement in order to generate guesses. For more information on the response object in each turn's history, see Game#evaluateGuess.

Parameters:
Name Type Description
history Array

A history of the guesses so far: [{ guess: [...], response: {...} }, ...]

history[].guess Array

An array of numbers representing the guess made in a particular turn.

history[].response Object

A response generated by Game#evaluateGuess() to the guess made that turn.

Returns:

An error! This method must be implemented by a sub-class.

Type
Error

play()

Plays the game using the class method .guess() to generate guesses. Modifies this.history to reflect the guesses (and responses) made during the game. Returns a boolean indicating victory.

Returns:

Whether the strategy won the game (true) or not (false).

Type
Boolean