Skip to content

Commit bd4e649

Browse files
Merge pull request #26 from Prajwal0225/tip-calculator
Adding Tip Calculator
2 parents d7ece80 + efb0336 commit bd4e649

4 files changed

Lines changed: 198 additions & 0 deletions

File tree

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

Rock-Paper-Scissors-Game/script.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
const scores = {'computerScore':0 , 'playerScore':0}
3+
4+
function getComputerChoice() {
5+
let options = ['Rock','Paper','Scissors']
6+
let n = Math.floor(Math.random()*3)
7+
8+
return options[n]
9+
}
10+
11+
12+
function getResult(playerChoice, computerChoice) {
13+
// return the result of score based on if you won, drew, or lost
14+
var score=0;
15+
// All situations where human draws, set `score` to 0
16+
if(playerChoice === computerChoice){
17+
score = 0;
18+
}else if(playerChoice==='Rock' && computerChoice==='Scissors'){
19+
score = 1
20+
}else if(playerChoice==='Paper' && computerChoice==='Rock'){
21+
score = 1
22+
}else if(playerChoice==='Scissors' && computerChoice==='Paper'){
23+
score = 1
24+
25+
}else{
26+
score = -1
27+
}
28+
29+
return score
30+
}
31+
32+
// ** 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**
33+
function showResult(score, playerChoice, computerChoice) {
34+
35+
const resultDiv = document.getElementById('result')
36+
const handsDiv = document.getElementById('hands')
37+
const playerScoreDiv = document.getElementById('player-score')
38+
39+
if(score == -1){
40+
resultDiv.innerText = 'You Lose!'
41+
}else if(score == 1){
42+
resultDiv.innerText = 'You Win!'
43+
}else{
44+
resultDiv.innerText = 'Draw!'
45+
}
46+
47+
handsDiv.innerText=`🧑‍🦰 ${playerChoice} vs 🤖 ${computerChoice}`
48+
playerScoreDiv.innerText =` Your Score: ${scores['playerScore']}`
49+
}
50+
51+
// ** Calculate who won and show it on the screen **
52+
function onClickRPS(playerChoice) {
53+
const computerChoice = getComputerChoice()
54+
const score = getResult(playerChoice.value,computerChoice)
55+
scores['playerScore'] += score
56+
showResult(score, playerChoice.value,computerChoice)
57+
}
58+
59+
60+
// ** Make the RPS buttons actively listen for a click and do something once a click is detected **
61+
function playGame() {
62+
// use querySelector to select all RPS Buttons
63+
const selection = document.querySelectorAll('.rpsButton')
64+
65+
selection.forEach(clickks =>{
66+
clickks.onclick = () => onClickRPS(clickks)
67+
})
68+
69+
70+
// Add a click listener to the end game button that runs the endGame() function on click
71+
let endGameButton = document.getElementById('endGameButton')
72+
endGameButton.onclick = () => endGame()
73+
}
74+
75+
// ** endGame function clears all the text on the DOM **
76+
function endGame() {
77+
let playerScore = document.getElementById('player-score')
78+
let hands = document.getElementById('hands')
79+
let result = document.getElementById('result')
80+
playerScore.innerText = ''
81+
hands.innerText = ''
82+
result.innerText = ''
83+
}
84+
85+
// ** endGame function clears all the text on the DOM **
86+
playGame()

Rock-Paper-Scissors-Game/style.css

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
39+
html, body {
40+
height: 100%;
41+
width: 100%;
42+
padding: 0;
43+
margin:0;
44+
}
45+
46+
.wrapper {
47+
background: #1c1c1c;
48+
color: white;
49+
display: flex;
50+
align-items: center;
51+
justify-content: center;
52+
width: 100%;
53+
height: 100%;
54+
flex-direction: column;
55+
}
56+
57+
.buttons {
58+
display: flex;
59+
gap: 20px;
60+
}
61+
62+
button {
63+
height: 100px;
64+
width: 100px;
65+
font-size: 48px;
66+
border-radius: 30px;
67+
cursor: pointer;
68+
69+
}
70+
71+
.resultContainer {
72+
font-size: 2rem;
73+
text-align: center;
74+
margin-top: 20px;
75+
}

0 commit comments

Comments
 (0)