Skip to content

Commit 5f49091

Browse files
added RockPaperScissor js game
1 parent b591985 commit 5f49091

File tree

5 files changed

+217
-0
lines changed

5 files changed

+217
-0
lines changed

Rock-Paper-Scissors/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The Rock Paper Scissors Game
2+
3+
This is a simple version of rock paper scissor game.
4+
Feel free to make it better in any way possible!
5+
6+
![Rock Paper Scissors](rps.png "Rock Paper Scissors")

Rock-Paper-Scissors/index.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width">
7+
<title>replit</title>
8+
<link href="style.css" rel="stylesheet" type="text/css" />
9+
</head>
10+
11+
<body>
12+
<div class="wrapper">
13+
<div class="buttons">
14+
<button class="rpsButton" value="Rock"></button>
15+
<button class="rpsButton" value="Paper">🤚</button>
16+
<button class="rpsButton" value="Scissors">✌️</button>
17+
</div>
18+
<div class="resultContainer">
19+
<div id="player-score"></div>
20+
<div id="hands"></div>
21+
<div id="result"></div>
22+
<button id='endGameButton'>🔴</button>
23+
</div>
24+
</div>
25+
26+
<script src="script.js"></script>
27+
</body>
28+
29+
</html>

Rock-Paper-Scissors/rps.png

32.9 KB
Loading

Rock-Paper-Scissors/script.js

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
Rock Paper Scissors 🚀🔥
3+
Concepts covered in this project
4+
👉 For loops
5+
👉 Dom Manipulation
6+
👉 Variables
7+
👉 Conditionals (if else if)
8+
👉 Template Literals
9+
👉 Event Listeners
10+
👉 Higher order Function (Math.random())
11+
*/
12+
13+
const totalScore = {computerScore : 0, playerScore : 0}
14+
15+
// ** getComputerChoice randomly selects between `rock` `paper` `scissors` and returns that string **
16+
// getComputerChoice() 👉 'Rock'
17+
// getComputerChoice() 👉 'Scissors'
18+
function getComputerChoice() {
19+
const rpsChoice = ["Rock", "Paper", "Scissors"]
20+
const randomNumber = Math.floor(Math.random() * 3)
21+
return rpsChoice[randomNumber];
22+
// console.log(rpsChoice[randomNumber])
23+
24+
}
25+
26+
// ** getResult compares playerChoice & computerChoice and returns the score accordingly **
27+
// human wins - getResult('Rock', 'Scissors') 👉 1
28+
// human loses - getResult('Scissors', 'Rock') 👉 -1
29+
// human draws - getResult('Rock', 'Rock') 👉 0
30+
function getResult(playerChoice, computerChoice) {
31+
// return the result of score based on if you won, drew, or lost
32+
let score;
33+
34+
35+
// All situations where human draws, set `score` to 0
36+
if (playerChoice == computerChoice) {
37+
score = 0;
38+
}
39+
else if(playerChoice=="Paper" && computerChoice=="Rock"){
40+
score = 1;
41+
}
42+
else if(playerChoice=="Scissors"&& computerChoice=="Paper"){
43+
score = 1;
44+
}
45+
else if(playerChoice=="Rock" && computerChoice=="Scissors"){
46+
score = 1;
47+
}
48+
else{
49+
score = -1;
50+
}
51+
52+
53+
54+
// All situations where human wins, set `score` to 1
55+
// make sure to use else ifs here
56+
57+
58+
// Otherwise human loses (aka set score to -1)
59+
60+
61+
// return score
62+
return score
63+
}
64+
65+
// ** showResult updates the DOM to `You Win!` or `You Lose!` or `It's a Draw!` based on the score. Also shows Player Choice vs. Computer Choice**
66+
function showResult(score, playerChoice, computerChoice) {
67+
// Hint: on a score of -1
68+
// You should do result.innerText = 'You Lose!'
69+
// Don't forget to grab the div with the 'result' id!
70+
const resultDiv = document.getElementById('result')
71+
const handsDiv = document.getElementById('hands')
72+
const playerScoreDiv = document.getElementById('player-score')
73+
74+
75+
if(score == -1){
76+
resultDiv.innerText = "You Lose"
77+
78+
}
79+
else if(score ==0){
80+
resultDiv.innerText = "That a Tie"
81+
82+
83+
}
84+
else{
85+
resultDiv.innerText= "You Win"
86+
87+
88+
}
89+
90+
handsDiv.innerText= `${playerChoice} VS ${computerChoice} `
91+
92+
playerScoreDiv.innerText = `Your Score ${totalScore.playerScore}`
93+
94+
95+
}
96+
97+
// ** Calculate who won and show it on the screen **
98+
function onClickRPS(playerChoice) {
99+
console.log(playerChoice)
100+
const computerChoice = getComputerChoice()
101+
console.log(computerChoice)
102+
const score = getResult(playerChoice,computerChoice)
103+
totalScore.playerScore += score
104+
totalScore.computerScore == score
105+
106+
console.log({score})
107+
console.log(totalScore)
108+
showResult(score,playerChoice,computerChoice)
109+
}
110+
111+
112+
// ** Make the RPS buttons actively listen for a click and do something once a click is detected **
113+
function playGame() {
114+
// use querySelector to select all RPS Buttons
115+
const rpsButtons = document.querySelectorAll('.rpsButton')
116+
console.log(rpsButtons)
117+
118+
rpsButtons.forEach(rpsButtons => {
119+
rpsButtons.onclick =() => onClickRPS(rpsButtons.value)
120+
})
121+
122+
123+
124+
// Add a click listener to the end game button that runs the endGame() function on click
125+
const endGameButton = document.getElementById("endGameButton")
126+
endGameButton.onclick = () => endGame(totalScore)
127+
}
128+
129+
// ** endGame function clears all the text on the DOM **
130+
function endGame(totalScore) {
131+
totalScore.playerScore = 0
132+
totalScore.computerScore = 0
133+
134+
const resultDiv = document.getElementById('result')
135+
const handsDiv = document.getElementById('hands')
136+
const playerScoreDiv = document.getElementById('player-score')
137+
138+
playerScoreDiv.innerText = ''
139+
handsDiv.innerText =''
140+
resultDiv.innerText=''
141+
142+
}
143+
144+
playGame()

Rock-Paper-Scissors/style.css

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
html, body {
2+
height: 100%;
3+
width: 100%;
4+
padding: 0;
5+
margin:0;
6+
}
7+
8+
.wrapper {
9+
background: #1c1c1c;
10+
color: white;
11+
display: flex;
12+
align-items: center;
13+
justify-content: center;
14+
width: 100%;
15+
height: 100%;
16+
flex-direction: column;
17+
}
18+
19+
.buttons {
20+
display: flex;
21+
gap: 20px;
22+
}
23+
24+
button {
25+
height: 100px;
26+
width: 100px;
27+
font-size: 48px;
28+
border-radius: 30px;
29+
cursor: pointer;
30+
31+
}
32+
33+
.resultContainer {
34+
font-size: 2rem;
35+
text-align: center;
36+
margin-top: 20px;
37+
}
38+

0 commit comments

Comments
 (0)