-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stone paper scissors game
- Loading branch information
0 parents
commit 76c6432
Showing
6 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
let userScore = 0; | ||
let compScore = 0; | ||
|
||
const choices = document.querySelectorAll(".choice"); | ||
const msg = document.querySelector("#msg") | ||
const userScorepara = document.querySelector("#user-score") | ||
const compScorepara = document.querySelector("#comp-score") | ||
const genCompChoice = ()=> { | ||
const options = ["rock", "paper", "scissors" ] | ||
const ranIdx = Math.floor(Math.random()*3); | ||
return options[ranIdx]; | ||
} | ||
const drawGame = ()=> { | ||
console.log("game was draw") | ||
msg.innerText = "Game was draw. play again."; | ||
msg.style.background = "grey" | ||
} | ||
const showWinner = (userWin)=> { | ||
if (userWin){ | ||
userScore++; | ||
userScorepara.innerText = userScore; | ||
console.log("user won"); | ||
msg.innerText = "you win!"; | ||
msg.style.background = "green" | ||
} | ||
else{ | ||
compScore++; | ||
compScorepara.innerText = compScore; | ||
console.log("computer won"); | ||
msg.innerText = "computer win"; | ||
msg.style.background = "red" | ||
} | ||
} | ||
|
||
const playGame =(userChoice) => { | ||
console.log("user choice = ",userChoice); | ||
const compChoice = genCompChoice(); | ||
console.log("comp choice =",compChoice) | ||
if (userChoice === compChoice) { | ||
drawGame(); | ||
} | ||
else { | ||
let userWin = true; | ||
if (userChoice === "rock"){ | ||
//comChoice = scissor,paper | ||
userWin = compChoice ==="paper" ? false:true ; | ||
} | ||
else if(userChoice ==="paper"){ | ||
//comChoice = rock,paper | ||
userWin = compChoice === "scissors" ? false: true; | ||
}else { | ||
userWin= compChoice === "rock" ? false: true; | ||
|
||
} | ||
showWinner(userWin); | ||
|
||
} | ||
} | ||
|
||
|
||
|
||
|
||
choices.forEach((choice)=>{choice.addEventListener("click", ()=>{ | ||
const userChoice = choice.getAttribute("id") | ||
playGame(userChoice); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width= | ||
, initial-scale=1.0"> | ||
<title>Rock Paper Scissors game</title> | ||
<link rel="stylesheet" href="style.css"> | ||
</head> | ||
<body> | ||
<h1>Rock Paper Scissors game</h1> | ||
<div class="choices"> | ||
<div class="choice" id="rock"> | ||
<img src="rock.png" alt="rock"> | ||
</div> | ||
<div class="choice" id="paper"> | ||
<img src="paper.png" alt="paper"> | ||
</div> | ||
<div class="choice" id="scissors" > | ||
<img src="scissors.png" alt="scissors"> | ||
</div> | ||
</div> | ||
|
||
<div class="scoreboard"> | ||
<div class="score"> | ||
<p id="user-score">0</p> | ||
<p>you</p> | ||
</div> | ||
<div class="score"> | ||
<p id="comp-score"> 0</p> | ||
<p>comp</p> | ||
</div> | ||
</div> | ||
|
||
<div class="msg-container"> | ||
<div id="msg">play your move</div> | ||
</div> | ||
|
||
|
||
<script src="app.js">javacsript</script> | ||
</body> | ||
|
||
</html> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
*{ | ||
margin: 0; | ||
padding: 0; | ||
text-align:center; | ||
} | ||
h1 { | ||
background-color: #081b31; | ||
color: #fff; | ||
height:5rem; | ||
line-height: 5rem; | ||
} | ||
.choice{ | ||
height: 165px; | ||
width: 165px; | ||
border-radius: 50%; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
|
||
} | ||
.choice:hover { | ||
background-color: #081b31; | ||
opacity: 0.5; | ||
cursor: pointer; | ||
} | ||
img { | ||
height: 150px; | ||
width: 150px; | ||
object-fit:cover ; | ||
border-radius: 50%; | ||
} | ||
.choices { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
gap: 3rem; | ||
margin-top: 5rem; | ||
} | ||
.scoreboard { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
font-size: 2rem; | ||
margin-top: 3rem; | ||
gap: 5rem; | ||
} | ||
#user-score,#comp-score { | ||
font-size: 4rem; | ||
} | ||
.msg-container { | ||
margin-top: 2rem; | ||
} | ||
#msg { | ||
background-color:#081b31; | ||
color: #fff; | ||
font-size: 2rem; | ||
display: inline; | ||
padding: 1rem; | ||
border-radius: 1rem; | ||
} | ||
|
||
|