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 6 of 6
  1. #1
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post

    This code does not work..

    For some reason it does not work. It should when a player value is = NaN than the players NaN value should be equal to the players value before he was NaN
    Code:
    <script type="text/javascript">
    function start(){
    var t=setTimeout("roll()",500);
    var t=setTimeout("p11()",1000);
    var t=setTimeout("roll()",3001);
    var t=setTimeout("p12()",4002);
    var t=setTimeout("roll()",5003);
    var t=setTimeout("p13()",6004);
    var t=setTimeout("roll()",7005);
    var t=setTimeout("p14()",8006);
    var t=setTimeout("roll()",9006);
    var t=setTimeout("start()",9007);
    }
    
    function roll(){
    var a = randoma=Math.floor(Math.random()*7);
    var b = randomb=Math.floor(Math.random()*7);
    document.getElementById("r1").value = a;
    document.getElementById("r2").value = b;
    }
    </script>
    <script type="text/javascript">
    function p11(){
    if (document.getElementById("r1").value == document.getElementById("r2").value )
      {
    document.getElementById("n1").value == document.getElementById("p1").value ;
    document.getElementById("p1").value = "NaN";
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
      }
    if (document.getElementById("r1").value < document.getElementById("r2").value )
      {
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
      }
    if (document.getElementById("r1").value > document.getElementById("r2").value )
      {
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
      }
      }
        </script>
      <script type="text/javascript">
      
      function p12(){
     if (document.getElementById("r1").value == document.getElementById("r2").value )
      {
      document.getElementById("n2").value == document.getElementById("p2").value ;
    document.getElementById("p2").value = "NaN";
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
      }
    if (document.getElementById("r1").value < document.getElementById("r2").value )
      {
    
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
      }
     if (document.getElementById("r1").value >  document.getElementById("r2").value )
      {
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
      } 
     }
        </script>
      <script type="text/javascript">
      
      function p13(){
      if (document.getElementById("r1").value == document.getElementById("r2").value )
      {
      document.getElementById("n3").value == document.getElementById("p3").value ;
    document.getElementById("p3").value = "NaN";
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
      }
    if (document.getElementById("r1").value < document.getElementById("r2").value )
      {
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
      }
    if (document.getElementById("r1").value > document.getElementById("r2").value )
      {
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
      } 
      }
      </script>
      <script type="text/javascript">
      
      function p14(){
     if (document.getElementById("r1").value == document.getElementById("r2").value )
      {
    document.getElementById("n4").value == document.getElementById("p4").value ;
    document.getElementById("p4").value = "NaN";
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p3").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
    document.getElementById("p1").value ++;
      }
    if (document.getElementById("r1").value < document.getElementById("r2").value )
      {
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
      }
    if (document.getElementById("r1").value > document.getElementById("r2").value )
      {
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
    document.getElementById("p4").value ++;
      } 
       
    }
    </script>
    Code:
    Player 1 $<input type="text" value="0" id="p1" size="1" style="background-color:transparent;border:0px solid white;"Readonly />
    <br/>
    Player 2 $<input type="text" value="0" id="p2" size="1" style="background-color:transparent;border:0px solid white;"Readonly />
    <br/>
    Player 3 $<input type="text" value="0" id="p3" size="1" style="background-color:transparent;border:0px solid white;"Readonly />
    <br/>
    Player 4 $<input type="text" value="0" id="p4" size="1" style="background-color:transparent;border:0px solid white;"Readonly />
    <br/>
    <input type="button" value="Roll" onclick="start()"/>
    <br/>
    <input type="hidden" value="0" id="r1" size="1" />
    
    <input type="hidden" value="0" id="r2" size="1" />
    
    Player 1 NaN $<input type="text" value="0" id="n1" size="1" style="background-color:transparent;border:0px solid white;"Readonly />
    <br/>
    Player 2 NaN $<input type="text" value="0" id="n2" size="1" style="background-color:transparent;border:0px solid white;"Readonly />
    <br/>
    Player 3 NaN $<input type="text" value="0" id="n3" size="1" style="background-color:transparent;border:0px solid white;"Readonly />
    <br/>
    Player 4 NaN $<input type="text" value="0" id="n4" size="1" style="background-color:transparent;border:0px solid white;"Readonly />

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,105
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Question

    What exactly are the rules of the game?
    What is supposed to be happening and what does or does not work?

    Also, why the following:
    Code:
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    document.getElementById("p2").value ++;
    and not just:
    Code:
    document.getElementById("p2").value = parseInt(document.getElementById("p2").value)+5;
    And, what is the intent of the start() function?
    I understand the rolls and calls to the functions, but why not just:
    Code:
    var t;     // set outside start() function so it could be use in a "clearTimeout()" function if desired.
    function start(){
      roll(); p11();
      roll(); p12();
      roll(); p13();
      roll(); p14();
      t=setTimeout("start()",9007);
    }
    Last edited by jmrker; 10-09-2011 at 01:19 AM.

  • #3
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post
    The rules are each player roles the dice if the first dice is greater than the second then that player gets 5 points and if the second dice is greater than the first than that player gets 10 points. If they are equal that player loses. What dose not work is when a player loses i would like the input boxes n1, n2, n3 and n4 should be equal to that players value before they last.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,105
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb Something to try ...

    Quote Originally Posted by sherlockturtle View Post
    The rules are each player roles the dice if the first dice is greater than the second then that player gets 5 points and if the second dice is greater than the first than that player gets 10 points. If they are equal that player loses. What dose not work is when a player loses i would like the input boxes n1, n2, n3 and n4 should be equal to that players value before they last.
    I can understand adding points, but I don't understand the loss condition. Do you want to subtract points from the n1...n4 values? If they are equal to the last value, you would just need to add zero, but you don't specify what to add or subtract when the die are equal.

    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    // From: http://www.codingforums.com/showthread.php?p=1145212#post1145212
    
    var t;  // better to make global if you decide later to add stop button: clearTimeout(t);
    function start(){
      roll();  p11();
      roll();  p12();
      roll();  p13();
      roll();  p14();
      t = setTimeout("start()",1000);  // for automatic rolling of dice???
    }
    function stop() { clearTimeout(t); }
    
    function roll(){
      document.getElementById('r1').value = Math.floor(Math.random()*7);
      document.getElementById('r2').value = Math.floor(Math.random()*7);
    }
    
    function addTo(IDS,value,NDS) {
      var info = parseInt(document.getElementById(IDS).value);
      document.getElementById(IDS).value = info+value;
      document.getElementById(NDS).value = info+value;
    // following does not do much since the value is zero for this condition ???
    //  if (value == 0) { document.getElementById(NDS).value = info-value; }
    }
    
    function p11() {
      var dice1 = document.getElementById('r1').value;
      var dice2 = document.getElementById('r2').value;
      if (dice1 == dice2) { addTo('p1',0,'n1'); alert('p1 equal');  
      } else { if (dice1 < dice2) { addTo('p1',5,'n1'); } else { addTo('p1',10,'n1'); } }
    }
    
    function p12(){
      var dice1 = document.getElementById('r1').value;
      var dice2 = document.getElementById('r2').value;
      if (dice1 == dice2) { addTo('p2',0,'n2');  alert('p2 equal');
      } else { if (dice1 < dice2) { addTo('p2',5,'n2'); } else { addTo('p2',10,'n2'); } }
    }
    
    function p13(){
      var dice1 = document.getElementById('r1').value;
      var dice2 = document.getElementById('r2').value;
      if (dice1 == dice2) { addTo('p3',0,'n3'); alert('p3 equal');
      } else { if (dice1 < dice2) { addTo('p3',5,'n3'); } else { addTo('p3',10,'n3'); } }
    }
    
    function p14(){
      var dice1 = document.getElementById('r1').value;
      var dice2 = document.getElementById('r2').value;
      if (dice1 == dice2) { addTo('p4',0,'n4'); alert('p4 equal');
      } else { if (dice1 < dice2) { addTo('p4',5,'n4'); } else { addTo('p4',10,'n4'); } }
    }
    </script>
    
    </head>
    <body>
    Player 1 $<input type="text" value="0" id="p1" size="1"
     style="background-color:transparent;border:0px solid white;"Readonly /><br/>
    Player 2 $<input type="text" value="0" id="p2" size="1"
     style="background-color:transparent;border:0px solid white;"Readonly /><br/>
    Player 3 $<input type="text" value="0" id="p3" size="1"
     style="background-color:transparent;border:0px solid white;"Readonly /><br/>
    Player 4 $<input type="text" value="0" id="p4" size="1"
     style="background-color:transparent;border:0px solid white;"Readonly /><br/>
    
     <input type="button" value="Auto Roll" onclick="start()"/>
     <input type="button" value="Stop" onclick="stop()"/>
     <br/>
     <input type="hidden" value="0" id="r1" size="1" />
     <input type="hidden" value="0" id="r2" size="1" />
    
    Player 1 $<input type="text" value="0" id="n1" size="1"
     style="background-color:transparent;border:0px solid white;"Readonly /><br/>
    Player 2 $<input type="text" value="0" id="n2" size="1"
     style="background-color:transparent;border:0px solid white;"Readonly /><br/>
    Player 3 $<input type="text" value="0" id="n3" size="1"
     style="background-color:transparent;border:0px solid white;"Readonly /><br/>
    Player 4 $<input type="text" value="0" id="n4" size="1" 
     style="background-color:transparent;border:0px solid white;"Readonly />
    </body>
    </html>

  • #5
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post
    if on p1's turn the die are equal p1 = NaN and the n1 would be equal to p1's value before p1 was equal to NaN

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,105
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Exclamation

    Quote Originally Posted by sherlockturtle View Post
    if on p1's turn the die are equal p1 = NaN and the n1 would be equal to p1's value before p1 was equal to NaN
    NaN represents "Not A Number".
    Usually means invalid numerical input.

    You are not letting the user enter any input, so there can be no user error.
    The random() function only returns numbers.

    Therefore, in your script, there should never be a condition when p1 equals NaN.


  •  

    Posting Permissions

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