summaryrefslogtreecommitdiff
path: root/utils/board.js
diff options
context:
space:
mode:
authorJulien Dessaux2022-12-31 18:13:35 +0100
committerJulien Dessaux2022-12-31 18:13:35 +0100
commit3121f9f60886b291103bfb10b26803196b7e4e6b (patch)
tree5cc4f3566b3c4a3c29700fbf861f1a5795985b37 /utils/board.js
parentFixed game creation (diff)
downloadjeux-de-mots-3121f9f60886b291103bfb10b26803196b7e4e6b.tar.gz
jeux-de-mots-3121f9f60886b291103bfb10b26803196b7e4e6b.tar.bz2
jeux-de-mots-3121f9f60886b291103bfb10b26803196b7e4e6b.zip
Refactored game creation
Diffstat (limited to '')
-rw-r--r--utils/board.js59
1 files changed, 29 insertions, 30 deletions
diff --git a/utils/board.js b/utils/board.js
index 72c7951..f9a3b9d 100644
--- a/utils/board.js
+++ b/utils/board.js
@@ -18,9 +18,9 @@ export const emptyBoard = [
export const letters_total = 102;
-export function makeLettersBag () {
- return {
- letters : {
+export class Bag {
+ constructor() {
+ this.letters = {
JOKER:{count:2, points:0 },
E:{count:15, points:1}, A:{count:9, points:1}, I:{count:9, points:1}, N:{count:6, points:1}, O:{count:6, points:1}, R:{count:6, points:1}, S:{count:6, points:1}, T:{count:6, points:1}, U:{count:6, points:1}, L:{count:5, points:1},
D:{count:3, points:2}, G:{count:2, points:2}, M:{count:3, points:2},
@@ -28,35 +28,34 @@ export function makeLettersBag () {
F:{count:2, points:4}, H:{count:2, points:4}, V:{count:2, points:4},
J:{count:1, points:8}, Q:{count:1, points:8},
K:{count:1, points:10}, W:{count:1, points:10}, X:{count:1, points:10}, Y:{count:1, points:10}, Z:{count:1, points:10},
- },
- remaining: letters_total,
- };
-}
-
-const allLetters = Object.keys(makeLettersBag().letters);
-
-export function pickLetters(bag, count) {
- if (count > bag.remaining) {
- count = bag.remaining;
+ };
+ this.remaining = letters_total;
}
- let ret = [];
- for (let i=0; i<count; i++) {
- let n = Math.floor(Math.random() * bag.remaining);
- let j = 0;
- for (;;) {
- if (bag.letters[allLetters[j]].count === 0) {
- j++;
- } else if (bag.letters[allLetters[j]].count < n) {
- n -= bag.letters[allLetters[j]].count;
- j++;
- } else {
- n = 0;
- break;
+ pick(count) {
+ if (count > this.remaining) {
+ count = this.remaining;
+ }
+ let ret = [];
+ for (let i=0; i<count; i++) {
+ let n = Math.floor(Math.random() * this.remaining);
+ let j = 0;
+ for (;;) {
+ if (this.letters[allLetters[j]].count === 0) {
+ j++;
+ } else if (this.letters[allLetters[j]].count < n) {
+ n -= this.letters[allLetters[j]].count;
+ j++;
+ } else {
+ n = 0;
+ break;
+ }
}
+ this.letters[allLetters[j]].count--;
+ this.remaining--;
+ ret.push(allLetters[j]);
}
- bag.letters[allLetters[j]].count--;
- bag.remaining--;
- ret.push(allLetters[j]);
+ return ret;
}
- return ret;
}
+
+const allLetters = Object.keys(new Bag().letters);