Create a basic simple Rock Paper Scissor game#8
Create a basic simple Rock Paper Scissor game#8thsh-odoo wants to merge 2 commits intoshsa-odoo:mainfrom
Conversation
task1.js
Outdated
| @@ -0,0 +1,69 @@ | |||
| const choices=['Rock','Paper','Scissor']; | |||
There was a problem hiding this comment.
Give proper spacing while assignment:
const choices = ['Rock','Paper','Scissor'];
update it for all cases in the code.
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.
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.
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.
discard the unwanted/ commeted out code while you push the code. make it a habit of checking the code you push.
…to perform the task
Completed the task of creating a small basic game of rock paper scissor