Penney's game is a game where two players bet on being the first to see a particular sequence of heads or tails in consecutive tosses of a 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.

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.



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.
 If going second, the computer should automatically play the optimum sequence.
 Successive coin tosses should be shown.


Show output of a game where the computer chooses first and a game where the user goes first here on this page.



See also
 The Penney Ante Part 1 (Video).
 The Penney Ante Part 2 (Video).


 
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
 