-
Notifications
You must be signed in to change notification settings - Fork 15
Create a basic simple Rock Paper Scissor game #8
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?
Conversation
task1.js
Outdated
@@ -0,0 +1,69 @@ | |||
const choices=['Rock','Paper','Scissor']; |
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.
Give proper spacing while assignment:
const choices = ['Rock','Paper','Scissor'];
update it for all cases in the code.
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.
Okay sir
task1.js
Outdated
function rock() | ||
{ | ||
let computer_choice=Math.floor(Math.random() * choices.length); | ||
console.log(choices[computer_choice]) | ||
|
||
if(choices[computer_choice]=='Rock') | ||
{ | ||
// console.log("helllllllo") | ||
document.getElementById("status").textContent="Draw"; | ||
} | ||
else if (choices[computer_choice]=='Paper') | ||
{ | ||
document.getElementById("status").textContent="Computer Wins"; | ||
computer_score+=1; | ||
document.getElementById("computer_score").textContent=computer_score; | ||
console.log(computer_score) | ||
} | ||
else if(choices[computer_choice]=='Scissor') | ||
{ | ||
document.getElementById("status").textContent="You Win"; | ||
user_scores+=1; | ||
document.getElementById("user_scores").textContent=user_scores; | ||
console.log(user_scores) | ||
} | ||
} | ||
function paper() | ||
{ | ||
let computer_choice=Math.floor(Math.random() * choices.length); | ||
console.log(choices[computer_choice]) | ||
if(choices[computer_choice]=='Paper') | ||
{ | ||
document.getElementById("status").textContent="Draw"; | ||
} | ||
else if (choices[computer_choice]=='Scissor') | ||
{ | ||
document.getElementById("status").textContent="Computer Wins"; | ||
computer_score+=1; | ||
document.getElementById("computer_score").textContent=computer_score; | ||
} | ||
else if(choices[computer_choice]=='Rock') | ||
{ | ||
document.getElementById("status").textContent="You Win"; | ||
user_scores+=1; | ||
document.getElementById("user_scores").textContent=user_scores; | ||
} | ||
} | ||
function scissor() | ||
{ | ||
let computer_choice=Math.floor(Math.random() * choices.length); | ||
console.log(choices[computer_choice]) | ||
if(choices[computer_choice]=='Scissor') | ||
{ | ||
document.getElementById("status").textContent="Draw"; | ||
} | ||
else if (choices[computer_choice]=='Rock') | ||
{ | ||
document.getElementById("status").textContent="Computer Wins"; | ||
computer_score+=1; | ||
document.getElementById("computer_score").textContent=computer_score; | ||
} | ||
else if(choices[computer_choice]=='Paper') | ||
{ | ||
document.getElementById("status").textContent="You Win"; | ||
user_scores+=1; | ||
document.getElementById("user_scores").textContent=user_scores; | ||
} | ||
} |
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.
Making separate function call for separate event is good.
But optimize the code:
- using ternary operator. as user can win in three cases only :
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;
3.(note* for this case only.) use concept of querySelectorAll("button"), and foreach click call a method that update the state of your game.
4. declare it a const computer_choice=Math.floor(Math.random() * choices.length); and on every time your method calls while button click , you will get a random method.
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.
Okay sir
I have optimize my code in today's RPS game using owl I will do it same in this code without owl
task1.js
Outdated
|
||
if(choices[computer_choice]=='Rock') | ||
{ | ||
// console.log("helllllllo") |
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.
discard the unwanted/ commeted out code while you push the code. make it a habit of checking the code you push.
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.
Okay sir
…to perform the task
Completed the task of creating a small basic game of rock paper scissor