-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.js
127 lines (111 loc) Β· 4.43 KB
/
play.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//Gobal Variables
const rock = document.getElementById("rock_btn");
const paper = document.getElementById("paper_btn");
const scissor = document.getElementById("scissor_btn");
const restart_btn = document.getElementById("restart_btn");
restart_btn.style.display = "none";
let playerSelection = "";
let gplayerCount = 0;
let gcomputerCount = 0;
roundNote.textContent = "Choose an option!";
countUser.textContent = `No points gained! | ${gplayerCount} | π¨βπ»`;
countComputer.textContent = `π₯ | ${gcomputerCount} | No points gained!`;
//function that generates a random number between 0 and 2 and convert that number in Rock, paper or scissor.
function computerSelection() {
const randomNumber = Math.floor(Math.random() * 3);
// Rock = 0 | Paper = 1 | Scissor = 2
if (randomNumber == 0) {
return "rock";
} else if (randomNumber == 1) {
return "paper";
} else if (randomNumber == 2) {
return "scissor";
}
}
function playRound(playerSelection) {
const roundNote = document.getElementById("roundNote");
let computerPlay = computerSelection();
if (playerSelection == "rock" && computerPlay == "scissor") {
roundNote.textContent = "Player wins, rock beats scissor";
return "Player";
} else if (playerSelection == "scissor" && computerPlay == "rock") {
roundNote.textContent = "Computer wins, rock beats scissor";
return "Computer";
} else if (playerSelection == "scissor" && computerPlay == "paper") {
roundNote.textContent = "Player wins, scissor beats paper";
return "Player";
} else if (playerSelection == "paper" && computerPlay == "scissor") {
roundNote.textContent = "Computer wins, scissor beats paper";
return "Computer";
} else if (playerSelection == "paper" && computerPlay == "rock") {
roundNote.textContent = "Player wins, paper beats rock";
return "Player";
} else if (playerSelection == "rock" && computerPlay == "paper") {
roundNote.textContent = "Computer wins, paper beats rock";
return "Computer";
} else if (playerSelection == "rock" && computerPlay == "rock") {
roundNote.textContent = "Tie, rock is equal to rock";
return "Tie";
} else if (playerSelection == "paper" && computerPlay == "paper") {
roundNote.textContent = "Tie, paper is equal to paper";
return "Tie";
} else if (playerSelection == "scissor" && computerPlay == "scissor") {
roundNote.textContent = "Tie, scissor is equal to scissor";
return "Tie";
} else {
roundNote.textContent = "oh no, a dinosaur wins this game";
return "error";
}
}
function countPoints(playerCount, computerCount) {
const countUser = document.getElementById("countUser");
const countComputer = document.getElementById("countComputer");
while (playerCount < 5 && computerCount < 5) {
let playRoundVar = playRound(playerSelection);
if (playRoundVar.includes("Player")) {
playerCount++;
countUser.textContent = `Point for player! | ${playerCount} | π¨βπ»`;
countComputer.textContent = `π₯ | ${computerCount} | No points gained!`;
} else if (playRoundVar.includes("Computer")) {
computerCount++;
countComputer.textContent = `π₯ | ${computerCount} | Point for computer!`;
countUser.textContent = `No points gained! | ${playerCount} | π¨βπ»`;
}
if (playerCount === 5) {
roundNote.textContent = "The Player has won the Game!";
restart_btn.style.display = "";
} else if (computerCount === 5) {
roundNote.textContent = "The Computer has won the Game!";
restart_btn.style.display = "";
}
gplayerCount = playerCount;
gcomputerCount = computerCount;
break;
}
}
function removeButton() {
const divRemove = document.getElementById("restart");
divRemove.removeChild(createButton);
}
function restart() {
playerSelection = "";
gplayerCount = 0;
gcomputerCount = 0;
roundNote.textContent = "Choose an option!";
countUser.textContent = `No points gained! | ${gplayerCount} | π¨βπ»`;
countComputer.textContent = `π₯ | ${gcomputerCount} | No points gained!`;
restart_btn.style.display = "none";
}
rock.addEventListener("click", () => {
playerSelection = "rock";
countPoints(gplayerCount, gcomputerCount);
});
paper.addEventListener("click", () => {
playerSelection = "paper";
countPoints(gplayerCount, gcomputerCount);
});
scissor.addEventListener("click", () => {
playerSelection = "scissor";
countPoints(gplayerCount, gcomputerCount);
});
restart_btn.addEventListener("click", restart);