Penney's game: Difference between revisions

m
m (Just to save spaces :))
m (→‎{{header|Wren}}: Minor tidy)
 
(61 intermediate revisions by 34 users not shown)
Line 1:
{{task|Games}}
[[wp:Penney's_game|Penneys game]] is a game where two players bet on being the first to see a particular sequence of Heads and Tails in consecutive tosses of a fair coin.
 
[[wp:Penney's_game|Penney's game]] is a game where two players bet on being the first to see a particular sequence of [[wp:Coin_flipping|heads or tails]] in consecutive tosses of a [[wp:Fair_coin|fair coin]].
It is common to agree on a sequence length of three then one player will openly choose a sequence, for example Heads, Tails, Heads, or HTH for short; then the other player on seeing the first players choice will choose his sequence. The coin is tossed and the first player to see his sequence in the sequence of coin tosses wins.
 
It is common to agree on a sequence length of three then one player will openly choose a sequence, for example:
For example: One player might choose the sequence HHT and the other THT. Successive coin tosses of HTTHT gives the win to the second player as the last three coin tosses are his sequence.
Heads, Tails, Heads, or '''HTH''' for short.
The other player on seeing the first players choice will choose his sequence. The coin is tossed and the first player to see his sequence in the sequence of coin tosses wins.
 
 
;The Task:
;Example:
One player might choose the sequence '''HHT''' and the other '''THT'''.
 
Successive coin tosses of '''HTTHT''' gives the win to the second player as the last three coin tosses are his sequence.
 
 
;Task
Create a program that tosses the coin, keeps score and plays Penney's game against a human opponent.
* Who chooses and shows their sequence of three should be chosen randomly.
* If going first, the computer should randomly choose its sequence of three randomly.
* If going second, the computer should automatically play [[wp:Penney's_game#Analysis_of_the_three-bit_game|the optimum sequence]].
* Successive coin tosses should be shown.
 
<br>
Show output of a game where the computer choses first and a game where the user goes first here on this page.
Show output of a game where the computer chooses first and a game where the user goes first here on this page.
 
 
;Refs:
;See also
* [https://www.youtube.com/watch?v=OcYnlSenF04 The Penney Ante Part 1] (Video).
* [https://www.youtube.com/watch?v=U9wak7g5yQA The Penney Ante Part 2] (Video).
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V first = random:choice([1B, 0B])
 
V you = ‘’
V me = ‘’
V ht = ‘HT’ // to get round ‘bug in MSVC 2017’[https://developercommunity.visualstudio.com/t/bug-with-operator-in-c/565417]
I first
me = random:sample(Array(‘HT’ * 3), 3).join(‘’)
print(‘I choose first and will win on first seeing #. in the list of tosses’.format(me))
L you.len != 3 | any(you.map(ch -> ch !C :ht)) | you == me
you = input(‘What sequence of three Heads/Tails will you win with: ’)
E
L you.len != 3 | any(you.map(ch -> ch !C :ht))
you = input(‘After you: What sequence of three Heads/Tails will you win with: ’)
me = (I you[1] == ‘T’ {‘H’} E ‘T’)‘’you[0.<2]
print(‘I win on first seeing #. in the list of tosses’.format(me))
 
print("Rolling:\n ", end' ‘’)
V rolled = ‘’
L
rolled ‘’= random:choice(‘HT’)
print(rolled.last, end' ‘’)
I rolled.ends_with(you)
print("\n You win!")
L.break
I rolled.ends_with(me)
print("\n I win!")
L.break
sleep(1)</syntaxhighlight>
 
{{out}}
<pre>
I choose first and will win on first seeing THH in the list of tosses
What sequence of three Heads/Tails will you win with: THT
Rolling:
THT
You win!
</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">Gui, font, s12
Gui, add, text, w90, Computer:
loop, 3
Gui, add, button, x+10 h30 w30 vCB%A_Index%
Gui, add, edit, xs w240 R3 vSequence
Gui, add, text, w90, Human:
loop, 3
Gui, add, button, x+10 h30 w30 vHB%A_Index% gHumButton, H
Gui, add, button, xm gToss, toss
Gui, add, button, x+10 gReset, Reset
Gui, show,, Penney's game
CompSeq := HumSeq := Seq := GameEnd := ""
 
RandomStart:
Random, WhoStarts, 1, 2
if (WhoStarts=1)
gosub, CompButton
return
;-----------------------------------------------
CompButton:
if CompSeq
return
Loop, 3
{
Random, coin, 1, 2
GuiControl,, CB%A_Index%, % coin=1?"H":"T"
CompSeq .= coin=1?"H":"T"
}
return
;-----------------------------------------------
HumButton:
if HumSeq
return
GuiControlGet, B,, % A_GuiControl
GuiControl,, % A_GuiControl, % B="H"?"T":"H"
return
;-----------------------------------------------
Toss:
if GameEnd
return
if !HumSeq
{
loop 3
{
GuiControlGet, B,, HB%A_Index%
HumSeq .= B
}
if (CompSeq = HumSeq)
{
MsgBox, 262160, Penney's game, Human's Selection Has to be different From Computer's Selection`nTry Again
HumSeq := ""
return
}
}
if !CompSeq
{
CompSeq := (SubStr(HumSeq,2,1)="H"?"T":"H") . SubStr(HumSeq,1,2)
loop, Parse, CompSeq
GuiControl,, CB%A_Index%, % A_LoopField
}
 
Random, coin, 1, 2
Seq .= coin=1?"H":"T"
GuiControl,, Sequence, % seq
if (SubStr(Seq, -2) = HumSeq)
MsgBox % GameEnd := "Human Wins"
else if (SubStr(Seq, -2) = CompSeq)
MsgBox % GameEnd := "Computer Wins"
return
;-----------------------------------------------
Reset:
loop, 3
{
GuiControl,, CB%A_Index%
GuiControl,, HB%A_Index%, H
}
GuiControl,, Sequence
CompSeq := HumSeq := Seq := GameEnd := ""
gosub, RandomStart
return
;-----------------------------------------------</syntaxhighlight>
 
=={{header|Amazing Hopper}}==
{{trans|Sinclair ZX81 BASIC}}
<syntaxhighlight lang="Amazing Hopper">
#include <jambo.h>
 
#define Getanuppercasecharinto(_X_) Getchar(_X_),Let( _X_ := Ucase(_X_) )
#define Isnotvalidin(_X_) Not( Occurs in (_X_) )
#define NOELIGEPCDESPUESDETI 0
#define ELIGEPCDESPUESDETI 1
 
Main
Set break
Get total args
If ( Is equal to '2' )
largo=0
Let ( largo := Get if( Argnum(2) Is less than '3', 3, Argnum(2) ) )
OTRO="S"
Cursor off
Loop
P="", C="", S="", L=0
Cls
Locate(1,1), Printnl ("***** Penney's Game *****")
If ( Geq( Rand(1), 0.5) )
Gosub ' Yo tiro ', Set 'NO ELIGE PC DESPUES DE TI',Gosub ' Tú tiras '
Else
Set 'ELIGE PC DESPUES DE TI', Gosub ' Tú tiras '
End If
 
// a jugar:
G="", SW=1
Loop
Let( G := Cat( G, Get if( Geq( Rand(1), 0.5), "T", "H" ) ) )
Locate( 6, 1 ), Printnl( "Tiradas: ", G )
Continue if ( Less ( Len (G) ---Backup to 'L'---, 3) )
Let ( S := Right( Sub(L, Minus one 'largo') , G) )
 
If ( Eq( S, P ) )
Set ("TU GANAS!!"), SW=0
Else If (Eq( S, C ) )
Set ("YO GANO!!"), SW=0
End If
Prnl
Sleep(1)
Back if( SW ) is not zero
Printnl ( Utf8("¿Otro juego? S/* ") )
Getchar(OTRO)
While( Eq (OTRO, "S") )
Cursor on
Else
Printnl("Modo de uso: hopper penny.jambo <n>\ndonde \"n\" es el largo de la secuencia")
End If
End
 
Subrutines
 
Define ' Yo tiro '
Locate(4,1), Print( "Yo elijo... " )
i=largo
Loop
Let( C := Cat( C, Get if( Geq( Rand(1), 0.5), "T", "H" ) ) )
--i
Back if (i) is positive
Printnl (C)
Return
 
Define ' Tú tiras, elige pc '
Locate(3,1), Printnl ( Utf8("Tú eliges...") )
i=largo, c=0
Loop
Get an uppercase char into 'c'
Continue if ( var(c) Is not valid in ("HT") )
Let( P:=Cat(P,c) )
Loccol(13), Printnl(P)
--i
Back if (i) is positive
 
If ( elige pc )
Let (C := P)
[2] Get from (C)
Set if( Is equal to ("H"), "T", "H" )
Cput (C)
Locate(4,1), Print( "Yo elijo... ", C )
End If
Return
</syntaxhighlight>
{{out}}
<pre>
$ hopper penney.jambo
Modo de uso: hopper penny.jambo <n>
donde "n" es el largo de la secuencia
 
$ hopper penney.jambo 5
***** Penney's Game *****
 
Tú eliges...HTHHT
Yo elijo... HHHHT
 
Tiradas: TTHTTTTHHTTHHTTHTHTHHT
TU GANAS!!
¿Otro juego? S/*
 
***** Penney's Game *****
 
Tú eliges...HTHTT
Yo elijo... HHHTT
 
Tiradas: THHHHTHHHHTT
YO GANO!!
¿Otro juego? S/*
$
</pre>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{trans|Pascal}}
<syntaxhighlight lang="gwbasic"> 100 R = RND ( - 1 * ( PEEK (78) + 256 * PEEK (79))): REM RANDOMIZE
110 DEF FN R(R) = INT ( RND (1) * R) * (3 ^ (R = 8)) + 1
120 READ P$
130 DATA HTHHHHHHTHTTTHHTHTTTHTTT
140 LET M$ = CHR$ (13)
150 PRINT "WELCOME TO PENNEY'S GAME!"M$
160 INPUT "HOW MANY ROUNDS WOULD YOU LIKE TO PLAY? ";ROUNDS%
170 PRINT M$"OK, LET'S PLAY "ROUNDS%" ROUNDS."
180 FOR COUNT = 1 TO ROUNDS%
190 PRINT M$"*** ROUND #"COUNT" ***"M$
200 ON FN R(2) GOSUB 300,400
210 PRINT "LET'S GO!"M$
220 GOSUB 450"PLAY ROUND
230 INPUT "PRESS RETURN TO GO ON...";N$
240 NEXT COUNT
250 PRINT M$"*** END RESULT ***"M$
260 IF YOU > ME THEN PRINT "CONGRATULATIONS! YOU WON!"
270 IF ME > YOU THEN PRINT "HOORAY! I WON"
280 IF ME = YOU THEN PRINT "COOL, WE TIED."
290 END
 
300 ME$ = MID$ (P$, FN R(8),3): REM RANDOM SEQUENCE
310 PRINT "I'LL PICK FIRST THIS TIME."M$" MY SEQUENCE IS "ME$"."
320 FOR REP = 0 TO 1 STEP 0
330 PRINT "WHAT SEQUENCE DO YOU WANT?"
340 GOSUB 590"GET YOUR SEQUENCE
350 LET REP = YOU$ < > ME$
360 IF NOT REP THEN PRINT "HEY, THAT'S MY SEQUENCE! THINK FOR YOURSELF!"
370 NEXT REP
380 PRINT YOU$", HUH? SOUNDS OK TO ME."
390 RETURN
 
400 PRINT "YOU PICK FIRST THIS TIME."
410 GOSUB 590"GET YOUR SEQUENCE
420 LET ME$ = MID$ ("HT",( MID$ (YOU$,2,1) = "H") + 1,1) + LEFT$ (YOU$,2)
430 PRINT "OK, SO YOU PICKED "YOU$M$M$"MY SEQUENCE WILL BE "ME$M$
440 RETURN
 
450 T0SSE$ = MID$ (P$, FN R(8),3): REM RANDOM SEQUENCE
460 PRINT "TOSSING THE COIN: "M$ MID$ (T0SSE$,2);
470 FOR T0SS = 0 TO 1 STEP 0
480 LET T0SSE$ = RIGHT$ (T0SSE$,2) + MID$ ("HT", FN R(2),1)
490 PRINT MID$ (T0SSE$,3);
500 IF YOU$ = T0SSE$ THEN 530"YOU WON
510 IF ME$ = T0SSE$ THEN 560"I WON
520 NEXT T0SS
 
530 YOU = YOU + 1
540 PRINT M$M$"CONGRATULATIONS! YOU WON THIS ROUND."M$"YOUR NEW SCORE IS "YOU"."
550 RETURN
 
560 ME = ME + 1
570 PRINT M$M$"YAY, I WON THIS ROUND!"M$"MY NEW SCORE IS "ME"."
580 RETURN
 
590 YOU$ = ""
600 PRINT "TYPE 3 LETTERS H OR T: "
610 FOR I = 1 TO 3
620 LET V$ = "HT"
630 GOSUB 670"GET VALID KEYPRESS
640 LET YOU$ = YOU$ + C$
650 NEXT I
660 LET V$ = M$ + M$
670 FOR C = 0 TO 1 STEP 0
680 GET C$
690 IF ASC (C$) > 95 THEN C$ = CHR$ ( ASC (C$) - 32)
700 LET C = C$ = LEFT$ (V$,1) OR C$ = MID$ (V$,2)
710 NEXT C
720 PRINT C$;
730 RETURN</syntaxhighlight>
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
global jugador, computador, secuencia
jugador = "" : computador = ""
 
function SecuenciaJugador(secuencia)
do
valido = TRUE
print chr(10) + "Ingresa una secuencia de 3 opciones, cada una de ellas H o T:";
input " > ", secuencia
secuencia = upper(secuencia)
if length(secuencia) <> 3 then valido = FALSE
if secuencia = computador then print "Pero no la misma que yo..."+ chr(10): valido = FALSE
if valido then
for i = 1 to 3
opcion = mid(secuencia, i, 1)
if opcion <> "H" and opcion <> "T" then valido = FALSE
next i
end if
until valido
SecuenciaJugador = secuencia
end function
 
function LanzamientoCPU()
secuencia = ""
computador =""
for i = 1 to 3
opcion = rand
if opcion < .5 then secuencia += "H" else secuencia += "T"
next i
LanzamientoCPU = secuencia
end function
 
do
cls
print "*** Penney's Game ***" + chr(10)
if rand < .5 then
print "Empiezas... ";
jugador = SecuenciaJugador(jugador)
computador = LanzamientoCPU()
if jugador = computador then computador = LanzamientoCPU()
print "Yo gano al ver "; computador; " por primera vez en la lista de lanzamientos..."
else
computador = LanzamientoCPU()
print "Elijo primero y gano al ver "; computador; " por primera vez en la lista de lanzamientos..."
jugador = SecuenciaJugador(jugador)
end if
print: print "Lanzamientos..."
secuencia = ""
winner = FALSE
do
pause 1
if rand <= .5 then
secuencia += "H"
print "H ";
else
print "T ";
secuencia += "T"
end if
if right(secuencia, 3) = jugador then print: print "¡Felicidades! ¡Ganaste!": winner = TRUE
if right(secuencia, 3) = computador then print: print "¡Yo gano!": winner = TRUE
until winner
do
valido = FALSE
print
input "¿Otra partida? (S/N) ", otra
if instr("SsNn", otra) then valido = TRUE
until valido
until upper(otra) = "N"
print "¡Gracias por jugar!"
end
</syntaxhighlight>
 
 
==={{header|Sinclair ZX81 BASIC}}===
Works with 1k of RAM.
<syntaxhighlight lang="basic"> 10 IF RND>=.5 THEN GOTO 100
20 PRINT "YOU PICK FIRST."
30 INPUT P$
40 PRINT "YOU CHOSE ";P$
50 LET C$="H"
60 IF P$(2)="H" THEN LET C$="T"
70 LET C$=C$+P$( TO 2)
80 PRINT "I CHOOSE ";C$
90 GOTO 190
100 PRINT "I PICK FIRST."
110 LET C$=""
120 FOR I=1 TO 3
130 LET C$=C$+"H"
140 IF RND>=.5 THEN LET C$(I)="T"
150 NEXT I
160 PRINT "I CHOOSE ";C$
170 INPUT P$
180 PRINT "YOU CHOSE ";P$
190 LET G$=""
200 IF RND>=.5 THEN GOTO 230
210 LET G$=G$+"H"
220 GOTO 240
230 LET G$=G$+"T"
240 PRINT AT 5,0;G$
250 IF LEN G$<3 THEN GOTO 200
260 IF G$(LEN G$-2 TO )=P$ THEN GOTO 290
270 IF G$(LEN G$-2 TO )=C$ THEN GOTO 310
280 GOTO 200
290 PRINT "YOU WIN"
300 STOP
310 PRINT "I WIN"</syntaxhighlight>
{{out}}
<pre>YOU PICK FIRST.
YOU CHOSE HTH
I CHOOSE HHT
 
 
HTTTHTTTTHHT
I WIN</pre>
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">::
::Penney's Game Task from Rosetta Code Wiki
::Batch File Implementation
Line 117 ⟶ 560:
 
:validate
echo "!you_bet!"|findstr /r /c:"^\"[hHtT][hHtT][hHtT]\"$">nul || (
set error=0
echo [Invalid Input...]&echo.&goto %1
if "!you_bet:~2,1!"=="" set error=1
if not "!you_bet:~3,1!"=="" set error=1
 
set nyou_bet=
for /l %%l in (0,1,2) do (
set valid=!you_bet:~%%l,1!
if /i "!valid!"=="H" (
set "nyou_bet=!nyou_bet!H"
) else (
if /i "!valid!"=="T" (
set "nyou_bet=!nyou_bet!T"
) else (
set error=1
)
)
)
if %error%/i "!you_bet!"==1"%cpu_bet%" (echo [InvalidBet something Inputdifferent...]&echo.&goto %1)
for %%i in ("t=T" "h=H") do set "you_bet=!you_bet:%%~i!"
if "!nyou_bet!"=="%cpu_bet%" (echo [Bet something different...]&echo.&goto %1)
set you_bet=!nyou_bet!&goto :EOF</langsyntaxhighlight>
{{out}}
Note: The outputs of tosses are 'delayed' just to make the game a little more dramatic.
Line 175 ⟶ 604:
 
[Type Y if U Wanna Beat Me, or Else, Exit...]</pre>
 
=={{header|BBC BASIC}}==
<syntaxhighlight lang="bbcbasic">REM >penney
PRINT "*** Penney's Game ***"
REPEAT
PRINT ' "Heads you pick first, tails I pick first."
PRINT "And it is... ";
WAIT 100
ht% = RND(0 - TIME) AND 1
IF ht% THEN
PRINT "heads!"
PROC_player_chooses(player$)
computer$ = FN_optimal(player$)
PRINT "I choose "; computer$; "."
ELSE
PRINT "tails!"
computer$ = FN_random
PRINT "I choose "; computer$; "."
PROC_player_chooses(player$)
ENDIF
PRINT "Starting the game..." ' SPC 5;
sequence$ = ""
winner% = FALSE
REPEAT
WAIT 100
roll% = RND AND 1
IF roll% THEN
sequence$ += "H"
PRINT "H ";
ELSE
PRINT "T ";
sequence$ += "T"
ENDIF
IF RIGHT$(sequence$, 3) = computer$ THEN
PRINT ' "I won!"
winner% = TRUE
ELSE
IF RIGHT$(sequence$, 3) = player$ THEN
PRINT ' "Congratulations! You won."
winner% = TRUE
ENDIF
ENDIF
UNTIL winner%
REPEAT
valid% = FALSE
INPUT "Another game? (Y/N) " another$
IF INSTR("YN", another$) THEN valid% = TRUE
UNTIL valid%
UNTIL another$ = "N"
PRINT "Thank you for playing!"
END
:
DEF PROC_player_chooses(RETURN sequence$)
LOCAL choice$, valid%, i%
REPEAT
valid% = TRUE
PRINT "Enter a sequence of three choices, each of them either H or T:"
INPUT "> " sequence$
IF LEN sequence$ <> 3 THEN valid% = FALSE
IF valid% THEN
FOR i% = 1 TO 3
choice$ = MID$(sequence$, i%, 1)
IF choice$ <> "H" AND choice$ <> "T" THEN valid% = FALSE
NEXT
ENDIF
UNTIL valid%
ENDPROC
:
DEF FN_random
LOCAL sequence$, choice%, i%
sequence$ = ""
FOR i% = 1 TO 3
choice% = RND AND 1
IF choice% THEN sequence$ += "H" ELSE sequence$ += "T"
NEXT
= sequence$
:
DEF FN_optimal(sequence$)
IF MID$(sequence$, 2, 1) = "H" THEN
= "T" + LEFT$(sequence$, 2)
ELSE
= "H" + LEFT$(sequence$, 2)
ENDIF</syntaxhighlight>
{{out}}
<pre>*** Penney's Game ***
 
Heads you pick first, tails I pick first.
And it is... heads!
Enter a sequence of three choices, each of them either H or T:
> HTT
I choose HHT.
Starting the game...
H H H H H T
I won!
Another game? (Y/N) Y
 
Heads you pick first, tails I pick first.
And it is... tails!
I choose HTH.
Enter a sequence of three choices, each of them either H or T:
> HHT
Starting the game...
T T H H H H T
Congratulations! You won.
Another game? (Y/N) N
Thank you for playing!</pre>
 
=={{header|C}}==
This solution stores the sequences in bit-packed integers. With minor adjustments, this can be extended to allow larger sequence lengths beyond the required 3. However, the ai's algorithm for the perfect choice would need to be altered. More robust methods of input could be chosen, as scanf is generally fairly unsafe. A safer alternative would be to use something like fgets, and parse the input string ourselves.
<syntaxhighlight lang="c">
<lang C>
#include <stdio.h>
#include <stdlib.h>
Line 300 ⟶ 835:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 319 ⟶ 854:
 
</pre>
 
=={{header|C sharp}}==
{{works with|C sharp|7}}
<syntaxhighlight lang="csharp">using static System.Console;
using static System.Threading.Thread;
using System;
 
public static class PenneysGame
{
const int pause = 500;
const int N = 3;
static Random rng = new Random();
 
static int Toss() => rng.Next(2);
 
static string AsString(this int sequence) {
string s = "";
for (int b = 0b100; b > 0; b >>= 1) {
s += (sequence & b) > 0 ? 'T' : 'H';
}
return s;
}
 
static int UserInput() {
while (true) {
switch (ReadKey().Key) {
case ConsoleKey.Escape: return -1;
case ConsoleKey.H: return 0;
case ConsoleKey.T: return 1;
}
Console.Write('\b');
}
}
 
public static void Main2() {
int yourScore = 0, myScore = 0;
while (true) {
WriteLine($"Your score: {yourScore}, My score: {myScore}");
WriteLine("Determining who goes first...");
Sleep(pause);
bool youStart = Toss() == 1;
WriteLine(youStart ? "You go first." : "I go first.");
int yourSequence = 0, mySequence = 0;
if (youStart) {
WriteLine("Choose your sequence of (H)eads and (T)ails (or press Esc to exit)");
int userChoice;
for (int i = 0; i < N; i++) {
if ((userChoice = UserInput()) < 0) return;
yourSequence = (yourSequence << 1) + userChoice;
}
mySequence = ((~yourSequence << 1) & 0b100) | (yourSequence >> 1);
} else {
for (int i = 0; i < N; i++) {
mySequence = (mySequence << 1) + Toss();
}
 
WriteLine("I chose " + mySequence.AsString());
do {
WriteLine("Choose your sequence of (H)eads and (T)ails (or press Esc to exit)");
int choice;
yourSequence = 0;
for (int i = 0; i < N; i++) {
if ((choice = UserInput()) < 0) return;
yourSequence = (yourSequence << 1) + choice;
}
if (yourSequence == mySequence) {
WriteLine();
WriteLine("You cannot choose the same sequence.");
}
} while (yourSequence == mySequence);
}
 
WriteLine();
WriteLine($"Your sequence: {yourSequence.AsString()}, My sequence: {mySequence.AsString()}");
WriteLine("Tossing...");
int sequence = 0;
for (int i = 0; i < N; i++) {
Sleep(pause);
int toss = Toss();
sequence = (sequence << 1) + toss;
Write(toss > 0 ? 'T' : 'H');
}
while (true) {
if (sequence == yourSequence) {
WriteLine();
WriteLine("You win!");
yourScore++;
break;
} else if (sequence == mySequence) {
WriteLine();
WriteLine("I win!");
myScore++;
break;
}
Sleep(pause);
int toss = Toss();
sequence = ((sequence << 1) + toss) & 0b111;
Write(toss > 0 ? 'T' : 'H');
}
WriteLine("Press a key.");
ReadKey();
Clear();
}
}
 
}
</syntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">
Your score: 0, My score: 0
Determining who goes first...
I go first.
I chose HHH
Choose your sequence of (H)eads and (T)ails (or press Esc to exit)
thh
Your sequence: THH, My sequence: HHH
Tossing...
HTHTHTHTTHH
You win!
Press a key.
 
 
Your score: 2, My score: 0
Determining who goes first...
You go first.
Choose your sequence of (H)eads and (T)ails (or press Esc to exit)
thh
Your sequence: THH, My sequence: TTH
Tossing...
HTTTTH
I win!
Press a key.</pre>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <time.h>
#include <iostream>
Line 407 ⟶ 1,075:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 424 ⟶ 1,092:
[Y] to play again
</pre>
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">(ns penney.core
(:gen-class))
 
(def heads \H)
(def tails \T)
 
(defn flip-coin []
(let [flip (rand-int 2)]
(if (= flip 0) heads tails)))
 
(defn turn [coin]
(if (= coin heads) tails heads))
 
(defn first-index [combo coll]
(some #(if (= (second %) combo) (first %)) coll))
 
(defn find-winner [h c]
(if (< h c)
(do (println "YOU WIN!\n") :human)
(do (println "COMPUTER WINS!\n") :computer)))
 
(defn flip-off [human comp]
(let [flips (repeatedly flip-coin)
idx-flips (map-indexed vector (partition 3 1 flips))
h (first-index (seq human) idx-flips)
c (first-index (seq comp) idx-flips)]
(println (format "Tosses: %s" (apply str (take (+ 3 (min h c)) flips))))
(find-winner h c)))
 
(defn valid? [combo]
(if (empty? combo) true (and (= 3 (count combo)) (every? #(or (= heads %) (= tails %)) combo))))
 
(defn ask-move []
(println "What sequence of 3 Heads/Tails do you choose?")
(let [input (clojure.string/upper-case (read-line))]
(if-not (valid? input) (recur) input)))
 
(defn optimize-against [combo]
(let [mid (nth combo 1)
comp (str (turn mid) (first combo) mid)]
(println (format "Computer chooses %s: " comp)) comp))
 
(defn initial-move [game]
(let [combo (apply str (repeatedly 3 flip-coin))]
(println "--------------")
(println (format "Current score | CPU: %s, You: %s" (:computer game) (:human game)))
(if (= (:first-player game) tails)
(do
(println "Computer goes first and chooses: " combo)
combo)
(println "YOU get to go first."))))
 
(defn play-game [game]
(let [c-move (initial-move game)
h-move (ask-move)]
(if-not (empty? h-move)
(let [winner (flip-off h-move (if (nil? c-move) (optimize-against h-move) c-move))]
(recur (assoc game winner (inc (winner game)) :first-player (flip-coin))))
(println "Thanks for playing!"))))
 
(defn -main [& args]
(println "Penney's Game.")
(play-game {:first-player (flip-coin)
:human 0, :computer 0}))</syntaxhighlight>
 
{{out}}
<pre>
Penney's Game.
--------------
Current score | CPU: 0, You: 0
Computer goes first and chooses: THT
What sequence of 3 Heads/Tails do you choose?
tth
Tosses: TTH
YOU WIN!
 
--------------
Current score | CPU: 0, You: 1
YOU get to go first.
What sequence of 3 Heads/Tails do you choose?
tht
Computer chooses TTH:
Tosses: HHHTTH
COMPUTER WINS!
 
--------------
Current score | CPU: 1, You: 1
YOU get to go first.
What sequence of 3 Heads/Tails do you choose?
 
Thanks for playing!
</pre>
 
=={{header|Common Lisp}}==
{{trans|UNIX Shell}}
<syntaxhighlight lang="lisp">(setf *random-state* (make-random-state t))
 
(defparameter *heads* #\H)
(defparameter *tails* #\T)
 
(defun main ()
(format t "Penney's Game~%~%")
(format t "Flipping to see who goes first ...")
 
(setq p2 nil)
(if (string= (flip) *heads*)
(progn (format t " I do.~%")
(setq p2 (choose-random-sequence))
(format t "I choose: ~A~%" p2))
(format t "You do.~%"))
 
(setq p1 nil)
(loop while (null p1) doing
(format t "Enter your three-flip sequence: ")
(setq p1 (string (read)))
(cond ((/= (length p1) 3)
(format t "Sequence must consist of three flips.~%")
(setq p1 nil))
((string= p1 p2)
(format t "Sequence must be different from mine.~%")
(setq p1 nil))
((notevery #'valid? (coerce p1 'list))
(format t "Sequence must be contain only ~A's and ~A's.~%" *heads* *tails*)
(setq p1 nil))))
(format t "You picked: ~A~%" p1)
 
(if (null p2)
(progn
(setq p2 (choose-optimal-sequence p1))
(format t "I choose: ~A~%" p2)))
 
(format t "Here we go. ~A, you win; ~A, I win.~%" p1 p2)
(format t "Flips:")
(let ((winner nil)
(flips '()))
(loop while (null winner) doing
(setq flips (cons (flip) flips))
(format t " ~A" (car flips))
(if (>= (length flips) 3)
(let ((trail (coerce (reverse (subseq flips 0 3)) 'string)))
(cond ((string= trail p1) (setq winner "You"))
((string= trail p2) (setq winner "I"))))))
(format t "~&~A win!" winner)))
 
(defparameter *sides* (list *heads* *tails*))
(defun flip () (nth (random 2 *random-state*) *sides*))
 
(defun valid? (flip) (not (null (member flip *sides*))))
 
(defun opposite (flip) (if (string= flip *heads*) *tails* *heads*))
 
(defun choose-random-sequence () (coerce (list (flip) (flip) (flip)) 'string))
 
(defun choose-optimal-sequence (against)
(let* ((opposed (coerce against 'list))
(middle (cadr opposed))
(my-list (list (opposite middle) (car opposed) middle)))
(coerce my-list 'string)))
 
(main)</syntaxhighlight>
 
{{Out}}
Computer player first:
<pre>Penney's Game
 
Flipping to see who goes first ... I do.
I choose: THH
Enter your three-flip sequence: TTH
You picked: TTH
Here we go. TTH, you win; THH, I win.
Flips: T T T H
You win!</pre>
 
Human player first:
<pre>Penney's Game
 
Flipping to see who goes first ...You do.
Enter your three-flip sequence: HTT
You picked: HTT
I choose: HHT
Here we go. HTT, you win; HHT, I win.
Flips: H T T
You win!</pre>
 
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.random, std.algorithm, std.string,
std.conv, std.range, core.thread;
Line 461 ⟶ 1,314:
Thread.sleep(1.seconds);
}
}</langsyntaxhighlight>
The output is the same as the Python entry.
 
=={{header|Elixir}}==
{{trans|Ruby}}
<syntaxhighlight lang="elixir">defmodule Penney do
@toss [:Heads, :Tails]
def game(score \\ {0,0})
def game({iwin, ywin}=score) do
IO.puts "Penney game score I : #{iwin}, You : #{ywin}"
[i, you] = @toss
coin = Enum.random(@toss)
IO.puts "#{i} I start, #{you} you start ..... #{coin}"
{myC, yC} = setup(coin)
seq = for _ <- 1..3, do: Enum.random(@toss)
IO.write Enum.join(seq, " ")
{winner, score} = loop(seq, myC, yC, score)
IO.puts "\n #{winner} win!\n"
game(score)
end
defp setup(:Heads) do
myC = Enum.shuffle(@toss) ++ [Enum.random(@toss)]
joined = Enum.join(myC, " ")
IO.puts "I chose : #{joined}"
{myC, yourChoice}
end
defp setup(:Tails) do
yC = yourChoice
myC = (@toss -- [Enum.at(yC,1)]) ++ Enum.take(yC,2)
joined = Enum.join(myC, " ")
IO.puts "I chose : #{joined}"
{myC, yC}
end
defp yourChoice do
IO.write "Enter your choice (H/T) "
choice = read([])
IO.puts "You chose: #{Enum.join(choice, " ")}"
choice
end
defp read([_,_,_]=choice), do: choice
defp read(choice) do
case IO.getn("") |> String.upcase do
"H" -> read(choice ++ [:Heads])
"T" -> read(choice ++ [:Tails])
_ -> read(choice)
end
end
defp loop(myC, myC, _, {iwin, ywin}), do: {"I", {iwin+1, ywin}}
defp loop(yC, _, yC, {iwin, ywin}), do: {"You", {iwin, ywin+1}}
defp loop(seq, myC, yC, score) do
append = Enum.random(@toss)
IO.write " #{append}"
loop(tl(seq)++[append], myC, yC, score)
end
end
 
Penney.game</syntaxhighlight>
 
{{out}}
<pre>
Penney game score I : 0, You : 0
Heads I start, Tails you start ..... Heads
I chose : Tails Heads Heads
Enter your choice (H/T) TTH
You chose: Tails Tails Heads
Heads Heads Tails Tails Tails Tails Heads
You win!
 
Penney game score I : 0, You : 1
Heads I start, Tails you start ..... Tails
Enter your choice (H/T) THT
You chose: Tails Heads Tails
I chose : Tails Tails Heads
Tails Heads Heads Heads Tails Tails Heads
I win!
 
Penney game score I : 1, You : 1
Heads I start, Tails you start ..... Tails
Enter your choice (H/T)
</pre>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: arrays ascii io kernel math prettyprint random sequences
strings ;
IN: rosetta-code.penneys-game
 
! Generate a random boolean.
: t|f ( -- t/f )
1 random-bits 0 = ;
 
! Checks whether the sequence chosen by the human is valid.
: valid-input? ( seq -- ? )
[ [ CHAR: H = ] [ CHAR: T = ] bi or ] filter length 3 = ;
 
! Prompt the human player for a sequence.
: input-seq ( -- seq )
"Please input a 3-long sequence of H or T (heads or tails)."
print "Example: HTH" print "> " write readln >upper >array ;
 
! Get the human player's input sequence with error checking.
: get-input ( -- seq )
t [ drop input-seq dup valid-input? not ] loop ;
! Add a random coin flip to a vector.
: flip-coin ( vector -- vector' )
t|f CHAR: H CHAR: T ? over push ;
 
! Generate a random 3-long sequence of coin flips.
: rand-seq ( -- seq )
V{ } clone 3 [ flip-coin ] times ;
! Generate the optimal sequence response to a given sequence.
: optimal ( seq1 -- seq2 )
[ second dup CHAR: H = [ CHAR: T ] [ CHAR: H ] if ]
[ first ] [ second ] tri 3array nip dup
"The computer chose " write >string write "." print ;
 
! Choose a random sequence for the computer and report what
! was chosen.
: computer-first ( -- seq )
"The computer picks a sequence first and chooses " write
rand-seq dup >string write "." print >array ;
 
! The human is prompted to choose any sequence with no
! restrictions.
: human-first ( -- seq )
"You get to go first." print get-input ;
 
! Forbid the player from choosing the same sequence as the
! computer.
: human-second ( cseq -- cseq hseq )
get-input [ 2dup = not ]
[ drop
"You may not choose the same sequence as the computer."
print get-input
] until ;
 
! Display a message introducing the game.
: welcome ( -- )
"Welcome to Penney's Game. The computer or the player" print
"will be randomly selected to choose a sequence of" print
"three coin tosses. The sequence will be shown to the" print
"opponent, and then he will choose a sequence." print nl
"Then, a coin will be flipped until the sequence" print
"matches the last three coin flips, and a winner" print
"announced." print nl ;
 
! Check for human victory.
: human-won? ( cseq hseq coin-flips -- ? )
3 tail* >array = nip ;
! Check for computer victory.
: computer-won? ( cseq hseq coin-flips -- ? )
3 tail* >array pick = 2nip ;
 
! Flip a coin until a victory is detected. Then, inform the
! player who won.
: flip-coins ( cseq hseq -- )
"Flipping coins..." print
rand-seq [ 3dup [ human-won? ] [ computer-won? ] 3bi or ]
[ flip-coin ] until dup >string print human-won?
[ "You won!" print ] [ "The computer won." print ] if ;
 
! Randomly choose a player to choose their sequence first.
! Then play a full round of Penney's Game.
: start-game ( -- )
welcome t|f [ human-first dup optimal swap ]
[ computer-first human-second ] if flip-coins ;
start-game</syntaxhighlight>
{{out}}
<pre>
Welcome to Penney's Game. The computer or the player
will be randomly selected to choose a sequence of
three coin tosses. The sequence will be shown to the
opponent, and then he will choose a sequence.
 
Then, a coin will be flipped until the sequence
matches the last three coin flips, and a winner
announced.
 
You get to go first.
Please input a 3-long sequence of H or T (heads or tails).
Example: HTH
> car
Please input a 3-long sequence of H or T (heads or tails).
Example: HTH
> htht
Please input a 3-long sequence of H or T (heads or tails).
Example: HTH
> tth
The computer chose HTT.
Flipping coins...
THHTHTHTHHTHHTHHTT
The computer won.
 
Welcome to Penney's Game. The computer or the player
will be randomly selected to choose a sequence of
three coin tosses. The sequence will be shown to the
opponent, and then he will choose a sequence.
 
Then, a coin will be flipped until the sequence
matches the last three coin flips, and a winner
announced.
 
The computer picks a sequence first and chooses HHH.
Please input a 3-long sequence of H or T (heads or tails).
Example: HTH
> hhh
You may not choose the same sequence as the computer.
Please input a 3-long sequence of H or T (heads or tails).
Example: HTH
> hht
Flipping coins...
HHH
The computer won.
</pre>
 
 
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
<syntaxhighlight lang="freebasic">
Sub Jugador_elige(secuencia As String)
Dim As String eleccion
Dim As Byte valido, i
Do
valido = True
Print !"\nIngresa una secuencia de 3 opciones, cada una de ellas H o T:";
Input " > ", secuencia
If Len(secuencia) <> 3 Then valido = False
If valido Then
For i = 1 To 3
eleccion = Mid(Ucase(secuencia), i, 1)
If eleccion <> "H" And eleccion <> "T" Then valido = False
Next
End If
Loop Until valido
End Sub
 
Function Aleatorio() As String
Dim As Byte eleccion, i
Dim As String tirada
tirada = ""
For i = 1 To 3
eleccion = Rnd And 1
If eleccion Then tirada += "H" Else tirada += "T"
Next
Return tirada
End Function
 
Function Optima(secuencia As String) As String
If Mid(secuencia, 2, 1) = "H" Then
Optima = "T" + Left(secuencia, 2)
Else
Optima = "H" + Left(secuencia, 2)
End If
End Function
 
Randomize Timer
Dim As String jugador, computador, otro
Dim Shared As String secuencia
Dim As Boolean ganador, valido
Do
Cls
Color 11
Print "*** Penney's Game ***" & Chr(10)
Color 7
Print "Cara eliges primero, cruz yo elijo primero."
Print "Y es... ";
Sleep 300
Dim As Double ht: ht = Rnd(0 - Timer) And 1
If ht Then
Print "­cara!"
Jugador_elige(jugador)
computador = Optima(jugador)
Print !"\nYo elijo "; computador; !".\n"
Else
Print "­cruz!"
computador = Aleatorio
Print !"\nYo elijo "; computador; !".\n"
Jugador_elige(jugador)
End If
Print "Comenzando el juego ..."
secuencia = ""
ganador = False
Do
Sleep 200
Dim As Integer lanzar = Rnd And 1
If lanzar Then
secuencia += "H"
Print "H ";
Else
Print "T ";
secuencia += "T"
End If
If Right(secuencia, 3) = computador Then
Print !"\n­Gan‚!\n"
ganador = True
Else
If Right(secuencia, 3) = jugador Then
Print !"\n­Felicidades! Ganaste.\n"
ganador = True
End If
End If
Loop Until ganador
Do
valido = False
Input "¨Otra ronda? (S/N) ", otro
If Instr("SYN", Ucase(otro)) Then valido = True
Loop Until valido
Loop Until Ucase(otro) = "N"
Print !"\n­Gracias por jugar!"
Sleep
End
</syntaxhighlight>
 
 
=={{header|Go}}==
<syntaxhighlight lang="go">
package main
import "fmt"
import "math/rand"
func main(){
var a1,a2,a3,y,match,j,k int
var inp string
y=1
for y==1{
fmt.Println("Enter your sequence:")
fmt.Scanln(&inp)
var Ai [3] int
var user [3] int
for j=0;j<3;j++{
if(inp[j]==104){
user[j]=1
}else{
user[j]=0
}
}
for k=0;k<3;k++{
Ai[k]=rand.Intn(2)
}
for user[0]==Ai[0]&&user[1]==Ai[1]&&user[2]==Ai[2]{
for k=0;k<3;k++{
Ai[k]=rand.Intn(2)
}
}
fmt.Println("You gave the sequence:")
printht(user)
fmt.Println()
fmt.Println("The computer generated sequence is:")
printht(Ai)
fmt.Println()
a1=rand.Intn(2)
a2=rand.Intn(2)
a3=rand.Intn(2)
fmt.Print("The generated sequence is:")
printh(a1)
printh(a2)
printh(a3)
match=0
for match==0{
if(matches(user,a1,a2,a3)==1){
fmt.Println()
fmt.Println("You have won!!!")
match=1
}else if(matches(Ai,a1,a2,a3)==1){
fmt.Println()
fmt.Println("You lost!! Computer wins")
match=1
}else{
a1=a2
a2=a3
a3=rand.Intn(2)
printh(a3)
}
}
fmt.Println("Do you want to continue(0/1):")
fmt.Scanln(&y)
}
}
func printht(a [3] int) int{
var i int
for i=0;i<3;i++{
if(a[i]==1){
fmt.Print("h")
}else{
fmt.Print("t")
}
}
return 1
}
func printh(a int) int{
if(a==1){
fmt.Print("h")
}else{
fmt.Print("t")
}
return 1
}
func matches(a [3] int,p int,q int,r int) int{
if(a[0]==p&&a[1]==q&&a[2]==r){
return 1
}else{
return 0
}
}
</syntaxhighlight>
Output:
<pre>
Enter your sequence:
tht
You gave the sequence:
tht
The computer generated sequence is:
hhh
The generated sequence is:hhtht
You have won!!!
Do you want to continue(0/1):
</pre>
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import qualified Data.List as L
import System.IO
import System.Random
Line 555 ⟶ 1,831:
stdgen <- getStdGen
let (cpuFirst, genA) = random stdgen
game cpuFirst 0 0 genA</langsyntaxhighlight>
{{out}}
<pre>
Line 578 ⟶ 1,854:
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight Jlang="j">require 'format/printf numeric'
 
randomize '' NB. randomize seed for new session
Line 605 ⟶ 1,881:
'Toss sequence is %s' printf < (3 + <./ Result) {. Tosses
echo ('No result';'You win!';'Computer won!') {::~ *-/ Result
)</langsyntaxhighlight>
'''Usage:'''
<langsyntaxhighlight Jlang="j"> playPenney''
Computer chose TTT
Choose a sequence of three coin tosses (H/T):
Line 618 ⟶ 1,894:
Computer chose HHT
Toss sequence is HTHTT
You win!</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.*;
 
public class PenneysGame {
Line 672 ⟶ 1,948:
return s;
}
}</langsyntaxhighlight>
 
<pre>Computer chooses HTH
Line 691 ⟶ 1,967:
Computer wins!</pre>
 
=={{header|Perl 6Julia}}==
'''Functions'''
<lang perl6>enum Coin <Heads Tails>;
<syntaxhighlight lang="julia">
enum Yay <Yay Good Super Hah Ooh Yipee Sweet Cool Yes Haha>;
const SLEN = 3
enum Boo <Drat Darn Crumb Oops Rats Bah Criminy Argh Shards>;
enum Bozo «'Dude' 'Cha' 'Bzzt' 'Hey' 'Silly dilly' 'Say what!?' 'You numbskull'»;
 
autobet() = randbool(SLEN)
sub flipping {
function autobet(ob::BitArray{1})
for 1..4 {
rlen = length(ob)
print "-\b"; sleep .1;
2 < rlen || printreturn "\\\b"; sleep .1;~ob
3 < rlen print "|\b";| return sleep[~ob[2], .ob[1;:2]]
opt = falses(rlen)
print "/\b"; sleep .1;
opt[1] = true
opt[end-1:end] = true
ob != opt || return ~opt
return opt
end
autobet(ob::Array{Bool,1}) = autobet(convert(BitArray{1}, ob))
 
function pgencode{T<:String}(a::T)
b = uppercase(a)
0 < length(b) || return trues(0)
!ismatch(r"[^HT]+", b) || error(@sprintf "%s is not a HT sequence" a)
b = split(b, "")
b .== "H"
end
pgdecode(a::BitArray{1}) = join([i ? "H" : "T" for i in a], "")
 
function humanbet()
b = ""
while length(b) != SLEN || ismatch(r"[^HT]+", b)
print("Your bet? ")
b = uppercase(chomp(readline()))
end
return b
end
</syntaxhighlight>
 
'''Game Setup'''
<syntaxhighlight lang="julia">
println("Playing Penney's Game Against the computer.")
 
if randbool()
mach = autobet()
println(@sprintf "The computer bet first, chosing %s." pgdecode(mach))
println("Now you can bet.")
human = pgencode(humanbet())
else
println("You bet first.")
human = pgencode(humanbet())
mach = autobet(human)
end
print(@sprintf "You bet %s " pgdecode(human))
println(@sprintf "and the computer bet %s." pgdecode(mach))
</syntaxhighlight>
 
'''Game Play'''
<syntaxhighlight lang="julia">
pg = randbool(SLEN)
pgtail = copy(pg)
while pgtail != mach && pgtail != human
push!(pg, randbool())
pgtail = [pgtail[2:end], pg[end]]
end
 
println(@sprintf("This game lasted %d turns yielding\n %s",
length(pg), pgdecode(pg)))
 
if human == mach
println("so you and the computer tied.")
elseif pgtail == mach
println("so the computer won.")
else
println("so you won.")
end
</syntaxhighlight>
 
{{output}}
<pre>
$ julia penneys_game.jl
Playing Penney's Game Against the computer.
You bet first.
Your bet? tht
You bet THT and the computer bet TTH.
This game lasted 10 turns yielding
HHTTTTTTTH
so the computer won.
 
$ julia penneys_game.jl
Playing Penney's Game Against the computer.
You bet first.
Your bet? hhh
You bet HHH and the computer bet THH.
This game lasted 8 turns yielding
TTHTHTHH
so the computer won.
 
$ julia penneys_game.jl
Playing Penney's Game Against the computer.
The computer bet first, chosing HTT.
Now you can bet.
Your bet? hht
You bet HHT and the computer bet HTT.
This game lasted 6 turns yielding
TTHHHT
so you won.
 
$ julia penneys_game.jl
Playing Penney's Game Against the computer.
The computer bet first, chosing HTT.
Now you can bet.
Your bet? hht
You bet HHT and the computer bet HTT.
This game lasted 6 turns yielding
THTHHT
so you won.
 
$ julia penneys_game.jl
Playing Penney's Game Against the computer.
You bet first.
Your bet? htt
You bet HTT and the computer bet HHT.
This game lasted 10 turns yielding
TTTTHHHHHT
so the computer won.
</pre>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.2.10
 
import java.util.Random
 
val rand = Random()
 
val optimum = mapOf(
"HHH" to "THH", "HHT" to "THH", "HTH" to "HHT", "HTT" to "HHT",
"THH" to "TTH", "THT" to "TTH", "TTH" to "HTT", "TTT" to "HTT"
)
 
fun getUserSequence(): String {
println("A sequence of three H or T should be entered")
var userSeq: String
do {
print("Enter your sequence: ")
userSeq = readLine()!!.toUpperCase()
}
while (userSeq.length != 3 || userSeq.any { it != 'H' && it != 'T' })
return userSeq
}
 
fun getComputerSequence(userSeq: String = ""): String {
sub your-choice($p is copy) {
val compSeq = if(userSeq == "")
loop (my @seq; @seq != 3; $p = "{Bozo.pick}! Please pick exactly 3: ") {
@seqString(CharArray(3) ={ if prompt($p)rand.uc.combnextInt(/2) H== |0) 'T' /).map:else {'H' })
else
when 'H' { Heads }
when 'T' { Tails }optimum[userSeq]!!
println("Computer's sequence: $compSeq")
return compSeq
}
 
fun main(args: Array<String>) {
var userSeq: String
var compSeq: String
val r = rand.nextInt(2)
if (r == 0) {
println("You go first")
userSeq = getUserSequence()
println()
compSeq = getComputerSequence(userSeq)
}
else {
println("Computer goes first")
compSeq = getComputerSequence()
println()
userSeq = getUserSequence()
}
 
println()
val coins = StringBuilder()
while (true) {
val coin = if (rand.nextInt(2) == 0) 'H' else 'T'
coins.append(coin)
println("Coins flipped: $coins")
val len = coins.length
if (len >= 3) {
val seq = coins.substring(len - 3, len)
if (seq == userSeq) {
println("\nYou win!")
return
}
else if (seq == compSeq) {
println("\nComputer wins!")
return
}
}
Thread.sleep(2000) // wait two seconds for next flip
}
@seq;
}
</syntaxhighlight>
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;
 
Sample game where computer goes first:
my @yours;
<pre>
my @mine;
Computer goes first
Computer's sequence: HTT
 
A sequence of three H or T should be entered
if $flip == $mefirst {
Enter your sequence: HHT
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: ");
Coins flipped: H
while @yours eqv @mine {
Coins flipped: HH
say "{Bozo.pick}! We'd both win at the same time if you pick that!";
Coins flipped: HHT
@yours = your-choice("Pick something different from me: ");
 
}
You win!
say "So, you'll win if we see: ", @yours;
</pre>
 
Sample game where user goes first:
<pre>
You go first
A sequence of three H or T should be entered
Enter your sequence: HTH
 
Computer's sequence: HHT
 
Coins flipped: T
Coins flipped: TT
Coins flipped: TTT
Coins flipped: TTTT
Coins flipped: TTTTH
Coins flipped: TTTTHT
Coins flipped: TTTTHTH
 
You win!
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
function penny_game()
local player, computer = "", ""
function player_choose()
io.write( "Enter your sequence of three H and/or T: " )
local t = io.read():upper()
if #t > 3 then t = t:sub( 1, 3 )
elseif #t < 3 then return ""
end
 
for i = 1, 3 do
c = t:sub( i, i )
if c ~= "T" and c ~= "H" then
print( "Just H's and T's!" )
return ""
end
end
return t
end
function computer_choose()
local t = ""
if #player > 0 then
if player:sub( 2, 2 ) == "T" then
t = "H"
else
t = "T";
end
t = t .. player:sub( 1, 2 )
else
for i = 1, 3 do
if math.random( 2 ) == 1 then
t = t .. "H"
else
t = t .. "T"
end
end
end
return t
end
if math.random( 2 ) == 1 then
computer = computer_choose()
io.write( "My sequence is: " .. computer .. "\n" )
while( true ) do
player = player_choose()
if player:len() == 3 then break end
end
else
while( true ) do
player = player_choose()
if player:len() == 3 then break end
end
computer = computer_choose()
io.write( "My sequence is: " .. computer .. "\n" )
end
local coin, i = "", 1
while( true ) do
if math.random( 2 ) == 1 then
coin = coin .. "T"
io.write( "T" )
else
coin = coin .. "H"
io.write( "H" )
end
if #coin > 2 then
local seq = coin:sub( i, i + 2 )
i = i + 1
if seq == player then
print( "\nPlayer WINS!!!" )
return 1
elseif seq == computer then
print( "\nComputer WINS!!!" )
return -1
end
end
end
end
math.randomseed( os.time() )
local cpu, user = 0, 0
repeat
r = penny_game()
if r > 0 then
user = user + 1
else
cpu = cpu + 1
end
print( "Player: " .. user .. " CPU: " .. cpu )
io.write( "Play again (Y/N)? " )
r = io.read()
until( r == "N" or r == "n" )
</syntaxhighlight>
{{out}}
<pre>
>lua -e "io.stdout:setvbuf 'no'" "penny.lua"
My sequence is: TTH
Enter your sequence of three H and/or T: HHT
TTTTH
Computer WINS!!!
Player: 0 CPU: 1
Play again (Y/N)? y
Enter your sequence of three H and/or T: HTH
My sequence is: HHT
THTTHTH
Player WINS!!!
Player: 1 CPU: 1
Play again (Y/N)? y
My sequence is: HHH
Enter your sequence of three H and/or T: TTH
HTTHHTH
Player WINS!!!
Player: 2 CPU: 1
Play again (Y/N)? n
>Exit code: 0
</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">randomTorH = function()
if rnd < 0.5 then return "T" else return "H"
end function
 
if rnd < 0.5 then
playerSeq = input("Input your sequence (e.g. HTH): ").upper
if playerSeq[1] == "T" then compSeq = "H" else compSeq = "T"
compSeq = compSeq + playerSeq[:2]
print "I choose: " + compSeq
else
compSeq = randomTorH + randomTorH + randomTorH
print "I choose: " + compSeq
playerSeq = input("Input your sequence (e.g. HTH): ").upper
end if
 
print "Here we go..."
seq = ""
while true
flip = randomTorH
print flip
seq = seq + flip
if seq[-3:] == playerSeq then
print "You win!"
break
else if seq[-3:] == compSeq then
print "I win!"
break
end if
wait
end while</syntaxhighlight>
 
{{out}}
<pre>Input your sequence (e.g. HTH): HTH
I choose: HHT
Here we go...
T
H
T
H
You win!</pre>
 
<pre>I choose: TTH
Input your sequence (e.g. HTH):
THH
Here we go...
H
T
T
T
T
H
I win!</pre>
 
=={{header|Nim}}==
{{trans|Python}}
<syntaxhighlight lang="nim">import os, random, sequtils, strutils
 
randomize()
 
let first = sample([false, true])
 
var you, me = ""
if first:
me = newSeqWith(3, sample("HT")).join()
echo "I choose first and will win on first seeing $# in the list of tosses." % me
while you.len != 3 or not allCharsInSet(you, {'H', 'T'}) or you == me:
stdout.write "What sequence of three Heads/Tails will you win with? "
you = stdin.readLine()
else:
while you.len != 3 or not allCharsInSet(you, {'H', 'T'}):
echo "After you: what sequence of three Heads/Tails will you win with? "
you = stdin.readLine()
me = (if you[1] == 'T': 'H' else: 'T') & you[0..1]
echo "I win on first seeing $# in the list of tosses" % me
 
var rolled = ""
stdout.write "Rolling:\n "
while true:
rolled.add sample("HT")
stdout.write rolled[^1]
stdout.flushFile()
if rolled.endsWith(you):
echo "\n You win!"
break
elif rolled.endsWith(me):
echo "\n I win!"
break
sleep(1000)</syntaxhighlight>
 
{{out}}
<pre>After you: what sequence of three Heads/Tails will you win with?
HTH
I win on first seeing HHT in the list of tosses
Rolling:
TTHTTTTHHT
I win!</pre>
 
=={{header|Pascal}}==
<syntaxhighlight lang="pascal">PROGRAM Penney;
 
TYPE
CoinToss = (heads, tails);
Sequence = array [1..3] of CoinToss;
Player = record
bet: Sequence;
score: integer;
end;
 
VAR
Human, Computer: Player;
Rounds, Count: integer;
 
Function TossCoin: CoinToss;
{ Returns heads or tails at random }
Begin
if random(2) = 1 then TossCoin := Heads
else TossCoin := tails
End;
 
Procedure PutToss(toss: CoinToss);
{ Outputs heads or tails as a letter }
Begin
if toss = heads then write('H')
else write('T')
End;
 
Function GetToss: CoinToss;
{ Reads H or T from the keyboard in either lettercase }
var c: char;
Begin
{ Keep reading characters until we get an appropriate letter }
repeat read(c) until c in ['H', 'h', 'T', 't'];
{ Interpret the letter }
if c in ['H', 'h'] then GetToss := heads
else GetToss := tails
End;
 
Procedure ShowSequence(tosses: Sequence);
{ Outputs three coin tosses at once }
Var
i: integer;
Begin
for i := 1 to 3 do PutToss(tosses[i])
End;
 
Procedure ReadSequence(var tosses: Sequence);
{ Accepts three coin tosses from the keyboard }
Var i: integer;
Begin
{ Get the 3 letters }
for i := 1 to 3 do tosses[i] := GetToss;
{ Ignore the rest of the line }
readln
End;
 
Function Optimum(opponent: Sequence): Sequence;
{ Generates the optimum sequence against an opponent }
Begin
case opponent[2] of
heads: Optimum[1] := tails;
tails: Optimum[1] := heads
end;
Optimum[2] := opponent[1];
Optimum[3] := opponent[2]
End;
 
Function RandomSequence: Sequence;
{ Generates three random coin tosses }
Var
i: integer;
Begin
for i := 1 to 3 do RandomSequence[i] := TossCoin
End;
 
Function Match(first, second: Sequence): Boolean;
{ Detects whether a sequence of tosses matches another }
Var
different: boolean;
i: integer;
Begin
different := false;
i := 1;
while (i <= 3) and not different do begin
if not (first[i] = second[i]) then different := true;
i := i + 1
end;
Match := not different
End;
 
Procedure PlayRound(var human, computer: Player);
{ Shows coin tosses and announces the winner }
Var
{ We only ever need to store the 3 most recent tosses in memory. }
tosses: Sequence;
Begin
{ Start with the first three tosses }
write('Tossing the coin: ');
tosses := RandomSequence;
ShowSequence(tosses);
{ Keep tossing the coin until there is a winner. }
while not (Match(human.bet, tosses) or Match(computer.bet, tosses)) do begin
tosses[1] := tosses[2];
tosses[2] := tosses[3];
tosses[3] := TossCoin;
PutToss(tosses[3])
end;
{ Update the winner's score and announce the winner }
writeln;
writeln;
if Match(human.bet, tosses) then begin
writeln('Congratulations! You won this round.');
human.score := human.score + 1;
writeln('Your new score is ', human.score, '.')
end
else begin
writeln('Yay, I won this round!');
computer.score := computer.score + 1;
writeln('My new score is ', computer.score, '.')
end
End;
 
{ Main Algorithm }
 
BEGIN
 
{ Welcome the player }
writeln('Welcome to Penney''s game!');
writeln;
write('How many rounds would you like to play? ');
readln(Rounds);
writeln;
writeln('Ok, let''s play ', Rounds, ' rounds.');
 
{ Start the game }
randomize;
Human.score := 0;
Computer.score := 0;
 
for Count := 1 to Rounds do begin
 
writeln;
writeln('*** Round #', Count, ' ***');
writeln;
 
{ Choose someone randomly to pick the first sequence }
if TossCoin = heads then begin
write('I''ll pick first this time.');
Computer.bet := RandomSequence;
write(' My sequence is ');
ShowSequence(Computer.bet);
writeln('.');
repeat
write('What sequence do you want? ');
ReadSequence(Human.bet);
if Match(Human.bet, Computer.bet) then
writeln('Hey, that''s my sequence! Think for yourself!')
until not Match(Human.bet, Computer.bet);
ShowSequence(Human.bet);
writeln(', huh? Sounds ok to me.')
end
else begin
write('You pick first this time. Enter 3 letters H or T: ');
ReadSequence(Human.bet);
Computer.bet := Optimum(Human.bet);
write('Ok, so you picked ');
ShowSequence(Human.bet);
writeln;
write('My sequence will be ');
ShowSequence(Computer.bet);
writeln
end;
 
{ Then we can actually play the round }
writeln('Let''s go!');
writeln;
PlayRound(Human, Computer);
writeln;
writeln('Press ENTER to go on...');
readln
 
end;
 
{ All the rounds are finished; time to decide who won }
writeln;
writeln('*** End Result ***');
writeln;
if Human.score > Computer.score then writeln('Congratulations! You won!')
else if Computer.score > Human.score then writeln('Hooray! I won')
else writeln('Cool, we tied.');
writeln;
writeln('Press ENTER to finish.');
readln
 
END.</syntaxhighlight>
 
{{out}}
<pre>Welcome to Penney's game!
 
How many rounds would you like to play? 3
 
Ok, let's play 3 rounds.
 
*** Round #1 ***
 
I'll pick first this time. My sequence is TTH.
What sequence do you want? H tt
HTT, huh? Sounds ok to me.
Let's go!
 
Tossing the coin: HHHHTHHHHHHTHHHHTHHTHTHTT
 
Congratulations! You won this round.
Your new score is 1.
 
Press ENTER to go on...
 
 
*** Round #2 ***
 
I'll pick first this time. My sequence is THT.
What sequence do you want? tth
TTH, huh? Sounds ok to me.
Let's go!
 
Tossing the coin: THT
 
Yay, I won this round!
My new score is 1.
 
Press ENTER to go on...
 
 
*** Round #3 ***
 
You pick first this time. Enter 3 letters H or T: h t h
Ok, so you picked HTH
My sequence will be HHT
Let's go!
 
Tossing the coin: HHT
 
Yay, I won this round!
My new score is 2.
 
Press ENTER to go on...
 
 
*** End Result ***
 
Hooray! I won
 
Press ENTER to finish.
</pre>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">
#!usr/bin/perl
use 5.020;
use strict;
use warnings;
 
#Choose who goes first
binaryRand() == 0 ? flipCoin(userFirst()) : flipCoin(compFirst());
 
#Return a randomly generated 1 or 0
sub binaryRand
{
return int(rand(2));
}
#Converts 1's and 0's to H's and T's, respectively.
sub convert
{
my $randNum = binaryRand();
if($randNum == 0)
{
return "T"
}
else {
{
@yours = your-choice("{Boo.pick}! First you choose: ");
sayreturn "OK, you'll win if we see: H", @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) {
#Prompts for and returns a user's sequence of 3
given [$a,$b,$c] {
sub uSeq
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 }
print("Please enter a sequence of 3 of \"H\" and \"T\". EG: HHT\n>");
default { Coin.roll }
my $uString = <STDIN>;
 
while(1)
{
#Make it uppercase and validate input
chomp($uString);
$uString = uc $uString;
#Check length and content (H's and T's only!)
if(length $uString == 3 && (substr($uString, 0, 1) =~ /[HT]/ &&
substr($uString, 1, 1) =~ /[HT]/ &&
substr($uString, 2, 1) =~ /[HT]/))
{
last;
}
else
{
print("Error, try again. \n");
print("Please enter a sequence of 3 of \"H\" and \"T\". EG: HHT\n");
$uString = <STDIN>;
}
}
return $uString;
}
 
#Returns an array with two elements: [0] user's seq, [1] random computer seq.
sleep 1;
sub compFirst
say < OK! Ready? Right... So... Yo!>.pick;
{
sleep .5;
my $cSeq;
say ("Pay attention now!",
#Randomly draw a sequence of 3
"Watch closely!",
for(my $i = 0; "Let's$i do< it...",3; $i++)
{
"You feeling lucky?",
"No$cSeq way= you$cSeq gonna. win this...",convert();
"Can I borrow that coin again?").pick;
sleep 1;
print "Here we go!\n\t";
for Coin.roll(3), &check ...^ :!defined {
flipping;
print "$_ ";
}
 
}</lang>
print("The computer guesses first:\ncomp- $cSeq\n");
my $uSeq = uSeq();
print("user- $uSeq\n");
my @seqArr = ($uSeq, $cSeq);
return @seqArr;
}
 
#Returns an array with two elements: [0] user's seq, [1] optimal computer seq.
sub userFirst
{
print("The user quesses first:\n");
my $uSeq = uSeq();
my $cSeq;
#Generate the optimal sequence based on $uSeq
my $middle = substr($uSeq, 1, 1);
$middle eq "H" ? $cSeq = "T" : $cSeq = "H";
$cSeq = $cSeq . substr($uSeq, 0, 2);
 
print("user- $uSeq\ncomp- $cSeq\n");
my @seqArr = ($uSeq, $cSeq);
return @seqArr;
}
 
#Flips a coin, checking both sequences against the contents of the given array
sub flipCoin
{
my ($uSeq, $cSeq) = @_;
my $coin;
while(1)
{
$coin = $coin . convert();
if($coin =~ m/$uSeq/)
{
print("The sequence of tosses was: $coin\n");
say("The player wins! ");
last;
}
elsif($coin =~ m/$cSeq/)
{
print("The sequence of tosses was: $coin\n");
say("The computer wins! ");
last;
}
}
}
</syntaxhighlight>
{{out}}
<pre>
Note: the actual run displays a little coin-flipping animation, but that won't show up here:
The computer guesses first:
<pre>Heads I start, tails you start, flipping...
comp- HHH
Heads
Please enter a sequence of 3 of "H" and "T". EG: HHT
Yipee! I get to choose first, and I choose: Heads Heads Tails
>thh
Now you gotta choose: tth
user- THH
So, you'll win if we see: Tails Tails Heads
The sequence of tosses was: HTTHTTHTHTHTTTHTTTHH
Yo!
The player wins!
Can I borrow that coin again?
================================================
Here we go!
The user quesses first:
Tails Tails Tails Tails Tails Heads
Please enter a sequence of 3 of "H" and "T". EG: HHT
Argh, you win, but I'll beat you next time!
>hht
Wanna play again? y
user- HHT
Tails I start, heads you start, flipping...
comp- HHH
Tails
The sequence of tosses was: THHH
Yes! I get to choose first, and I choose: Heads Tails Tails
The computer wins!
Now you gotta choose: H T T
</pre>
Dude! We'd both win at the same time if you pick that!
 
Pick something different from me: heads tails tails
=={{header|Phix}}==
Silly dilly! We'd both win at the same time if you pick that!
{{trans|C}}
Pick something different from me: h,h,h
Robert's robot's name is Robort.
So, you'll win if we see: Heads Heads Heads
<syntaxhighlight lang="phix">function trio(integer pick)
OK!
return substitute_all(sprintf("%03b",pick),"10","HT")
You feeling lucky?
end function
Here we go!
 
Tails Tails Tails Heads Heads Heads
function getuser(integer bot)
Drat, you win, but I'll beat you next time!
integer user
Wanna play again? y
while 1 do
Heads I start, tails you start, flipping...
user = 8 -- (a bit that clears after 3 shifts)
Tails
printf(1,"Enter your sequence of 3 (H/T):");
Shards! First you choose: tht
while user>7 do
OK, you'll win if we see: Tails Heads Tails
integer c = upper(wait_key())
In that case, I'll just randomly choose: Tails Tails Heads
if c=#1B then abort(0) end if -- (Escape)
Right...
if find(c,"HT") then
Pay attention now!
puts(1,c)
Here we go!
user = and_bits(user*2+(c='H'),0b111111)
Heads Tails Tails Heads
end if
Hah, I win, and you lose!
end while
Wanna play again? n</pre>
if user!=bot then exit end if
printf(1,"\nYou may not pick the same as Robort!\n")
end while
printf(1,"\n")
return user
end function
function getbot(int user)
int bot = iff(user=-1?rand(8)-1
:4-and_bits(user,2)*2+floor(user/2))
printf(1,"Robort picks %s\n", {trio(bot)})
return bot
end function
function rungame(integer user, bot)
/* We only need to store the last 3 tosses, as 0..7 */
int last3 = rand(8)-1
printf(1,"Rolling: %s",{trio(last3)})
while 1 do
if user=last3 then
printf(1,"\nUser wins!\n")
return 1
elsif bot=last3 then
printf(1,"\nRobort wins!\n")
return 0
end if
last3 = and_bits(last3,3)*2+(rand(2)=1)
printf(1,"%c", iff(and_bits(last3,1) ? 'H' : 'T'))
sleep(0.5)
end while
end function
procedure main()
integer playerwins = 0,
totalgames = 0,
robortwins = 0
/* Just use ctrl-c or Escape to exit */
while 1 do
integer user = -1,
bot = -1
printf(1,"\n")
if rand(2)=1 then
bot = getbot(-1)
user = getuser(bot)
else
user = getuser(-1)
bot = getbot(user)
end if
playerwins += rungame(user, bot)
totalgames += 1
robortwins = totalgames-playerwins
printf(1,"Robort:%d You:%d out of %d games\n",
{robortwins, playerwins, totalgames})
printf(1,"==================================\n")
end while
end procedure
main()</syntaxhighlight>
{{out}}
<pre>
Robort picks TTT
Enter your sequence of 3 (H/T):TTT
You may not pick the same as Robort!
Enter your sequence of 3 (H/T):HHH
Rolling: HTTT
Robort wins!
Robort:1 You:0 out of 1 games
==================================
 
Enter your sequence of 3 (H/T):HTH
Robort picks HHT
Rolling: THTTTHHHHHHT
Robort wins!
Robort:2 You:0 out of 2 games
==================================
 
Enter your sequence of 3 (H/T):TTH
Robort picks HTT
Rolling: THTT
Robort wins!
Robort:3 You:0 out of 3 games
==================================
 
Enter your sequence of 3 (H/T):HTH
Robort picks HHT
Rolling: TTHHHHHHT
Robort wins!
Robort:4 You:0 out of 4 games
==================================
 
Enter your sequence of 3 (H/T):HHT
Robort picks THH
Rolling: TTHH
Robort wins!
Robort:5 You:0 out of 5 games
==================================
 
Enter your sequence of 3 (H/T):THH
Robort picks TTH
Rolling: THH
User wins!
Robort:5 You:1 out of 6 games
==================================
</pre>
 
=={{header|PicoLisp}}==
<syntaxhighlight lang="picolisp">(seed (in "/dev/urandom" (rd 8)))
 
(setq *S (list 0 0))
 
(de toss (N)
(make
(do N
(link (if (rand T) "T" "H")) ) ) )
 
(de comp (A Lst)
(or
(for ((I . L) Lst (cddr L) (cdr L))
(T (fully = A L) I) )
T ) )
 
(de score NIL
(prinl)
(prinl "Total score:")
(prinl "^Iuser: " (car *S))
(prinl "^Icomp: " (cadr *S)) )
 
(de play NIL
(let (C (toss 3) Lst (toss (rand 5 12)) U)
(prinl)
(prin "Select toss: ")
(setq U (in NIL (skip) (line)))
(prinl "Comp toss: " C)
(prinl "Toss: " Lst)
(setq @ (comp U Lst) @@ (comp C Lst) )
(cond
((< @ @@) (prinl "User win.") (inc *S))
((> @ @@) (prinl "Comp win.") (inc (cdr *S)))
(T (prinl "Draw, lets play again.")) )
(score) ) )
 
(de go NIL
(loop
(play)
(T
(prog
(prinl)
(prin "Want play again? Y/N: ")
(= "N" (uppc (in NIL (char)))) ) ) ) )
 
(go)</syntaxhighlight>
{{out}}
<pre>
Select toss: HTH
Comp toss: THT
Toss: HHTHTTHT
User win.
 
Total score:
user: 1
comp: 0
 
Want play again? Y/N: y
 
Select toss: HHH
Comp toss: HTH
Toss: THHHTTHTHHH
User win.
 
Total score:
user: 2
comp: 0
 
Want play again? Y/N: y
 
Select toss: TTT
Comp toss: THH
Toss: TTHTTHHHHTH
Comp win.
 
Total score:
user: 2
comp: 1
 
Want play again? Y/N: n
</pre>
 
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">play :- rand1(R), game(R).
 
game(h) :-
format('Your turn first!~n'), player_move(P),
response(P,C), format('I am choosing '), maplist(writec, C), nl,
rand3(R), maplist(writec,R),
roll(P, C, R).
 
game(t) :-
rand3(C),
format('I am choosing '), maplist(writec, C), nl,
player_move(P),
rand3(R), maplist(writec, R),
roll(P, C, R).
 
player_move([P1,P2,P3]) :-
read_line_to_codes(user_input,Codes),
maplist(char_code,[P1,P2,P3],Codes).
 
roll(P, _, P) :- format('~nYou Win!~n'), !.
roll(_, C, C) :- format('~nI Win!~n'), !.
 
roll(P, C, [_,A,B]) :-
rand1(R),
coin_s(R,S),
write(S),
roll(P,C,[A,B,R]).
 
response([A,B,_], [C,A,B]) :- opp(A,C).
 
writec(A) :- coin_s(A,A1), write(A1).
rand1(R) :- random(V), round(V,I), coin(I,R).
rand3([R1,R2,R3]) :- rand1(R1), rand1(R2), rand1(R3).
 
coin(0,h). coin(1,t).
coin_s(h, 'H'). coin_s(t, 'T').
opp(h, t). opp(t, h).</syntaxhighlight>
Output:
<pre>
?- play.
I am choosing HTT
|: tth
HHTHHHHHHHHTHHHHTHTHTHTHTHHTT
I Win!
true.
 
?- play.
Your turn first!
|: hht
I am choosing THH
TTHTHH
I Win!
</pre>
 
=={{Headerheader|Python}}==
<langsyntaxhighlight lang="python">from __future__ import print_function
import random
from time import sleep
Line 834 ⟶ 3,099:
print('\n I win!')
break
sleep(1) # For dramatic effect</langsyntaxhighlight>
 
{{out}}
Line 851 ⟶ 3,116:
You win!
>>> </pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ 2 random iff char H else char T ] is flip ( --> c )
 
[ 3 times flip join join ] is randstart ( --> $ )
 
[ -1 split drop
dup -1 peek
char T = iff char H else char T
swap join ] is beststart ( $ --> $ )
 
[ stack ] is playergoal ( --> s )
[ stack ] is computergoal ( --> s )
 
[ say "This is Penney's game."
cr cr
say "We each guess a different "
say "sequence of three coin "
say "flips."
cr
say "I will releatedly flip "
say "a coin until one of the "
say "sequences happens."
cr
say "The winner is the one who "
say "guesses the sequence that "
say "happens first."
cr cr
say "Please enter your sequence "
say "carefully as there is no "
say "error checking."
cr
say 'For example, for "heads '
say 'tails heads" type "HTH".'
cr cr ] is intro ( --> )
 
[ intro
randomise
flip char H = iff
[ say "I start. My guess is: "
randstart dup echo$
computergoal put
cr
$ "Please enter your guess: "
input playergoal put ]
else
[ say "You start. "
$ "Please enter your guess: "
input dup playergoal put
beststart dup
say "I guess: " echo$
computergoal put
cr ]
cr
$ ""
[ flip join
dup -3 split nip dup
computergoal share = iff
[ drop say "I win." ] done
playergoal share = iff
[ say "You win." ] done
again ]
cr
say "The complete sequence was: "
echo$ cr
computergoal release
playergoal release ] is play ( --> )</syntaxhighlight>
 
{{out}}
As a dialogue in the Quackery shell (REPL).
 
<pre>/O> play
...
This is Penney's game.
 
We each guess a different sequence of three coin flips.
I will releatedly flip a coin until one of the sequences happens.
The winner is the one who guesses the sequence that happens first.
 
Please enter your sequence carefully as there is no error checking.
For example, for "heads tails heads" type "HTH".
 
You start. Please enter your guess: HTH
I guess: HHT
 
You win.
The complete sequence was: TTHTH
 
Stack empty.
 
/O> play
...
This is Penney's game.
 
We each guess a different sequence of three coin flips.
I will releatedly flip a coin until one of the sequences happens.
The winner is the one who guesses the sequence that happens first.
 
Please enter your sequence carefully as there is no error checking.
For example, for "heads tails heads" type "HTH".
 
I start. My guess is: HTT
Please enter your guess: HHT
 
You win.
The complete sequence was: HTHTHHHT
 
Stack empty.
 
/O> </pre>
 
=={{header|R}}==
<syntaxhighlight lang="rsplus">#===============================================================
# Penney's Game Task from Rosetta Code Wiki
# R implementation
#===============================================================
 
penneysgame <- function() {
#---------------------------------------------------------------
# Who goes first?
#---------------------------------------------------------------
first <- sample(c("PC", "Human"), 1)
#---------------------------------------------------------------
# Determine the sequences
#---------------------------------------------------------------
if (first == "PC") { # PC goes first
pc.seq <- sample(c("H", "T"), 3, replace = TRUE)
cat(paste("\nI choose first and will win on first seeing", paste(pc.seq, collapse = ""), "in the list of tosses.\n\n"))
human.seq <- readline("What sequence of three Heads/Tails will you win with: ")
human.seq <- unlist(strsplit(human.seq, ""))
} else if (first == "Human") { # Player goest first
cat(paste("\nYou can choose your winning sequence first.\n\n"))
human.seq <- readline("What sequence of three Heads/Tails will you win with: ")
human.seq <- unlist(strsplit(human.seq, "")) # Split the string into characters
pc.seq <- c(human.seq[2], human.seq[1:2]) # Append second element at the start
pc.seq[1] <- ifelse(pc.seq[1] == "H", "T", "H") # Switch first element to get the optimal guess
cat(paste("\nI win on first seeing", paste(pc.seq, collapse = ""), "in the list of tosses.\n"))
}
#---------------------------------------------------------------
# Start throwing the coin
#---------------------------------------------------------------
cat("\nThrowing:\n")
ran.seq <- NULL
while(TRUE) {
ran.seq <- c(ran.seq, sample(c("H", "T"), 1)) # Add a new coin throw to the vector of throws
cat("\n", paste(ran.seq, sep = "", collapse = "")) # Print the sequence thrown so far
if (length(ran.seq) >= 3 && all(tail(ran.seq, 3) == pc.seq)) {
cat("\n\nI win!\n")
break
}
if (length(ran.seq) >= 3 && all(tail(ran.seq, 3) == human.seq)) {
cat("\n\nYou win!\n")
break
}
Sys.sleep(0.5) # Pause for 0.5 seconds
}
}
</syntaxhighlight>
{{out}}
Human first.
 
<pre>
You can choose your winning sequence first.
 
What sequence of three Heads/Tails will you win with: THH
 
I win on first seeing TTH in the list of tosses.
 
Throwing:
 
T
TT
TTT
TTTH
 
I win!
</pre>
PC first
 
<pre>
I choose first and will win on first seeing THH in the list of tosses.
 
What sequence of three Heads/Tails will you win with: TTH
 
Throwing:
 
T
TH
THT
THTT
THTTH
 
You win!
</pre>
 
=={{header|Racket}}==
This does what's required of it from the task... just don't input anything outside the alphabet "htHT" for the human move.
 
<langsyntaxhighlight lang="racket">#lang racket
;; Penney's Game. Tim Brown 2014-10-15
(define (flip . _) (match (random 2) (0 "H") (1 "T")))
Line 891 ⟶ 3,369:
(printf "~a chooses: ~a~%" (car p1) (flips->string (cdr p1)))
(values p1 (cons 'Hom-Sap (get-human-sequence)))]))
(printf "~a wins!~%" (game-on player-1 player-2)))</langsyntaxhighlight>
 
{{out}}
Line 925 ⟶ 3,403:
</pre>
(Nail-biting stuff!)
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.02}}
 
<syntaxhighlight lang="raku" line>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 "$_ ";
}
}</syntaxhighlight>
{{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}}==
The REXX program keeps a running score &nbsp; (number of wins out of so many games played) &nbsp; as well as
<br>as well as allowing the human to pick the number (length) of the coin toss sequence.
 
<br>Extra code was added to ensure a valid response from the human player as well as give an informative error message.
A fair amount of code was added to ensure a valid response from the human player as well as give an informative error message.
<br>The human player is allowed to spell out the '''H''' or '''T''' &nbsp; (as '''heads''' or '''tails''').
 
<br>A feature also added was to allow a seed for the &nbsp; '''random''' &nbsp; BIF to allow repeatability for a game.
The human player is allowed to spell out the &nbsp; '''H''' &nbsp; or &nbsp; '''T''' &nbsp; (as '''heads''' or '''tails''').
<lang rexx>/*REXX program plays Penney's Game, a 2-player coin toss sequence game.*/
 
__=copies('─',9) /*literal for eyecatching fence. */
A feature also added was to allow a seed for the &nbsp; '''random''' &nbsp; BIF to allow repeatability for a game.
signal on halt /*a clean way out if CLBF quits. */
<syntaxhighlight lang="rexx">/*REXX program plays/simulates Penney's Game, a two─player coin toss sequence game. */
parse arg # ? . /*get optional args from the C.L.*/
if__= #==copies('', 9) | #=="," then #=3 /*defaultliteral coinfor sequenceeye─catching lengthfence. */
signal on halt /*a clean way out if CLBF quits. */
if ?\=='' & ?\==',' then call random ,,? /*use seed for RANDOM #s ?*/
wins=0;parse arg # seed . do games=1 /*playobtain aoptional numberarguments offrom Penney'sthe gamesCL*/
if #=='' | #=="," then #= call game 3 /*playNot aspecified? single inningThen ofuse athe gamedefault.*/
if datatype(seed,'W') then call random ,,seed /*use seed for RANDOM #s repeatability.*/
end /*games*/ /*keep at it 'til QUIT or halt.*/
exit wins=0; do games=1 /*sticksimulate a forknumber inof it, wePenney'res donegames. */
call game /*simulate a single inning of a game. */
/*──────────────────────────────────one-line subroutines────────────────*/
end /*games*/ /*keep at it until QUIT or halt. */
halt: say; say __ "Penney's Game was halted."; say; exit 13
r:exit arg ,$; do arg(1); $=$||random(0,1); end; return $ /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1) /*plural*/
halt: say; say __ "Penney's Game was halted."; say; exit 13
/*──────────────────────────────────GAME subroutine─────────────────────*/
gamer: @.=; tosses=@. arg ,$; do arg(1); $=$ || random(100, 9991) /*the/ coin2; toss sequence soend; far. */ return $
toss1=r(1) s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1) /*result: 0=computer, 1=CBLFpluralizer.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
if \toss1 then call randComp /*maybe let the computer go first*/
ifgame: toss1@.=; thentosses=@. say __ "You win the first toss, so you pick your /*the coin toss sequence firstso far." */
toss1= r(1) /*result: 0=computer, 1=CBLF.*/
else say __ "The computer won first toss, the pick was: " @.comp
if \toss1 then call randComp call prompter /*getmaybe thelet human'sthe guesscomputer fromgo C.L.first*/
if toss1 then say __ "You win callthe randCompfirst toss, so you pick your sequence /*get computer's guess if neededfirst.*/"
else say __ "The computer won first toss, the pick was: " /*CBLF: carbon-based life form@. */comp
say __ " your pick:" @.CBLF call prompter /*echoget the human's pickguess tofrom terminalC.L. */
say __ "computer's pick:" @.comp /* " comp.'s " " call randComp " /*get computer's guess if needed.*/
say /* [↓] flip the coin 'til a win /*CBLF: carbon-based life form. */
say __ " your pick:" @.CBLF /*echo human's pick to terminal. */
do flips=1 until pos(@.CBLF,tosses)\==0 | pos(@.comp,tosses)\==0
say __ "computer's pick:" @.comp /* " comp.'s " " " */
tosses=tosses || translate(r(1),'HT',10)
end say /*flips*/ /* [] flip thisthe iscoin 'til a flipping coin,win.*/
do flips=1 until pos(@.CBLF, tosses)\==0 | /* [↓] series ofpos(@.comp, tosses*/)\==0
tosses= tosses || translate( r(1), 'HT', 10)
say __ "The tossed coin series was: " tosses /*show the coin tosses.*/
end /*flips*/ /* [↑] this is a flipping coin,*/
say
/* [↓] series of tosses*/
@@@="won this toss with " flips ' coin tosses.' /*handy literal.*/
if pos(@.CBLF,tosses)\==0 then do; say __ "You"The tossed @@@;coin series wins=wins+1;was: end" tosses
say
else say __ "The computer" @@@
@@@="won this toss with " flips ' coin tosses.'
_=wins; if _==0 then _='no' /*use gooder English.*/
if pos(@.CBLF,tosses)\==0 then do; say __ "You" @@@; wins=wins+1; end
say __ "You've won" _ "game"s(wins) 'out of ' games"."
else say __ "The computer" @@@
say; say copies('╩╦',79%2)'╩'; say /*show eyeball fence.*/
_=wins; if _==0 then _='no'
return
say __ "You've won" _ "game"s(wins) 'out of ' games"."
/*──────────────────────────────────PROMPTER subroutine─────────────────*/
say; say copies('╩╦', 79 % 2)'╩'; say
prompter: oops=__ 'Oops! '; a= /*define some handy REXX literals*/
return
@a_z='ABCDEFG-IJKLMNOPQRS+UVWXYZ' /*the extraneous alphabetic chars*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
p=__ 'Pick a sequence of' # "coin tosses of H or T (Heads or Tails) or Quit:"
prompter: oops= __ 'Oops! '; do until ok; say;a= say p; pull a /*uppercasedefine some handy theREXX answer.literals*/
if@a_z= abbrev('QUITABCDEFG-IJKLMNOPQRS+UVWXYZ',a,1) then exit 1 /*human wantsthe toextraneous quit.alphabetic chars*/
p=__ 'Pick a sequence of' # "coin tosses of H or T (Heads or Tails) or Quit:"
a=space(translate(a,,@a_z',./\;:_'),0) /*elide extraneous chrs*/
b=translate(a,10,'HT') do until ok; say; say p; pull L=length(a) /*tran───►bin;uppercase getthe lenanswer. */
ok=0 if abbrev('QUIT', a, 1) then exit 1 /*the human wants to quit. /*response is OK so far*/
a= space( translate(a,,@a_z',./\;:_'), 0) /*elide extraneous characters. */
select /*verify user response.*/
b= when \datatypetranslate(ba, 10, 'BHT'); L= length(a) then/*translate say───► oopsbin; "Illegalget responselength."*/
ok= when0 \datatype(a,'M') then say oops "Illegal characters in /*the response is OK (so far)." */
when L==0 select then say oops "No choice was given /*verify the user response." */
when L<# when \datatype(b, 'B') then say oops "Not enough coinIllegal choicesresponse."
when L># when \datatype(a, 'M') then say oops "TooIllegal manycharacters coinin choicesresponse."
when aL==@.comp0 then say oops "You can't choose the computer'sthen choice:say oops "No @choice was given.comp"
otherwise when L<# ok=1 then say oops "Not enough coin choices."
end /*select*/when L># then say oops "Too many coin choices."
when a==@.comp then say oops "You can't choose the computer's" ,
end /*until ok*/
@.CBLF=a; @.CBLF!=b /*we have the human's guess now. */ "choice: " @.comp
otherwise ok= 1
return
end /*select*/
/*──────────────────────────────────RANDCOMP subroutine─────────────────*/
end /*until ok*/
randComp: if @.comp\=='' then return /*the computer already has a pick*/
_=@.CBLF! @.CBLF= a; @.CBLF!= b /*we [↓]have usethe best-choicehuman's algorithmguess now. */
return
if _\=='' then g=left((\substr(_,min(2,#),1))left(_,1)substr(_,3),#)
/*──────────────────────────────────────────────────────────────────────────────────────*/
do until g\==@.CBLF!; g=r(#); end /*otherwise, generate a choice. */
randComp: if @.comp\=='' then return /*the computer already has a pick*/
@.comp=translate(g,'HT',10)
_= @.CBLF! /* [↓] use best-choice algorithm.*/
return</lang>
if _\=='' then g= left((\substr(_, min(2, #), 1))left(_, 1)substr(_, 3), #)
'''output''' of a six-game session &nbsp; (ended by user entering a '''quit'''):
do until g\==@.CBLF!; g= r(#); end /*otherwise, generate a choice. */
@.comp= translate(g, 'HT', 10)
return</syntaxhighlight>
{{out|output|text=&nbsp; of a six-game session &nbsp; (ended by user entering a '''quit'''):}}
<pre>
───────── The computer won first toss, the pick was: HHH
 
───────── Pick a sequence of 3 coin tosses of H or T (Heads or Tails) or Quit:
tail tail tails ◄■■■■■■■■■■■ human's input
tail tail tails
 
───────── your pick: TTT
Line 1,020 ⟶ 3,618:
 
───────── Pick a sequence of 3 coin tosses of H or T (Heads or Tails) or Quit:
h, h, t ◄■■■■■■■■■■■ human's input
h, h, t
 
───────── your pick: HHT
Line 1,036 ⟶ 3,634:
 
───────── Pick a sequence of 3 coin tosses of H or T (Heads or Tails) or Quit:
heads heads heads ◄■■■■■■■■■■■ human's input
 
───────── your pick: HHH
Line 1,052 ⟶ 3,650:
 
───────── Pick a sequence of 3 coin tosses of H or T (Heads or Tails) or Quit:
t t h ◄■■■■■■■■■■■ human's input
t t h
 
───────── your pick: TTH
Line 1,068 ⟶ 3,666:
 
───────── Pick a sequence of 3 coin tosses of H or T (Heads or Tails) or Quit:
t,h,h ◄■■■■■■■■■■■ human's input
t,h,h
 
───────── your pick: THH
Line 1,084 ⟶ 3,682:
 
───────── Pick a sequence of 3 coin tosses of H or T (Heads or Tails) or Quit:
tth ◄■■■■■■■■■■■ human's input
tth
───────── Oops! You can't choose the computer's choice: TTH
 
───────── Pick a sequence of 3 coin tosses of H or T (Heads or Tails) or Quit:
tht ◄■■■■■■■■■■■ human's input
tht
 
───────── your pick: THT
Line 1,104 ⟶ 3,702:
 
───────── Pick a sequence of 3 coin tosses of H or T (Heads or Tails) or Quit:
quit ◄■■■■■■■■■■■ human's input
quit
</pre>
 
=={{Headerheader|Ruby}}==
<syntaxhighlight lang="ruby">Toss = [:Heads, :Tails]
<lang ruby># Penney's Game
 
Toss = [:Heads, :Tails]
def yourChoice
puts "Enter your choice (H/T)"
Line 1,124 ⟶ 3,720:
end
 
puts "%s I start, %s you start ..... #{coin = Toss.sample}" % Toss
if coin == Toss[0]
myC = Array.new(3){Toss.sample}
puts "I chose #{myC.join(' ')}"
yC = yourChoice
else
yC = yourChoice
myC = Toss - [yC[1]] + yC.first(2)
puts "I chose #{myC.join(' ')}"
end
 
seq = Array.new(3){Toss.sample}
print seq.join(' ')
loop do
puts "\n%s I win!"start, or%s breakyou start ..... if%s" seq% [*Toss, coin == myCToss.sample]
if coin == Toss[0]
puts "\n You win!" or break if seq == yC
seq myC = Toss.push(shuffle << Toss.sample).shift
print puts "I chose #{seq[-1]myC.join(' ')}"
yC = yourChoice
end</lang>
else
yC = yourChoice
myC = Toss - [yC[1]] + yC.first(2)
puts "I chose #{myC.join(' ')}"
end
seq = Array.new(3){Toss.sample}
print seq.join(' ')
loop do
puts "\n I win!" or break if seq == myC
puts "\n You win!" or break if seq == yC
seq.push(Toss.sample).shift
print " #{seq[-1]}"
end
end</syntaxhighlight>
 
{{out}}
Line 1,161 ⟶ 3,759:
Heads Heads Tails Heads Tails Heads Tails Tails Tails Tails Tails Tails Tails Heads Heads
You win!
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
extern crate rand;
 
use std::io::{stdin, stdout, Write};
use std::thread;
use std::time::Duration;
 
use rand::Rng;
 
fn toss_coin<R: Rng>(rng: &mut R, print: bool) -> char {
let c = if rng.gen() { 'H' } else { 'T' };
if print {
print!("{}", c);
stdout().flush().expect("Could not flush stdout");
}
c
}
 
fn gen_sequence<R: Rng>(rng: &mut R, seed: Option<&str>) -> String {
let mut seq = String::new();
match seed {
Some(s) => {
let mut iter = s.chars();
let c0 = iter.next().unwrap();
let next = if c0 == 'H' { 'T' } else { 'H' };
seq.push(next);
seq.push(c0);
seq.push(iter.next().unwrap());
}
None => {
for _ in 0..3 {
seq.push(toss_coin(rng, false))
}
}
}
seq
}
 
fn read_sequence(used_seq: Option<&str>) -> String {
let mut seq = String::new();
loop {
seq.clear();
println!("Please, enter sequence of 3 coins: H (heads) or T (tails): ");
stdin().read_line(&mut seq).expect("failed to read line");
seq = seq.trim().to_uppercase();
// do the cheapest test first
if seq.len() == 3
&& seq.chars().all(|c| c == 'H' || c == 'T')
&& seq != used_seq.unwrap_or("")
{
return seq;
}
 
println!("Please enter correct sequence!");
}
}
 
fn main() {
let mut rng = rand::thread_rng();
 
println!("--Penney's game--");
loop {
let useq: String;
let aiseq: String;
if rng.gen::<bool>() {
println!("You choose first!");
useq = read_sequence(None);
println!("Your sequence: {}", useq);
aiseq = gen_sequence(&mut rng, Some(&useq));
println!("My sequence: {}", aiseq);
} else {
println!("I choose first!");
aiseq = gen_sequence(&mut rng, None);
println!("My sequence: {}", aiseq);
useq = read_sequence(Some(&aiseq));
println!("Your sequence: {}", useq);
}
println!("Tossing coins...");
let mut coins = String::new();
for _ in 0..2 {
// toss first 2 coins
coins.push(toss_coin(&mut rng, true));
thread::sleep(Duration::from_millis(500));
}
loop {
coins.push(toss_coin(&mut rng, true));
thread::sleep(Duration::from_millis(500));
if coins.contains(&useq) {
println!("\nYou win!");
break;
}
if coins.contains(&aiseq) {
println!("\nI win!");
break;
}
}
 
println!(" Play again? 'Y' to play, 'Q' to exit.");
let mut input = String::new();
stdin().read_line(&mut input).expect("failed to read line");
match input.trim_start().chars().next().unwrap() {
'Y' | 'y' => continue,
_ => break,
}
}
}
 
</syntaxhighlight>
{{out}}
<pre>
--Penney's game--
You choose first!
Please, enter sequence of 3 coins: H (heads) or T (tails):
hht
Your sequence: HHT
My sequence: THH
Tossing coins...
TTTTHTTHTHTHTTTHH
I win!
Play again? 'Y' to play, 'Q' to exit.
</pre>
 
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
oo::class create Player {
Line 1,262 ⟶ 3,983:
}
 
game 3 HumanPlayer RobotPlayer</langsyntaxhighlight>
{{out|Sample Game}}
<!-- Huh! This was a long one… -->
Line 1,295 ⟶ 4,016:
=={{header|UNIX Shell}}==
{{works with|Bash}}
<langsyntaxhighlight lang="sh">#!/bin/bash
main() {
echoprintf "$'Penney\'s Game"\n\n'
echoprintf -n "'Flipping to see who goes first ... "'
 
if [[ $(flip) == H ]]; then
echoprintf "'I do."\n'
p2=$(choose_sequence)
echoprintf "'I choose: %s\n' "$p2"
else
echoprintf "'You do."\n'
fi
 
while true; do
echoread "-p 'Enter your three-flip sequence:" ' p1
readp1=$(tr a-z A-Z <<<"$p1")
case "$p1" in
"$p2") echoprintf "'Sequence must be different from mine"\n';;
[hHTtHT][hHtTHT][hHtTHT]) break;;
*) echoprintf "$'Sequence must be three H\'s or T\'s"\n';;
esac
done
p1=$(tr a-z A-Z <<<"$p1")
 
if [ -z "$p2" ]; then
p2=$(choose_sequence "$p1")
echoprintf "'I choose: %s\n' "$p2"
fi
 
printf '\nHere we go. %s, you win; %s, I win.\n' "$p1" "$p2"
echo
printf 'Flips:'
echo "Here we go. $p1, you win; $p2, I win."
flips=
 
flips=
while true; do
flip=$(flip)
echoprintf -n' %s' "$flip"
flips+=$flip
while (( ${#flips} > 3 )); do
flips="${flips#?}"
done
case "$flips" in
*$p1) echoprintf $'\nYou win!\n'; exit 0;;
*$p2) echoprintf $'\nI win!\n'; exit 1;;
esac
done
Line 1,346 ⟶ 4,063:
if (( $# )); then
case "$1" in
?[Hh]?) result=T;;
*) result=H;;
esac
Line 1,353 ⟶ 4,070:
result=$(flip)$(flip)$(flip)
fi
echoprintf '%s\n' "$result"
}
 
flip() {
if (( RANDOM % 2 )); then
echoprintf '%s\n' H
else
echoprintf '%s\n' T
fi
}
 
main "$@"</lang>
</syntaxhighlight>
 
{{Output}}
Line 1,379 ⟶ 4,097:
 
Human first:
<pre>penneyPenney's Game
 
Penney's Game
Flipping to see who goes first ... I do.
I choose: HHH
Enter your three-flip sequence: THH
 
Here we go. THH, you win; HHH, I win.
Flips: H H T H H
You win!</pre>
 
Human first:
<pre>Penney's Game
 
Flipping to see who goes first ... You do.
Enter your three-flip sequence: THH
I choose: TTH
HTH
I choose: HHT
 
Here we go. HTHTHH, you win; HHTTTH, I win.
Flips: H H T T T H
HHHT
I win!</pre>
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">
Option Explicit
 
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const HT As String = "H T"
 
Public Sub PenneysGame()
Dim S$, YourSeq$, ComputeSeq$, i&, Seq$, WhoWin$, flag As Boolean
 
Do
S = WhoWillBeFirst(Choice("Who will be first"))
If S = "ABORT" Then Exit Do
Debug.Print S; " start"
YourSeq = Choice("Your sequence", 3)
If YourSeq Like "*ABORT*" Then Exit Do
Debug.Print "Your sequence : " & YourSeq
ComputeSeq = vbNullString
For i = 1 To 3
ComputeSeq = ComputeSeq & Random
Next i
Debug.Print "Computer sequence : " & ComputeSeq
Seq = vbNullString
Do
Seq = Seq & Random
Debug.Print Seq
Sleep 1000
Loop While Not Winner(ComputeSeq, YourSeq, Seq, WhoWin)
Debug.Print WhoWin; " win"
If MsgBox(WhoWin & " win" & vbCrLf & "Play again?", vbYesNo) = vbNo Then flag = True
Debug.Print ""
Loop While Not flag
Debug.Print "Game over"
End Sub
 
Private Function WhoWillBeFirst(YourChoice As String) As String
Dim S$
S = Random
Select Case YourChoice
Case "ABORT": WhoWillBeFirst = YourChoice
Case Else:
WhoWillBeFirst = IIf(S = YourChoice, "You", "Computer")
End Select
End Function
 
Private Function Choice(Title As String, Optional Seq As Integer) As String
Dim S$, i&, t$
If Seq = 0 Then Seq = 1
t = Title
For i = 1 To Seq
S = vbNullString
Do
S = InputBox("Choose between H or T : ", t)
If StrPtr(S) = 0 Then S = "Abort"
S = UCase(S)
Loop While S <> "H" And S <> "T" And S <> "ABORT"
Choice = Choice & S
t = Title & " " & Choice
If Choice Like "*ABORT*" Then Exit For
Next i
End Function
 
Private Function Random() As String
Randomize Timer
Random = Split(HT, " ")(CInt(Rnd))
End Function
 
Private Function Winner(Cs$, Ys$, S$, W$) As Boolean
If Len(S) < 3 Then
Winner = False
Else
If Right(S, 3) = Cs And Right(S, 3) = Ys Then
Winner = True
W = "Computer & you"
ElseIf Right(S, 3) = Cs And Right(S, 3) <> Ys Then
Winner = True
W = "Computer"
ElseIf Right(S, 3) = Ys And Right(S, 3) <> Cs Then
Winner = True
W = "You"
End If
End If
End Function</syntaxhighlight>
{{out}}
<pre>You start
Your sequence : HHT
Computer sequence : TTH
H
HT
HTT
HTTH
Computer win
 
You start
Your sequence : TTH
Computer sequence : HHH
T
TT
TTT
TTTH
You win
 
Game over</pre>
 
=={{header|V (Vlang)}}==
{{trans|Kotlin}}
<syntaxhighlight lang="v (vlang)">
import rand
import os
import time
 
const optimum = {
"HHH": "THH", "HHT": "THH", "HTH": "HHT", "HTT": "HHT",
"THH": "TTH", "THT": "TTH", "TTH": "HTT", "TTT": "HTT"
}
 
fn main() {
r := rand.intn(2) or {exit(1)}
mut user_seq :=''
mut comp_seq :=''
mut coin :=''
mut coins :=''
mut len := 0
mut seq :=''
 
if r == 0 {
println("You go first")
user_seq = user_sequence()
println('\n')
comp_seq = computer_sequence(user_seq)
}
else {
println("Computer goes first")
comp_seq = computer_sequence(user_seq)
println('\n')
user_seq = user_sequence()
}
println('\n')
for true {
if (rand.intn(2) or {exit(1)}) == 0 {coin = 'H'} else {coin = 'T'}
coins += coin
println("Coins flipped: $coins")
len = coins.len
if len >= 3 {
seq = coins.substr(len - 3, len)
if seq == user_seq {
println("\nYou win!")
exit(0)
}
if seq == comp_seq {
println("\nComputer wins!")
exit(0)
}
}
time.sleep(1 * time.second) // extraneous
}
}
 
fn user_sequence() string {
mut user_seq :=''
println("A sequence of three H or T should be entered")
for (user_seq.len != 3) || (user_seq.to_upper().split('').any(it != 'H' && it != 'T') == true) {
user_seq = os.input('Enter your sequence: ').str().to_upper()
}
return user_seq
}
 
fn computer_sequence(user_seq string) string {
mut char_array := []string{len:3}
mut comp_seq :=''
if user_seq == '' {
for _ in 0..2 {
if (rand.intn(2) or {exit(1)}) == 0 {char_array << 'T'} else {char_array << 'H'}
comp_seq = char_array.join('')
}
}
else {
comp_seq = optimum[user_seq]
}
println("Computer's sequence: $comp_seq")
return comp_seq
}
</syntaxhighlight>
 
{{out}}
<pre>
Computer goes first
Computer's sequence: TH
 
 
A sequence of three H or T should be entered
Enter your sequence: thh
 
 
Coins flipped: H
Coins flipped: HT
Coins flipped: HTT
Coins flipped: HTTH
Coins flipped: HTTHT
Coins flipped: HTTHTH
Coins flipped: HTTHTHT
Coins flipped: HTTHTHTT
Coins flipped: HTTHTHTTH
Coins flipped: HTTHTHTTHT
Coins flipped: HTTHTHTTHTH
Coins flipped: HTTHTHTTHTHT
Coins flipped: HTTHTHTTHTHTH
Coins flipped: HTTHTHTTHTHTHT
Coins flipped: HTTHTHTTHTHTHTH
Coins flipped: HTTHTHTTHTHTHTHH
 
You win!
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-str}}
<syntaxhighlight lang="wren">import "random" for Random
import "io" for Stdin, Stdout
import "timer" for Timer
import "./str" for Str
 
var rand = Random.new()
 
var optimum = {
"HHH": "THH", "HHT": "THH", "HTH": "HHT", "HTT": "HHT",
"THH": "TTH", "THT": "TTH", "TTH": "HTT", "TTT": "HTT"
}
 
var getUserSequence = Fn.new {
System.print("A sequence of three H or T should be entered")
var userSeq
while (true) {
System.write("Enter your sequence: ")
Stdout.flush()
userSeq = Str.upper(Stdin.readLine())
if (userSeq.count == 3 && userSeq.all { |c| c == "H" || c == "T" }) break
}
return userSeq
}
 
var getComputerSequence = Fn.new { |userSeq|
var compSeq
if (userSeq == "") {
var chars = List.filled(3, null)
for (i in 0..2) chars[i] = (rand.int(2) == 0) ? "T" : "H"
compSeq = chars.join()
} else {
compSeq = optimum[userSeq]
}
System.print("Computer's sequence: %(compSeq)")
return compSeq
}
 
var userSeq
var compSeq
var r = rand.int(2)
if (r == 0) {
System.print("You go first")
userSeq = getUserSequence.call()
System.print()
compSeq = getComputerSequence.call(userSeq)
} else {
System.print("Computer goes first")
compSeq = getComputerSequence.call("")
System.print()
userSeq = getUserSequence.call()
}
System.print()
var coins = ""
while (true) {
var coin = (rand.int(2) == 0) ? "H" : "T"
coins = coins + coin
System.print("Coins flipped: %(coins)")
var len = coins.count
if (len >= 3) {
var seq = coins[len-3...len]
if (seq == userSeq) {
System.print("\nYou win!")
return
} else if (seq == compSeq) {
System.print("\nCompter wins!")
return
}
}
Timer.sleep(2000) // wait two seconds for next flip
}</syntaxhighlight>
 
{{out}}
Sample game where computer goes first:
<pre>
Computer goes first
Computer's sequence: HHH
 
A sequence of three H or T should be entered
Enter your sequence: TTH
 
Coins flipped: H
Coins flipped: HH
Coins flipped: HHH
 
Compter wins!
</pre>
 
Sample game where user goes first:
<pre>
You go first
A sequence of three H or T should be entered
Enter your sequence: TTH
 
Computer's sequence: HTT
 
Coins flipped: H
Coins flipped: HH
Coins flipped: HHH
Coins flipped: HHHT
Coins flipped: HHHTT
 
Compter wins!
</pre>
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">fcn coinToss{ (0).random(2) and "H" or "T" } // (0).random(2) --> 0<=r<2
reg myBet, yourBet;
if(coinToss()=="H"){ // The toss says I go first
myBet=(3).pump(String,coinToss);
println("I bet ",myBet);
yourBet=ask("Your bet of three (H/T): ")[0,3].toUpper();
}else{
yourBet=ask("Your bet of three (H/T): ")[0,3].toUpper();
myBet=((yourBet[1]=="H") and "T" or "H") + yourBet[0,2];
println("I bet ",myBet);
}
print("Flipping: "); coins:="";
while(1){
print(toss:=coinToss()); coins=coins + toss;
if(Void!=coins.find(yourBet)){ println(" You win!"); break; }
if(Void!=coins.find(myBet)) { println(" I win!"); break; }
// ignore we both won
}</syntaxhighlight>
{{out}}
<pre>
$ zkl bbb
Your bet of three (H/T): tth
I bet HTT
Flipping: HHHHHHTHHTHHHTHTHHHTHTHTT I win!
 
$ zkl bbb
I bet THT
Your bet of three (H/T): hhh
Flipping: THHH You win!
</pre>
9,482

edits