private static string GetRollsOfLastFrame(Frame f) { if (f.GetRollCount() == 0) return " "; if (f.GetRollCount() == 1) { if (StrikeRule.IsStrike(f[0])) return " X "; return string.Format(" {0} ", f[0].GetPin()); } if (f.GetRollCount() == 2) { if (StrikeRule.IsStrike(f[0])) { if (StrikeRule.IsStrike(f[1])) return " XX "; return string.Format(" X{0} ", f[1].GetPin()); } if (SpareRule.IsSpare(f)) return string.Format(" {0}/ ", f[0].GetPin()); return string.Format(" {0} {1}", f[0].GetPin(), f[1].GetPin()); } if (f.GetRollCount() == 3) { if (StrikeRule.IsStrike(f[0])) { if (StrikeRule.IsStrike(f[1])) { if (StrikeRule.IsStrike(f[2])) return " XXX"; return string.Format(" XX{0}", f[2].GetPin()); } if (SpareRule.IsSpare(f[1], f[2])) return string.Format(" X{0}/", f[1].GetPin()); return string.Format(" X{0}{1}", f[1].GetPin(), f[2].GetPin()); } if (StrikeRule.IsStrike(f[2])) return string.Format(" {0}/X", f[0].GetPin()); return string.Format(" {0}/{1}", f[0].GetPin(), f[2].GetPin()); } throw new Exception(); }
public void LastFrame1Roll_x() { List<Roll> rolls = new List<Roll>(); rolls.Add(new Roll(10)); Frame f = new Frame(FrameCountRule.GetCount() - 1, rolls); Assert.AreEqual(" X ", RollFormatter.GetRollsOfFrame(f)); }
private static int CalcBonusFrame(Frame f, Frames frames) { int bonus = 0; int bonusCount = GetBonusCount(f); foreach (int pins in GetPinsAfterFrame(f, frames)) { if (bonusCount == 0) return bonus; bonus += pins; bonusCount--; } return bonus; }
private static IEnumerable<int> GetPinsAfterFrame(Frame target, Frames frames) { bool found = false; foreach (Frame f in frames) { if (target == f) { found = true; continue; } if (!found) continue; foreach (int pin in f.GetPins()) yield return pin; } }
public static Frames Replace(Frames old, Frame from, Frame to) { Frames frames = new Frames(old._frames.Count); for (int i = 0; i < old._frames.Count; i++) { if (old._frames[i] == from) { frames._frames[i] = to; } else { frames._frames[i] = old._frames[i]; } } return frames; }
public static string GetRollsOfFrame(Frame f) { if (FrameCountRule.IsLastFrame(f)) { return GetRollsOfLastFrame(f); } if (StrikeRule.IsStrike(f)) { return " X"; } if (SpareRule.IsSpare(f)) { return string.Format("{0,2}", f[0].GetPin()) + " /"; } StringBuilder buf = new StringBuilder(); foreach (int pin in f.GetPins()) { buf.AppendFormat("{0,2}", pin); } return buf.ToString(); }
public static Frame Append(Frame old, Roll roll) { List<Roll> tmp = new List<Roll>(old._rolls); tmp.Add(roll); return new Frame(old.GetFrameIndex(), tmp); }
public static bool IsStrike(Frame f) { return IsCountMatch(f) && IsPinMatch(f); }
private static int SumPin(Frame f) { int sum = 0; foreach (int pin in f.GetPins()) sum += pin; return sum; }
private static bool IsPinMatch(Frame f) { return SumPin(f) == PinNumberRule.GetMax(); }
private static bool IsCountMatch(Frame f) { return f.GetRollCount() == 1; }
private static bool IsFullLastFrame(Frame f) { if (f.GetRollCount() == 2) return SumPin(f) < PinNumberRule.GetMax(); return f.GetRollCount() == 3; }
public void LastFrame3Roll_asb() { List<Roll> rolls = new List<Roll>(); rolls.Add(new Roll(1)); rolls.Add(new Roll(9)); rolls.Add(new Roll(2)); Frame f = new Frame(FrameCountRule.GetCount() - 1, rolls); Assert.AreEqual(" 1/2", RollFormatter.GetRollsOfFrame(f)); }
private static int GetBonusCount(Frame f) { if (SpareRule.IsSpare(f)) return 1; if (StrikeRule.IsStrike(f)) return 2; return 0; }
public static bool IsFull(Frame f) { if (IsLastFrame(f)) return IsFullLastFrame(f); if (StrikeRule.IsStrike(f)) return true; return IsCountFull(f) || IsPinFull(f); }
private static bool IsLastFrame(Frame f) { return f.GetFrameIndex() == FrameCountRule.GetCount() - 1; }
public static bool IsLastFrame(Frame f) { return f.GetFrameIndex() == GetCount() - 1; }
private static bool IsCountFull(Frame f) { return f.GetRollCount() == 2; }