JSFiddle - React, Tailwind, and code Playground
by eurica
JavaScript
function generatePlayers(numPlayers) {
const players = [];
for (let i = 0; i < numPlayers; i++) {
players.push({
id: i,
skill: Math.random(),
elo: 1500,
gamesPlayed: 0,
score: 0
});
}
return players;
}
function updatePlayerElo(player1, player2, whoWon) {
const kFactor = 32;
const player1Expected = 1 / (1 + Math.pow(10, (player2.elo - player1.elo) / 400));
const player2Expected = 1 / (1 + Math.pow(10, (player1.elo - player2.elo) / 400));
let score1, score2;
if (whoWon === 1) {
score1 = 1;
score2 = 0;
} else if (whoWon === 2) {
score1 = 0;
score2 = 1;
}
player1.elo = player1.elo + kFactor * (score1 - player1Expected);
player2.elo = player2.elo + kFactor * (score2 - player2Expected);
}
// Generate 100 random chess players
const chessPlayers = generatePlayers(100);
console.log("Generated 100 players")
const simulateMatch = (playerA, playerB) => {
const outcome = Math.random() < (playerA.skill / (playerA.skill + playerB.skill)) ? 2 : 1;
updatePlayerElo(playerA, playerB, outcome);
};
for (let i = 0; i < chessPlayers.length; i++) {
for (let j = i + 1; j < chessPlayers.length; j++) {
simulateMatch(chessPlayers[i], chessPlayers[j])
// Simulate a game between chessPlayers[i] and chessPlayers[j]
// The result of the game would determine who is the winner
// Call updatePlayerElo() with the appropriate arguments based on the game outcome
}
}
for (let i = 0; i < chessPlayers.length; i++) {
console.log(chessPlayers[i])
}