Full "rock, paper, rope, scissors" complete


#1



var game = function() {
    var userChoice = prompt("Do you choose rock, paper, rope or scissors?");
    var computerChoice = Math.random();
    if (computerChoice <= 0.25) {
        computerChoice = "rock";
    } else if(computerChoice <= 0.50) {
        computerChoice = "paper";
    } else if(computerChoice <= 0.75) {
        computerChoice = "rope";
    }
    else {
        computerChoice = "scissors";
    }
    console.log("Your choice: " + userChoice);
    console.log("Computer: " + computerChoice);
    console.log(compare(userChoice, computerChoice));
};

var compare = function(choice1, choice2) {
    if (choice1 == choice2) {
        console.log("The result is a tie!");
        return game();
    }
    else if (choice1 == "rock") {
        if (choice2 == "scissors") {
            return "rock wins";
        }
        else if (choice2 == "rope") {
            return "rock wins";
        }
        else {
            return "paper wins";
        }
    }
    else if (choice1 == "paper") {
        if (choice2 == "rock") {
            return "paper wins";
        }
        else if (choice2 == "rope") {
            return "rope wins";
        }
        else {
            return "scissors wins";
        }
    }
    else if (choice1 == "rope") {
        if (choice2 == "rock") {
            return "rock wins";
        }
        else if (choice2 == "paper") {
            return "rope wins";
        }
        else {
            return "scissors wins";
        }
    }
    else if (choice1 == "scissors") {
        if (choice2 == "paper") {
            return "scissors wins";
        }
        else if (choice2 == "rope") {
            return "scissors wins";
        }
        else {
            return "rock wins";
        }
    }
    else {
        return "Wrong Input!";
    }
};

game();


#2

@bitsconsumer,

If you are extending the choice-possibillity, you have to extend the
so-called truth-table.

Your new Decisions-Table: fill-in ?? wins...

              | ch-2  |  ch-2  | ch-2     | ch-2   |
              | rock  |  paper | scissors | rope   |
  ------------|-------|--------|----------|--------|
              |       | paper  |  rock    | ????   |
 choice1  rock|   X   | wins   |  wins    |  wins  | (1st else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              | paper |        |scissors  |  ? ?   |
 choice1 paper| wins  |    X   |  wins    |  wins  | (2nd else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              |  rock |scissors|          |  ?  ?  | 
 ch-1 scissors|  wins | wins   |   X      |  wins  | (3rd else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              |  ? ?  | ? ? ?  |  ? ? ?   |        | 
 ch-1 rope    |  wins | wins   |   wins   |    X   | (4th else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|

With your current setup
the frequency is

rock     4
paper    2
scissors 4
rope     2

and the graetest change to win would be if you choose rock or scissors


#3

well, I didn't realize that! I might have to see it through deep to fully understand it. but hey, could you help me with the full code? i mean can you write to me the full perfect code for this game. I want to perfect it but need the perfect full code to compare and understand it. Thanks


#4

@bitsconsumer
Your wish is my command....

1 To extend the userChoice-prompting with a validity-check, you would
embed the prompting into a function-body...
So you would start by
var getUserChoice = function() { your prompting here };

2 If you are extending the choice-possibillity, you have to extend the
so-called truth-table.

Your new Decisions-Table: fill-in ?? wins...

              | ch-2  |  ch-2  | ch-2     | ch-2   |
              | rock  |  paper | scissors | rope   |
  ------------|-------|--------|----------|--------|
              |       | paper  |  rock    | ????   |
 choice1  rock|   X   | wins   |  wins    |  wins  | (1st else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              | paper |        |scissors  |  ? ?   |
 choice1 paper| wins  |    X   |  wins    |  wins  | (2nd else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              |  rock |scissors|          |  ?  ?  | 
 ch-1 scissors|  wins | wins   |   X      |  wins  | (3rd else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              |  ? ?  | ? ? ?  |  ? ? ?   |        | 
 ch-1 rope    |  wins | wins   |   wins   |    X   | (4th else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|

3 What you want is a loop-construct...
create the getUserChoice function using return-statement
create the getComputerChoice function using return-statement
creat the compare function using return-statement ( allready done !!)

As you are using return-statements you call the compare-function
var theResult = compare(userChoice,computerChoice);
and thus having the =result= of the game in the theResult variable

===========================================

var getUserChoice = function () {
 var userChoice;
 do {
      userChoice = prompt("Choose either rock, paper or scissors","paper");
     } while (userChoice !== "rock" &&
          userChoice !== "paper" &&
          userChoice !== "scissors");
     // console.log("UserChoice is 1: "+ userChoice);
     return userChoice;
};
var getComputerChoice = function() {
var computerChoice = Math.random();
  if (computerChoice < 0.34) {
    computerChoice = "rock";
  } else if(computerChoice <= 0.67) {
	computerChoice = "paper";
  } else {
	computerChoice = "scissors";
  }
  //console.log("computerChoice 1: " + computerChoice);
  return computerChoice;
};

//var userChoice = getUserChoice();
//var computerChoice = getComputerChoice();

//=================================

var compare = function(choice1, choice2){ ///opens the function
  if(choice1 === choice2){ ///opens the if
      return("The result is a tie!");
  } ///closes the if

  else if (choice1 === "rock"){ ///opens the else if
      if(choice2 === "scissors"){ ///opens the if
          return "rock wins";
      }  ///closes the if
      else{ ///opens the else
          return "paper wins";
      } ///closes the else
  } ///closes the else if

  else if (choice1 === "paper") { ///opens the else if
      if (choice2 === "rock") { ///opens the if
          return "paper wins";
      } ///closes the if
      else { ///opens the else
          return "scissors wins";
      } ///closes the else
  } ///closes the else if

  else if (choice1 === "scissors") { ///opens the else if
      if (choice2 === "rock") { ///opens the if
          return("rock wins");
      } ///closes the if
      else { ///opens the else
          return("scissors wins");
      } ///closes the else
  } ///closes the else if

}; ///closes the function

==============================================================

var playTheGame =function() {
    var playAgain =true;
    var userChoice="";
    var computerChoice="";
    var theResult="";
    while (playAgain) {
    //execute the pre-declared getUserChoice-function
    userChoice = getUserChoice();
    console.log("User choice is " + userChoice);

```
        //execute the pre-declared getComputerChoice-function
        computerChoice = getComputerChoice();
        console.log("Computer choice is " + computerChoice);

        //execute the compare-function and capture the-result
        theResult = compare(userChoice,computerChoice);
        console.log("The result is " + theResult);

        //if (theResult === "It is a tie!") {
        //    console.log("It is a tie!");
        if (theResult === "The result is a tie!") {
            //console.log("The result is a tie!");
            playAgain = true;
            console.log("==== Play once more =====");
        } else {
            playAgain = false;
            console.log("==== End of Game =====");
        }

    //end of -while- loop is reached, and if VALUE of playAgain === true
    // the loop will start over
    } //closing the while-loop

}; //closing the function-body of playTheGame

##======================================================================

// call the function playTheGame
playTheGame();

#5

so basically, both our codes are the same except you have more functions to group the variables(getuserchoice and getcomputerchoice) better. That's really clean code you have there, but I guess the only wrong thing with my code was that rock & scissors got more probability to win due to the rope. I guess if rope is added anyway, an imbalance in probabillity would be introduced no matter what rope does. don't you think? or am I wrong. idk


#6

@bitsconsumer
If you look at the Decisions/Truth-Table
you have 16 possibilities.....
minus 4, being the choice1===choice2 leaving you with 12 possibilities....

You have 4 choice Values
and 12 decisions.....
to have an even-distribution each choice should get 3 wins


#7

how do you think the rope will do against rock, paper and scissors? scissors will cut rope but in your opinion, what would the rock and paper do?


#8

@bitsconsumer
It is you filling out the table......
you think of something like
if computerChoice is "rope" it is weaker than userChoice being ''rope" as the user can make a knot....


#9

ok here the problem lies... we need equal wins for all. so 3 for rope, 3 for rock, 3 for paper, 3 for scissors.

as you can see from the pic, rock has 2 wins(blue) already, paper 2(red), and so does scissors(green). now we need 3 rope wins and 1 of each(rock, paper, scissors). But now the real problem arises. if we assume scissors wins against rope, scissors will have 2 more wins,hence becoming 4 wins in total. So, this contradicts the point of all having each equal wins(3). So, adding rope is a disaster for this game, right?


#10

just as a bit of fun what about rock, paper, scissors, lizard, spock?


#11

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