-
Notifications
You must be signed in to change notification settings - Fork 14
[ADD] Rock-paper-scissor game #3
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
function getvalue(id) { | ||
var number = document.getElementById(id).value; | ||
let obj = new Game(number); | ||
obj.fight(); | ||
} | ||
|
||
class Game { | ||
constructor(choice) { | ||
this.choice = choice; | ||
} | ||
|
||
computer_choice() { | ||
var x = Math.random() * 3; | ||
var x = Math.floor(x) | ||
var choices = ['rock', 'paper', 'scissor'] | ||
|
||
return choices[x]; | ||
} | ||
|
||
fight() { | ||
|
||
var a = this.computer_choice(); | ||
let b = this.choice; | ||
let c = Number(document.getElementById('computer_score').textContent); | ||
let u = Number(document.getElementById('user_score').textContent); | ||
let n = Number(document.getElementById('points').value); | ||
|
||
if (n) { | ||
document.getElementById('computer_img').src = `img/${a}.png` | ||
document.getElementById('user_img').src = `img/${b}.png` | ||
if ((a == "rock" & b == "scissor") | (a == "scissor" & b == "paper") | (a == "paper" & b == "rock")) { | ||
document.getElementById('computer_score').innerHTML = c += 1; | ||
document.getElementById('score').innerHTML = "Computer Wins"; | ||
} | ||
else if ((b == "rock" & a == "scissor") | (b == "scissor" & a == "paper") | (b == "paper" & a == "rock")) { | ||
document.getElementById('user_score').innerHTML = u += 1; | ||
document.getElementById('score').innerHTML = "User Wins"; | ||
} else { | ||
document.getElementById('score').innerHTML = "Tie"; | ||
} | ||
} | ||
|
||
if (n == c) { | ||
const buttons = document.getElementsByClassName("btn"); | ||
for (let i = 0; i < buttons.length; i++) { | ||
buttons[i].disabled = true; | ||
} | ||
document.getElementById("computer_header").classList.add("bg-success"); | ||
document.getElementById("computer_body").classList.add("border-success"); | ||
document.getElementById("user_header").classList.add("bg-danger"); | ||
document.getElementById("user_body").classList.add("border-danger"); | ||
} | ||
else if (n == u) { | ||
const buttons = document.getElementsByClassName("btn"); | ||
for (let i = 0; i < buttons.length; i++) { | ||
buttons[i].disabled = true; | ||
} | ||
document.getElementById("user_header").classList.add("bg-success"); | ||
document.getElementById("user_body").classList.add("border-success"); | ||
document.getElementById("computer_header").classList.add("bg-danger"); | ||
document.getElementById("computer_body").classList.add("border-danger"); | ||
|
||
} | ||
|
||
} | ||
Comment on lines
+28
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<!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 href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> | ||
<script src="game.js"></script> | ||
</head> | ||
<body> | ||
<div class="container mt-3 shadow p-3 mb-5 bg-body rounded"> | ||
<div class="card text-center"> | ||
<div class="card-header"> | ||
Rock Paper Scissor Game | ||
</div> | ||
<div class="card-body"> | ||
<div class="row row-cols-3"> | ||
<div class="col-4"> | ||
<div id="computer_header" class="card-header"> | ||
Computer | ||
</div> | ||
<div id="computer_body" class="card-body border"> | ||
<img id="computer_img" src="img/paper.png" class="img-thumbnail w-50 mb-2" alt="..."> | ||
</div> | ||
</div> | ||
<div class="col-4 align-self-center"> | ||
<div class="card"> | ||
<div class="card-body"> | ||
<select id="points" class="form-select form-select-sm mb-2" aria-label=".form-select-sm example" required="True"> | ||
<option selected>Select Points</option> | ||
<option value="5">5</option> | ||
<option value="10">10</option> | ||
<option value="15">15</option> | ||
</select> | ||
|
||
<h5 class="card-title">Score</h5> | ||
<h6 class="card-subtitle mb-2 text-muted">Computer VS User</h6> | ||
<h1 class="card-text"><span id="computer_score">0</span> : <span id="user_score">0</span></h1> | ||
<h6 id="score" class="card-subtitle mb-2 text-muted mt-3">Result</h6> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="col-4"> | ||
<div id="user_header" class="card-header"> | ||
User | ||
</div> | ||
<div id="user_body" class="card-body border"> | ||
<img id="user_img" src="img/paper.png" class="img-thumbnail w-50 mb-2" alt="..."> | ||
<div class="btn-group" role="group" aria-label="Basic outlined example"> | ||
<button id="rock" type="button" class="btn btn-outline-secondary" onclick="getvalue(id)" value="rock">Rock</button> | ||
<button id="paper" type="button" class="btn btn-outline-secondary" onclick="getvalue(id)" value="paper">Paper</button> | ||
<button id="scissor" type="button" class="btn btn-outline-secondary" onclick="getvalue(id)" value="scissor">Scissor</button> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="card-footer text-muted"> | ||
Good Luck | ||
</div> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const promiseMethod = () => { | ||
return new Promise((reslove, request)=> { | ||
setTimeout(() => { | ||
reslove('hi'); | ||
}, 2000); | ||
}); | ||
} | ||
|
||
promiseMethod().then((value) => { | ||
console.log("done",value); | ||
}, | ||
(value)=>{ | ||
console.log("reject"); | ||
}); | ||
|
||
|
||
async function abc() | ||
{ | ||
const data = await fetch('https://portal.tycoonstats.com/api/demo') | ||
console.log(data.text()) | ||
} | ||
abc() |
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.
use better naming conventions, representing the property of value assingned.