Today I have solved the problem in javascript
In chess, the Elo rating system is used to calculate player strengths based on game results.
A simplified description of the Elo system is as follows. Every player begins at the same score. For each subsequent game, the loser transfers some points to the winner, where the amount of points transferred depends on how unlikely the win is. For example, a 1200-ranked player should gain much more points for beating a 2000-ranked player than for beating a 1300-ranked player.
Implement this system.
const initialRating = 1000;
const players = {
"Alice": initialRating,
"Bob": initialRating,
"Charlie": initialRating
};
const winner = "Alice";
const loser = "Bob";
const { winnerNewRating, loserNewRating } = updateRatings(players[winner], players[loser]);
players[winner] = winnerNewRating;
players[loser] = loserNewRating;
function expectedScore(playerRating, opponentRating) {
return 1 / (1 + Math.pow(10, (opponentRating - playerRating) / 400));
}
function updateRatings(winnerRating, loserRating, kFactor = 32) {
const expectedWin = expectedScore(winnerRating, loserRating);
const changeInRating = kFactor * (1 - expectedWin);
return {
winnerNewRating: winnerRating + changeInRating,
loserNewRating: loserRating - changeInRating
};
}
console.log("Alice's new rating:", players["Alice"]);
console.log("Bob's new rating:", players["Bob"]);
console.log("Charlie's rating:", players["Charlie"]);
Output:
Alice's new rating: 1016 Bob's new rating: 984 Charlie's rating: 1000
Comments
Post a Comment