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).


 
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
 