前端面试题目-保龄球计分算法

题目要求

Create a program, which, given a valid sequence of rolls for one line of American Ten-Pin Bowling,produces the total score for the game.

We can briefly summarize the scoring for this form of bowling:

  • Each game, or “line” of bowling, includes ten turns, or “frames” for the bowler.
  • In each frame, the bowler gets up to two tries to knock down all the pins.
  • If in two tries, he fails to knock them all down, his score for that frame is the total number of pins knocked down in his two tries.
  • If in two tries he knocks them all down, this is called a “spare” and his score for the frame is ten plus the number of pins knocked down on his next throw (in his next turn).
  • If on his first try in the frame he knocks down all the pins, this is called a “strike”. His turn is over, and his score for the frame is ten plus the simple total of the pins knocked down in his next two rolls.
  • If he gets a spare or strike in the last (tenth) frame, the bowler gets to throw one or two more bonus balls, respectively. These bonus throws are taken as part of the same turn. If the bonus throws knock down all the pins, the process does not repeat: the bonus throws are only used to calculate the score of the final frame.
  • The game score is the total of all frame scores

Invalid input should be handled appropriately.

When scoring “X” indicates a strike, “/” indicates a spare, “-” indicates a miss

Notes:

  • X X X X X X X X X X X X (12 rolls: 12 strikes) = 10 frames * 30 points = 300
  • 9- 9- 9- 9- 9- 9- 9- 9- 9- 9- (20 rolls: 10 pairs of 9 and miss) = 10 frames * 9 points = 90
  • 5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/5 (21 rolls: 10 pairs of 5 and spare, with a final 5) = 10 frames * 15
  • points = 150

Rules for submission:

  • Your submission must run without errors and be compiled and built by us.
  • Ensure your test coverage is sufficient and validates your program against sample input
  • You are not permitted to use any external libraries to solve this problem. You may use external
  • libraries in your tests only, such as a mocking framework and related components.
  • Use an appropriate build tool – Maven, Gradle etc
  • We are looking at the way you design and write your code using sound principles and
  • conventions.
  • Clean code is especially important to the way we build software

代码实现

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>保龄球计分算法</title>
    <!-- <script src="./main.js" charset="utf-8"></script> -->
  </head>
  <body>
    <h1>保龄球计分算法</h1>
  </body>
  <script type="text/javascript">
    // 保龄球计分入口
    function Bowling(){
      const frames = 10; // 每一局固定十格
      const tries = 2; // 每一轮有两次投球机会

      // 转换字符为数字
      this.parseNumber = function(input){
        if (input.length > 1) return false;
        let out;
        switch(input){
          case "X":
            out = 10;
            break
          case "/":
            out = 10;
            break;
          case "-":
            out = 0;
            break;
          default:
            out = parseInt(input);
            break;
        }
        return out;
      }

      // 判断字符串是否合法
      this.isValid = function(seq){
        if (seq.length < 10 || seq.length > 12) return false;

        if (seq[9] == "X" && seq.slice(10,seq.length).join("").length != 2) return false;

        for (let item of seq){
          if (item.substring(0,1) == "/") return false;
          else if (item.indexOf("0") != -1) return false;
          else if (item.indexOf("X") == -1 && item.length != 2 && seq.length < 11) return false;
          else if (item.length > 1 && item.indexOf("X") != -1) return false;
          else if (item.length == 2){
            reg = /^[0-9][0-9]$/
            _ = item.split("");
            if (reg.test(item) && this.parseNumber(_[0]) + this.parseNumber(_[1]) > 10) return false;
          }
        }
        return true;
      }

      // 格式化字符串
      this.normalStr = function(seq){
        seq = seq.replace(/s+/g,"");
        let out = new Array(); // 存储结果

        let i = 0;
        while(i < seq.length){
          if (seq[i] == "X"){
            out.push(seq.substring(i,i+1));
            i++;
          } else{
            out.push(seq.substring(i,i+2));
            i+=2
          }
        }
        return out;
      }

      this.parse = function(sequence){
        let score = 0; // 统计得分
        let seq = this.normalStr(sequence);
        if (!this.isValid(seq)) return -1;

        for (var i=0;i<frames;i++){
          let data = seq[i]; // 每一格数据
          let out = 0; // 统计每一格的得分
          let _this = this;
          for(j = 0; j < tries; j++){
            if(data[j]=="X"){
              out += 10;
              next_throw = seq[i+1].split("")
              if (next_throw.length == 2){
                if (next_throw[1]=="/"){
                  out += 10
                } else{
                  out += next_throw.reduce(function(x,y){return _this.parseNumber(x)+ _this.parseNumber(y)});
                }
              } else if(next_throw.length < 2){
                out += this.parseNumber(next_throw[0]);
                out += this.parseNumber(seq[i+2].split("")[0]);
              }
              break; // 如果一次全中,就不投第二次了
            } else if(data[j]=="/"){
              out = 10;
              out += this.parseNumber(seq[i+1].substring(0,1)); // 取下一格的第一位
            } else if (data[j]=="-"){
              out += 0;
            } else {
              out += this.parseNumber(data[j]);
            }
          }
          score += out;
        }
        return score;
      } // end of function
    }

    let g = new Bowling();

    console.assert(g.parse("10") == -1 )
    console.assert(g.parse("XXXXXXXXXX") == -1);
    console.assert(g.parse("XXXXXXXXXXX") == -1);
    console.assert(g.parse("XXXXXXXXXX101") == -1);
    console.assert(g.parse("XXXXXXXXXX1-1") == -1);

    console.assert(g.parse("12 34 56 78 9- 12 34 56 78 90") == -1);
    console.assert(g.parse("12 34 55 78 9- 12 34 56 78 90") == -1);
    console.assert(g.parse("12 34 5/ 72 9- 12 34 5/ 71 90") == -1);
    console.assert(g.parse("12 34 5/ 72 9- 12 34 5/ 71 X/2") == -1);
    console.assert(g.parse("12 34 5/ 72 9- 12 34 5/ 71 XX") == -1);
    console.assert(g.parse("12 34 5/  X 29 -1 23 45 /7 1X") == -1);
    console.assert(g.parse("12 34 5/  X 18 -1 23 45 /7 1X") == -1);
    console.assert(g.parse("12 34 5/  X 18 -1 23 45 27 1X") == -1);

    console.assert(g.parse("XXXXXXXXXX1-") == 272);
    console.assert(g.parse("--------------------") == 0);

    console.assert(g.parse("12 34 5/ 72 9- 12 34 5/ 71 9-") == 89);
    console.assert(g.parse("12 34 5/ 72 9- 12 34 5/ 71 9/1") == 91);
    console.assert(g.parse("12 34 5/ 72 9- 12 34 5/ 71 9/X") == 100);
    console.assert(g.parse("12 34 5/  X 18 -1 23 45 27 XX1") == 103);

    console.assert(g.parse("X X X X X X X X X X X X") == 300);
    console.assert(g.parse("9- 9- 9- 9- 9- 9- 9- 9- 9- 9-") == 90);
    console.assert(g.parse("5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/5") == 150);

  </script>
</html>

暂无评论

发表评论

您的电子邮件地址不会被公开,必填项已用*标注。

相关推荐

暂无相关文章!