www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Javascript logic question

  1. #1
    Join Date
    Mar 2014
    Posts
    2

    resolved [RESOLVED] Javascript logic question

    Hi,

    I created a dice game using the code below. I would like to build on it, to build a craps game, asking for help on the programming logic either using if else, or switch statements. The dice can roll random numbers, would like to be able to:

    1) First roll, if the dice roll 7 or 11 you win
    2) First roll, if the dice roll 2,3,12 you lose
    3) First roll, if the dice roll 4,5,6,8,9,10 - that becomes the point, you must roll one of these to win before you roll a 7, if you roll 7 you lose

    Thanks!!
    DM


    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>
    <head>
    <script>

    var diceFace=new Array("images/c1.gif", "images/c2.gif", "images/c3.gif", "images/c4.gif", "images/c5.gif", "images/c6.gif");

    function throwdice(){
    //create a random integer between 0 and 5
    var randomdice=Math.round(Math.random()*5);
    var randomdice2=Math.round(Math.random()*5);
    document.images["mydice"].src=diceFace[randomdice];
    document.images["mydice2"].src=diceFace[randomdice2];
    }
    </script>
    </head>

    <body>

    <img src="images/d1.gif" name="mydice" id="mydice">
    <img src="images/d1.gif" name="mydice2" id="mydice2">


    <form>
    <input type="button" value="Throw dice!" onClick="timer()">
    </form>

    </body>
    </html>

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    868
    I don't think I understand your third rule but in any case I think if/else statements might be the better way to go here. I only say that because with a case/switch you'll have a case for each dice total (and a default), and with if/else statements you'll only need 3 or 4 logical statements to achieve your game rules. I can't say that one is faster than the other in terms of processing but if we are being honest here there is very little chance it will have any impact on the performance of your game. Frankly I just feel it gives you fewer lines as you can group the conditions.

    It looks like you should add a global array that holds the dice totals (since rule 3 seems to apply to more than one roll). Other than that you just seem to need to add up the dice rolls (adding +1 to each since you are using the dice rolls as array indexes) and then running them through a set of if/else statements. For functionality you would probably set it up similar to:
    Code:
    if(some_condition_here) {
      // Stuff
    } else if(the_second_condition) {
      // More stuff
    } else if(the_third_condition) {
      // Even more stuff
    } else {
      // Catch any weird cases that weren't in your rules?
    }
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Mar 2014
    Posts
    2
    Thanks! I have been working on the logic and it's working well so far, I just got stuck on how to count the wins & losses, appreciate any feedback.
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Craps Game</title>
    </head>
    <head>
    <script>
    
    //Name: Michael Manning
     
     var dicefaces = new Array;
     dicefaces[0] = new Image(50,50);
     dicefaces[0].src = "images/d1.gif";
     dicefaces[1] = new Image(50,50);
     dicefaces[1].src = "images/d2.gif";
     dicefaces[2] = new Image(50,50);
     dicefaces[2].src = "images/d3.gif";
     dicefaces[3] = new Image(50,50);
     dicefaces[3].src = "images/d4.gif";
     dicefaces[4] = new Image(50,50);
     dicefaces[4].src = "images/d5.gif";
     dicefaces[5] = new Image(50,50);
     dicefaces[5].src = "images/d6.gif";
    
    var playerturn;
     playerturn = true;
    var pointvalue;
    document.game.point.value = pointvalue;
    
    
    /////////////////////////////////
    
    function throwdice() {
    //create a random integer between 0 and 5
    var genvalue;
    var randomdice1;
    var randomdice2;
    var sum;
    var playerwin;
    var playerloss;
    genvalue=Math.floor(Math.random()*6);
    randomdice1 = genvalue+1;
    document.dieimage1.src=dicefaces[genvalue].src;
    genvalue=Math.floor(Math.random()*6);
    randomdice2 = genvalue+1;
    document.dieimage2.src=dicefaces[genvalue].src;
    sum = randomdice1 + randomdice2;
    
    if (playerturn) {
     document.game.point.value = "";
     switch (sum) {
      case 7:
      case 11:
        document.game.status.value = "You win! Throw Again!";
        break;
      case 2:
      case 3:
      case 12:
        document.game.status.value = "You lose. Throw Again!";
        break;
      default:
        playerturn = false;
        pointvalue = sum;
        document.game.point.value = pointvalue;
        document.game.status.value ="Throw again, hit your point & win.";
      }
    }
    else {
      switch (sum) {
      case 7:
        document.game.status.value = "You lose. Throw Again!";
        playerturn = true;
        break;
      case pointvalue:
        document.game.status.value = "You win. Throw Again!";
        playerturn = true;
      }
     }
    }
    
    //set timeout function
    function timer() {
    	setTimeout(function() { throwdice() }, 1000);
    }
    
    function endgame(){
    
    }
    
    </script>
    </head>
    
    <body>
    
    <img src="images/d1.gif" name="dieimage1" id="mydice">
    <img src="images/d1.gif" name="dieimage2" id="mydice">
    <br>
    <a href="" onClick="timer(); return false;">Click to throw the dice.</a>
    
    <form name="game">
    Status <input name="status" value="" size=35>
    <br/>
    Point value <input name="point" value="" size=4>
    <br/>
    Win total <input name="win" value="" size=4>
    Loss total <input name="loss" value="" size=4>
    </form>
    
    <a href="" onClick="endgame(); return false;">Click to end the game.</a>
    
    
    </body>
    </html>

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