Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    5
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Exclamation Please help with my javascript game

    I am trying to create scissors, paper, rock, lizard, spock, the unofficial extended version of scissors, paper, rocks. I have most of it working but I can't get the switch statement to work, I tried an if/else statement but that didn't work either. I have tried putting it in JS Bin and a couple other debuggers but they all returned no errors.

    The switch statement is under the comment "//The game" and the if/else statement is under "//The original game".

    Please take a look!






    [CODE]

    // The testing area



    // The players

    var playerOne = {
    name: prompt("What is player one's name?"),
    choice: 0,
    score: 0
    };

    var playerTwo = {
    name: prompt("What is player two's name?"),
    choice: 0,
    score: 0
    };

    // The choices

    choices = function(name,attack,prey,predator) {
    this.name = name,
    this.attack = attack,
    this.prey = prey,
    this.predator = predator;
    };

    var scissors = choices("Scissors","cut",[lizard,paper],[rock,spock]);
    var paper = choices("Paper", "covers",[rock,spock],[scissors,lizard]);
    var rock = choices("Rock","crushes",[scissors,lizard],[spock,paper]);
    var lizard = choices("Lizard","bites",[spock,paper],[rock,scissors]);
    var spock = choices("Spock","vaporises",[scissors,rock],[lizard,paper]);

    //The game

    var letsPlay = function() {
    playerOne.choice = prompt("What hand will " + playerOne.name + " choose?","Scissors, Paper, Rock, Lizard, Spock").toLowerCase;
    playerTwo.choice = prompt("What hand will " + playerTwo.name + " choose?","Scissors, Paper, Rock, Lizard, Spock").toLowerCase;
    if(playerOne.choice === playerTwo.choice.prey){
    playerOne.score = playerOne.score + 1;
    alert(playerOne.choice.name + " " + playerOne.choice.attack + " " + playerTwo.choice.name + "!" + playerOne.name + "Wins!");
    }else if(playerTwo.choice === playerOne.choice.prey){
    playerTwo.score = playerTwo.score + 1;
    alert(playerTwo.choice.name + " " + playerTwo.choice.attack + " " + playerOne.choice.name + "!" + playerTwo.name + "Wins!");
    }else{
    alert("Hmmm this shouldn't happen");
    }
    };

    //The menu

    var menu = function() {
    for(var i=0; i<10; i++) {
    var option = prompt("Would you like to play, see info, check scores, or quit?","Enter play, info, scores or quit").toLowerCase;
    switch(option) {
    case 'play':
    letsplay();
    break;
    case 'info':
    alert("Scissors is beaten by " + scissors.predator + " and beats " + scissors.prey);
    alert("Paper is beaten by " + paper.predator + " and beats " + paper.prey);
    alert("Rock is beaten by " + rock.predator + " and beats " + rock.prey);
    alert("Lizard is beaten by " + lizard.predator + " and beats " + lizard.prey);
    alert("Spock is beaten by " + spock.predator + " and beats " + spock.prey);
    alert("Enjoy the Game! Coding by Nathan Squire");
    break;
    case 'scores':
    alert(playerOne.name + " is on " + playerOne.score + " points!");
    alert(playerTwo.name + " is on " + playerTwo.score + " points!");
    break;
    default:
    i=10;
    break;
    }
    }
    };

    menu();

    //The original menu
    /*

    var menu = function() {
    for(var i=0; i<10; i++) {
    var option = prompt("Would you like to play, see info, check scores, or quit?","Enter play, info, scores or quit").toLowerCase;
    if(option === "play") {
    letsPlay();
    }else if(option === "info") {
    alert("Scissors is beaten by " + scissors.predator + " and beats " + scissors.prey);
    alert("Paper is beaten by " + paper.predator + " and beats " + paper.prey);
    alert("Rock is beaten by " + rock.predator + " and beats " + rock.prey);
    alert("Lizard is beaten by " + lizard.predator + " and beats " + lizard.prey);
    alert("Spock is beaten by " + spock.predator + " and beats " + spock.prey);
    alert("Enjoy the Game! Coding by Nathan Squire");
    }else if(option === "score") {
    alert(playerOne.name + " is on " + playerOne.score + " points!");
    alert(playerTwo.name + " is on " + playerTwo.score + " points!");
    }else{
    i=10;
    }
    }
    };

    menu();

    */

    /CODE

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,151
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    var option = prompt("Would you like to play, see info, check scores, or quit?","Enter play, info, scores or quit").toLowerCase();

    I would make it

    var option = prompt("Would you like to play, see info, check scores, or quit?, Enter P(play), I(info), S(scores) or Q(quit)","").toLowerCase().charAt(0);
    that is, P, I, S or Q.

    My opinion of that shot is unprintable on air. - Commentator BBC 4.
    Last edited by Philip M; 01-24-2014 at 12:08 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    nsqui5 (01-25-2014)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    This isn't going to work:
    Code:
    var scissors = choices("Scissors","cut",[lizard,paper],[rock,spock]);
    var paper = choices("Paper", "covers",[rock,spock],[scissors,lizard]);
    var rock = choices("Rock","crushes",[scissors,lizard],[spock,paper]);
    var lizard = choices("Lizard","bites",[spock,paper],[rock,scissors]);
    var spock = choices("Spock","vaporises",[scissors,rock],[lizard,paper]);
    At the time you create scissors, none of the other choices are created.
    At the time you create paper, none of the other choices except scissors are created.
    etc.

    Try this:
    Code:
    var scissors, paper, rock, lizard, spock;
    scissors = choices("Scissors","cut",[lizard,paper],[rock,spock]);
    paper = choices("Paper", "covers",[rock,spock],[scissors,lizard]);
    rock = choices("Rock","crushes",[scissors,lizard],[spock,paper]);
    lizard = choices("Lizard","bites",[spock,paper],[rock,scissors]);
    spock = choices("Spock","vaporises",[scissors,rock],[lizard,paper]);
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    nsqui5 (01-25-2014)

  • #4
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    5
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Thumbs up

    Quote Originally Posted by Philip M View Post
    var option = prompt("Would you like to play, see info, check scores, or quit?","Enter play, info, scores or quit").toLowerCase();

    I would make it

    var option = prompt("Would you like to play, see info, check scores, or quit?, Enter P(play), I(info), S(scores) or Q(quit)","").toLowerCase().charAt(0);
    that is, P, I, S or Q.

    My opinion of that shot is unprintable on air. - Commentator BBC 4.
    Thanks for the advise, but what does "charAt(0);" do?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,151
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    Quote Originally Posted by nsqui5 View Post
    Thanks for the advice, but what does "charAt(0);" do?
    I'll give you three guesses. You really ought to make an effort to find these things out for yourself. Is it not obvious?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    nsqui5 (01-25-2014)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •