PROGRAM Penney; //http://www.rosettacode.org/wiki/Penney%27s_game#Pascal TYPE CoinToss = (heads, tails); Sequence = array [1..3] of CoinToss; Player = record bet: Sequence; score: integer; end; VAR Human, Computer: Player; Rounds, Count: integer; Function TossCoin: CoinToss; { Returns heads or tails at random } Begin if random(2) = 1 then result := Heads else result := tails End; Procedure PutToss(toss: CoinToss); { Outputs heads or tails as a letter } Begin if toss = heads then write('H') else write('T') End; Function GetToss: string; { Reads H or T from the keyboard in either lettercase } var c: char; inp: string; Begin { Keep reading characters until we get an appropriate letter } //repeat read(c) until c in ['H', 'h', 'T', 't']; { Interpret the letter } //c:= readln('please enter H or T, 3 times')[1]; //writeln(c) inp:= readln('sequence of H or T, like hht or tht'); writeln(inp) for it:= 1 to 3 do begin c:=inp[it] //c := sr[1]; if (c ='H') or ( c='h') then result:= result + 'h' else result := result + 't' end; End; Procedure ShowSequence(tosses: Sequence); { Outputs three coin tosses at once } Var i: integer; Begin for i := 1 to 3 do PutToss(tosses[i]) End; Procedure ReadSequence(var tosses: Sequence); { Accepts three coin tosses from the keyboard } Var i: integer; srt: string; Begin { Get the 3 letters } srt:= getToss; for i := 1 to 3 do begin case srt[i] of 'h','H': tosses[i]:= heads; 't','T': tosses[i]:= tails; end; end; //writeln('') { Ignore the rest of the line } End; Function Optimum(opponent: Sequence): Sequence; { Generates the optimum sequence against an opponent } Begin case opponent[2] of heads: result[1] := tails; tails: result[1] := heads end; result[2] := opponent[1]; result[3] := opponent[2] End; Function RandomSequence: Sequence; { Generates three random coin tosses } Var i: integer; Begin for i := 1 to 3 do result[i] := TossCoin End; Function Match(first, second: Sequence): Boolean; { Detects whether a sequence of tosses matches another } Var different: boolean; i: integer; Begin different := false; i := 1; while (i <= 3) and not different do begin if not (first[i] = second[i]) then different := true; i := i + 1 end; result := not different End; Procedure PlayRound(var human, computer: Player); { Shows coin tosses and announces the winner } Var { We only ever need to store the 3 most recent tosses in memory. } tosses: Sequence; Begin { Start with the first three tosses } write('Tossing the coin: '); tosses := RandomSequence; ShowSequence(tosses); { Keep tossing the coin until there is a winner. } while not (Match(human.bet,tosses) or Match(computer.bet,tosses)) do begin tosses[1] := tosses[2]; tosses[2] := tosses[3]; tosses[3] := TossCoin; PutToss(tosses[3]) end; { Update the winner's score and announce the winner } writeln(''); if Match(human.bet, tosses) then begin writeln('Congratulations! You won this round.'); human.score := human.score + 1; writeln('Your new score is '+itoa(human.score)+'.') end else begin writeln('Yay, I won this round!'); computer.score := computer.score + 1; writeln('My new score is '+itoa(computer.score)+'.') end End; procedure ScrollToLastLine(Memo: TMemo); begin SendMessage(Memo.Handle, EM_LINESCROLL, 0,Memo.Lines.Count); end; { Main Algorithm } BEGIN //@main maxform1.orangestyle1click(self) { Welcome the player } writeln('Welcome to Penney''s game!'); voice2('Welcome to Penney''s game!'); writeln(''); write('How many rounds would you like to play? '); rounds:= strtoint(readln('How many Rounds?')); //ScrollToLastLine(memo2) writeln('Ok, let''s play '+itoa(Rounds)+' rounds.'); { Start the game } randomize; Human.score := 0; Computer.score := 0; for Count := 1 to Rounds do begin writeln(''); writeln('*** Round #'+itoa(Count)+' ***'); ScrollToLastLine(memo2) { Choose someone randomly to pick the first sequence } if TossCoin = heads then begin writeln('I''ll pick first this time.'); Computer.bet := RandomSequence; write(' My sequence is '); ShowSequence(Computer.bet); writeln('.'); repeat tone(500,1144) writeln('What sequence do you want? '); ReadSequence(Human.bet); if Match(Human.bet, Computer.bet) then begin writeln('Hey, that''s my sequence! Think for yourself!'); voice2('Hey, that''s my sequence! Think for yourself!'); end; until not Match(Human.bet, Computer.bet); ShowSequence(Human.bet); writeln(', huh? Sounds ok to me.') end else begin ScrollToLastLine(memo2) tone(500,600) writeln('You pick first this time. Enter 3 letters H or T: '); ReadSequence(Human.bet); Computer.bet := Optimum(Human.bet); writeln('Ok, so you picked '); ShowSequence(Human.bet); writeln('My sequence will be '); ShowSequence(Computer.bet); //writeln('') end; { Then we can actually play the round } writeln('Let''s go! '); PlayRound(Human, Computer); if not (count = rounds) then begin writeln('Press OK to go on...'); YesNoDialog('pgame','Press YES to go on...'); end; end; { All the rounds are finished; time to decide who won } writeln(''); writeln('*** End Result ***'); voice2('that''s it'); if Human.score > Computer.score then voice2('Congratulations! You won!') else if Computer.score > Human.score then writeln('Hooray! I won') else writeln('Cool, we tied.'); writeln(''); writeln('Press ENTER to finish or just stop.'); //readln // for it:= 1 to 10 do writeln(itoa(random(2))) //*) END. ----app_template_loaded_code---- Doc: 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: Heads, Tails, Heads, or HTH for short. 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. Example: 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. code ref: from __future__ import print_function import random from time import sleep first = random.choice([True, False]) you = '' if first: me = ''.join(random.sample('HT'*3, 3)) print('I choose first and will win on first seeing {} in the list of tosses'.format(me)) while len(you) != 3 or any(ch not in 'HT' for ch in you) or you == me: you = input('What sequence of three Heads/Tails will you win with: ') else: while len(you) != 3 or any(ch not in 'HT' for ch in you): you = input('After you: What sequence of three Heads/Tails will you win with: ') me = ('H' if you[1] == 'T' else 'T') + you[:2] print('I win on first seeing {} in the list of tosses'.format(me)) print('Rolling:\n ', end='') rolled = '' while True: rolled += random.choice('HT') print(rolled[-1], end='') if rolled.endswith(you): print('\n You win!') break if rolled.endswith(me): print('\n I win!') break sleep(1) # For dramatic effect ----File newtemplate.txt not exists - now saved!----