9/9 Send RPS game to friends


#1

Hope that this is relevant: how do I send the code that I made in 9/9 to some friends so that they can play it on their phone or PC?....or am I getting way ahead of myself?

My code is as follows:

var x=-1;
while(x<0){
    if (x==-1) {
        userChoice = prompt('Pick Rock, Paper, Scissors or Rope');
        }
        if( userChoice.toLowerCase() == "rock" ) x = 1;
        if( userChoice.toLowerCase() == "paper" ) x = 1;
        if( userChoice.toLowerCase() == "scissors" ) x = 1;
        if( userChoice.toLowerCase() == "rope" ) x = 1;
        if(x==-1){alert("That\'s not right!");}
}
console.log('You chose: ' + userChoice );

var computerChoice = Math.random();
if (computerChoice <= 0.25) {
    computerChoice = "rock";
} else if(computerChoice > 0.25 && computerChoice <= 0.50) {
    computerChoice = "paper";
} else if(computerChoice > 0.50 && computerChoice <= 0.75) {
    computerChoice = "scissors";
} else {
    computerChoice = "rope";
} console.log("Computer: " + computerChoice);

var compare = function(choice1, choice2) {
if (choice1 === choice2) {
    return "TIE GAME! Choose again.";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        }
          else if (choice2 === "rope") {
            return "rope 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 === "scissors") {
        if (choice2 === "paper") {
            return "scissors wins";
        }
          else if (choice2 === "rope") {
            return "scissors wins";
        }
            else {
                return "rock wins";
            }
        }
    else if (choice1 === "rope") {
        if (choice2 === "paper") {
            return "rope wins";
        }
          else if (choice2 === "scissors") {
            return "scissors wins";
        }
            else {
                return "rope wins";
            }
        }
};
compare(userChoice, computerChoice);

#2

One way would be to post it here: labs repl.it. Or use any other site with such a feature. When you’re done with the HTML+CSS track you can as well copy this to a file with .js ending and include it in your website.


#3

@jcdenton001
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

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 playTheGame =function() {
    var playAgain =true;
    var userChoice="";
    var computerChoice="";
    var theResult="";

   while (playAgain) {

       //execute the pre-declared getUserChoice-function
       var userChoice = getUserChoice();
       console.log("User choice is " + userChoice);

        //execute the pre-declared getComputerChoice-function
        var 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!") {
       playAgain = true;
    } else {
      playAgain = false;
    }

    //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();

#4

@jcdenton001

nice to know

You could test in labs.codecademy.com repl.it
choose Javascript
You will get a split-screen
In the left-hand-side you paste-in your code
click on the Run button.
The right-hand-side is the console

references

www.crockford.com --> for most respected guide-line in Javascript
like coding convention
www.crockford.com
http://javascript.crockford.com/code.html
http://javascript.crockford.com/survey.html


#5

After you created play-the-game
you will then have to integrate the Javascript-code into a script-Tag
of a HTML-Document.

In the HTML-Document you will need to have
- an =input= for your userChoice
- a specified area to present the result
- a play-button
- an exit-button

A lot of work...


#6

Thanks, good enough for now.


#7

Thanks for the super detailed posts. Eventually I want to do this.