Spoof game: Difference between revisions

Added Perl 6 example
(Added Perl example)
(Added Perl 6 example)
Line 673:
My pot is: 1
My guess is: 6
You won!</pre>
 
=={{header|Perl 6}}==
{{trans|Perl}}
<lang perl6>sub query_for_integer ($prompt) { loop { ($_ = prompt "$prompt ") ~~ /^ \d+ $/ ?? return $_ !! say '(need an integer)'; } }
 
sub spoof_for_2 {
my ($mypot, $myguess, $yourpot, $yourguess) = 0, 0, 0, 0;
my $ngames = query_for_integer('How many games do you want?');
 
for 1 .. $ngames {
 
repeat {
$mypot = 1 + 3.rand.Int;
$myguess = 1 + 6.rand.Int;
} until $mypot+3 < $myguess;
say 'I have set my pot and guess.';
 
repeat {
$yourpot = query_for_integer('Your pot?' );
$yourguess = query_for_integer('Your guess?');
} until 0 <= $yourpot & $yourguess <= 6 and $yourpot+4 > $yourguess;
 
say "My pot is: $mypot\nMy guess is: $myguess";
 
given $mypot + $yourpot {
when $myguess & $yourguess { say 'Draw!' }
when $myguess { say 'I won!' }
when $yourguess { say 'You won!' }
default { say 'No winner!' }
}
}
}
 
spoof_for_2();</lang>
{{out}}
<pre>How many games do you want? 1
Your pot? 2
Your guess? 3
I have set my pot and guess.
My pot is: 1
My guess is: 5
You won!</pre>
 
2,392

edits