-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame3.js
36 lines (29 loc) · 1.25 KB
/
game3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var attempts = 0; // game counter
var userChoices = []; // array to store user choices
function play(choice) {
userChoices.push(choice); // add user choice to the array
var comp = ["rock", "paper", "scissors"][Math.floor(Math.random() * 3)];
var result = {
rock: {w: "scissors", l: "paper"},
paper: {w: "rock", l: "scissors"},
scissors: {w: "paper", l: "rock"}
};
var message = "Round " + (attempts + 1) + ": You chose " + choice + ". Computer chose " + comp + ". ";
if (choice === comp) {
message += "It's a tie!";
} else if (comp === result[choice].w) {
message += "You win!";
} else {
message += "You lose!";
}
attempts++; // increment game counter
if (attempts >= 5) {
message += " Game over. You played 5 rounds with choices: " + userChoices.join(", ") + ". Refreshing the page to restart the game...";
document.getElementById('msg').innerHTML = message; // display the final message
setTimeout(function() {
location.reload(); // refresh the page after showing choices
}, 5000); // 5 sec delay for refresh
return;
}
document.getElementById('msg').innerHTML = message; // display the message for current round
}