Rock Paper Scissors, Next Steps


#1


Rock Paper Scissors, Next Steps

Hello to Everyone!

I looked threw similar topics on the forum, but still havent got a solution. I need help with step 3, which is :

In this version, if both players make the same choice, the game returns a tie. What if the game didn't end there but instead asked both players for new choices?

This is the 4th time Im overwriting this :slight_smile: I tried to do it many diffrent ways, but I cant seem to grasp how to do it, my last attempt and current code is this :

if (choice1 === choice2) {
        prompt("The result is a tie! Choose your 'Weapon' again!");
        return compare(choice1, choice2);

This code above is horrible, since when theres a tie, Doesnt matter what I put in, it still pops up and I have to reload the page. I just there was some kind of break word so that the programm would stop, and start from the top, not only do I need to re-run the compare(); function, but I also need to generate new inputs, both for the user and the computer.

Another problem I've encountered is that my while loop is not as good as I thought, since it only check's for the correct input at the start of the programm, but after that its just processed and forgotten.

I've spent a couple of hours on this programm so far, it's kinda intresting how I want it to be just perfect. All these problems push me to find solutions that I've never even thought would be an issue. So now I think for such a programm to be able to work properly, I need to declare computer input inside the function, like :

var userInput = prompt("Pick one : rock, paper or scissors!");
var computerInput; // creates a null value
var compare = function(choice1, choice2) {
  choice2 = Math.Random(); // computerInput change's from null, to 0-1
// rest of code
}
compare(userInput, computerInput);

Does this make sence at all ? Anyway Id apreceate some help on calling functions within a function among other things I wrote, need to take a breether. Also if you do want to help, I would really apreceate help that is explained, so I can learn, dont bother with just a code block solution, Thank you !


var userChoice = prompt("Please 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("Computer: " + computerChoice);

var compare = function(choice1, choice2) {
    while (choice1 != "rock" && choice1 != "paper" && choice1 != "scissors") {
        var choice1 = prompt(choice1 + " Is not a valid input. Input rock, paper or scissors!");
    }
    if (choice1 === choice2) {
        prompt("The result is a tie! Choose your 'Weapon' again!");
        return compare(choice1, choice2);
    } else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        }
    } else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        }
    } else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins";
        } else {
            return "scissors wins";
        }
    }
}

compare(userChoice, computerChoice);


#2

Instead of using a prompt like you do in the if statement,

Create a variable outside of your function and then return that variable when the user and computer guess the same thing..or a simpler way would be to return your userChoice variable instead because it will compare it dynamically because of your function call.


#3

can you be more specific, perhaps an example ?


#4

I have done IT ! The game is fully operational !

  • Input CAPITAL or small letters, or both !
  • Invalid input protection (while loop used)
  • Multiple rounds if a tie occurs.

Although I still think the code could be shorter and somethings done diffrently(better), if you see something like that, be sure to point it out, Thanks.

// This is for my future reference
var userChoice; // create null, empty value for userInput
var computerChoice; // create null, empty value for computerInput

var compare = function(choice1, choice2) { // funcion start, with two null argument
    console.log("**************************************************************");
    choice1 = prompt("Choose carefully. Rock, Paper or Scissors ?"); // Player input
    while (choice1.toLowerCase() != "rock" && choice1.toLowerCase() != "paper" && choice1.toLowerCase() != "scissors") {
        var choice1 = prompt("\"" + choice1 + "\"" + " Is not a valid input. Input rock, paper or scissors!"); // Runs until a valid Input is entered
    }
    choice1 = choice1.toLowerCase(); // Player input converted to lower case
    console.log("You show: " + choice1 + "!");
    choice2 = Math.random(); // Computer random input
    if (choice2 < 0.34) {
        choice2 = "rock";
    } else if (choice2 < 0.67) {
        choice2 = "paper";
    } else {
        choice2 = "scissors";
    } console.log("The Computer shows: " + choice2 + "!");
    if (choice1 == choice2) { // actual start of comparing the two inputs
        console.log("The result is a tie! Choose your \"Weapon\" again!");
        return compare(choice1, choice2);
    } else if (choice1 == "rock") {
        if (choice2 == "scissors") {
            return "Rock wins!";
        } else {
            return "Paper wins!";
        }
    } else if (choice1 == "paper") {
        if (choice2 == "rock") {
            return "Paper wins!";
        } else {
            return "Scissors wins!";
        }
    } else if (choice1 == "scissors") {
        if (choice2 == "rock") {
            return "Rock wins!";
        } else {
            return "Scissors wins!";
        }
    }
}

compare(userChoice, computerChoice);

#5

Instead of using if/else statements. Use the switch/case method,

switch(example) {
    case n:
        code block
        break;
    case n:
        code block
        break;
    default:
        default code block
}

#6

The lesson wants the user to use the if/else. The switch/case hasn't been taught at that point yet.


#7

This is his own personal code at the rock paper scissors and he was asking for alternative methods to make his code DRY,


#8

thanks, that would save some lines, any other additions ? Something in the lines of this works but you could use "x" in "x" spot that would improve "x" functuonalty, something like that ?


#9

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