Skip to content
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

[ADD]: Rock Paper Scissor Game Code #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
Binary file added rock-paper-scissor-game/images/fist.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rock-paper-scissor-game/images/hello.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rock-paper-scissor-game/images/peace.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions rock-paper-scissor-game/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!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>Rock Paper Scissor</title>
<link rel="stylesheet" href="./styles.css"/>

</head>
<body>
<h1>ROCK PAPER SCISSOR GAMEPLAY</h1>
<p id="user_score">YOUR SCORE : <span id="user_score_display">0</span></p>
<p id="comp_score">COMPUTER SCORE : <span id="comp_score_display">0</span></p>
<p id="result"></p>
<button type="button" id="rock" onclick="check('rock')"><img src="./images/fist.png"/></button>
<button type="button" id="paper" onclick="check('paper')"><img src="./images/hello.png"/></button>
<button type="button" id="scissor" onclick="check('scissor')"><img src="./images/peace.png"/></button>


Comment on lines +19 to +20
Copy link
Owner

Choose a reason for hiding this comment

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

remove unnecessary spacings.

<script src="index.js"></script>
</body>
</html>

33 changes: 33 additions & 0 deletions rock-paper-scissor-game/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var comp_score = 0;
var user_score = 0;


function check(str) {
var vals = ['rock', 'paper', 'scissor'];
var index = Math.floor((Math.random() * 3));
var comp_res = vals[index];

if (str == comp_res) {
document.getElementById('result').innerHTML="Draw!";
}
else if(str == 'rock' && comp_res == 'scissor') {
document.getElementById('result').innerHTML='You Won';
user_score += 1;
document.getElementById('user_score_display').innerHTML=user_score;
}
else if(str == 'paper' && comp_res == 'rock') {
document.getElementById('result').innerHTML='You Won';
user_score += 1;
document.getElementById('user_score_display').innerHTML=user_score;
}
else if(str == 'scissor' && comp_res == 'paper') {
document.getElementById('result').innerHTML="You Won";
user_score += 1;
document.getElementById('user_score_display').innerHTML=user_score;
}
else {
document.getElementById('result').innerHTML='You Lose';
comp_score += 1;
document.getElementById('comp_score_display').innerHTML=comp_score;
}
Comment on lines +10 to +32
Copy link
Owner

Choose a reason for hiding this comment

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

instead of multiple if..else statemets and repeating code section use concept of ternary operator:
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;

}
47 changes: 47 additions & 0 deletions rock-paper-scissor-game/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
body {
text-align: center;
font-family: 'Courier New', Courier, monospace;
}

h1 {
font-size: 4rem;
}

button {
height: 10rem;
width: 10rem;
border-radius: 2rem;
overflow: visible;
margin: 2rem;
padding: 2rem;
background-color:antiquewhite;
}

img {
max-width: 100%;
max-height: 100%;
}


/* styles of ids */
#user_score {
display: inline-block ;
font-size: 2.5rem;
margin: 4rem 1rem 4rem 2rem;
}

#comp_score {
display: inline-block;
font-size: 2.5rem;
margin: 4rem 1rem 4rem 4rem;
}

#result {
font-size: 1.5rem;
}


/* hovering effect */
button:hover {
background-color:burlywood;
}