Last active
October 13, 2019 22:18
-
-
Save mikeyakymenko/85c0928132aca7a23d39628e30a0fb9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const probs = { | |
0: { | |
0: 0.7, | |
1: 0.14, | |
2: 0.1, | |
3: 0.03, | |
4: 0.02, | |
5: 0.01 | |
}, | |
1: { | |
0: 0.68, | |
1: 0.14, | |
2: 0.11, | |
3: 0.04, | |
4: 0.02, | |
5: 0.01 | |
}, | |
2: { | |
0: 0.64, | |
1: 0.15, | |
2: 0.12, | |
3: 0.05, | |
4: 0.03, | |
5: 0.01 | |
}, | |
3: { | |
0: 0.6, | |
1: 0.15, | |
2: 0.12, | |
3: 0.06, | |
4: 0.04, | |
5: 0.02 | |
} | |
} | |
const makeAttempt = (spec) => { | |
let sum = 0 | |
for (let prop in spec) { | |
sum += spec[prop] | |
if (Math.random() <= sum) { | |
return prop | |
} | |
} | |
} | |
const getTeamYards = (teamPrestige, score, fullYards) => { | |
const teamRound = makeAttempt(probs[teamPrestige]) | |
return { | |
fullYArds: teamRound == 5 ? fullYards++ : fullYards, | |
yards: score + parseInt(teamRound * 10) | |
} | |
} | |
const game = (teamA, teamB) => { | |
let yardsTeamA = 0 | |
let yardsTeamB = 0 | |
let fullYardsTeamA = 0 | |
let fullYardsTeamB = 0 | |
for (let i=0; i < 11; i++) { | |
const teamAGetTeamYards = getTeamYards(teamA.prestige, yardsTeamA, fullYardsTeamA) | |
const teamBGetTeamYards = getTeamYards(teamB.prestige, yardsTeamB, fullYardsTeamA) | |
yardsTeamA = teamAGetTeamYards.yards | |
fullYardsTeamA = teamAGetTeamYards.fullYArds | |
yardsTeamB = teamBGetTeamYards.yards | |
fullYardsTeamB = teamBGetTeamYards.fullYArds | |
} | |
const finalScoreTeamA = yardsTeamA + (fullYardsTeamA * 10) | |
const finalScoreTeamB = yardsTeamB + (fullYardsTeamB * 10) | |
return { | |
teamA: finalScoreTeamA, | |
teamB: finalScoreTeamB, | |
displayResult: `${teamA.name} ${finalScoreTeamA} - ${finalScoreTeamB} ${teamB.name}` | |
} | |
} | |
console.log(game({name: 'Madrid', prestige: 0}, {name: 'Dnipro', prestige: 1})) | |
console.log(game({name: 'New York', prestige: 0}, {name: 'Bangkok', prestige: 0})) | |
console.log(game({name: 'Valencia', prestige: 0}, {name: 'London', prestige: 0})) | |
console.log(game({name: 'Vancover', prestige: 0}, {name: 'Krakow', prestige: 0})) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment