9/9 Full "Rock, Paper, Scissors" game + extended

javascript

#1



Hi guys, in order to help to some of you that are stucked somewhere I created full game plus extended version that includes:
1. Input for your name when you start a game
2.Continue playing when you get a tie with prompt Pick again rock, paper or scissors?
3.Continue playing when you write wrong input istead of "rock" "paper" or "scissors"

*I checked code cannot be shorter because of this 3 extensions on the basic game and everything runs perfectly fine.
If you don't like any of these extensions or just want a basic game you can manually delete unnecessary code.
If you stuck somewhere you can ask in posts, we are here to share and learn from each other :slight_smile:


var userName = prompt("What is your name?");
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
}
else if(computerChoice < 0.67) {
	computerChoice = "paper";
}
else {
	computerChoice = "scissors";
} 
console.log(userName + ": " + userChoice);
console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
    {
    console.log("The result is a tie!");
    }
    var userChoice = prompt("Pick again rock, paper or scissors?");
    var computerChoice = Math.random();
    if (computerChoice < 0.34) {
	    computerChoice = "rock";
    }
    else if(computerChoice < 0.67) {
	computerChoice = "paper";
    }
    else {
	computerChoice = "scissors";
    }
    console.log(userName + ": " + userChoice);
    console.log("Computer: " + computerChoice);
    return compare(userChoice, computerChoice);
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors")
        return "rock wins";
        else (choice2 === "paper")
        return "paper wins";
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock")
        return "paper wins";
        else (choice2 === "scissors")
        return "scissors wins";
    }
    else if (choice1 === "scissors") {
        if (choice2 === "rock")
        return "rock wins";
        else (choice2 === "paper")
        return "scissors wins";
    }
    else { 
        var userChoice = prompt("Wrong input! Please choose between rock, paper or scissors.");
        var computerChoice = Math.random();
        if (computerChoice < 0.34) {
            computerChoice = "rock";
        }
        else if(computerChoice < 0.67) {
	    computerChoice = "paper";
        }
        else {
	    computerChoice = "scissors";
        }
        console.log(userName + ": " + userChoice);
        console.log("Computer: " + computerChoice);
        return compare(userChoice, computerChoice);
    }
}
compare(userChoice, computerChoice);


#2

Here's a tip. If you store this whole code in a function you wouldn't need to type all this you would just need to call the function using one line.

var userChoice = prompt("Pick again rock, paper or scissors?");
    var computerChoice = Math.random();
    if (computerChoice < 0.34) {
	    computerChoice = "rock";
    }
    else if(computerChoice < 0.67) {
	computerChoice = "paper";
    }
    else {
	computerChoice = "scissors";
    }
    console.log(userName + ": " + userChoice);
    console.log("Computer: " + computerChoice);
    return compare(userChoice, computerChoice);
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors")
        return "rock wins";
        else (choice2 === "paper")
        return "paper wins";
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock")
        return "paper wins";
        else (choice2 === "scissors")
        return "scissors wins";
    }
    else if (choice1 === "scissors") {
        if (choice2 === "rock")
        return "rock wins";
        else (choice2 === "paper")
        return "scissors wins";
    }
    else { 
        var userChoice = prompt("Wrong input! Please choose between rock, paper or scissors.");
        var computerChoice = Math.random();
        if (computerChoice < 0.34) {
            computerChoice = "rock";
        }
        else if(computerChoice < 0.67) {
	    computerChoice = "paper";
        }
        else {
	    computerChoice = "scissors";
        }
        console.log(userName + ": " + userChoice);
        console.log("Computer: " + computerChoice);
        return compare(userChoice, computerChoice);
    }
}

#3

@bandit is right. I've been playing your game. The way you have it written causes it to prompt userChoice twice regardless of whether or not there is a tie condition. Can you see why?

In your code for the wrong input condition, there is no code for a tie condition. You may also wish to move your console log coding from inside your compare function and have it run afterwards.


#4

Thanks i will try both i'm currently learning python and looking for ways of shortening the scripts down.

will be fun to have a game of rocks and scissors.

Thumbs up to both your guys


#5

Cool :slight_smile:
Make sure to share your games once they're ready


#6

!/usr/bin/env python3

var userChoice = prompt("Pick again rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
}
else if(computerChoice < 0.67) {
computerChoice = "paper";
}
else {
computerChoice = "scissors";
}
console.log(userName + ": " + userChoice);
console.log("Computer: " + computerChoice);
return compare(userChoice, computerChoice);
}
else if (choice1 === "rock") {
if (choice2 === "scissors")
return "rock wins";
else (choice2 === "paper")
return "paper wins";
}
else if (choice1 === "paper") {
if (choice2 === "rock")
return "paper wins";
else (choice2 === "scissors")
return "scissors wins";
}
else if (choice1 === "scissors") {
if (choice2 === "rock")
return "rock wins";
else (choice2 === "paper")
return "scissors wins";
}
else {
var userChoice = prompt("Wrong input! Please choose between rock, paper or scissors.");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
}
else if(computerChoice < 0.67) {
computerChoice = "paper";
}
else {
computerChoice = "scissors";
}
console.log(userName + ": " + userChoice);
console.log("Computer: " + computerChoice);
return compare(userChoice, computerChoice);
}
}

I copyed the code just like this as stated when i run in linux i get /home/blueberry/Documents/rock1.py
File "/home/blueberry/Documents/rock1.py", line 3
var userChoice = prompt("Pick again rock, paper or scissors?");
^
SyntaxError: invalid syntax


#7

Yeah @bandit I know the meaning of function but I dont know how to create new function from this repeating code. Can you please write me the code how it will look like?
Game will be the same but it will be usefull to learn how to eficiently cut the repeating codes.
In this case repeating code is this:

    var computerChoice = Math.random();
    if (computerChoice < 0.34) {
	    computerChoice = "rock";
    }
    else if(computerChoice < 0.67) {
	computerChoice = "paper";
    }
    else {
	computerChoice = "scissors";
    }
    console.log(userName + ": " + userChoice);
    console.log("Computer: " + computerChoice);
    return compare(userChoice, computerChoice);

#8

@biff75 Its not promting userChoice twice, maybe you didnt pay attention: first time promt is asking you to type your name, second time is asking you to choose "rock, paper or scissors". I wanted to be like that, so when you type your name, in game now you won't see "User:", instead of that you will see your name.

Second thing, you dont need to type again code for tie condition or any comparison choices, you wrote that allready and just need this code to repeat all proces:
return compare(userChoice, computerChoice);

So everything is just I wanted to be except that @bandit is right I could shorten my code because some lines are repeating.


#9

@anglachell when you run your compare function and there is no tie, tell me what your code runs.


#10

I cannot write you the code but I can explain to you how it should look like.


#11

Ok nvm ty @bandit, I will ask my friend, he is expert programer and when he writes me the shorten code I will put it here for all users who like to learn new things from javaScript.


#12

One way of shortening the code is putting the computer's choices in an array and doing something like this.

choices = ["Rock","Paper","Scissors"]
com_choice = choices[rand(3) - 1]

Using ||'s and &&'s could also shorten it instead of nesting if statements.


#13

I never said I wouldn't help you. :smile: Asking other people to write for you code beats the purpose of you learning how to create your own programs.


#14

@biff75 obviously you copied wrong code, you copied bandit's code which dont have compare function for tie. Copy all code from my first post and you will get all game running without any problem, you will find code for tie also, just better look.
If you're stuck somewhere in making this game or need any help I will be here, but please don't talk this is not good, that is not good without checking first.
I wrote in description that this game is extended, so some thing are modified by me and that is:
1. Input for your name when you start a game
2.How to continue playing when you get a tie
3.How to continue playing when you write wrong input istead of "rock" "paper" or "scissors"
If you dont like some of these things you can manually remove them from code.


#15

Thanks @tehluigi for sharing one of solutions, its good to know :wink:


#16

@javarockstar98800
You also copied wrong code from bandit which is not good. There is no any new function to make all code lesser, he just copied half of my code but it is not enough to work properly.
In this code you posted you dont have if statement for tie condition. Also if you dont like my first line of code where message box is asking for your name then your name is written in the comparison table at the end, you need to fix, instead of this:
console.log(userName + ": " + userChoice);
you need this code to make things correct:
console.log("User: " + userChoice);

But easiest way to make things work, copy all my code from first post and then change what you want to change line by line.


#17

@anglachell I've only played the 1st game you posted. At the beginning of your game, there is a username prompt and a userchoice prompt. Then, in your compare function, a prompt runs after your tie condition. Where did I err?


#18

So at last you get some of my extensions on the basic game:

  1. Input for your name when you start a game
    (You get prompt for your name, then your name is typed instead of 'User' and I think its better like this.

3.How to continue playing when you write wrong input istead of "rock" "paper" or "scissors"
(I think you understood this one also, when you type some wrong word you get new prompt to pick somenthing between 'rock, paper or scissors' and continue playing).

But still you didnt understood extension 2
2.How to continue playing when you get a tie
(When you get a tie, you get new prompt Pick again rock, paper or scissors? to reroll picks because you get a tie and no one wins so you will continue playing until someone wins you or computer. Now you can choose again 'rock, paper or scissors' and computer will also choose again. IF again tie happens you will again get prompt Pick again rock, paper or scissors? and so on until someone wins. I think its nicer to continue playing until someone wins but if you dont like this method you can just delete this code:

var userChoice = prompt("Pick again rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
}
else if(computerChoice < 0.67) {
computerChoice = "paper";
}
else {
computerChoice = "scissors";
}
console.log(userName + ": " + userChoice);
console.log("Computer: " + computerChoice);
return compare(userChoice, computerChoice);

*If you want to remove this code, you need also to remove unneeded brackets { } if you want to game work properly without errors.

and without this when you get a tie you will get a draw and game will be over without continuing.

I hope so now is everything clear :slight_smile:


#19

Crystal clear :ok_hand:


#20

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.