-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
87 lines (72 loc) · 1.92 KB
/
game.js
File metadata and controls
87 lines (72 loc) · 1.92 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
let myCount = 0;
let computerCount = 0;
// Game Rule
function comparingBoth() {
if (playerChoice === compData) {
return "Its Tie";
}
if (
(playerChoice === "snake" && compData === "water") ||
(playerChoice === "water" && compData === "gun") ||
(playerChoice === "gun" && compData === "snake")
) {
myCount += 1;
updateScore();
return "You Win!";
} else {
computerCount += 1;
updateScore();
return "Computer Wins!";
}
}
// Computer Logic
let computerSelector = ["snake", "water", "gun"];
let computerSelected = () => {
let compScore = Math.floor(Math.random() * 3);
console.log(computerSelector[compScore]);
return computerSelector[compScore];
};
// let compData = computerSelected();
// My Self Playing
let snake = document.getElementById("snake");
let water = document.getElementById("water");
let gun = document.getElementById("gun");
let compData = "";
let playerChoice = "";
snake.addEventListener("click", () => {
playerChoice = "snake";
console.log("snake was clicked");
playGame();
});
water.addEventListener("click", () => {
playerChoice = "water";
console.log("water was clicked");
playGame();
});
gun.addEventListener("click", () => {
playerChoice = "gun";
console.log("gun was clicked");
playGame();
});
function playGame() {
compData = computerSelected();
console.log(`Player chose: ${playerChoice}, Computer chose: ${compData}`);
let result = comparingBoth();
console.log(result);
if (myCount >= 5) {
alert("You Won🫡🫡");
updateScore();
} else if (computerCount >= 5) {
alert("Computer Won🥲");
updateScore();
}
}
function updateScore() {
let CScore = (document.getElementById("ComputerCount").textContent =
computerCount);
let PScore = (document.getElementById("MyCount").textContent = myCount);
}
let restart = document.getElementById("Restart");
restart.addEventListener("click", () => {
location.reload();
});