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

rps game #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

rps game #9

wants to merge 1 commit into from

Conversation

adsh-odoo
Copy link

Rps game initial commit

const bot_score = document.getElementById('bot')
bot_score.innerText = bot_count

const random_number_generator = ()=>(Math.ceil(Math.random()*3)) //random number generator
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use proper spacings.

const random_number_generator = () => (Math.ceil(Math.random()*3))
update the same in the code.

Comment on lines +22 to +94
if(id===1){
if(computer_choice == 2){
status_human.innerText ="Rock"
stat.innerText= "You lose"
status_bot.innerText= "Paper"
bot_count++
bot_score.innerText = bot_count
}
else if(computer_choice == 3){
status_human.innerText ="Rock"
stat.innerText = "You won"
status_bot.innerText= "Scissor"
human_count++
human_score.innerText = human_count
}
else{
status_human.innerText ="Rock"
stat.innerText = "Score are tied"
status_bot.innerText= "Rock"
}

stat.remove
}
// For paper
else if(id===2){
if(computer_choice == 1) {
status_human.innerText ="Paper"
stat.innerText= "You won"
status_bot.innerText= "Rock"
human_count++
human_score.innerText = human_count
}

else if(computer_choice == 3){
status_human.innerText ="Paper"
stat.innerText = "You Lose"
status_bot.innerText= "Scissor"
bot_count++
bot_score.innerText = bot_count
}
else{
status_human.innerText ="Paper"
stat.innerText = "Score are tied"
status_bot.innerText= "paper"
}

stat.remove
}
// For Scissors
else{
if(computer_choice == 1) {
status_human.innerText ="Scissor"
stat.innerText= "You Lose"
status_bot.innerText= "Rock"
bot_count++
bot_score.innerText = bot_count
}

else if(computer_choice == 2){
status_human.innerText ="Scissor"
stat.innerText = "You Won"
status_bot.innerText= "Paper"
human_count++
human_score.innerText = human_count
}
else{
status_human.innerText ="Scissor"
stat.innerText = "Score are tied"
status_bot.innerText= "Scissor"
}

stat.remove
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optimize 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 === 0 ) && (opponent === 2) ||
(you === 1 ) && (opponent === 0) ||
(you === 2) && (opponent === 1) ) // 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;

Comment on lines +8 to +9


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont push unnecessary spaces. always check the code before updating.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants