jeudi 10 octobre 2019

How to write tennis score trackerusing good code rules

I wrote application which return current score of tennis match like Current score in sets, current score in games, current points. Score change when player 1 or 2 won point. I did it in simplest way, just have 3 functions in each check conditions in ifs and then go to next method like that:

Score {
    Point currentPoints;
    Integer currentGamesWon;
    Integer currentSetsWon;
}

addPoint (playerScore, rivalScore){
        if(15)
            set 30;
        ...
        if(40 && more than 2 points of advantage)
            setGamesWon()
    }

setGamesWon(playerScore, rivalScore) {
   gamesWon++;
   if(games won > 6 && advantage > 2 && NotFifthSet) {
       setSetsWon(playerScore, rivalScore);
   }
}

setSetsWon(playerScore, rivalScore) {
    ...
}

I unfortunately can't put here full implementation but I hope you understand idea. There were also tie break code and all needed logic.

And my question is, do you see any clever way to write it ? I mean using design patters or some clever way to remove a lot of ifs which are in current solution ?

Aucun commentaire:

Enregistrer un commentaire