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

Published on 12 May 2024 09:40 PM

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

Source code in the zkl programming language

fcn coinToss{ (0).random(2) and "H" or "T" } // (0).random(2) --> 0<=r<2
reg myBet, yourBet;
if(coinToss()=="H"){ // The toss says I go first
   myBet=(3).pump(String,coinToss);
   println("I bet ",myBet);
   yourBet=ask("Your bet of three (H/T): ")[0,3].toUpper();
}else{
   yourBet=ask("Your bet of three (H/T): ")[0,3].toUpper();
   myBet=((yourBet[1]=="H") and "T" or "H") + yourBet[0,2];
   println("I bet ",myBet);
}
print("Flipping: "); coins:="";
while(1){
   print(toss:=coinToss()); coins=coins + toss;
   if(Void!=coins.find(yourBet)){ println(" You win!"); break; }
   if(Void!=coins.find(myBet))  { println(" I win!"); break; }
   // ignore we both won
}

  

You may also check:How to resolve the algorithm String prepend step by step in the Phixmonti programming language
You may also check:How to resolve the algorithm Validate International Securities Identification Number step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Smarandache-Wellin primes step by step in the C++ programming language
You may also check:How to resolve the algorithm Zig-zag matrix step by step in the PHP programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the R programming language