-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathLottoDevice.js
More file actions
71 lines (57 loc) · 1.66 KB
/
Copy pathLottoDevice.js
File metadata and controls
71 lines (57 loc) · 1.66 KB
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
import { Random } from "@woowacourse/mission-utils";
import Lotto from "./Lotto.js";
const DEFAULT_RANKS = [
[1, 0],
[2, 0],
[3, 0],
[4, 0],
[5, 0],
[0, 0],
];
class LottoDevice {
#lottos;
constructor() {
this.#lottos = [];
}
issueLottos(amount) {
const issuedCount = LottoDevice.getCanIssueAmount(amount);
while (this.#lottos.length !== issuedCount) {
const numbers = LottoDevice.getRandomLottoNumbers();
const lotto = new Lotto(numbers);
this.#lottos.push(lotto);
}
}
getLottos() {
return this.#lottos.map((lotto) => lotto.getLottoNumbers());
}
static getCanIssueAmount(amount) {
return amount / Lotto.cost;
}
static getRandomLottoNumbers() {
return Random.pickUniqueNumbersInRange(1, 30, 5).sort((a, b) => a - b);
}
getRankResult(winningNumbers, bonusNumber) {
const ranks = new Map(DEFAULT_RANKS);
this.#lottos.forEach((lotto) => {
const rank = LottoDevice.calcRank(lotto, winningNumbers, bonusNumber);
const prev = ranks.get(rank);
ranks.set(rank, prev + 1);
});
return ranks;
}
static calcRank(lotto, numbers, bonusNumber) {
let correctCount = 0;
const lottoNumbers = lotto.getLottoNumbers();
numbers.forEach((n) => {
if (lottoNumbers.includes(n)) correctCount += 1;
});
const isBonusNumCorrect = lottoNumbers.includes(bonusNumber);
if (correctCount === 5) return 1;
if (correctCount === 4 && isBonusNumCorrect) return 2;
if (correctCount === 4) return 3;
if (correctCount === 3 && isBonusNumCorrect) return 4;
if (correctCount === 2 && isBonusNumCorrect) return 5;
return 0;
}
}
export default LottoDevice;