By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,259 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

PERL Craps Game

P: 17
This is my script it works fine but it comes out on my webpage as just text how can i change this around and make it so if i like click a button a 2 different numbers come up almost like the 2 dice and remember how much i make each roll. Is there anyway anyone can help me? I can show you what it looks like in the browser right now if you ask i can show you if that will help. Thanks i really need this because its a project i have to get finished. If anyone i mean anyone can help me i will be greatful.Thanks

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. # How many games would you like to roll?
  4. $iterations = 1000;
  5.  
  6. for ($count = $iterations; $count >= 1; $count--) {
  7.  
  8. print "$count \n";
  9.  
  10. # Bankroll per game. Rules are set up for a $5 bet on a 3x/4x/5x table.
  11. $bankroll = 200;
  12.  
  13. $rollcount = 0;
  14. $high = 200;
  15.  
  16. $itson = 1;
  17.  
  18. while ($itson == 1) {
  19.  
  20. $die1 = int(rand() * 6) + 1;
  21. $die2 = int(rand() * 6) + 1;
  22. $roll = $die1 + $die2;
  23. $rollcount = $rollcount + 1;
  24.  
  25. if ($roll == 7 || $roll == 11) {
  26. $bankroll = $bankroll + 5;
  27. # print "W $roll $bankroll $rollcount\n";
  28. if ($bankroll > $high) {
  29. $high = $bankroll;
  30. }
  31. }
  32.  
  33. elsif ($roll == 2 || $roll == 3 || $roll == 12) {
  34. $bankroll = $bankroll - 5;
  35. # print "L $roll $bankroll $rollcount\n";
  36. }
  37.  
  38. else {
  39. $setnum = $roll;
  40. $notcrapped = 1;
  41. while ($notcrapped == 1) {
  42.  
  43. $die1 = int(rand() * 6) + 1;
  44. $die2 = int(rand() * 6) + 1;
  45. $roll = $die1 + $die2;
  46. $rollcount = $rollcount + 1;
  47.  
  48. if ($roll == $setnum) {
  49. $bankroll = $bankroll + 35;
  50. # print "W $roll $bankroll $rollcount\n";
  51. if ($bankroll > $high) {
  52. $high = $bankroll;
  53. }
  54. $notcrapped = 0;
  55. }
  56.  
  57. if ($roll == 7) {
  58.  
  59. if ($setnum == 4 || $setnum == 10) {
  60. $bankroll = $bankroll - 20;
  61. }
  62.  
  63. if ($setnum == 5 || $setnum == 9) {
  64. $bankroll = $bankroll - 25;
  65. }
  66.  
  67. if ($setnum == 6 || $setnum == 8) {
  68. $bankroll = $bankroll - 30;
  69. }
  70.  
  71. # print "L $roll $bankroll $rollcount\n";
  72. $notcrapped = 0;
  73. }
  74. }
  75. }
  76.  
  77. # Game ends when you have less than $5
  78. if ($bankroll < 5) {
  79. $losses = $losses + 1;
  80. $itson = 0;
  81. }
  82.  
  83. # It also ends if youmake $1000
  84. if ($bankroll >= 1000) {
  85. $wins = $wins + 1;
  86. $itson = 0;
  87. }
  88.  
  89. }
  90.  
  91. if ($high > $biggest) {
  92. $biggest = $high;
  93. }
  94.  
  95. if ($rollcount > $mostrolls) {
  96. $mostrolls = $rollcount;
  97. }
  98.  
  99. $bigrollcount = $bigrollcount + $rollcount;
  100. $bighigh = $bighigh + $high;
  101.  
  102. }
  103.  
  104. print "$iterations Iterations \n";
  105. print "Ave Rollcounts: " . sprintf("%.3f", $bigrollcount/$iterations) . "\n";
  106. print "Ave Rollcounts Hours: " . sprintf("%.3f", int($bigrollcount/$iterations)/180) . "\n";
  107. print "Most Rollcounts: $mostrolls\n";
  108. # Most Rollcounts Days assumes 3 rolls/minute.
  109. print "Most Rollcounts Days: " . sprintf("%.3f", ($mostrolls/180)/24) ."\n";
  110. print "Ave Most Won: " . ($bighigh/$iterations) . "\n";
  111. print "Biggest Win: $biggest\n";
  112. print "$wins Wins | $losses Losses";
  113.  
  114.  
Nov 21 '09 #1
Share this Question
Share on Google+
18 Replies


P: 17
no one can help me with what im looking for? i want kind of like a form so when i click something 2 numbers come up and according to the rules if i get a certain number i win and if i dont i lose. Also something that will keep track of how much i win and if i get to $1000 game over and if i get to $5 dollars the game is over.
Nov 23 '09 #2

numberwhun
Expert Mod 2.5K+
P: 3,503
First, please be patient. I know you posted this on Saturday, but considering everyone here are volunteers, people tend to do other things on the weekend.

As far as your script goes, if you are wanting this to print to your browser, then you need to make sure that your web server is setup to execute Perl scripts as cgi. You should reference documentation for whatever web server you are using.

I don't know what extension you have on your script (whether it is .pl or .cgi), but if you don't tell your web server to execute it, it will just display the script as text.

There are probably some Perl CGI tutorials you can reference as well for learning Perl CGI.


Regards,

Jeff
Nov 23 '09 #3

P: 17
This is the site: http://ctech.smccme.edu/~rhodge/cw/c...i-bin/craps.pl

i want like some type of button or something so when i click it the two numbers come up resembling a dice and then it shows me if i win or lose and if i win i get money and if i lose i lose moeny thats as simple as it needs to be.

How do i tell it to execute it? Im like running my own web server so.
Nov 23 '09 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
I hate to say this, but this sounds too much like a homework problem for me to just give you the code. You are going to have to do the legwork. If you do not know how to display information in the browser from Perl, I again suggest you go through a CGI tutorial.

As for displaying dice for the numbers, you will need pics of each side in order to do that and display the correct dice based on the numbers rolled.

Regards,

Jeff
Nov 24 '09 #5

P: 17
Well the script i have up top i now know is only for shell command line program. But for just wanting a button and when i click it the pictures change and by that number either i win or lose. Im just trying to figure out how to even start this. Thanks for replying jeff
Nov 24 '09 #6

numberwhun
Expert Mod 2.5K+
P: 3,503
What you are looking to do in order to produce the output you are looking for, is to write some CGI code. With the CGI module, you can produce HTML output to have displayed in a browser. Using that code, you can write Perl code to input the name of an image depending on the number rolled. (That's just an idea, btw, on how to do the dice pictures).

I would definitely go over to CPAN and learn the CGI module. You will then understand how to create a web page with Perl.

Regards,

Jeff
Nov 24 '09 #7

P: 17
Ya i'd have to save images of all sides of the dice and depending on what get's rolled it can tell me if i win or lose thats what im looking for your on the right topic but where would i input the "button" and images into this code
Nov 24 '09 #8

numberwhun
Expert Mod 2.5K+
P: 3,503
CGI outputs HTML. Do you know HTML? If not, I suggest a tutorial on that as well.

Regards,

Jeff
Nov 25 '09 #9

P: 17
If this helps i want something just like this..

http://ctech.smccme.edu/~zeke/cw/save/craps/craps.pl

if you want the code i have as of right now and you think you can help me a little at a time i'd appreciate it.
Dec 3 '09 #10

numberwhun
Expert Mod 2.5K+
P: 3,503
The script that you showed more than likely outputs the html using a CGI module. What you are asking to do should not be that hard, but will require a little work. The hardest part is the processing behind the scenes.

But, you didn't tell us whether this is homework or a class project? They usually assign remedial projects like these to get you used to coding and concepts. Is this homework or a class project?

Regards,

Jeff
Dec 3 '09 #11

P: 17
Project why? I'll show you my code if you want and maybe then you can help me
Dec 3 '09 #12

P: 17
Im not quite done with the calculations yet or if i even need them?


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use CGI ':standard';
  3. use CGI::Carp "fatalsToBrowser";
  4.  
  5. print header;
  6.  
  7. &form;
  8. &input;
  9. &cal;
  10.  
  11. ######sub routines#########
  12.  
  13. sub form{
  14.  
  15. print"
  16.  
  17. <form method=post action=craps.pl>
  18. <input type=hidden name=dice1 value=dice1>
  19. <input type=hidden name=dice2 value=dice2>
  20. <input type=hidden name=total value=total>
  21. <input type=hidden name=ctrl value=crtl>
  22. <input type=submit value='Throw Again'>
  23. </form>
  24.  
  25. <img src=images/1.gif><img src=images/3.gif><hr>
  26. <img src=images/blank.gif><img src=images/blank.gif>
  27.  
  28. ";
  29.  
  30. ######param###############
  31.  
  32. sub input{
  33.  
  34. $dice1=param('dice1');
  35. $dice2=param('dice2');
  36. $total=param('total');
  37. $ctrl=param('ctrl');
  38.  
  39. }
  40.  
  41. #######calculations########
  42.  
  43. sub cal{
  44.  
  45. ($dice1+$dice2);
  46.  
  47. }
Dec 3 '09 #13

Expert Mod 100+
P: 589
numberwhun FYI this Q is cross posted in several other forums.

http://codingforums.com/showthread.php?t=183612
http://forums.devshed.com/perl-progr...me-655550.html
http://forums.devshed.com/perl-progr...me-657929.html

and probably several others

BTW, I'm aka FishMonger
Dec 3 '09 #14

P: 17
and yet no one has helped me yet
Dec 4 '09 #15

Expert Mod 100+
P: 589
We've helped, but it appears to me that you're looking for someone to do your assignment for you.

You're in a college course. One would assume that you would have learned enough study skills to take the info that we've given you and work out some of the details on your own, test some code, and then ask followup questions on the parts you don't understand.

Your original question on devshed included a complete, and I assume, working script that was designed as a console program. You can take that script and with little effort alter the print statements for use in a web version.
Dec 4 '09 #16

P: 17
Im not using that script anymore i have made my own i just need to know how when i click the submit button to make the 2 images change through 6 random ones.
Dec 4 '09 #17

numberwhun
Expert Mod 2.5K+
P: 3,503
@RonB Thanks for letting me know about those other postings. They certainly answered a question or two that have gone unanswered. Also Ron, please check your PMs.

@Rhodge09 From looking at the other links, I see you admitted in another forum that this is definitely school work. If you read the forum rules, you will see that you cannot expect answers to homework/school work. But, we will certainly guide you in the right direction.

That said, I see your code and it looks like you are off to a start. As far as the calculations go, if you don't write them, Perl won't do it for you. This code should include all it needs to run. If you already had a working script on the command line, then you just need to add the CGI parts to it to get it to where you want.

(By the way, what's wrong with command line output programs? :) )

Anywho, try to incorporate your code and if you have any issues, please ask questions. Also, do not forget to "use strict;" and "use warnings;".

Regards,

Jeff
Dec 4 '09 #18

P: 1
I predict Rhodge drops the class and switches majors before the year is over. Can't handle perl scripting? Come now! Show some effort!
Dec 11 '09 #19

Post your reply

Sign in to post your reply or Sign up for a free account.