dimanche 4 septembre 2022

Is the logic for this 3 player tournament sound? How would I handle a 3 way tie?

I was bored today and was watching Youtube. In one video I saw 3 people competing against each-other in a game of pool to win a prize. We'll list the 3 people as a, b, and c. In the tournament if someone one they got a point, and then competed against the person who hasn't played yet.

So, a played against b. If a wins, c plays against a. if c wins, b plays against c. etc...

The only issue I see in my program is that sometimes there is a 3 way tie, and i'm not sure how to fairly compute that? I am using a random number generator to generate a number 1-2 giving each team a 50% chance to win against eachother.

<html>
    <body>
        <h1>Challenge</h1>
        <h2 id="genRanNumb">_</h2>
        <h2 id="rankings">_</h2>
    </body>
    <script>
        let genRanNumb = (n) => {
            return randomInt = Math.floor(Math.random() * n) + 1;
        }
        let [scoreA, scoreB, scoreC] = [0, 0, 0];
        let [teamAB, teamBC, teamAC] = [true, null, null]


        if (teamAB === true) { //team that fights
            let winnerAB = genRanNumb(2); // 50% Chance of each team winning when they fight
            console.log("winnerAB:", winnerAB);
            if(winnerAB === 1) { //1 means A wins, 2 means B wins
                scoreA++; //Score goes up for winner
                teamAC = true; //next challenge starts against the next contestant
                if(teamAC === true){
                    let winnerAC = genRanNumb(2);
                    console.log("winnerAC:", winnerAC);
                    if(winnerAC === 1){
                        scoreA++;
                    }else if(winnerAC === 2){
                        scoreC++;
                        teamBC = true;
                        if(teamBC === true) {
                            let winnerBC = genRanNumb(2);
                            console.log("winnerBC:", winnerBC);
                            if(winnerBC === 1){
                                scoreB++;
                            }else if(winnerBC === 2){
                                scoreC++;
                            }
                        }
                    }
                }
            }else if (winnerAB === 2){
                scoreB++;
                teamBC = true;
                if(teamBC === true){
                    let winnerBC = genRanNumb(2);
                    console.log("winnerBC:", winnerBC);
                    if(winnerBC === 1){
                        scoreB++;
                    }else if(winnerBC === 2){
                        scoreC++;
                        let winnerAC = genRanNumb(2);
                        console.log("winnerAC:", winnerAC);
                        if(winnerAC === 1){
                            scoreA++;
                        }else if(winnerAC === 2){
                            scoreC++;
                        }
                    }
                }
            }
            
            document.getElementById("rankings").innerHTML = "A Team: " + scoreA + "<br />B Team: " + scoreB + "<br />C Team: " + scoreC;

        }
    </script>
</html>

Also is there a cleaner way to write this? I was trying to make the repetitive parts into a function but since they all have unique variables I am struggling a bit with that.

Any advice helps! I'm just doing this for fun and to try to learn a bit of statistics in all honesty haha.




Aucun commentaire:

Enregistrer un commentaire