Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions rps_game/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rock Paper Scissors</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>

<body>
<h1>Opponent Score</h1>
<h1 id="opponent-score">0</h1>
<img id="opponent-choice">
<br>
<img id="your-choice">
<div id="choices">
</div>
<h1 id="your-score">0</h1>
<h1>User Score</h1>

</body>
</html>
Binary file added rps_game/paper.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 rps_game/rock.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 rps_game/scissors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions rps_game/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
let you;
let yourScore = 0;
let opponent;
let opponentScore = 0;

let choices = ["rock", "paper", "scissors"];

window.onload = function() {
for (let i = 0; i < 3; i++) {
// <img id="rock" src="rock.png">
let choice = document.createElement("img");
choice.id = choices[i];
choice.src = choices[i] + ".png";
choice.addEventListener("click", selectChoice);
document.getElementById("choices").append(choice);
}
}

function selectChoice() {
you = this.id;
document.getElementById("your-choice").src = you + ".png";

//random for oppponent
opponent = choices[Math.floor(Math.random() * 3)];
document.getElementById("opponent-choice").src = opponent + ".png";

//check for winner
if (you == opponent) {
alert('Its a tie')
}
else {
if (you == "rock") {
if (opponent == "scissors") {
alert('yuppie😀 you got a point')
yourScore += 1;
}
else if (opponent == "paper") {
alert('shit😣 opponent got a point')
opponentScore += 1;
}
}
else if (you == "scissors") {
if (opponent == "paper") {
alert('yuppie😀 you got a point')
yourScore += 1;
}
else if (opponent == "rock") {
alert('shit😣 opponent got a point')
opponentScore += 1;
}
}
else if (you == "paper") {
if (opponent == "rock") {
alert('yuppie😀 you got a point')
yourScore += 1;
}
else if (opponent == "scissors") {
alert('shit😣 opponent got a point')
opponentScore += 1;
}
}
}
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 multiple if..else and 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;


document.getElementById("your-score").innerText = yourScore;
document.getElementById("opponent-score").innerText = opponentScore;
}
27 changes: 27 additions & 0 deletions rps_game/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
body{
font-family: Arial, Helvetica, sans-serif;
text-align: center;
background-color: blanchedalmond;
}
#opponent-choice{
width: 240px;
height: 240px;
margin-top: 10px;

}
#your-choice{
width: 240px;
height: 240px;
margin-top: 10px;

}
#choices{
width: 240px;
height: 80px;
margin-top: 10px;
margin: 0 auto;
}
#choices img{
width: 80px;
height: 80px;
}