Beside your typo idea=
/ id=
...
you could drastically minify your game logic and code by using indexes integers.
Game of integers
- Use
data-*
attribute for user buttons. The attributes should hold numerical values 0, 1, 2
. On click, the value will represents the player choice.
- The AI should play numbers too:
const AI = ~~(Math.random() * 3) // 0, 1, 2
Now that you know AI and Player both use integers (instead of strange letters combinations), you can store the Move names into an array const moves = ["Rock", "Paper", "Scissors"];
(where 0
is Rock... etc)
Rock Paper Scissors Logic
The game has three possible round resolutions, PL wins, AI wins, Draw.
Let's convert those "human" values to integers, in the same order:
0
= PL win
1
= AI win
2
= Draw
Here's how to calculate those:
Draw
To calculate a Draw is the simplest. It's when both AI
and PL
integers are equal. Let's return 2
result = PL === AI ? 2
Player wins
To calculate Player win, simply increment AI choice by 1 and do a modulo 3. If the result of this operation is equal to player's choice, than Player must have won! Let's return 0
AI wins
Else, since our game has only 3 possible states, it's not a draw, and it's not a player win, than must be AI win! And let's return 1
const result = PL===AI ? 2 : (AI+1)%3 === PL? 0 : 1; // Possible results: 0, 1, 2
The cool thing in having a game result index based too is that now you can use also an array of messages like messages = ["You won!", "AI won", "it's a draw!", ]
and get the desired message by the result index!. And bonus! You can also increment the score
array values, 0
being the player's index and 1
being AIs!
const moves = ["Rock", "Paper", "Scissors"],
messages = ["You won!", "AI won", "It's a draw!"], // [PL, AI, draw]
score = [0, 0, 0], // [PL, AI, draw]
EL = sel => document.querySelector(sel),
EL_result = EL("#result"),
EL_PLScore = EL("#PLScore"),
EL_AIScore = EL("#AIScore");
function game() {
const PL = +this.dataset.playermove; // Get dataset value as integer
const AI = ~~(Math.random() * 3); // All you need: 0, 1, 2
const result = PL === AI ? 2 : (AI + 1) % 3 === PL ? 0 : 1; // 0=PLwins 1=AIwins 2=draw
score[result]++; // Increment PL or AI's score (Increments number of draws too ;) )
EL_result.innerHTML = `You: ${moves[PL]}<br>AI: ${moves[AI]}<br>${messages[result]}`;
EL_PLScore.textContent = score[0];
EL_AIScore.textContent = score[1];
}
// EVENTS:
document.querySelectorAll("[data-playermove]")
.forEach(el => el.addEventListener("click", game));
<button data-playermove="0">ROCK</button>
<button data-playermove="1">PAPER</button>
<button data-playermove="2">SCISSORS</button>
<br>
YOU | <span id="PLScore">0</span>:<span id="AIScore">0</span> | AI
<div id="result"></div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…