How to resolve the algorithm Penney's game step by step in the AutoHotkey programming language
How to resolve the algorithm Penney's game step by step in the AutoHotkey 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 AutoHotkey programming language
Source code in the autohotkey programming language
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
;-----------------------------------------------
You may also check:How to resolve the algorithm Balanced brackets step by step in the Befunge programming language
You may also check:How to resolve the algorithm Burrows–Wheeler transform step by step in the Julia programming language
You may also check:How to resolve the algorithm Abelian sandpile model/Identity step by step in the F# programming language
You may also check:How to resolve the algorithm Chat server step by step in the Ada programming language
You may also check:How to resolve the algorithm MD5 step by step in the Emacs Lisp programming language