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

49 Views Asked by At

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.

1

There are 1 best solutions below

0
Lajos Arpad On

You are over-complicating this problem. Basically what you need is to have a concept for player and some functionalities. If you manage to define reusable concepts, then you can avoid the confusion you had in your code. Example:

class Player {
    constructor(name) {
        this.name = name;
        this.score = 0;
    }
    
    win() {
        this.score++;
    }
}

function display(players) {
    document.getElementById("scores").innerHTML = `
        <ul>
            <li>${players[0].name}: ${players[0].score}</li>
            <li>${players[1].name}: ${players[1].score}</li>
            <li>${players[2].name}: ${players[2].score}</li>
        </ul>
    `;
}

function start() {
    let players = [
        new Player('a'),
        new Player('b'),
        new Player('c')
    ];
    let numberOfGames = document.getElementById("count").value;
    if (numberOfGames <= 0) alert ("Invalid game number");
    else {
        let currentPlayers = [0, 1];
        let waiting = 2;
        while (numberOfGames--) {
            let result = parseInt(Math.random() * 2);
            players[currentPlayers[result]].win();
            let aux = waiting;
            waiting = currentPlayers[1 - result];
            currentPlayers[1 - result] = aux;
        }
        display(players);
    }
}
#scores {
    background-color: green;
    color: white;
    font-weight: bold;
}
<input placeholder="How many games" type="number" id="count">
<input type="button" value="Start" onclick="start()">
<div id="scores"></div>

Here I'm merely outputting the score, but of course we can handle the result (whatever it may be, even a three-way tie if needed), but in order to add further logic I would need to know what else is needed.