-
Notifications
You must be signed in to change notification settings - Fork 1
/
mm.js
126 lines (99 loc) · 2.49 KB
/
mm.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var Mastermind = function (type) {
var type = (type === 1) ? 1 : 0;
var turn;
var guesses;
var answer;
var initialize = function (gameType) {
type = (gameType !== undefined) ? gameType : type;
guesses = (function (r, c) {
var i;
var m = [];
while (m.length < r) {
m.push([]);
i = m.length - 1;
while (m[i].length < c) {
m[i].push(0);
}
}
return m;
})(10, 5);
turn = 0;
answer = makeAnswer(type);
};
var makeAnswer = function (type) {
var answer = [];
var options = [1, 2, 3, 4, 5, 6];
var i, j;
[0, 1, 2, 3].forEach(function (i) {
j = Math.floor(Math.random() * options.length);
answer[i] = options[j];
if (type === 0) { // unique, therefore remove options
options[j] = 0;
options.sort();
options.shift();
}
});
return answer;
};
var submitGuess = function (i) {
if ( ! (guesses[i][0] && guesses[i][1] && guesses[i][2] && guesses[i][3]) ) return false;
var guess = [0, 1, 2, 3].map(function (j) {
return guesses[i][j];
});
var correct = guess.every(function (v, k) {
return v === answer[k];
});
if ( correct ) {
turn = -1;
return true;
} else {
turn = i + 1;
if (turn === 10) {
turn = -1;
}
return reportResults(guess, answer);
}
};
var rotateColour = function (i, j) {
var n = guesses[i][j];
if ( turn != i ) return n;
if (n === 6 ) guesses[i][j] = 0;
else guesses[i][j] = n + 1;
return guesses[i][j];
};
var reportResults = function (picks, answer) {
var clues = [];
picks.forEach(function( pick, index ) {
if (pick === answer[index]) {
clues[index] = 2;
} else {
var position = answer.map(function(v,i) { return i; }).filter(function(i) {
return clues[i] === undefined && answer[i] === pick;
})[0];
if (position !== undefined) {
clues[position] = 1;
}
}
});
return clues.filter(function (v) { return v !== undefined }).sort().reverse();
};
var getTurn = function () {
return turn;
};
var getType = function () {
return type;
};
var getAnswer = function () {
turn = -1;
return answer;
};
return {
reportResults: reportResults,
rotateColour: rotateColour,
initialize: initialize,
submitGuess: submitGuess,
getType: getType,
getTurn: getTurn,
getAnswer: getAnswer
};
};