-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrps.js
156 lines (145 loc) · 4.46 KB
/
rps.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// Javascript: Rock, Paper, Scissors Game
//
// Original Code from:
// https://codepen.io/bradtraversy/pen/wLgPzr
//
// Code adopted for standalone by:
// Greg Jewett, http://geocaching.ejewett.com/
//
// This script allows a user to interact with three symbols (rock, paper, scissors)
// displayed on the screen as their choice in the standard Rock, Paper, Scissors
// Game (RPS) battle.
//
// ---------------------------------------------------------------------------------------
// CHANGE LOG
// 2024-01-26 (GSJ) Initial version
// Adapted original code to display final coordinates on 10 wins.
// 2024-01-26 (GSJ) Added a "losing" scenario, where if the computer reaches 10 wins,
// the game is over, and the match must restart.
// ---------------------------------------------------------------------------------------
//
// =======================================================================================
// BEGIN Script
// =======================================================================================
const choices = document.querySelectorAll('.choice');
const score = document.getElementById('score');
const result = document.getElementById('result');
const restart = document.getElementById('restart');
const modal = document.querySelector('.modal');
const finalcoord = document.getElementById('finalcoord');
const scoreboard = {
player: 0,
computer: 0
};
window.sessionStorage.clear();
window.sessionStorage.setItem("cheatCheck", false)
// Play game
function play(e) {
restart.style.display = 'inline-block';
const playerChoice = e.target.id;
const computerChoice = getComputerChoice();
const winner = getWinner(playerChoice, computerChoice);
showWinner(winner, computerChoice);
}
// Get computers choice
function getComputerChoice() {
const rand = Math.random();
if (rand < 0.34) {
return 'rock';
} else if (rand <= 0.67) {
return 'paper';
} else {
return 'scissors';
}
}
// Get game winner
function getWinner(p, c) {
if (p === c) {
return 'draw';
} else if (p === 'rock') {
if (c === 'paper') {
return 'computer';
} else {
return 'player';
}
} else if (p === 'paper') {
if (c === 'scissors') {
return 'computer';
} else {
return 'player';
}
} else if (p === 'scissors') {
if (c === 'rock') {
return 'computer';
} else {
return 'player';
}
}
}
function showWinner(winner, computerChoice) {
if (winner === 'player') {
// Inc player score
scoreboard.player++;
// Show modal result
result.innerHTML = `
<h1 class="text-win">You Win</h1>
<i class="fas fa-hand-${computerChoice} fa-10x"></i>
<p>Computer Chose <strong>${computerChoice.charAt(0).toUpperCase() +
computerChoice.slice(1)}</strong></p>
`;
} else if (winner === 'computer') {
// Inc computer score
scoreboard.computer++;
// Show modal result
result.innerHTML = `
<h1 class="text-lose">You Lose</h1>
<i class="fas fa-hand-${computerChoice} fa-10x"></i>
<p>Computer Chose <strong>${computerChoice.charAt(0).toUpperCase() +
computerChoice.slice(1)}</strong></p>
`;
} else {
result.innerHTML = `
<h1>It's A Draw</h1>
<i class="fas fa-hand-${computerChoice} fa-10x"></i>
<p>Computer Chose <strong>${computerChoice.charAt(0).toUpperCase() +
computerChoice.slice(1)}</strong></p>
`;
}
// Show score
score.innerHTML = `
<p>Player: ${scoreboard.player}</p>
<p>Computer: ${scoreboard.computer}</p>
`;
if (scoreboard.player > 9) {
window.sessionStorage.setItem("cheatCheck", true)
window.location.replace("win.html");
}
if (scoreboard.computer > 9) {
window.location.replace("lose.html");
}
modal.style.display = 'block';
}
// Restart game
function restartGame() {
scoreboard.player = 0;
scoreboard.computer = 0;
score.innerHTML = `
<p>Player: 0</p>
<p>Computer: 0</p>
`;
finalcoord.style.display = "none";
}
// Clear modal
function clearModal(e) {
if (e.target == modal) {
modal.style.display = 'none';
}
}
// Event listeners
choices.forEach(choice => choice.addEventListener('click', play));
window.addEventListener('click', clearModal);
restart.addEventListener('click', restartGame);
// =======================================================================================
// END Script
// =======================================================================================