Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 25th, 2012, 01:53 PM
CGI newb CGI newb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 4 CGI newb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 18 sec
Reputation 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
";

Reply With Quote
  #2  
Old September 25th, 2012, 05:28 PM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Sep 2001
Location: Shanghai, An tSín
Posts: 6,894 ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 14 m 53 sec
Reputation Power: 3885
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!

Reply With Quote
  #3  
Old September 25th, 2012, 06:47 PM
CGI newb CGI newb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 4 CGI newb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 18 sec
Reputation Power: 0
Could you guide my way of direction to be able to collect a win/loss column?

Reply With Quote
  #4  
Old September 25th, 2012, 08:02 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 502 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 18 h 47 m 12 sec
Reputation Power: 385
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:
Original - Perl Code
  1. use strict;
  2. 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.

Reply With Quote
  #5  
Old September 25th, 2012, 08:07 PM
CGI newb CGI newb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 4 CGI newb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 18 sec
Reputation 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.

Reply With Quote
  #6  
Old September 25th, 2012, 09:37 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 502 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 18 h 47 m 12 sec
Reputation Power: 385
Quote:
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.

Reply With Quote
  #7  
Old September 26th, 2012, 12:21 AM
CGI newb CGI newb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 4 CGI newb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 18 sec
Reputation Power: 0
The only thing that I really wanted to accomplish was to count the number of wins and losses after each match.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Dice Game

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap