File: lotto.pl

package info (click to toggle)
liblist-objects-withutils-perl 2.028003-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,292 kB
  • sloc: perl: 1,957; makefile: 17; sh: 6
file content (68 lines) | stat: -rw-r--r-- 1,480 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env perl
use strict; use warnings FATAL => 'all';

use Lowu;

my $selected = [];

while ($selected->count < 6) {

  if ($selected->count == 5) {
    print " > Selected balls: ", $selected->join('-'), "\n";

    my $eball;
    do { 
      print "Select an extra ball between 1 and 35: ";
      $eball = <STDIN>;
      chomp $eball;
    } until $eball and $eball > 0 and $eball < 35;

    $selected->push($eball);

    last
  }

  my $ball;  do {
    my $current = $selected->count;
    print " > $current balls selected: ",
          $selected->join('-'), "\n";

    print "Select a ball between 1 and 59: ";
    $ball = <STDIN>;
    chomp $ball;
  } until $ball and $ball > 0 and $ball < 59
    and $selected->all_items != $ball;
  
  $selected->push($ball);
}

print " > You selected ", 
       $selected->sliced(0 .. 4)->join('-'), 
      " (",
       $selected->get(5), 
      ")\n";

my $balls = [ 1 .. 59 ]
              ->shuffle
              ->sliced( 1 .. 5 );
my $extra = [ 1 .. 35 ]
              ->random;

print "! Drew: ", $balls->join('-'), ' ', $extra, "\n";

my $hits = $selected->sliced(0 .. 4)
             ->grep(sub { $balls->any_items == $_[0] });

my $did_hit;  
if ($hits->has_any) {
  print "!! You hit on ", $hits->count, 
        " balls: ", $hits->join(', '), "\n";
  ++$did_hit
}

if ($selected->get(0) == $extra) {
  print "!! You hit on the extra ball ($extra)!\n";
  ++$did_hit
}

print " >> Better luck next time :(\n" unless $did_hit;