forked from dapenguin/Mastermind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mastermind.ts
executable file
·67 lines (56 loc) · 1.89 KB
/
mastermind.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { getPegValueTotals } from './lib/getPegValueTotals';
import { generateCode } from './lib/generateCode';
import { getGuessResult } from './lib/getGuessResult';
import { Guess, GuessResult, PegValueTotals, SecretCode } from './lib/types';
export const Mastermind = function (): void {
// The code the player has to crack
let _secretCode: SecretCode = [];
// Maximum number of tries
const _maxTries = 12;
// Number of tries left
let _triesLeft = 0;
// The number of pegs that make up the code
const _numberOfPegs = 4;
// The maximum zero based value that can be given to a peg
const _maxPegValue = 5;
// Total number of each peg value in the secret code
let _pegValueTotals: PegValueTotals = [];
/**
* Get the secret code. Useful for telling the player the code if they've lost.
* @returns The secret code.
*/
this.getSecretCode = (): SecretCode => _secretCode;
/**
* Process the player's guess.
* @param codeGuess An array containing the player's guess.
* @returns Object containing the results of the player's guess.
*/
this.guess = (codeGuess: Guess): GuessResult => {
_triesLeft--;
return getGuessResult(
codeGuess,
_numberOfPegs,
_secretCode,
_pegValueTotals,
_triesLeft
);
};
/**
* Returns the number of tries the player has left to guess the code.
* @return The number of tries the player has left.
*/
this.getTriesLeft = (): number => _triesLeft;
/**
* Start the game!
* @param secretCode The secret code to play the game with. Mainly used for
* debugging. If no secret code is provided, one will be
* generated.
*/
this.start = (secretCode?: SecretCode) => {
// Reset the number of tries left
_triesLeft = _maxTries;
// Generate the secret code
_secretCode = secretCode || generateCode(_numberOfPegs, _maxPegValue);
_pegValueTotals = getPegValueTotals(_secretCode, _numberOfPegs);
};
};