-
Notifications
You must be signed in to change notification settings - Fork 1
/
Puzzles.ts
301 lines (287 loc) · 9.15 KB
/
Puzzles.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import { sudokuStrategyArray } from "sudokuru";
import { gameResults, puzzle, statistics } from "../../Types/Puzzle.Types";
import { activeGame } from "../../Types/Puzzle.Types";
import { returnLocalGame } from "../LocalStore/DataStore/LocalDatabase";
import {
getKeyJSON,
removeData,
storeData,
} from "../AsyncStorage/AsyncStorage";
import { Statistics } from "./Statistics";
// Random games to be used by getRandomGame for landing page
const DEMO_RANDOM_GAMES: puzzle[][] = [
[
{
puzzle:
"003070040006002301089000000000107080517000006000400000271009005095000000000020000",
puzzleSolution:
"123675948456982371789314562964157283517238496832496157271849635395761824648523719",
moves: [
{
puzzleCurrentState: "",
puzzleCurrentNotesState: "",
},
],
strategies: ["NAKED_SINGLE", "HIDDEN_SINGLE", "NAKED_PAIR"],
difficulty: 348,
drillStrategies: ["NAKED_SINGLE", "POINTING_PAIR", "POINTING_TRIPLET"],
},
{
puzzle:
"020400000006009130000015200000080000300000500800064700014702805000001090000000000",
puzzleSolution:
"123476958456829137789315246247583619361297584895164723914732865578641392632958471",
moves: [
{
puzzleCurrentState: "",
puzzleCurrentNotesState: "",
},
],
strategies: ["NAKED_SINGLE", "HIDDEN_SINGLE"],
difficulty: 2,
drillStrategies: ["POINTING_PAIR", "NAKED_TRIPLET", "HIDDEN_QUADRUPLET"],
},
{
puzzle:
"103000900006000001009300024000006040060007813817005002090000430000009080000020000",
puzzleSolution:
"123574968456982371789361524932816745564297813817435692291658437375149286648723159",
moves: [
{
puzzleCurrentState: "",
puzzleCurrentNotesState: "",
},
],
strategies: ["NAKED_SINGLE", "HIDDEN_SINGLE"],
difficulty: 64,
drillStrategies: [
"NAKED_SINGLE",
"POINTING_TRIPLET",
"HIDDEN_QUADRUPLET",
],
},
{
puzzle:
"020609780050000090709050340071008000004000000060020000030001000900007020000000160",
puzzleSolution:
"123649785456873291789152346271938654594716832368524917632481579915367428847295163",
moves: [
{
puzzleCurrentState: "",
puzzleCurrentNotesState: "",
},
],
strategies: ["NAKED_SINGLE", "HIDDEN_SINGLE"],
difficulty: 654,
drillStrategies: ["HIDDEN_PAIR", "POINTING_TRIPLET"],
},
{
puzzle:
"020700080050091007709304000000500098004010000290000040008060300000180004900000070",
puzzleSolution:
"123756489456891237789324561317542698864913725295678143578469312632187954941235876",
moves: [
{
puzzleCurrentState: "",
puzzleCurrentNotesState: "",
},
],
strategies: ["NAKED_SINGLE", "HIDDEN_SINGLE"],
difficulty: 835,
drillStrategies: ["NAKED_SINGLE", "NAKED_QUADRUPLET"],
},
{
puzzle:
"120000000400009017780500034060000003000065000800070002000000400500900001300080050",
puzzleSolution:
"123647895456839217789512634261498573937265148845371962618753429574926381392184756",
moves: [
{
puzzleCurrentState: "",
puzzleCurrentNotesState: "",
},
],
strategies: ["NAKED_SINGLE", "HIDDEN_SINGLE"],
difficulty: 96,
drillStrategies: [
"NAKED_PAIR",
"HIDDEN_PAIR",
"HIDDEN_TRIPLET",
"POINTING_TRIPLET",
"NAKED_QUADRUPLET",
],
},
{
puzzle:
"100060500006780300709020406200040005090000021000007030002000800640070000900400000",
puzzleSolution:
"123964578456781392789523416237149685594836721861257934312695847648372159975418263",
moves: [
{
puzzleCurrentState: "",
puzzleCurrentNotesState: "",
},
],
strategies: [
"NAKED_SINGLE",
"HIDDEN_SINGLE",
"NAKED_PAIR",
"HIDDEN_PAIR",
"POINTING_PAIR",
],
difficulty: 24,
drillStrategies: ["HIDDEN_PAIR", "POINTING_PAIR", "NAKED_TRIPLET"],
},
{
puzzle:
"000050080000003109780000005000320008500090060000000200041006073000002050600007040",
puzzleSolution:
"123459786456873129789261435967324518512798364834615297241586973378942651695137842",
moves: [
{
puzzleCurrentState: "",
puzzleCurrentNotesState: "",
},
],
strategies: ["NAKED_SINGLE", "HIDDEN_SINGLE", "NAKED_PAIR"],
difficulty: 246,
},
{
puzzle:
"000000800450000001009000004030080900800370000000526040000400070608002010007000506",
puzzleSolution:
"123647895456893721789215634532184967864379152971526348315468279698752413247931586",
moves: [
{
puzzleCurrentState: "",
puzzleCurrentNotesState: "",
},
],
strategies: ["NAKED_SINGLE", "HIDDEN_SINGLE", "NAKED_PAIR"],
difficulty: 622,
drillStrategies: ["NAKED_PAIR"],
},
{
puzzle:
"020000008000700900000000010007009100000008056240006000005302000890004030300800560",
puzzleSolution:
"123945678456781923789623415567239184931478256248156397675312849892564731314897562",
moves: [
{
puzzleCurrentState: "",
puzzleCurrentNotesState: "",
},
],
strategies: ["NAKED_SINGLE", "HIDDEN_SINGLE", "NAKED_PAIR"],
difficulty: 291,
drillStrategies: [
"HIDDEN_SINGLE",
"NAKED_PAIR",
"POINTING_TRIPLET",
"NAKED_QUADRUPLET",
],
},
],
];
/**
* Functions to handle puzzle related operations
*/
export class Puzzles {
/**
* Given a difficulty and an user auth token retrieves a random puzzle close to the difficulty that the user hasn't solved before
* @param difficulty - difficulty number (between 0 and 1)
* @param strategies - new game can have subset of these strategies
* @returns promise of puzzle JSON object
*/
public static async startGame(
difficulty: number,
strategies: sudokuStrategyArray
): Promise<puzzle[]> {
difficulty = difficulty * 1000;
let concatUrlString = "";
for (let i = 0; i < strategies.length; i++) {
concatUrlString =
concatUrlString + "&learnedStrategies[]=" + strategies[i];
}
// for now just returning a random game
// this will be updated to account for difficulty and strategies learned
return returnLocalGame();
}
/**
* Given an user auth token retrieves the users active game or returns null if the user doesn't have an active game
* @returns promise of activeGame JSON object
*/
public static async getGame(): Promise<activeGame[]> {
return await getKeyJSON("active_game");
}
/**
* Given a game saves it to AsyncStorage
* @param game - activeGame JSON object
*/
public static async saveGame(game: activeGame) {
storeData("active_game", JSON.stringify([game]));
}
/**
* Given deletes the users active game and returns game results/statistics
* @returns promise of gameResults JSON object
*/
public static async finishGame(): Promise<gameResults> {
// retrieve the active game
let activeGame: activeGame[] = await getKeyJSON("active_game");
// remove the game from storage
await removeData("active_game");
// retrieve statistics from the game
let numWrongCellsPlayed = activeGame[0].numWrongCellsPlayed
? activeGame[0].numWrongCellsPlayed
: 0;
let finalTime = activeGame[0].currentTime;
let difficulty = activeGame[0].difficulty;
let numHintsUsed = activeGame[0].numHintsUsed
? activeGame[0].numHintsUsed
: 0;
// calculate score
let difficultyScore: number = (difficulty / 1000) * 30;
let hintAndIncorrectCellsScore: number =
numWrongCellsPlayed + numHintsUsed > 40
? 0
: 40 - numWrongCellsPlayed - numHintsUsed;
let timeScore: number = finalTime / 60 > 30 ? 0 : 30 - finalTime / 60;
let score: number = Math.round(
difficultyScore + hintAndIncorrectCellsScore + timeScore
);
// Create or update user's statistics
let statistics: statistics = await Statistics.getStatistics();
statistics.totalScore += score;
if (
finalTime < statistics.fastestSolveTime ||
statistics.fastestSolveTime === 0
) {
statistics.fastestSolveTime = finalTime;
}
statistics.totalSolveTime += finalTime;
statistics.numGamesPlayed += 1;
statistics.numHintsUsed += numHintsUsed;
statistics.numWrongCellsPlayed += numWrongCellsPlayed;
statistics.averageSolveTime = Math.round(
statistics.totalSolveTime / statistics.numGamesPlayed
);
Statistics.saveStatisitics(statistics);
// return results
return {
score: score,
solveTime: finalTime,
numHintsUsed: numHintsUsed,
numWrongCellsPlayed: numWrongCellsPlayed,
difficulty: difficulty,
};
}
/**
* Returns a random game to be displayed on the landing page
* @returns JSON puzzle object
*/
public static getRandomGame(): puzzle[] {
return DEMO_RANDOM_GAMES[
Math.floor(Math.random() * DEMO_RANDOM_GAMES.length)
];
}
}