From ce740885c75f1977af022310aeafb8040cff2e8e Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Tue, 11 Apr 2023 13:00:03 +0200 Subject: [PATCH] get random cpu --- game.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/game.js b/game.js index b3e3ff8..89f87aa 100644 --- a/game.js +++ b/game.js @@ -1,15 +1,28 @@ +var cpuList; +var currentCpu; +var nextCpu; + async function main() { - var cpuList; await fetch('./data.json') .then((response) => response.json()) .then((json) => cpuList = json); - + currentCpu = getRandomCpu(); + nextCpu = getRandomCpu(); } function getRandomCpu() { - + let randomIndex = getRandomInt(0, cpuList.length) + return { + name: cpuList[randomIndex]["name"].split('@')[0], + score: cpuList[randomIndex]["cpuScore"] + } } +function getRandomInt(min, max) { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive +} main(); \ No newline at end of file