Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. looping through an array that has value based on a dice roll. Need to properly calculate the rolls.

looping through an array that has value based on a dice roll. Need to properly calculate the rolls.

Scheduled Pinned Locked Moved JavaScript
htmlcomgame-devdata-structureshelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 12962889
    wrote on last edited by
    #1

    Created a loop to loop through an array based on the values from the dice roll. Trying to get all the possible scores for a yahtzee game. so far I have it working to that it does add the scores based on the roll example1 I also have it working so that it does not continue to add the score if the a certain die is selected However my issue is that it keeps increasing the score instead of starting over on subsequent rolls example2

    $(document).ready(function(){
    var die1=$('.die1');
    var die2=$('.die2');
    var die3=$('.die3');
    var die4=$('.die4');
    var die5=$('.die5');
    var turns=3;
    var dice;
    var dice_value = new Array(5);
    var ones=0;
    var twos=0;
    var threes=0;
    var fours=0;
    var fives=0;
    var sixes=0;
    var die;
    //roll function
    function roll(die,i){
    rando = Math.floor(Math.random()*6)+1;

         die.html("![](images/die"+rando+".png)");
          die.find('img').attr('class', rando);
          
          dice\_value\[i\]=rando;
         
       $('img').height(50);
      
    };
    
    
    
    $('.die').click(function(){
         $(this).toggleClass('selected');   //adds border around die if clicked
    

    });

    //attaches roll funcition to each die
    $('.button').click(function(){

     for(i=0; i<=turns; turns--){
          if(turns>0){
    	    dice =\[die1,die2,die3,die4,die5\];
    
    		  for(i=0; i 0){
     	
       if( dice\_value\[i\] == 1){
       	 ones= ones+1;
       	  
       	 $('.aceScore').html(ones);
           
       } 
        if( dice\_value\[i\] == 2){
       	 twos= twos+2;
       	  $('.twoScore').html(twos);
       	  
       } 
        if( dice\_value\[i\] == 3){
       	 threes= threes+3;
       	  $('.threeScore').html(threes);
       	 
       } 
        if( dice\_value\[i\] == 4){
       	 fours= fours+4;
       	  $('.fourScore').html(fours);
       	  
       } 
        if( dice\_value\[i\] == 5){
       	 fives= fives+5;
       	  $('.fiveScore').html(fives);
       	 
       } 
        if( dice\_value\[i\]
    
    L 1 Reply Last reply
    0
    • U User 12962889

      Created a loop to loop through an array based on the values from the dice roll. Trying to get all the possible scores for a yahtzee game. so far I have it working to that it does add the scores based on the roll example1 I also have it working so that it does not continue to add the score if the a certain die is selected However my issue is that it keeps increasing the score instead of starting over on subsequent rolls example2

      $(document).ready(function(){
      var die1=$('.die1');
      var die2=$('.die2');
      var die3=$('.die3');
      var die4=$('.die4');
      var die5=$('.die5');
      var turns=3;
      var dice;
      var dice_value = new Array(5);
      var ones=0;
      var twos=0;
      var threes=0;
      var fours=0;
      var fives=0;
      var sixes=0;
      var die;
      //roll function
      function roll(die,i){
      rando = Math.floor(Math.random()*6)+1;

           die.html("![](images/die"+rando+".png)");
            die.find('img').attr('class', rando);
            
            dice\_value\[i\]=rando;
           
         $('img').height(50);
        
      };
      
      
      
      $('.die').click(function(){
           $(this).toggleClass('selected');   //adds border around die if clicked
      

      });

      //attaches roll funcition to each die
      $('.button').click(function(){

       for(i=0; i<=turns; turns--){
            if(turns>0){
      	    dice =\[die1,die2,die3,die4,die5\];
      
      		  for(i=0; i 0){
       	
         if( dice\_value\[i\] == 1){
         	 ones= ones+1;
         	  
         	 $('.aceScore').html(ones);
             
         } 
          if( dice\_value\[i\] == 2){
         	 twos= twos+2;
         	  $('.twoScore').html(twos);
         	  
         } 
          if( dice\_value\[i\] == 3){
         	 threes= threes+3;
         	  $('.threeScore').html(threes);
         	 
         } 
          if( dice\_value\[i\] == 4){
         	 fours= fours+4;
         	  $('.fourScore').html(fours);
         	  
         } 
          if( dice\_value\[i\] == 5){
         	 fives= fives+5;
         	  $('.fiveScore').html(fives);
         	 
         } 
          if( dice\_value\[i\]
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You just need to reset all the counters to zero before each throw.

      U 1 Reply Last reply
      0
      • L Lost User

        You just need to reset all the counters to zero before each throw.

        U Offline
        U Offline
        User 12962889
        wrote on last edited by
        #3

        when i do that i doesnt add the score to the dice that I have selected to keep.

        L 1 Reply Last reply
        0
        • U User 12962889

          when i do that i doesnt add the score to the dice that I have selected to keep.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Then you need to keep those scores separately also. Think about how you actually play Yahtzee, and what you need to do before and after each throw.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups