-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.ts
72 lines (62 loc) · 1.93 KB
/
game.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
// 需要实现的类
class BLGame {
constructor() {}
// 每次投球得分记录
private records: number[] = [];
// 计算出来的每一轮有效得分,长度为 10
private roundScores: number[] = [];
// 记录击倒球瓶数
public roll(pins: number): void {
this.records.push(pins);
}
// 获取总分
public score(): number {
this.goThroughRecords();
const result = this.roundScores.reduce((sum, record) => {
return isNaN(record) ? sum : sum + record;
}, 0);
return result;
}
private goThroughRecords(): void {
let roundIndex = 0; // 每一轮,总共 10 轮
let skip = false; // 是否跳过,补中情况和两次得分不够10,第二次 record 应该跳过,算作补中这一轮分数
this.records.forEach((record: number, index: number) => {
let currentRoundScore = 0; // 该轮得分
if (roundIndex > 10) {
return;
}
if (skip) {
skip = false;
return;
}
if (record === 10) {
// 全中
currentRoundScore =
10 + this.records[index + 1] + this.records[index + 2];
this.roundScores.push(currentRoundScore);
roundIndex++;
return;
} else if (record + this.records[index + 1] === 10) {
// 补中
currentRoundScore = 10 + this.records[index + 2];
this.roundScores.push(currentRoundScore);
roundIndex++;
skip = true;
return;
} else {
// 两次得分不到 10
currentRoundScore = record + this.records[index + 1];
this.roundScores.push(currentRoundScore);
roundIndex++;
skip = true;
return;
}
});
}
}
// // 所有击倒球瓶数:(1, 4), (4, 5), (6, 4), (5, 5), (10), (0, 1), (7, 3), (6, 4), (10), (2, 8, 6)
const example = new BLGame();
[1, 4, 4, 5, 6, 4, 5, 5, 10, 0, 1, 7, 3, 6, 4, 10, 2, 8, 6].forEach((pins) => {
example.roll(pins);
});
example.score(); // 133