summaryrefslogtreecommitdiff
path: root/utils/board.js
blob: ba43c458746609964f8f1f067a9b7c1344ec361d (plain)
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
export const emptyBoard = [
	[ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
	[ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
	[ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
	[ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
	[ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
	[ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
	[ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
	[ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
	[ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
	[ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
	[ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
	[ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
	[ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
	[ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
	[ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ]
];

export const letters_total = 102;

export function makeLettersBag () {
	return {
		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},
			B:{count:2, points:3}, C:{count:2, points:3}, P:{count:2, points:3},
			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;
	}
	let ret = [];
	for (let i=0; i<count; i++) {
		let n = Math.floor(Math.random() * bag.remaining) -1;
		console.log(n);
		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;
			}
		}
		console.log("j:", j);
		bag.letters[allLetters[j]].count--;
		ret.push(allLetters[j]);
	}
	return ret;
}