vendredi 1 mars 2024

a program which returns the current state of a tennis game.

 

reminder: 

first scrore = 15 points, 

second scor = 30 p , 

third score = 40 ppoints. 

After a player reaches 40 ppoints, he can: 

Enter a DEUCE state if both players have scored the same number of times. 

Enter an ADVANTAGE state if both players scores at least three times and the player scored 1 time more than his opponent. 

WIN the game if he has scored at least four times and 2 times more than the other player. 

Implementation methode: public static string ComputeGameState(string nameP1, string nameP2, string[] wins) wich returns the current state of a game. 

nameP1: the name of first player, 

nameP2: name of second player. 

wins is an array listing the name of each ball's winner.

===============================================


public static string ComputeGameState(string nameP1, string nameP2, string[] wins)
    {
        // Write your code here
        // To debug: Console.Error.WriteLine("Debug messages...");
       
        int scoreP1 = wins.Count(w => w == nameP1);
        int scoreP2 = wins.Count(w => w == nameP2);

        if (scoreP1 >= 4 && scoreP1 >= scoreP2 + 2)
            return $"{nameP1} WINS";
        if (scoreP2 >= 4 && scoreP2 >= scoreP1 + 2)
            return $"{nameP2} WINS";

        if (scoreP1 >= 3 && scoreP2 >= 3)
        {
            if (scoreP1 == scoreP2)
                return "DEUCE";
            if (scoreP1 == scoreP2 + 1)
                return $"{nameP1} ADVANTAGE";
            if (scoreP2 == scoreP1 + 1)
                return $"{nameP2} ADVANTAGE";
        }

        string[] scoreNames = { "0", "15", "30", "40" };
        string score1 = scoreP1 < 3 ? scoreNames[scoreP1] : "40";
        string score2 = scoreP2 < 3 ? scoreNames[scoreP2] : "40";

        if (score1 == score2 && score1 == "15"){
            return "15a";
        }
        if (score1 == score2 && score1 == "30"){
            return "30a";
        }

        return $"{nameP1} {score1} - {nameP2} {score2}";
    }


Aucun commentaire:

Enregistrer un commentaire