public static string GetScore(IPlayer player1, IPlayer player2, IMatchKeeper matchKeeper, ScoreType scoreType) { if (scoreType == ScoreType.Current) { Enums.Score currentPoint; Enums.Score currentOppPoint; var p1IsServing = matchKeeper.CurrentServingPlayerId == player1.Id; if (p1IsServing) { currentPoint = matchKeeper.GetCurrentScore(player1.Id); currentOppPoint = matchKeeper.GetCurrentScore(player2.Id); } else { currentPoint = matchKeeper.GetCurrentScore(player2.Id); currentOppPoint = matchKeeper.GetCurrentScore(player1.Id); } if (currentPoint == Enums.Score.WinningPoint || currentPoint == Enums.Score.Advantage) { return(currentPoint == Enums.Score.WinningPoint ? $"Win for {player1.Name}" : $"Advantage {player1.Name}"); } if (currentOppPoint == Enums.Score.WinningPoint || currentOppPoint == Enums.Score.Advantage) { return(currentOppPoint == Enums.Score.WinningPoint ? $"Win for {player2.Name}" : $"Advantage {player2.Name}"); } return(GetScoreString(currentPoint, currentOppPoint)); } else { return(GetScoreString(player1, player2, matchKeeper)); } }
private static string BuildScore(int playerId, IMatchKeeper matchKeeper) { var sb = new StringBuilder(); var playerScores = matchKeeper.MatchScoreList .Where(msl => msl.PlayerId == playerId && msl.SetNumber <= matchKeeper.CurrentSet && msl.GameNumber < matchKeeper.CurrentGame) .GroupBy(item => item.SetNumber); foreach (var set in playerScores) { var numWins = 0; foreach (var game in set) { if (game.FinalScore == Enums.Score.WinningPoint) { numWins++; } } sb.Append($"{numWins}\t"); } return(sb.ToString()); }
public TennisGame(string player1Name, string player2Name) { var playerList = new List <IPlayer>(); var player1 = new Player(1, player1Name); playerList.Add(player1); var player2 = new Player(2, player2Name); playerList.Add(player2); PlayerList = playerList; MatchKeeper = new MatchKeeper(player1.Id, player2.Id); MatchKeeper.CurrentServingPlayerId = player1.Id; }
private static string GetScoreString(IPlayer player1, IPlayer player2, IMatchKeeper matchKeeper) { var sb = new StringBuilder(); sb.Append($"{(player1.Name.Length > 10 ? player1.Name.Remove(10) : player1.Name)} - \t"); sb.Append($"{(int)matchKeeper.GetCurrentScore(player1.Id)}\t"); var longScore = BuildScore(player1.Id, matchKeeper); sb.Append($"{longScore}\r\n"); sb.Append($"{(player2.Name.Length > 10 ? player2.Name.Remove(10) : player2.Name)} - \t"); sb.Append($"{(int)matchKeeper.GetCurrentScore(player2.Id)}\t"); longScore = BuildScore(player2.Id, matchKeeper); sb.Append($"{longScore}\r\n"); return(sb.ToString()); }