public int GetScore(FinalFrame frame) { var frameScore = 0; if (!frame.IsStrike && !frame.IsSpare) { for (var throwIndex = 0; throwIndex < Constants.ThrowsPerFrame; throwIndex++) { frameScore += frame.Throws[throwIndex].Score; } } else { if (frame.IsStrike) { frameScore = Constants.StrikeBonus * Constants.StrikeScore; } else { frameScore = Constants.SpareBonus * Constants.SpareScore; } frameScore += frame.Throws[Constants.ThrowsPerFrame].Score; } return(frameScore); }
public FinalFrame BuildFrame(int firstThrow, int secondThrow, int thirdThrow) { var resultingFrame = new FinalFrame { Throws = new List <Throw> { new Throw(firstThrow), new Throw(secondThrow) } }; if (firstThrow == Constants.StrikeScore) { resultingFrame.IsStrike = true; } if (firstThrow + secondThrow == Constants.SpareScore) { resultingFrame.IsSpare = true; } if (resultingFrame.IsStrike || resultingFrame.IsSpare) { resultingFrame.Throws.Add(new Throw(thirdThrow)); } return(resultingFrame); }
private void BuildGame() { var pinCount = 0; var frameCount = 0; int firstThrow, secondThrow, thirdThrow = 0; while (frameCount < Constants.FramesPerGame - 1) { firstThrow = PinsRolled[pinCount]; secondThrow = PinsRolled[pinCount + 1]; var currentFrame = frame.BuildFrame(firstThrow, secondThrow); Frames.Add(currentFrame); frameCount++; if (currentFrame.IsStrike) { pinCount++; } else { pinCount += 2; } } firstThrow = PinsRolled[pinCount]; secondThrow = PinsRolled[pinCount + 1]; if (pinCount + 2 < PinsRolled.Count) { thirdThrow = PinsRolled[pinCount + 2]; } var gameFinalFrame = finalFrame.BuildFrame(firstThrow, secondThrow, thirdThrow); FinalFrame = gameFinalFrame; }
public int GetScore(List <Frame> frames, FinalFrame finalFrame) { var currentFrame = frames[0]; var firstNextFrame = frames[1]; var secondNextFrame = frames[2]; var frameScore = 0; if (currentFrame.IsStrike) { frameScore = Constants.StrikeScore; frameScore += GetStrikeBonus(firstNextFrame, secondNextFrame, finalFrame); } else if (currentFrame.IsSpare) { frameScore = Constants.SpareScore; frameScore += GetSpareBonus(firstNextFrame, finalFrame); } else { frameScore += currentFrame.Throws[0].Score; frameScore += currentFrame.Throws[1].Score; } return(frameScore); }
private int GetSpareBonus(Frame nextFrame, FinalFrame finalFrame) { if (nextFrame != null) { if (nextFrame.IsStrike) { return(Constants.StrikeScore); } return(nextFrame.Throws[0].Score); } if (finalFrame.IsStrike) { return(Constants.StrikeScore); } return(finalFrame.Throws[0].Score); }
private int GetStrikeBonus(Frame firstNextFrame, Frame secondNextFrame, FinalFrame finalFrame) { if (firstNextFrame == null) { return(finalFrame.Throws[0].Score + finalFrame.Throws[1].Score); } if (firstNextFrame.IsStrike) { return(Constants.StrikeScore + GetNextThrowScore(secondNextFrame, finalFrame)); } if (firstNextFrame.IsSpare) { return(Constants.SpareScore); } return(firstNextFrame.Throws[0].Score + firstNextFrame.Throws[1].Score); }