Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Rock_paper_scissor_game/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
*{
font-family: 'Source Code Pro', monospace;
background-color: rgb(248, 244, 238);
}
.outer_container{
margin: auto;
display: flex;
flex-direction: column;
height: 80%;
width: 60%;
border: 2px solid black;
/* background-color: rgb(236, 228, 228); */
box-shadow: 0px 0px 10px inset ;
border-radius: 20px;
}
.score_container{
border: 2px solid black;
border-radius: 15px;
box-shadow: 0px 0px 5px ;
margin: 3rem;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
.status{
border: 2px solid black;
margin: 3rem;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
border-radius: 15px;
box-shadow: 0px 0px 5px ;
}
.buttons_container{
border: 2px solid black;
border-radius: 15px;
box-shadow: 0px 0px 5px ;
margin: 3rem;
display: flex;
flex-wrap: wrap;
justify-content: space-around;

}
#rock ,#paper, #scissor{
padding: 20px;
border-radius: 25px;
background-color:#16437E;
color: white;
cursor: pointer;
margin: 1rem;
border: 2px solid white;
font-size: 1.3rem;
}
42 changes: 42 additions & 0 deletions Rock_paper_scissor_game/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<link rel="stylesheet" href="index.css">
</head>
<body>
<!-- outer container -->
<div class="outer_container">
<!-- container showing the score -->
<div class="score_container">
<h3>Your Score
<div class="score" id="human"></div>
</h3>
<h3> Bot score
<div class="score" id="bot"></div>
</h3>

</div>
<!-- container showing the status -->
<div class="status">
<h1 id="status_human"></h1>
<h1 id = "status"></h1>
<h1 id="status_bot"></h1>
</div>
<!-- container containing the buttons -->
<div class="buttons_container">
<button class="btn" id = "rock">Rock</button>
<button class="btn" id = "paper">Paper</button>
<button class="btn" id = "scissor">Scissor</button>
</div>
</div>

<script src="index.js"></script>
</body>
</html>
106 changes: 106 additions & 0 deletions Rock_paper_scissor_game/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const rock = document.getElementById("rock")
const paper = document.getElementById("paper")
const scissor = document.getElementById("scissor")
const stat = document.getElementById("status")
stat.innerText = "Start"
const status_human = document.getElementById("status_human")
const status_bot = document.getElementById("status_bot")


Comment on lines +8 to +9
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont push unnecessary spaces. always check the code before updating.

let human_count = 0
let bot_count = 0
const human_score = document.getElementById('human')
human_score.innerText = human_count
const bot_score = document.getElementById('bot')
bot_score.innerText = bot_count

const random_number_generator = ()=>(Math.ceil(Math.random()*3)) //random number generator
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use proper spacings.

const random_number_generator = () => (Math.ceil(Math.random()*3))
update the same in the code.

const evaluator = (id)=>{
const computer_choice = random_number_generator()
console.log(computer_choice)
// For rock
if(id===1){
if(computer_choice == 2){
status_human.innerText ="Rock"
stat.innerText= "You lose"
status_bot.innerText= "Paper"
bot_count++
bot_score.innerText = bot_count
}
else if(computer_choice == 3){
status_human.innerText ="Rock"
stat.innerText = "You won"
status_bot.innerText= "Scissor"
human_count++
human_score.innerText = human_count
}
else{
status_human.innerText ="Rock"
stat.innerText = "Score are tied"
status_bot.innerText= "Rock"
}

stat.remove
}
// For paper
else if(id===2){
if(computer_choice == 1) {
status_human.innerText ="Paper"
stat.innerText= "You won"
status_bot.innerText= "Rock"
human_count++
human_score.innerText = human_count
}

else if(computer_choice == 3){
status_human.innerText ="Paper"
stat.innerText = "You Lose"
status_bot.innerText= "Scissor"
bot_count++
bot_score.innerText = bot_count
}
else{
status_human.innerText ="Paper"
stat.innerText = "Score are tied"
status_bot.innerText= "paper"
}

stat.remove
}
// For Scissors
else{
if(computer_choice == 1) {
status_human.innerText ="Scissor"
stat.innerText= "You Lose"
status_bot.innerText= "Rock"
bot_count++
bot_score.innerText = bot_count
}

else if(computer_choice == 2){
status_human.innerText ="Scissor"
stat.innerText = "You Won"
status_bot.innerText= "Paper"
human_count++
human_score.innerText = human_count
}
else{
status_human.innerText ="Scissor"
stat.innerText = "Score are tied"
status_bot.innerText= "Scissor"
}

stat.remove
}
Comment on lines +22 to +94
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optimize this code using ternary operator, avoid using multiple if..else and repeating statements,
what i can see is there are only three cases when user can win:
you_win = ( (you === 0 ) && (opponent === 2) ||
(you === 1 ) && (opponent === 0) ||
(you === 2) && (opponent === 1) ) // returns true for your win cases only.
.
.
so (you === opponent) ? "draw message" : you_win ? call a method for updating score and message for user : call method for updating score and message fro computer;

}
rock.addEventListener('click',()=>{
evaluator(1)
})
paper.addEventListener('click',()=>{
evaluator(2)

})
scissor.addEventListener('click',()=>{
evaluator(3)
})