#!/usr/bin/perl
# PERLotto 1.3
# Your Customizable Lotto Ticket Checker.
#
# Copyright (c) 2002, 2003 Andy Burns. All rights reserved.
# Email yoy@comcast.net with questions/answers
##### THIS CODE IS NOT PRETTY BUT IT WORKS.
# Fixed in version 1.3:
# Added missing games file error cheking.
# Fixed in version 1.2:
# Don't print "--0" for no-hit games
# Cosmetic print statement adjustments.
# Added missing "official" bonus winning number error checks.
# Fixed in version 1.1:
# Print to screen instead of file.
# Read or setup 'perlotto.cfg" variables
SETOPTIONS:
open (IN ,"<perlotto.cfg") or goto SETTINGS;
@a = ("");
while (<IN>) {
chomp;
$a[$.-1]= $_;
}
close (IN) || die "Error closing config file.\n";
$lotto_Max_Number = $a[0];
$picks_Per_Game = $a[1];
$bonus_Ball_On = $a[2];
$bonus_Max_Number = $a[3];
$bonus_Ball = 0;
goto MAINMENU;
SETTINGS:
print "\n\n-- SETTINGS --\n\n";
print "\n Does your lotto use a \"bonus ball\"? (y/n): ";
chomp($bonus_Ball_On = <STDIN>);
$bonus_Ball_On = lc(substr ($bonus_Ball_On,0,1));
if ($bonus_Ball_On eq 'y') {
print "\n What's the highest bonus number allowed? ";
chomp($bonus_Max_Number = <STDIN>);
} else {
$bonus_Max_Number = 0;
}
print "\n How many numbers are in your main lotto game? ";
chomp($picks_Per_Game = <STDIN>);
print "\n What's the highest number allowed? ";
chomp($lotto_Max_Number = <STDIN>);
if ($bonus_Ball_On eq 'y') {
$picks_Per_Game = $picks_Per_Game+1;
}
open (OUT, ">perlotto.cfg");
print OUT "$lotto_Max_Number\n";
print OUT "$picks_Per_Game\n";
print OUT "$bonus_Ball_On\n";
print OUT "$bonus_Max_Number\n";
close OUT || die "Couldn't close 'perlotto.cfg file.\n";
MAINMENU:
goto SETOPTIONS if ($lotto_Max_Number eq undef);
print "\n\n-- MAIN MENU --\n\n";
print " 1 Check games.\n\n";
print " 2 Setup Lotto rules.\n\n";
print " Selection: ";
chomp ($choice = <STDIN>);
$choice = substr($choice,0,1);
if ($choice == 1) {
goto BEGINPLAY; }
elsif ($choice == 2) {
goto SETTINGS; }
else {
print " ALERT: Answer not understood. Try again.\n";
goto MAINMENU;
}
BEGINPLAY:
print "\n Enter winning numbers separated by spaces: ";
chomp ($input = <STDIN>);
@winners = (split '\s+', $input);
foreach(@winners) {
if (/\D/) {
print " ALERT: Non-numeric entry, $_. Try again.\n";
goto BEGINPLAY;
}
if (@winners < $picks_Per_Game) {
print " ALERT: Missing ", $picks_Per_Game-@winners, " numbers. T
+ry again.\n";
goto BEGINPLAY;
}
if (@winners > $picks_Per_Game) {
print " ALERT: Extra ", @winners-$picks_Per_Game, " numbers. Try
+ again.\n";
goto BEGINPLAY;
}
}
if ($bonus_Ball_On eq 'y') {
$bonus_Ball = pop(@winners);
}
foreach my $i (0 .. $#winners) {
foreach my $j ($i + 1 .. $#winners) {
if ($winners[$i] == $winners[$j]) {
print " ALERT: Duplicate number '$winners[$i]'. Try agai
+n.\n";
goto BEGINPLAY;
}
}
}
if ($bonus_Ball_On eq 'y') {
push (@winners,$bonus_Ball);
}
if ($bonus_Ball_On eq 'y') {
$bonus_Ball = pop(@winners);
foreach (@winners) {
if ($_ > $lotto_Max_Number) {
print "n ALERT: Number '$_' is above Lotto max: $lotto_M
+ax_Number.\n";
goto BEGINPLAY;
}
}
if ($bonus_Ball > $bonus_Max_Number) {
print "\n ALERT: Number '$bonus_Ball' is above the Bonus max
+: $bonus_Max_Number.\n";
goto BEGINPLAY;
}
if ($bonus_Ball < 1) {
print "\n ALERT: Bonus number is less than 1. Try again.\n";
goto BEGINPLAY;
}
if ($bonus_Ball eq /\D/) {
print "\n ALERT: Non-numeric bonus number $_. Try again.
+\n";
goto BEGINPLAY;
}
}
foreach (@winners) {
if ($_ > $lotto_Max_Number) {
print "\n ALERT: Number '$_' is above Lotto max: $lotto_
+Max_Number.\n";
goto BEGINPLAY;
}
}
open(IN ,"<games.txt") or die "Can't open 'games' file: $!\n";
# open(OUT, ">results.txt");
system("cls");
if ($bonus_Ball_On eq 'y') {
$short = $picks_Per_Game - 1;
print "\nLotto: $short/$lotto_Max_Number";
} else {
print "\nLotto: $picks_Per_Game/$lotto_Max_Number";
}
if ($bonus_Ball_On eq 'y') {
print " + 1/$bonus_Max_Number\n";
} else {
print "\n";
}
print "\nWinning numbers: @winners";
if ($bonus_Ball_On eq 'y') {
print " + $bonus_Ball\n\n";
} else {
print "\n";
}
while (<IN>) {
if (/^\//|/^$/) {
print ;
next;
}
chomp;
# Game File Numbers Error-Checking
@game = split;
if ($bonus_Ball_On eq 'y') {
$game_bonus_Ball = pop @game;
print "\nALERT: Bonus number '$game_bonus_Ball' is above the Bonu
+s max: $bonus_Max_Number.\n" if $game_bonus_Ball gt $bonus_Max_Number
+;
print "\nALERT: Bonus number '$game_bonus_Ball' is below one.\n"
+if $game_bonus_Ball < 1;
print "\nALERT: Non-numeric entry $game_bonus_Ball in bonus ball
+below.\n" if $game_bonus_Ball =~/\D/;
}
print "\nALERT: Two few numbers in game below.\n" if @game < @win
+ners;
print "\nALERT: Two many numbers in game below.\n" if @game > @wi
+nners;
$picks = 0;
undef(@picked);
## Games File Error-Checking
L2:
foreach my $i (0 .. $#game) {
foreach my $j ($i + 1 .. $#game) {
if ($game[$i] == $game[$j]) {
print "\nALERT: Duplicate number '$game[$i]' in game below.\n";
last L2;
}
}
if ($game[$i] < 1) {
print "ALERT: Number is below one in game below.\n";
}
}
## Check game file for winners
foreach my $y (@winners) {
for my $x (@game) {
if ($y == $x) {
$picks++;
push @picked, $x;
}
}
}
## End Check game file for winners
## Start console output
if ($bonus_Ball_On eq 'y') {
$b = 0;
# push @game, $game_bonus_Ball;
if ($picks != 0) {
print "@game $game_bonus_Ball --$picks";
} else {
print "@game $game_bonus_Ball"
}
if ($bonus_Ball == $game_bonus_Ball) {
push @picked, $game_bonus_Ball;
$b = 1;
}
if ($#picked > -1) {
if ($b == 1) {
if ($picks == 0) {
print " --0b Hit: @picked\n";
}else {
print "b Hit: @picked\n";
}
} else {
print " Hit: @picked\n";
}
} else {
print "\n";
}
}
if ($bonus_Ball_On ne 'y') {
if ($picks != 0) {
print "@game --$picks";
if ($#picked > -1) {
print " Hit: @picked\n";
} else {
print "\n";
}
} else {
print "@game\n";
}
}
## End console output
}
print "\n<Press Return to exit>";
chomp($lotto_Max_Number = <STDIN>);
In reply to PERLotto
by PERLost
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.