Я немного изменил ваш код. Сейчас вызывается только одна функция, но у нее есть параметр, который соответствует вашему массиву выборов.
let choices = ['Rock', 'Paper', 'Scissor', 'Lizard', 'Spock'];
0 1 2 3 4
Теперь разметка выглядит следующим образом
<div class="main" id="rock" onclick="action(0)">
<i class="fas fa-hand-rock"></i>
<h3> Rock </h3>
</div>
<div class="main" id="paper" onclick="action(1)">
<i class="fas fa-hand-paper"></i>
<h3> Paper </h3>
</div>
<div class="main" id="scissor" onclick="action(2)">
<i class="fas fa-hand-scissors"></i>
<h3> Scissor </h3>
</div>
<div class="main" id="lizard" onclick="action(3)">
<i class="fas fa-hand-lizard"></i>
<h3> Lizard </h3>
</div>
<div class="main" id="spock" onclick="action(4)">
<i class="fas fa-hand-spock"></i>
<h3> Spock </h3>
</div>
Если пользователь выбирает параметр, который мы выполняемигровая логика
const choices = ['Rock', 'Paper', 'Scissor', 'Lizard', 'Spock'];
let wins = 0;
let losses = 0;
//adds scores
document.getElementById('wins').innerHTML = wins;
document.getElementById('losses').innerHTML = losses;
//makes the AI pick Rock Paper or Scissor
function goComputer () {
return Math.floor(Math.random() * choices.length);
}
function action(playerChoice) {
// get computer choice as a number
const computerChoice = goComputer();
// check who won
// draw
if (computerChoice === playerChoice) {
document.getElementById('message').innerHTML = 'Draw';
}
// Rock
if (computerChoice === 0 && (playerChoice === 2 || playerChoice == 3)) {
playerLooses(computerChoice);
} else if (playerChoice === 0 && (computerChoice === 2 || computerChoice == 3)) {
// dry win action
playerWins(playerChoice, computerChoice);
}
// paper
if (computerChoice === 1 && (playerChoice === 0 || playerChoice == 4)) {
// dry loose action
playerLooses(computerChoice);
} else if (playerChoice === 1 && (computerChoice === 0 || computerChoice == 4)){
playerWins(playerChoice, computerChoice);
}
// scissor
if (computerChoice === 2 && (playerChoice === 1 || playerChoice == 3)) {
playerLooses(computerChoice);
} else if (playerChoice === 2 && (computerChoice === 1 || computerChoice == 3)) {
playerWins(playerChoice, computerChoice);
}
// lizzard
if (computerChoice === 3 && (playerChoice === 1 || playerChoice == 4)) {
playerLooses(computerChoice);
} else if (playerChoice === 3 && (computerChoice === 1 || computerChoice == 4)) {
playerWins(playerChoice, computerChoice);
}
//spock
if (computerChoice === 4 && (playerChoice === 2 || playerChoice == 0)) {
playerLooses(computerChoice);
} else if (playerChoice === 4 && (computerChoice === 2 || computerChoice == 0)) {
playerWins(playerChoice, computerChoice);
}
}
function playerLooses(computerChoice) {
losses++;
document.getElementById('losses').innerHTML = losses;
document.getElementById('message').innerHTML = `AI picked ${choices[computerChoice]} you lose`;
}
function playerWins(playerChoice, computerChoice) {
wins++;
document.getElementById(choices[playerChoice].toLowerCase()).setAttribute('class', 'wins');
document.getElementById('wins').innerHTML = wins;
document.getElementById('message').innerHTML = `AI picked ${choices[computerChoice]} you win`;
}
Мы по-прежнему используем массив для получения нужного элемента и вывода текста.