保龄球积分算法

保龄球积分算法

Posted by nolan on March 7, 2022

简要介绍下保龄球的记分规则:

1、一局游戏的记分分为 10 格,每格可投球两次,投球数记录在格内上方小格。

2、每格的记分需要用到后续格的击倒球瓶数,因此第 10 格投完后,还需要额外的投球用于记分,在第 10 格上方有三个投球数小格。

3、全中格:第一次投球,就击倒全部 10 个球瓶,不进行第二次投球,小格全黑即表示全中。

  全中格的记分方式:10 + 接下来的两次投球击倒瓶数之和。

4、补中格:该格的第二次投球,击倒剩余的全部球瓶,小格半黑即表示补中。

  补中格的记分方式:10 + 接下来的一次投球击倒瓶数。

5、失误格:该格的两次投球,未能击倒全部球瓶。

  失误格的计分方式:两次投球击倒瓶数之和。

正常游戏的一局记分示例如下:

image

所有击倒球瓶数:(1, 4), (4, 5), (6, 4), (5, 5), (10), (0, 1), (7, 3), (6, 4), (10), (2, 8, 6)

第一格为失误,该格分数为 1 + 4 = 5, 填入 0 + 5 = 5。

第二格为失误,该格分数为 4 + 5 = 9,填入 5 + 9 = 14。

第三格为补中,该格分数为 10 + 5 = 15,填入 14 + 15 = 29。

第四格为补中,该格分数为 10 + 10 = 20,填入 29 + 20 = 49。

第五格为全中,该格分数为 10 + 0 + 1 = 11,填入 49 + 11 = 60。

第六格为失误,该格分数为 0 + 1 = 1,填入 60 + 1 = 61。

第七格为补中,该格分数为 10 + 6 = 16,填入 61 + 16 = 77。

第八格为补中,该格分数为 10 + 10 = 20,填入 77 + 20 = 97。

第九格为全中,该格分数为 10 + 2 + 8 = 20,填入 97 + 20 = 117。

第十格为补中,该格分数为 10 + 6 = 16,填入 117 + 16 = 133。

总分为 133。

设计一个类实现保龄球的游戏记分:


// 需要实现的类
class Game {
  // 记录击倒球瓶数
  roll(pins: number): void;
  // 获取总分
  score(): number;
}

// 开始游戏
const game = new Game();

// 每次投球都记录一次击倒球瓶数
game.roll(1);
game.roll(4);
//...
game.roll(3);

// 全部投球完毕都获取总分
const score = game.score();

console.log(score);

// 根据上图示例
// 所有击倒球瓶数:(1, 4), (4, 5), (6, 4), (5, 5), (10), (0, 1), (7, 3), (6, 4), (10), (2, 8, 6)
const example = new Game();
[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

我的实现

注意: 我是用 ts-node 编译执行的,打印的话需要 console, 需要 npm i @types/node -D ,要不会找不到 console 这个变量

// 需要实现的类
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