Penney's game: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Added C#)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 464:
=================================
 
</pre>
=={{header|C++}}==
<lang cpp>
#include <time.h>
#include <iostream>
#include <string>
 
using namespace std;
 
class penney
{
public:
penney()
{ pW = cW = 0; }
void gameLoop()
{
string a;
while( true )
{
playerChoice = computerChoice = "";
if( rand() % 2 )
{ computer(); player(); }
else
{ player(); computer(); }
 
play();
 
cout << "[Y] to play again "; cin >> a;
if( a[0] != 'Y' && a[0] != 'y' )
{
cout << "Computer won " << cW << " times." << endl << "Player won " << pW << " times.";
break;
}
cout << endl << endl;
}
}
private:
void computer()
{
if( playerChoice.length() == 0 )
{
for( int x = 0; x < 3; x++ )
computerChoice.append( ( rand() % 2 ) ? "H" : "T", 1 );
}
else
{
computerChoice.append( playerChoice[1] == 'T' ? "H" : "T", 1 );
computerChoice += playerChoice.substr( 0, 2 );
}
cout << "Computer's sequence of three is: " << computerChoice << endl;
}
void player()
{
cout << "Enter your sequence of three (H/T) "; cin >> playerChoice;
}
void play()
{
sequence = "";
while( true )
{
sequence.append( ( rand() % 2 ) ? "H" : "T", 1 );
if( sequence.find( playerChoice ) != sequence.npos )
{
showWinner( 1 );
break;
}
else if( sequence.find( computerChoice ) != sequence.npos )
{
showWinner( 0 );
break;
}
}
}
void showWinner( int i )
{
string s;
if( i ) { s = "Player wins!"; pW++; }
else { s = "Computer wins!"; cW++; }
cout << "Tossed sequence: " << sequence << endl << s << endl << endl;
}
string playerChoice, computerChoice, sequence;
int pW, cW;
};
int main( int argc, char* argv[] )
{
srand( static_cast<unsigned>( time( NULL ) ) );
penney game; game.gameLoop();
return 0;
}
</lang>
{{out}}
<pre>
Computer's sequence of three is: HHH
Enter your sequence of three (H/T) THH
Tossed sequence: TTHTTTTTTTTTTTHTHTTTHTTHTTHH
Player wins!
 
[Y] to play again y
 
Enter your sequence of three (H/T) HTH
Computer's sequence of three is: HHT
Tossed sequence: THHT
Computer wins!
 
[Y] to play again
</pre>
 
Line 702 ⟶ 597:
I win!
Press a key.</pre>
 
=={{header|C++}}==
<lang cpp>
#include <time.h>
#include <iostream>
#include <string>
 
using namespace std;
 
class penney
{
public:
penney()
{ pW = cW = 0; }
void gameLoop()
{
string a;
while( true )
{
playerChoice = computerChoice = "";
if( rand() % 2 )
{ computer(); player(); }
else
{ player(); computer(); }
 
play();
 
cout << "[Y] to play again "; cin >> a;
if( a[0] != 'Y' && a[0] != 'y' )
{
cout << "Computer won " << cW << " times." << endl << "Player won " << pW << " times.";
break;
}
cout << endl << endl;
}
}
private:
void computer()
{
if( playerChoice.length() == 0 )
{
for( int x = 0; x < 3; x++ )
computerChoice.append( ( rand() % 2 ) ? "H" : "T", 1 );
}
else
{
computerChoice.append( playerChoice[1] == 'T' ? "H" : "T", 1 );
computerChoice += playerChoice.substr( 0, 2 );
}
cout << "Computer's sequence of three is: " << computerChoice << endl;
}
void player()
{
cout << "Enter your sequence of three (H/T) "; cin >> playerChoice;
}
void play()
{
sequence = "";
while( true )
{
sequence.append( ( rand() % 2 ) ? "H" : "T", 1 );
if( sequence.find( playerChoice ) != sequence.npos )
{
showWinner( 1 );
break;
}
else if( sequence.find( computerChoice ) != sequence.npos )
{
showWinner( 0 );
break;
}
}
}
void showWinner( int i )
{
string s;
if( i ) { s = "Player wins!"; pW++; }
else { s = "Computer wins!"; cW++; }
cout << "Tossed sequence: " << sequence << endl << s << endl << endl;
}
string playerChoice, computerChoice, sequence;
int pW, cW;
};
int main( int argc, char* argv[] )
{
srand( static_cast<unsigned>( time( NULL ) ) );
penney game; game.gameLoop();
return 0;
}
</lang>
{{out}}
<pre>
Computer's sequence of three is: HHH
Enter your sequence of three (H/T) THH
Tossed sequence: TTHTTTTTTTTTTTHTHTTTHTTHTTHH
Player wins!
 
[Y] to play again y
 
Enter your sequence of three (H/T) HTH
Computer's sequence of three is: HHT
Tossed sequence: THHT
Computer wins!
 
[Y] to play again
</pre>
 
=={{header|Clojure}}==
Line 1,158 ⟶ 1,159:
Do you want to continue(0/1):
</pre>
 
=={{header|Haskell}}==
<lang Haskell>import qualified Data.List as L
Line 2,174 ⟶ 2,176:
The computer wins!
</pre>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2018.02}}
 
<lang perl6>enum Coin <Heads Tails>;
enum Yay <Yay Good Super Hah Ooh Yipee Sweet Cool Yes Haha>;
enum Boo <Drat Darn Crumb Oops Rats Bah Criminy Argh Shards>;
enum Bozo «'Dude' 'Cha' 'Bzzt' 'Hey' 'Silly dilly' 'Say what!?' 'You numbskull'»;
 
sub flipping {
for 1..4 {
print "-\b"; sleep .1;
print "\\\b"; sleep .1;
print "|\b"; sleep .1;
print "/\b"; sleep .1;
}
}
sub your-choice($p is copy) {
loop (my @seq; @seq != 3; $p = "{Bozo.pick}! Please pick exactly 3: ") {
@seq = prompt($p).uc.comb(/ H | T /).map: {
when 'H' { Heads }
when 'T' { Tails }
}
}
@seq;
}
repeat until prompt("Wanna play again? ").lc ~~ /^n/ {
my $mefirst = Coin.roll;
print tc "$mefirst I start, {Coin(+!$mefirst).lc} you start, flipping...\n\t";
flipping;
say my $flip = Coin.roll;
 
my @yours;
my @mine;
 
if $flip == $mefirst {
print "{Yay.pick}! I get to choose first, and I choose: "; sleep 2; say @mine = |Coin.roll(3);
@yours = your-choice("Now you gotta choose: ");
while @yours eqv @mine {
say "{Bozo.pick}! We'd both win at the same time if you pick that!";
@yours = your-choice("Pick something different from me: ");
}
say "So, you'll win if we see: ", @yours;
}
else {
@yours = your-choice("{Boo.pick}! First you choose: ");
say "OK, you'll win if we see: ", @yours;
print "In that case, I'll just randomly choose: "; sleep 2; say @mine = Coin(+!@yours[1]), |@yours[0,1];
}
sub check($a,$b,$c) {
given [$a,$b,$c] {
when @mine { say "\n{Yay.pick}, I win, and you lose!"; Nil }
when @yours { say "\n{Boo.pick}, you win, but I'll beat you next time!"; Nil }
default { Coin.roll }
}
}
 
sleep 1;
say < OK! Ready? Right... So... Yo!>.pick;
sleep .5;
say ("Pay attention now!",
"Watch closely!",
"Let's do it...",
"You feeling lucky?",
"No way you gonna win this...",
"Can I borrow that coin again?").pick;
sleep 1;
print "Here we go!\n\t";
for |Coin.roll(3), &check ...^ :!defined {
flipping;
print "$_ ";
}
}</lang>
{{out}}
Note: the actual run displays a little coin-flipping animation, but that won't show up here:
<pre>Heads I start, tails you start, flipping...
Heads
Yipee! I get to choose first, and I choose: Heads Heads Tails
Now you gotta choose: tth
So, you'll win if we see: Tails Tails Heads
Yo!
Can I borrow that coin again?
Here we go!
Tails Tails Tails Tails Tails Heads
Argh, you win, but I'll beat you next time!
Wanna play again? y
Tails I start, heads you start, flipping...
Tails
Yes! I get to choose first, and I choose: Heads Tails Tails
Now you gotta choose: H T T
Dude! We'd both win at the same time if you pick that!
Pick something different from me: heads tails tails
Silly dilly! We'd both win at the same time if you pick that!
Pick something different from me: h,h,h
So, you'll win if we see: Heads Heads Heads
OK!
You feeling lucky?
Here we go!
Tails Tails Tails Heads Heads Heads
Drat, you win, but I'll beat you next time!
Wanna play again? y
Heads I start, tails you start, flipping...
Tails
Shards! First you choose: tht
OK, you'll win if we see: Tails Heads Tails
In that case, I'll just randomly choose: Tails Tails Heads
Right...
Pay attention now!
Here we go!
Heads Tails Tails Heads
Hah, I win, and you lose!
Wanna play again? n</pre>
 
=={{header|Phix}}==
Line 2,502 ⟶ 2,389:
</pre>
 
=={{Headerheader|Prolog}}==
<lang prolog>play :- rand1(R), game(R).
 
Line 2,558 ⟶ 2,445:
</pre>
 
=={{Headerheader|Python}}==
<lang python>from __future__ import print_function
import random
Line 2,781 ⟶ 2,668:
</pre>
(Nail-biting stuff!)
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.02}}
 
<lang perl6>enum Coin <Heads Tails>;
enum Yay <Yay Good Super Hah Ooh Yipee Sweet Cool Yes Haha>;
enum Boo <Drat Darn Crumb Oops Rats Bah Criminy Argh Shards>;
enum Bozo «'Dude' 'Cha' 'Bzzt' 'Hey' 'Silly dilly' 'Say what!?' 'You numbskull'»;
 
sub flipping {
for 1..4 {
print "-\b"; sleep .1;
print "\\\b"; sleep .1;
print "|\b"; sleep .1;
print "/\b"; sleep .1;
}
}
sub your-choice($p is copy) {
loop (my @seq; @seq != 3; $p = "{Bozo.pick}! Please pick exactly 3: ") {
@seq = prompt($p).uc.comb(/ H | T /).map: {
when 'H' { Heads }
when 'T' { Tails }
}
}
@seq;
}
repeat until prompt("Wanna play again? ").lc ~~ /^n/ {
my $mefirst = Coin.roll;
print tc "$mefirst I start, {Coin(+!$mefirst).lc} you start, flipping...\n\t";
flipping;
say my $flip = Coin.roll;
 
my @yours;
my @mine;
 
if $flip == $mefirst {
print "{Yay.pick}! I get to choose first, and I choose: "; sleep 2; say @mine = |Coin.roll(3);
@yours = your-choice("Now you gotta choose: ");
while @yours eqv @mine {
say "{Bozo.pick}! We'd both win at the same time if you pick that!";
@yours = your-choice("Pick something different from me: ");
}
say "So, you'll win if we see: ", @yours;
}
else {
@yours = your-choice("{Boo.pick}! First you choose: ");
say "OK, you'll win if we see: ", @yours;
print "In that case, I'll just randomly choose: "; sleep 2; say @mine = Coin(+!@yours[1]), |@yours[0,1];
}
sub check($a,$b,$c) {
given [$a,$b,$c] {
when @mine { say "\n{Yay.pick}, I win, and you lose!"; Nil }
when @yours { say "\n{Boo.pick}, you win, but I'll beat you next time!"; Nil }
default { Coin.roll }
}
}
 
sleep 1;
say < OK! Ready? Right... So... Yo!>.pick;
sleep .5;
say ("Pay attention now!",
"Watch closely!",
"Let's do it...",
"You feeling lucky?",
"No way you gonna win this...",
"Can I borrow that coin again?").pick;
sleep 1;
print "Here we go!\n\t";
for |Coin.roll(3), &check ...^ :!defined {
flipping;
print "$_ ";
}
}</lang>
{{out}}
Note: the actual run displays a little coin-flipping animation, but that won't show up here:
<pre>Heads I start, tails you start, flipping...
Heads
Yipee! I get to choose first, and I choose: Heads Heads Tails
Now you gotta choose: tth
So, you'll win if we see: Tails Tails Heads
Yo!
Can I borrow that coin again?
Here we go!
Tails Tails Tails Tails Tails Heads
Argh, you win, but I'll beat you next time!
Wanna play again? y
Tails I start, heads you start, flipping...
Tails
Yes! I get to choose first, and I choose: Heads Tails Tails
Now you gotta choose: H T T
Dude! We'd both win at the same time if you pick that!
Pick something different from me: heads tails tails
Silly dilly! We'd both win at the same time if you pick that!
Pick something different from me: h,h,h
So, you'll win if we see: Heads Heads Heads
OK!
You feeling lucky?
Here we go!
Tails Tails Tails Heads Heads Heads
Drat, you win, but I'll beat you next time!
Wanna play again? y
Heads I start, tails you start, flipping...
Tails
Shards! First you choose: tht
OK, you'll win if we see: Tails Heads Tails
In that case, I'll just randomly choose: Tails Tails Heads
Right...
Pay attention now!
Here we go!
Heads Tails Tails Heads
Hah, I win, and you lose!
Wanna play again? n</pre>
 
=={{header|REXX}}==
Line 2,966 ⟶ 2,969:
</pre>
 
=={{Headerheader|Ruby}}==
<lang ruby>Toss = [:Heads, :Tails]
 
Line 3,248 ⟶ 3,251:
HHHT
I win!</pre>
 
=={{header|VBA}}==
<lang vb>
10,327

edits