Penney's game: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(14 intermediate revisions by 5 users not shown)
Line 153:
return
;-----------------------------------------------</syntaxhighlight>
 
=={{header|Amazing Hopper}}==
{{trans|Sinclair ZX81 BASIC}}
<syntaxhighlight lang="Amazing Hopper">
#include <jambo.h>
 
#define Getanuppercasecharinto(_X_) Getchar(_X_),Let( _X_ := Ucase(_X_) )
#define Isnotvalidin(_X_) Not( Occurs in (_X_) )
#define NOELIGEPCDESPUESDETI 0
#define ELIGEPCDESPUESDETI 1
 
Main
Set break
Get total args
If ( Is equal to '2' )
largo=0
Let ( largo := Get if( Argnum(2) Is less than '3', 3, Argnum(2) ) )
OTRO="S"
Cursor off
Loop
P="", C="", S="", L=0
Cls
Locate(1,1), Printnl ("***** Penney's Game *****")
If ( Geq( Rand(1), 0.5) )
Gosub ' Yo tiro ', Set 'NO ELIGE PC DESPUES DE TI',Gosub ' Tú tiras '
Else
Set 'ELIGE PC DESPUES DE TI', Gosub ' Tú tiras '
End If
 
// a jugar:
G="", SW=1
Loop
Let( G := Cat( G, Get if( Geq( Rand(1), 0.5), "T", "H" ) ) )
Locate( 6, 1 ), Printnl( "Tiradas: ", G )
Continue if ( Less ( Len (G) ---Backup to 'L'---, 3) )
Let ( S := Right( Sub(L, Minus one 'largo') , G) )
 
If ( Eq( S, P ) )
Set ("TU GANAS!!"), SW=0
Else If (Eq( S, C ) )
Set ("YO GANO!!"), SW=0
End If
Prnl
Sleep(1)
Back if( SW ) is not zero
Printnl ( Utf8("¿Otro juego? S/* ") )
Getchar(OTRO)
While( Eq (OTRO, "S") )
Cursor on
Else
Printnl("Modo de uso: hopper penny.jambo <n>\ndonde \"n\" es el largo de la secuencia")
End If
End
 
Subrutines
 
Define ' Yo tiro '
Locate(4,1), Print( "Yo elijo... " )
i=largo
Loop
Let( C := Cat( C, Get if( Geq( Rand(1), 0.5), "T", "H" ) ) )
--i
Back if (i) is positive
Printnl (C)
Return
 
Define ' Tú tiras, elige pc '
Locate(3,1), Printnl ( Utf8("Tú eliges...") )
i=largo, c=0
Loop
Get an uppercase char into 'c'
Continue if ( var(c) Is not valid in ("HT") )
Let( P:=Cat(P,c) )
Loccol(13), Printnl(P)
--i
Back if (i) is positive
 
If ( elige pc )
Let (C := P)
[2] Get from (C)
Set if( Is equal to ("H"), "T", "H" )
Cput (C)
Locate(4,1), Print( "Yo elijo... ", C )
End If
Return
</syntaxhighlight>
{{out}}
<pre>
$ hopper penney.jambo
Modo de uso: hopper penny.jambo <n>
donde "n" es el largo de la secuencia
 
$ hopper penney.jambo 5
***** Penney's Game *****
 
Tú eliges...HTHHT
Yo elijo... HHHHT
 
Tiradas: TTHTTTTHHTTHHTTHTHTHHT
TU GANAS!!
¿Otro juego? S/*
 
***** Penney's Game *****
 
Tú eliges...HTHTT
Yo elijo... HHHTT
 
Tiradas: THHHHTHHHHTT
YO GANO!!
¿Otro juego? S/*
$
</pre>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{trans|Pascal}}
<syntaxhighlight lang="gwbasic"> 100 R = RND ( - 1 * ( PEEK (78) + 256 * PEEK (79))): REM RANDOMIZE
110 DEF FN R(R) = INT ( RND (1) * R) * (3 ^ (R = 8)) + 1
120 READ P$
130 DATA HTHHHHHHTHTTTHHTHTTTHTTT
140 LET M$ = CHR$ (13)
150 PRINT "WELCOME TO PENNEY'S GAME!"M$
160 INPUT "HOW MANY ROUNDS WOULD YOU LIKE TO PLAY? ";ROUNDS%
170 PRINT M$"OK, LET'S PLAY "ROUNDS%" ROUNDS."
180 FOR COUNT = 1 TO ROUNDS%
190 PRINT M$"*** ROUND #"COUNT" ***"M$
200 ON FN R(2) GOSUB 300,400
210 PRINT "LET'S GO!"M$
220 GOSUB 450"PLAY ROUND
230 INPUT "PRESS RETURN TO GO ON...";N$
240 NEXT COUNT
250 PRINT M$"*** END RESULT ***"M$
260 IF YOU > ME THEN PRINT "CONGRATULATIONS! YOU WON!"
270 IF ME > YOU THEN PRINT "HOORAY! I WON"
280 IF ME = YOU THEN PRINT "COOL, WE TIED."
290 END
 
300 ME$ = MID$ (P$, FN R(8),3): REM RANDOM SEQUENCE
310 PRINT "I'LL PICK FIRST THIS TIME."M$" MY SEQUENCE IS "ME$"."
320 FOR REP = 0 TO 1 STEP 0
330 PRINT "WHAT SEQUENCE DO YOU WANT?"
340 GOSUB 590"GET YOUR SEQUENCE
350 LET REP = YOU$ < > ME$
360 IF NOT REP THEN PRINT "HEY, THAT'S MY SEQUENCE! THINK FOR YOURSELF!"
370 NEXT REP
380 PRINT YOU$", HUH? SOUNDS OK TO ME."
390 RETURN
 
400 PRINT "YOU PICK FIRST THIS TIME."
410 GOSUB 590"GET YOUR SEQUENCE
420 LET ME$ = MID$ ("HT",( MID$ (YOU$,2,1) = "H") + 1,1) + LEFT$ (YOU$,2)
430 PRINT "OK, SO YOU PICKED "YOU$M$M$"MY SEQUENCE WILL BE "ME$M$
440 RETURN
 
450 T0SSE$ = MID$ (P$, FN R(8),3): REM RANDOM SEQUENCE
460 PRINT "TOSSING THE COIN: "M$ MID$ (T0SSE$,2);
470 FOR T0SS = 0 TO 1 STEP 0
480 LET T0SSE$ = RIGHT$ (T0SSE$,2) + MID$ ("HT", FN R(2),1)
490 PRINT MID$ (T0SSE$,3);
500 IF YOU$ = T0SSE$ THEN 530"YOU WON
510 IF ME$ = T0SSE$ THEN 560"I WON
520 NEXT T0SS
 
530 YOU = YOU + 1
540 PRINT M$M$"CONGRATULATIONS! YOU WON THIS ROUND."M$"YOUR NEW SCORE IS "YOU"."
550 RETURN
 
560 ME = ME + 1
570 PRINT M$M$"YAY, I WON THIS ROUND!"M$"MY NEW SCORE IS "ME"."
580 RETURN
 
590 YOU$ = ""
600 PRINT "TYPE 3 LETTERS H OR T: "
610 FOR I = 1 TO 3
620 LET V$ = "HT"
630 GOSUB 670"GET VALID KEYPRESS
640 LET YOU$ = YOU$ + C$
650 NEXT I
660 LET V$ = M$ + M$
670 FOR C = 0 TO 1 STEP 0
680 GET C$
690 IF ASC (C$) > 95 THEN C$ = CHR$ ( ASC (C$) - 32)
700 LET C = C$ = LEFT$ (V$,1) OR C$ = MID$ (V$,2)
710 NEXT C
720 PRINT C$;
730 RETURN</syntaxhighlight>
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
Line 2,926 ⟶ 3,116:
You win!
>>> </pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ 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 ( --> )</syntaxhighlight>
 
{{out}}
As a dialogue in the Quackery shell (REPL).
 
<pre>/O> play
...
This is Penney's game.
 
We each guess a different sequence of three coin flips.
I will releatedly flip a coin until one of the sequences happens.
The winner is the one who guesses the sequence that happens first.
 
Please enter your sequence carefully as there is no error checking.
For example, for "heads tails heads" type "HTH".
 
You start. Please enter your guess: HTH
I guess: HHT
 
You win.
The complete sequence was: TTHTH
 
Stack empty.
 
/O> play
...
This is Penney's game.
 
We each guess a different sequence of three coin flips.
I will releatedly flip a coin until one of the sequences happens.
The winner is the one who guesses the sequence that happens first.
 
Please enter your sequence carefully as there is no error checking.
For example, for "heads tails heads" type "HTH".
 
I start. My guess is: HTT
Please enter your guess: HHT
 
You win.
The complete sequence was: HTHTHHHT
 
Stack empty.
 
/O> </pre>
 
=={{header|R}}==
Line 3,920 ⟶ 4,221:
 
Game over</pre>
 
=={{header|V (Vlang)}}==
{{trans|Kotlin}}
<syntaxhighlight lang="v (vlang)">
import rand
import os
import time
 
const optimum = {
"HHH": "THH", "HHT": "THH", "HTH": "HHT", "HTT": "HHT",
"THH": "TTH", "THT": "TTH", "TTH": "HTT", "TTT": "HTT"
}
 
fn main() {
r := rand.intn(2) or {exit(1)}
mut user_seq :=''
mut comp_seq :=''
mut coin :=''
mut coins :=''
mut len := 0
mut seq :=''
 
if r == 0 {
println("You go first")
user_seq = user_sequence()
println('\n')
comp_seq = computer_sequence(user_seq)
}
else {
println("Computer goes first")
comp_seq = computer_sequence(user_seq)
println('\n')
user_seq = user_sequence()
}
println('\n')
for true {
if (rand.intn(2) or {exit(1)}) == 0 {coin = 'H'} else {coin = 'T'}
coins += coin
println("Coins flipped: $coins")
len = coins.len
if len >= 3 {
seq = coins.substr(len - 3, len)
if seq == user_seq {
println("\nYou win!")
exit(0)
}
if seq == comp_seq {
println("\nComputer wins!")
exit(0)
}
}
time.sleep(1 * time.second) // extraneous
}
}
 
fn user_sequence() string {
mut user_seq :=''
println("A sequence of three H or T should be entered")
for (user_seq.len != 3) || (user_seq.to_upper().split('').any(it != 'H' && it != 'T') == true) {
user_seq = os.input('Enter your sequence: ').str().to_upper()
}
return user_seq
}
 
fn computer_sequence(user_seq string) string {
mut char_array := []string{len:3}
mut comp_seq :=''
if user_seq == '' {
for _ in 0..2 {
if (rand.intn(2) or {exit(1)}) == 0 {char_array << 'T'} else {char_array << 'H'}
comp_seq = char_array.join('')
}
}
else {
comp_seq = optimum[user_seq]
}
println("Computer's sequence: $comp_seq")
return comp_seq
}
</syntaxhighlight>
 
{{out}}
<pre>
Computer goes first
Computer's sequence: TH
 
 
A sequence of three H or T should be entered
Enter your sequence: thh
 
 
Coins flipped: H
Coins flipped: HT
Coins flipped: HTT
Coins flipped: HTTH
Coins flipped: HTTHT
Coins flipped: HTTHTH
Coins flipped: HTTHTHT
Coins flipped: HTTHTHTT
Coins flipped: HTTHTHTTH
Coins flipped: HTTHTHTTHT
Coins flipped: HTTHTHTTHTH
Coins flipped: HTTHTHTTHTHT
Coins flipped: HTTHTHTTHTHTH
Coins flipped: HTTHTHTTHTHTHT
Coins flipped: HTTHTHTTHTHTHTH
Coins flipped: HTTHTHTTHTHTHTHH
 
You win!
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-str}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "io" for Stdin, Stdout
import "timer" for Timer
import "./str" for Str
 
var rand = Random.new()
9,476

edits