How to resolve the algorithm Penney's game step by step in the 11l programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Penney's game step by step in the 11l 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 11l programming language

Source code in the 11l programming language

V first = random:choice([1B, 0B])

V you = ‘’
V me = ‘’
V ht = ‘HT’ // to get round ‘bug in MSVC 2017’[https://developercommunity.visualstudio.com/t/bug-with-operator-in-c/565417]
I first
   me = random:sample(Array(‘HT’ * 3), 3).join(‘’)
   print(‘I choose first and will win on first seeing #. in the list of tosses’.format(me))
   L you.len != 3 | any(you.map(ch -> ch !C :ht)) | you == me
      you = input(‘What sequence of three Heads/Tails will you win with: ’)
E
   L you.len != 3 | any(you.map(ch -> ch !C :ht))
      you = input(‘After you: What sequence of three Heads/Tails will you win with: ’)
   me = (I you[1] == ‘T’ {‘H’} E ‘T’)‘’you[0.<2]
   print(‘I win on first seeing #. in the list of tosses’.format(me))

print("Rolling:\n  ", end' ‘’)
V rolled = ‘’
L
   rolled ‘’= random:choice(‘HT’)
   print(rolled.last, end' ‘’)
   I rolled.ends_with(you)
      print("\n  You win!")
      L.break
   I rolled.ends_with(me)
      print("\n  I win!")
      L.break
   sleep(1)

  

You may also check:How to resolve the algorithm Humble numbers step by step in the EasyLang programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the KSI programming language
You may also check:How to resolve the algorithm Feigenbaum constant calculation step by step in the Factor programming language
You may also check:How to resolve the algorithm Loops/For step by step in the Racket programming language
You may also check:How to resolve the algorithm Soundex step by step in the Racket programming language