Thread: Dice Game

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Dice Game


    Hello, I am having yet one issue I really need to resolve and I assume its pretty easy, but it's been one of those days. I need to be able to count my wins and losses and I'm having such a difficult time with this. I included what I thought would work below, but if you have any reference where I went wrong please let me know.

    Thanks for the help in advance.


    Code:
    #!/usr/bin/perl
    print "Content-type:text/html\n\n";
    use CGI ':standard';
    
    print start_html(-title=>"Dice Game",
            -background=>"dicenew.jpg");
    print end_html;
    $win=param('win');
    $loss=param('loss');
    $roll=param('roll');
    $dice1=int(rand(6)+1);
    $dice2=int(rand(6)+1);
    
    if($roll==1){
    open (DATA, '>roll.txt');
    print "$dice1|$dice2";
    }else{
    open (DATA, 'roll.txt');
    while (<DATA>){
    ($froll1,$froll2)=split(/\|/);
    }
    close DATA;
    }
    
    $dice1>=3;
    $dice2>=5;
    $froll1>=3;
    $froll2>=5;
    
    if ($roll!=1){
    print" <center> roll1 $froll1--$froll2</center>";
    }
    
    if(($dice1+$dice2==7)or($dice1+$dice2==11)){$win++;
    $result="You LOSE!--<a href=http://www.sample.org/scgi-bin/dice.html>Roll Again</a>";
    }elsif (($dice1+$dice2==2)or($dice1+$dice2==12)){$loss++;
    $result="You WIN!--<a href=http://www.sample.org/scgi-bin/dice.html>Roll Again</a>";
    }else{
    $result="<a href=dice.cgi>Roll Again?</a>"
    }
    
    sub hf1{
    print "
    Your wins $win
    Your losses $loss
    <form method=post action=dice.cgi >
    <input type=hidden name=win value='$win' />
    <input type=hidden name=loss value='$loss' />
    </form>
    ";
    }
    
    print" <center><font face= Gabriola size=6 color=black>Dice Game<hr>
    
    <table>
    <tr>
    
    <td><img src=http://www.sample.org/scgi-bin/pix/dice$dice1.gif>
    <td><img src=http://www.sample.org/scgi-bin/pix/dice$dice2.gif>
    </tr>
    </table>
    
    $result
    ";
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    To directly answer your question, your code doesn't do anything with the $win or $loss variables. You've defined a subroutine (called hf1) that's supposed to print out a form that records them, but it doesn't get called anywhere.

    I'd have a few more general tips for you also:
    - Indenting your code properly makes it much easier to read: for yourself, for anyone you collaborate with and also for us on forums who are trying to read it
    - When you try printing to the roll.txt file, you forgot to print to the DATA filehandle.
    - You don't declare any of your variables. In its default mode, perl will allow this, but it's bad practice. If you acidentally used variables called $froll1 and $frol11, they'd look very similar with many fonts, but they're totally different variables. A very hard bug to track down in larger programs.
    - You can ask perl to force you to get into good habits by adding "use strict;" and "use warnings;" to the top of your program (just after the first line would be a good place).
    - You're using the CGI module for some things but not others: better to be consistent (e.g. print header()).
    - You've defined a subroutine in the middle of your code. Another thing that makes it harder to figure out what's going on. Generally, put subroutines in their own section at the end.
    - You're printing the end of the HTML document (end_html) before the middle!
    - There's a section in the middle that doesn't do anything! $dice1>=3; etc.

    That's probably enough for now

    Comments on this post

    • Laurent_R agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0
    Could you guide my way of direction to be able to collect a win/loss column?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    I haven't tried your code (I am traveling this week and have no way to test code), but I suspect that if you include the following directives in your code:

    Perl Code:
    use strict;
    use warnings;


    you will get from the compiler error messages and warnings pointing at a number of errors or useless constructs.

    Just as an example:

    Code:
    $dice1>=3;
    $dice2>=5;
    $froll1>=3;
    $froll2>=5;
    appear to be completely useless. It is useful to compare two values if you make a decision based on that comparison. Here it looks like you are saying "compare and do nothing". I would think the compiler would probably warn you on this if you turn on strict and warnings pragmas.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0
    Thanks for recognizing that, I did see that myself and I just left it because I am editing an old code I had and I just want to touch it up.

    Not sure if I should of kept it or deleted it.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    Originally Posted by CGI newb
    Thanks for recognizing that, I did see that myself and I just left it because I am editing an old code I had and I just want to touch it up.

    Not sure if I should of kept it or deleted it.
    As it is, I do not see much sense to keep it, as it does nothing. Now, if these comparisons have a sense in what you are trying to do, then you need to change them and do something on the basis of the comparisons' results.

    I can't tell, since you did not really say what you want to do and how.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0
    The only thing that I really wanted to accomplish was to count the number of wins and losses after each match.

IMN logo majestic logo threadwatch logo seochat tools logo