/// <summary> /// Simulates the over of the match. /// </summary> /// <remarks>Rotate the striker at the end of over.</remarks> /// <param name="currentMatch"></param> /// <param name="striker"></param> /// <param name="nonStriker"></param> /// <param name="currentOver"></param> public void NextOver(CurrentMatchDetails currentMatch, ref PlayerDetails striker, ref PlayerDetails nonStriker, ref double currentOver) { for (int j = 1; j <= 6; j++) { currentOver += 0.1; NextBall(currentMatch, ref striker, ref nonStriker, currentOver); } RotateStriker(ref striker, ref nonStriker); }
/// <summary> /// Updates the score card as well as striker score based on the runScored. /// </summary> /// <param name="currentMatch"></param> /// <param name="striker"></param> /// <param name="runsScored"></param> /// <param name="currentOver"></param> public static void UpdateScore(CurrentMatchDetails currentMatch, PlayerDetails striker, int runsScored, double currentOver) { currentMatch.BallsRemaining -= 1; striker.RunsScored += runsScored; striker.BallsFaced += 1; currentMatch.RunsNeedToWin -= runsScored; UpdateCommentory(striker, currentOver, runsScored); if (currentMatch.RunsNeedToWin <= 0) { DisplayMatchResult(currentMatch); } }
/// <summary> /// Prints the match result along with batsman scre details. /// </summary> /// <param name="currentMatch"></param> /// <param name="isWonByChasing"></param> public static void DisplayMatchResult(CurrentMatchDetails currentMatch, bool isWonByChasing = true) { if (isWonByChasing) { Console.WriteLine($"{Environment.NewLine}{currentMatch.Team2_Name} won by {currentMatch.WicketsLeft} wickets with {currentMatch.BallsRemaining} balls remaining."); } else { Console.WriteLine($"{Environment.NewLine}{currentMatch.Team1_Name} won the match with {currentMatch.BallsRemaining} balls remaining."); } foreach (var player in currentMatch.Players) { Console.WriteLine($"{player.Name} - {player.RunsScored}{(!player.IsOut && player.IsOnField ? "*" : "")} runs ({player.BallsFaced})"); } Console.ReadKey(); System.Environment.Exit(1); }
/// <summary> /// Simulates the match for the remaining overs. /// </summary> /// <remarks>Prints the match result once it's done.</remarks> /// <param name="currentMatch">currentMatch details</param> public void SimulateMatch(CurrentMatchDetails currentMatch) { PlayerDetails striker = currentMatch.Players.First(t => t.IsOnField); PlayerDetails nonStriker = currentMatch.Players.Last(t => t.IsOnField); var oversLeft = currentMatch.OversLeft; currentMatch.BallsRemaining = oversLeft * 6; double currentOver = 0; for (int i = 1; i <= oversLeft; i++) { Console.WriteLine($"{Environment.NewLine}{currentMatch.OversLeft} overs left. {currentMatch.RunsNeedToWin} runs to win."); NextOver(currentMatch, ref striker, ref nonStriker, ref currentOver); currentMatch.OversLeft -= 1; currentOver += 0.4; } ScoreBoardService.DisplayMatchResult(currentMatch, false); }
/// <summary> /// Simulates the ball of the match. /// </summary> /// <remarks>Updates the score card based on the player score.</remarks> /// <param name="currentMatch"></param> /// <param name="striker"></param> /// <param name="nonStriker"></param> /// <param name="currentOver"></param> public void NextBall(CurrentMatchDetails currentMatch, ref PlayerDetails striker, ref PlayerDetails nonStriker, double currentOver) { switch (_playerScoreHelper.Strike(striker)) { case 0: currentMatch.BallsRemaining -= 1; striker.BallsFaced += 1; ScoreBoardService.UpdateCommentory(striker, currentOver, 0); break; case 1: ScoreBoardService.UpdateScore(currentMatch, striker, 1, currentOver); RotateStriker(ref striker, ref nonStriker); break; case 2: ScoreBoardService.UpdateScore(currentMatch, striker, 2, currentOver); break; case 3: ScoreBoardService.UpdateScore(currentMatch, striker, 3, currentOver); RotateStriker(ref striker, ref nonStriker); break; case 4: ScoreBoardService.UpdateScore(currentMatch, striker, 4, currentOver); break; case 5: ScoreBoardService.UpdateScore(currentMatch, striker, 5, currentOver); RotateStriker(ref striker, ref nonStriker); break; case 6: ScoreBoardService.UpdateScore(currentMatch, striker, 6, currentOver); break; case 7: ScoreBoardService.UpdateWicket(currentMatch, ref striker, nonStriker, currentOver); break; default: throw new ArgumentException("Ivalid strike"); } }
/// <summary> /// Updates the score card as well as striker for an wicket. /// </summary> /// <remarks>Gets the new batsman if wickets left else ends the match by printing match summary.</remarks> /// <param name="currentMatch"></param> /// <param name="striker"></param> /// <param name="nonStriker"></param> /// <param name="currentOver"></param> public static void UpdateWicket(CurrentMatchDetails currentMatch, ref PlayerDetails striker, PlayerDetails nonStriker, double currentOver) { striker.BallsFaced += 1; currentMatch.BallsRemaining -= 1; striker.IsOut = true; striker.IsOnField = false; currentMatch.WicketsLeft -= 1; UpdateCommentory(striker, currentOver, 0, true); if (currentMatch.WicketsLeft == 0) { DisplayMatchResult(currentMatch, false); } else { int battingOrder = striker.BattingOrder; striker = striker.BattingOrder < nonStriker.BattingOrder ? currentMatch.Players.FirstOrDefault(t => t.BattingOrder.Equals(nonStriker.BattingOrder + 1)) : currentMatch.Players.FirstOrDefault(t => t.BattingOrder.Equals(battingOrder + 1)); striker.IsOnField = true; } }