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
54 changes: 54 additions & 0 deletions RPS/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const rock_button = document.querySelector(".rock");
const paper_button = document.querySelector(".paper");
const scissors_button = document.querySelector(".scissor");
const result_text = document.getElementById("result_text");
const user_win_count = document.getElementById("p-count");
const computer_win_count = document.getElementById("c-count");
let computer_score = 0;
let player_score = 0;

rock_button.addEventListener("click", function () {
start_game("rock");

});
paper_button.addEventListener("click", function () {
start_game("paper");

});
scissors_button.addEventListener("click", function () {
start_game("scissors");

});

computer_selection = () => {
computer_options = ["rock", "paper", "scissors"];
computer_number = Math.floor(Math.random() * 3);
computer_choice = computer_options[computer_number];
return computer_choice;
};

start_game = (user_choice) => {
const comp_choice = computer_selection();

if (user_choice == comp_choice) {
result_text.textContent = "It's Draw";
} else if (
(user_choice == "rock") & (comp_choice == "scissors") ||
(user_choice == "paper") & (comp_choice == "rock") ||
(user_choice == "scissors") & (comp_choice == "paper")
) {
player_score++;
user_win_count.innerText = player_score;
result_text.textContent = "You Win";

} else if (
(user_choice == "rock") & (comp_choice == "paper") ||
(user_choice == "paper") & (comp_choice == "scissors") ||
(user_choice == "scissors") & (comp_choice == "rock")
) {
computer_score++;
computer_win_count.innerText = computer_score;
result_text.textContent = "You Lose";

}
Comment on lines +33 to +53
Copy link
Owner

Choose a reason for hiding this comment

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

update this code using ternary operator, avoid using repeating statements,
what i can see is there are only three cases when user can win:
you_win = ( (you === "rock") && (opponent === "scissor") ||
(you === "paper") && (opponent === "rock") ||
(you === "scissor") && (opponent === "rock") ) // 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;

};
45 changes: 45 additions & 0 deletions RPS/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!-- index.html -->

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css">
<title>Rock Paper Scissor</title>
</head>
<body>
<section class="game">
<div class="title">Rock Paper Scissor</div>
<div class="score">
<div class="playerScore">
<h2>Player</h2>
<p id="p-count">0</p>
</div>
<div class="computerScore">
<h2>Computer</h2>
<p id="c-count">0</p>
</div>
</div>

<div class="result">
<h2 id="result_text">hello</h2>
</div>

<div class="move">Choose your move</div>
<!-- <div class="movesleft">Moves Left: 10</div> -->

<div class="options">
<button class="rock far fa-hand-rock"></button>
<button class="paper far fa-hand-paper"></button>
<button class="scissor far fa-hand-scissors"></button>
</div>


<!-- <button class="reload"></button> -->
</section>

<script src="app.js"></script>
</body>
</html>
57 changes: 57 additions & 0 deletions RPS/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.game{
display: flex;
flex-direction: column;
height: 100vh;
width: 100vw;
justify-content: center;
align-items: center;
}

.title{
position: absolute;
top: 0;
font-size: 4rem;
z-index: 2;
}

.score{
display: flex;
width: 30vw;
justify-content: space-evenly;
position: absolute;
top: 70px;
z-index: 1;
}

.p-count,.c-count{
text-align: center;
font-size: 1.5rem;
margin-top: 1rem;
}

.options{
display: flex;
width: 50vw;
justify-content: space-evenly;
margin-top: 2rem;
}

.rock, .paper, .scissor{
padding: 0.8rem;
outline: none;
border: none;
cursor: pointer;
height: 100px;
font-size: 50px;
width: 100px;
border-radius: 50%;
}

.result{
font-size: 1.2rem;
}