- 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.
publicint? Calculate(List<int> fellPins) { var frames = new List<Frame>();
//todo remove this condition after pass single frame test if (fellPins.Count == 2) { if (fellPins[0] != 10) { frames.Add(new Frame(fellPins[0], fellPins[1])); } } if (fellPins.Count == 3 && fellPins[0] + fellPins[1] == 10) { var frame = new Frame(fellPins[0], fellPins[1]); frame.SetBonus(fellPins[2]); frames.Add(frame); } for (int i = 0; i < fellPins.Count; i++) { frames.Add(new Frame(fellPins[0])); } return NullableSum(frames); }
publicint? Calculate(List<int> fellPins) { var frames = new List<Frame>(); //todo remove this condition after pass single frame test if (fellPins.Count == 2) { if (fellPins[0] != 10) { frames.Add(new Frame(fellPins[0], fellPins[1])); } } if (fellPins.Count == 3 && fellPins[0] + fellPins[1] == 10) { var frame = new Frame(fellPins[0], fellPins[1]); frame.SetBonus(fellPins[2]); frames.Add(frame); } for (int i = 0; i < fellPins.Count; i++) { frames.Add(new Frame(fellPins[0])); }
return NullableSum(frames); }
我們建一個 For Loop 目標要將這些醜醜的 if 判斷式移到 Loop 之中 結果大致如下, 過程當然也是逐步的抽離