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] RPS game #12

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
53 changes: 53 additions & 0 deletions main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<div class="container">
<div class="screen">
<div class="buttons">
<button onclick="game(0)">Rock</button>
<button onclick="game(1)">Paper</button>
<button onclick="game(2)">Scissor</button>
</div>
<h1 id="computer">Computer</h1>
</div>
<div>
<p class="me">Me: <span id="me_score">0</span></p>
<p class="comp">Computer: <span id="computer_score">0</span></p>
</div>
</div>

<script type="text/javascript">
Copy link
Owner

Choose a reason for hiding this comment

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

Make a separate file for JS.
avoid putting your code to script tags.


var me =0;
var computer=0;

function game(x) {
var list =["Rock","Paper","Scissor"]

var computer_value= Math.floor(Math.random()*3);
document.getElementById('computer').innerHTML=list[computer_value];

(x == computer_value) ? "" : "" ;

(x == 0 && computer_value== 1) ? computer++ : "";
(x == 0 && computer_value== 2) ? me++ : "";

(x == 1 && computer_value== 0) ? me++ : "";
(x == 1 && computer_value== 2) ? computer++ : "";


(x == 2 && computer_value== 0) ? computer++ : "";
(x == 2 && computer_value== 1) ? me++ : "";

Comment on lines +33 to +44
Copy link
Owner

Choose a reason for hiding this comment

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

avoid using repetitive code. here you can use nested ternary operator:
what i can see is there are only three cases when you 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 ? you ++ : opponent++;

Copy link
Author

Choose a reason for hiding this comment

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

I will update my code sir . Thank you sir

document.getElementById('me_score').innerHTML=me;
document.getElementById('computer_score').innerHTML=computer;
}
</script>


</body>

</html>
44 changes: 44 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins',sans-serif;
}

.container {
display: flex;
flex-direction: column;
width: 480px;
margin: auto;
padding-top: 120px;


}

.screen {
display: flex;
}

.buttons , #computer {
margin: 2px;
flex-basis: 50%;
height: 210px;
background-color: lightblue;
display: flex;
justify-content: center;
align-items: center;
}

button{
font-size: 18px;
padding: 8px;
}

button:focus{
font-weight: bold;
}

.me{float: left;}

.comp{float: right;}