Penney's game: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(10 intermediate revisions by 5 users not shown)
Line 159:
#include <jambo.h>
 
#define GetauppercharintoGetanuppercasecharinto(_X_) Getchar(_X_),Let( _X_ := Ucase(_X_) )
#define Isnotvalidin(_X_) Not( Occurs in (_X_) )
#define NOELIGEPCDESPUESDETI 0
#define ELIGEPCDESPUESDETINOELIGEPCDESPUESDETI 1 0
#define ELIGEPCDESPUESDETI 1
 
Main
Line 168 ⟶ 169:
If ( Is equal to '2' )
largo=0
Let ( largo := Get if( Argnum(2) Is less than '3', 3, Argnum(2) ) )
OTRO="S"
Line 228 ⟶ 229:
i=largo, c=0
Loop
Get aan upperuppercase char into 'c'
IfContinue if ( var(c) Not(Is Occursnot valid in ("HT") ))
Let ( c := Get if( Geq( Rand(1), 0.5), "T", "H" ) )
End If
Let( P:=Cat(P,c) )
Loccol(13), Printnl(P)
Line 274 ⟶ 273:
 
=={{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 3,045 ⟶ 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 4,040 ⟶ 4,222:
Game over</pre>
 
=={{header|V (Vlang)}}==
{{trans|Kotlin}}
<syntaxhighlight lang="v (vlang)">
import rand
import os
Line 4,090 ⟶ 4,272:
}
}
time.sleep(1 * time.second) // extraneous
}
}
Line 4,153 ⟶ 4,335:
{{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