Multiplayer ELO score in Swift

I’m in the process of developing my first game for iOS. At first I wanted to include the so-called ELO score in the 2nd or 3rd version of the game, but this week my thoughts came around and now I believe that the competitiveness factor that leaderboards and a global ranking bring to the game should be a first release feature.

The Elo-score in short is a system that ranks players in comparison to their peers. Losing a game to a better player is not as bad as losing against a worse player. You also gain more by beating opponents that are better than you. Elo was created for duel games, so it remains to be seen how well it works here.

The design of the multiplayer Elo here is identical to the duel Elo, the only difference being that all players are compared to all their opponents. Please find below the code, hope it helps! I have only tested it shortly and it seems to be working fine for now.

    func adjustEloScores()
    {
        var qScores = Double[]()

        // Populate the QScores, according to ELO-system
        for player in allPlayers
        {
            qScores.append(pow(10.0, Double(player.ELO / 400)))
        }

        // Calculate and apply delta to each player ELO
        for (indexP, player) in enumerate(allPlayers)
        {
            var eScores = Double[]()
            var sScores = Double[]()

            for (indexQ, qScore) in enumerate(qScores)
            {
                if indexP != indexQ
                {
                    eScores.append(qScores[indexP] / (qScores[indexP] + qScore))
                    sScores.append(player.finishedInPosition < allPlayers[indexQ].finishedInPosition ? 1.0 : 0.0)
                }
            }

            var deltaELO = 0

            for (i, eS) in enumerate(eScores)
            {
                deltaELO += Int(KFACTOR * (sScores[i] - eS))
            }

            player.ELO += deltaELO
            player.deltaELO = deltaELO
        }
    }
 
14
Kudos
 
14
Kudos

Now read this

Dealing with large numbers in Swift

Type inference is a handy feature in Swift that makes declaring most variables fun and easy. However, there are cases when you must explicitly define the type of the variable. In this example, the starting point is having 4 big numbers,... Continue →