-
Notifications
You must be signed in to change notification settings - Fork 14
Created Rock,Paper and Scissors game #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yava-odoo
wants to merge
1
commit into
shsa-odoo:main
Choose a base branch
from
yava-odoo:javascript-training-yava
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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"; | ||
|
||
} | ||
}; |
This file contains hidden or 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,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> |
This file contains hidden or 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,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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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;