Skip to content
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
66 changes: 66 additions & 0 deletions game/game.js
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);
Comment on lines +22 to +26
Copy link
Owner

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.


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
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 for computer;

}
Binary file added game/img/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 game/img/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 game/img/scissor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions game/index.html
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>
22 changes: 22 additions & 0 deletions game/promise.js
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()