How to resolve the algorithm Penney's game step by step in the Amazing Hopper programming language
How to resolve the algorithm Penney's game step by step in the Amazing Hopper programming language
Table of Contents
Problem Statement
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: 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.
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.
Create a program that tosses the coin, keeps score and plays Penney's game against a human opponent.
Show output of a game where the computer chooses first and a game where the user goes first here on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Penney's game step by step in the Amazing Hopper programming language
Source code in the amazing programming language
#include
#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 \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
You may also check:How to resolve the algorithm Primes - allocate descendants to their ancestors step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Substitution cipher step by step in the VBScript programming language
You may also check:How to resolve the algorithm SHA-256 step by step in the DWScript programming language
You may also check:How to resolve the algorithm 2048 step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the Wart programming language