-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.js
132 lines (110 loc) · 3.33 KB
/
code.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
128
129
130
131
132
// app.js
// Complete logic of game inside this function
const game = () => {
let playerScore = 0;
let computerScore = 0;
let moves = 0;
// Function to
const playGame = () => {
const rockBtn = document.querySelector('.rock');
const paperBtn = document.querySelector('.paper');
const scissorBtn = document.querySelector('.scissor');
const playerOptions = [rockBtn, paperBtn, scissorBtn];
const computerOptions = ['rock', 'paper', 'scissors']
// Function to start playing game
playerOptions.forEach(option => {
option.addEventListener('click', function () {
const movesLeft = document.querySelector('.movesleft');
moves++;
movesLeft.innerText = `Moves Left: ${10 - moves}`;
const choiceNumber = Math.floor(Math.random() * 3);
const computerChoice = computerOptions[choiceNumber];
// Function to check who wins
winner(this.innerText, computerChoice)
// Calling gameOver function after 10 moves
if (moves == 10) {
gameOver(playerOptions, movesLeft);
}
})
})
}
// Function to decide winner
const winner = (player, computer) => {
const result = document.querySelector('.result');
const playerScoreBoard = document.querySelector('.p-count');
const computerScoreBoard = document.querySelector('.c-count');
player = player.toLowerCase();
computer = computer.toLowerCase();
if (player === computer) {
result.textContent = 'Tie'
}
else if (player == 'rock') {
if (computer == 'paper') {
result.textContent = 'Computer Won';
computerScore++;
computerScoreBoard.textContent = computerScore;
} else {
result.textContent = 'Player Won'
playerScore++;
playerScoreBoard.textContent = playerScore;
}
}
else if (player == 'scissors') {
if (computer == 'rock') {
result.textContent = 'Computer Won';
computerScore++;
computerScoreBoard.textContent = computerScore;
} else {
result.textContent = 'Player Won';
playerScore++;
playerScoreBoard.textContent = playerScore;
}
}
else if (player == 'paper') {
if (computer == 'scissors') {
result.textContent = 'Computer Won';
computerScore++;
computerScoreBoard.textContent = computerScore;
} else {
result.textContent = 'Player Won';
playerScore++;
playerScoreBoard.textContent = playerScore;
}
}
}
// Function to run when game is over
const gameOver = (playerOptions, movesLeft) => {
const chooseMove = document.querySelector('.move');
const result = document.querySelector('.result');
const reloadBtn = document.querySelector('.reload');
playerOptions.forEach(option => {
option.style.display = 'none';
})
chooseMove.innerText = 'Game Over!!'
movesLeft.style.display = 'none';
if (playerScore > computerScore) {
result.style.fontSize = '2rem';
result.innerText = 'You Won The Game'
result.style.color = '#308D46';
}
else if (playerScore < computerScore) {
result.style.fontSize = '2rem';
result.innerText = 'You Lost The Game';
result.style.color = 'red';
}
else {
result.style.fontSize = '2rem';
result.innerText = 'Tie';
result.style.color = 'grey'
}
reloadBtn.innerText = 'Restart';
reloadBtn.style.display = 'flex'
reloadBtn.addEventListener('click', () => {
window.location.reload();
})
}
// Calling playGame function inside game
playGame();
}
// Calling the game function
game();