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

Created a Stone Paper Scissor game #77

Open
wants to merge 3 commits 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
7 changes: 7 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ https://github.com/wmt-rn-shubham
Name: Vishwa Desai
https://github.com/wmt-vishwad

Name: Zibras Ismail
https://github.com/ZibrasIsmail
City: Batticaloa, Sri Lanka
About: I am a software engineering undergraduate and expertise in web development.
Language: JavaScript
Email: [email protected]

54 changes: 54 additions & 0 deletions Stone-Paper-Scissor-game/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const computerChoiceDisplay = document.getElementById('computer-choice')
const userChoiceDisplay = document.getElementById('user-choice')
const resultDisplay = document.getElementById('result')
const possibleChoices = document.querySelectorAll('button')
let userChoice
let computerChoice
let result

possibleChoices.forEach(possibleChoice => possibleChoice.addEventListener('click', (e) => {
userChoice = e.target.id
userChoiceDisplay.innerHTML = userChoice
generateComputerChoice()
getResult()
}))

function generateComputerChoice() {
const randomNumber = Math.floor(Math.random() * 3) + 1 // or you can use possibleChoices.length

if (randomNumber === 1) {
computerChoice = 'stone'
}
if (randomNumber === 2) {
computerChoice = 'scissors'
}
if (randomNumber === 3) {
computerChoice = 'paper'
}
computerChoiceDisplay.innerHTML = computerChoice
}

function getResult() {
if (computerChoice === userChoice) {
result = 'its a draw!'
}
if (computerChoice === 'stone' && userChoice === "paper") {
result = 'you win!'
}
if (computerChoice === 'stone' && userChoice === "scissors") {
result = 'you lost!'
}
if (computerChoice === 'paper' && userChoice === "scissors") {
result = 'you win!'
}
if (computerChoice === 'paper' && userChoice === "stone") {
result = 'you lose!'
}
if (computerChoice === 'scissors' && userChoice === "stone") {
result = 'you win!'
}
if (computerChoice === 'scissors' && userChoice === "paper") {
result = 'you lose!'
}
resultDisplay.innerHTML = result
}
19 changes: 19 additions & 0 deletions Stone-Paper-Scissor-game/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Stone Paper Scissors Tutorial</title>
</head>
<body>

<h2>Computer Choice: <span id="computer-choice"></span></h2>
<h2>Your Choice: <span id="user-choice"></span></h2>
<h2>Result: <span id="result"></span></h2>

<button id="stone">stone</button>
<button id="paper">paper</button>
<button id="scissors">scissors</button>

<script src="app.js" charset="utf-8"></script>
</body>
</html>