How to resolve the algorithm Penney's game step by step in the Quackery programming language
How to resolve the algorithm Penney's game step by step in the Quackery 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 Quackery programming language
Source code in the quackery programming language
[ 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 ( --> )
You may also check:How to resolve the algorithm Strong and weak primes step by step in the Lua programming language
You may also check:How to resolve the algorithm Abbreviations, automatic step by step in the Racket programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the MANOOL programming language
You may also check:How to resolve the algorithm HTTP step by step in the Swift programming language
You may also check:How to resolve the algorithm Window creation/X11 step by step in the Go programming language