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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Need help with my code for a game of Craps

    >w< ello ello...I do not know what I'm doing wrong... >.< any help possible i'd feel really grateful :3. I'm in an intro to computer programming class XD so i do not know much for coding in javascript.

    The player’s initial roll is known as his point. The point can be an immediate winner (7 or 11) or an immediate loser (2 or 3). If the player’s point does not equal one of these game-ending totals, then the player must continue rolling until he either rolls his point again (a winner) or obtains a 7 (a loser).

    For example, the following sequence of rolls would cause the player to win:

    Initial roll: 6 (player’s point)
    Next roll: 10
    Next roll: 4
    Next roll: 11
    Next roll: 6
    WINNER!

    When the user clicks a button, this event should call a function that rolls the dice until the player wins or loses. When implementing this function, use the below pseudocode as a guide. The value of each roll should be displayed in a text area, along with a message indicating whether the player won or lost (like in the example above).

    this is what i have so far :O~

    Code:
    <html>
    <!-- craps.html                  Dave Reed -->
    <!-- This page stimulates a game of craps. -->
    <!-- ===================================== -->
    
    <head>
       <title> A Game of Craps </title>
    
       <script type="text/javascript">
        function Craps()
          //Assumes: Text area is avaliable for displaying results
          //Results: displays the value of each roll, display a message whether the play won or lost
         
          {
            var roll1, roll2, nextRoll, myPoint;
            
            roll1 = RandomInt(1, 6);
            roll2 = RandomInt(1, 6);
            myPoint = roll1 + roll2;
            myPoint = parseFloat(myPoint);
    
            document.getElementById("outputArea").value = "Initial Roll: " + myPoint + "\n";
            if (myPoint == 7 || myPoint == 11) {
             document.getElementById("outputArea").value = document.getElementById("outputArea").value + "WINNER!" + "\n";
               }
             else if (myPoint == 2 || myPoint == 3) {
              document.getElementById("outputArea").value = document.getElementById("outputArea").value + "Loser!" + "\n";
             }
    
               else {
                 while (myPoint != 7 || myPoint != nextRoll) {
                   roll1 = RandomInt(1, 6);
                   roll2 = RandomInt(1, 6);
                   myPoint = roll1 + roll2;
                   nextRoll = parseFloat(myPoint);
               document.getElementById("outputArea").value = document.getElementById("outputArea").value + "Next Roll " + myPoint + "\n";
               }
                if (myPoint == nextRoll) {
                document.getElementById("outputArea").value = document.getElementById("outputArea").value + "WINNER!" + "\n";
                }
                 if (nextRoll == 7){
                 document.getElementById("outputArea").value = document.getElementById("outputArea").value + "LOSER!" + "\n";
                }
              }
           }
       </script>
       </head>
      <body>
        <div style="text-align:center">
        <h2> A Game of Craps </h2>
        <p>
          <input type="button" value="Press to play a game of Craps" onclick="Craps();" />
        </p>
        <p>
        <textarea id="outputArea" rows="15" cols="20"> </textarea>
        </p>
       </div>
      </body>
    </html>

    Thank you :3
    Last edited by seulki; 12-11-2010 at 10:52 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Have you tried using the Search feature of this forum? If so you would have found several threads, including e.g.

    Simple Game of Craps


    roll1 = RandomInt(1, 6);
    roll2 = RandomInt(1, 6);
    myPoint = roll1 + roll2;
    myPoint = parseFloat(myPoint);

    I do not see any function RandomInt(). And as roll1 and roll2 (ought to) return integers why have you changed myPoint into a floating point number?

    If you assign nextRoll = myPoint then myPoint will always = nextRoll.

    while (myPoint != 7 && myPoint != nextRoll) {

    BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.



    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 12-11-2010 at 08:47 AM.

  • Users who have thanked Philip M for this post:

    seulki (12-14-2010)

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you~ ^^ i figured it out~


  •  

    Posting Permissions

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