www.webdeveloper.com
Results 1 to 5 of 5

Thread: I do i make this code add up the rolls in the score

  1. #1
    Join Date
    Feb 2012
    Posts
    6

    I do i make this code add up the rolls in the score

    I am trying this snake eye game using recursive method. The problem is i could not make it add the two rolls. Help would be appreciated. Hereis the code:

    function rollSingleDice(){

    return Math.floor(Math.random()*6+1);
    }

    function rollDice(){
    var playerScore = 0;
    var die1 = rollSingleDice();
    var die2 = rollSingleDice()

    playerScore = playerScore + die1 + die2;
    console.log("Die 1: " + die1+" Die 2: "+
    die2+ " Score "+playerScore);
    if (die1 !== die2){
    return;
    }else {
    console.log("Congratulations Double Thrown!");
    return rollDice();
    }
    }

    rollDice();

  2. #2
    Join Date
    May 2011
    Location
    Lancashire
    Posts
    35
    I have just run your code replacing console.log with document.write to display the results on the screen and your code works fine for me. (i.e. it displays the reults of each individual roll and the sum of both rolls. If it is not working for you, maybe there is an error elsewhere in your code?
    best regards
    LF

  3. #3
    Join Date
    Feb 2012
    Posts
    6
    Quote Originally Posted by LutinFou View Post
    I have just run your code replacing console.log with document.write to display the results on the screen and your code works fine for me. (i.e. it displays the reults of each individual roll and the sum of both rolls. If it is not working for you, maybe there is an error elsewhere in your code?
    best regards
    LF
    I tried the document.write in the codecadey and it sort of stucks. When i use console log, the code return the score for each roll while i was expecting the second score to be a total of the two rolls during a double.
    Last edited by newPro; 05-10-2012 at 01:06 PM. Reason: for clarity of question

  4. #4
    Join Date
    Nov 2010
    Posts
    978
    assuming that you get another throw if you roll a double after rolling a double...

    Code:
    <script type="text/javascript">
    var prevScore = 0;
    function rollSingleDice(){
    return Math.floor(Math.random()*6+1);
    }
    
    function rollDice(){
    var die1 = rollSingleDice();
    var die2 = rollSingleDice();
    var score=die1 + die2;
    console.log("Die 1: " + die1+" Die 2: "+
    die2+ " Score "+Number(score+prevScore));
    if (die1 == die2){
    console.log("Congratulations Double Thrown!");
    prevScore=prevScore+die1 + die2; 
    rollDice();
    	}
    }
    
    rollDice();
    </script>

  5. #5
    Join Date
    Feb 2012
    Posts
    6
    Quote Originally Posted by xelawho View Post
    assuming that you get another throw if you roll a double after rolling a double...

    Code:
    <script type="text/javascript">
    var prevScore = 0;
    function rollSingleDice(){
    return Math.floor(Math.random()*6+1);
    }
    
    function rollDice(){
    var die1 = rollSingleDice();
    var die2 = rollSingleDice();
    var score=die1 + die2;
    console.log("Die 1: " + die1+" Die 2: "+
    die2+ " Score "+Number(score+prevScore));
    if (die1 == die2){
    console.log("Congratulations Double Thrown!");
    prevScore=prevScore+die1 + die2; 
    rollDice();
    	}
    }
    
    rollDice();
    </script>
    Thanks xelawho:
    That cetainly fixed the problem. I have a question though about the solution you proposed. Which references to die1 and die2 actually call the random number generator? My first thought about your solution is the last call to rollDice() in the if statement would call the roll for a third time? I would appreciate to get some explanation around this point.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles