Penney's game: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 33: | Line 33: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V first = random:choice([1B, 0B]) |
||
V you = ‘’ |
V you = ‘’ |
||
Line 60: | Line 60: | ||
print("\n I win!") |
print("\n I win!") |
||
L.break |
L.break |
||
sleep(1)</ |
sleep(1)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 72: | Line 72: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">Gui, font, s12 |
||
Gui, add, text, w90, Computer: |
Gui, add, text, w90, Computer: |
||
loop, 3 |
loop, 3 |
||
Line 152: | Line 152: | ||
gosub, RandomStart |
gosub, RandomStart |
||
return |
return |
||
;-----------------------------------------------</ |
;-----------------------------------------------</syntaxhighlight> |
||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|BASIC256}}=== |
==={{header|BASIC256}}=== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
<syntaxhighlight lang="basic256"> |
|||
<lang BASIC256> |
|||
global jugador, computador, secuencia |
global jugador, computador, secuencia |
||
jugador = "" : computador = "" |
jugador = "" : computador = "" |
||
Line 227: | Line 227: | ||
print "¡Gracias por jugar!" |
print "¡Gracias por jugar!" |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
==={{header|Sinclair ZX81 BASIC}}=== |
==={{header|Sinclair ZX81 BASIC}}=== |
||
Works with 1k of RAM. |
Works with 1k of RAM. |
||
< |
<syntaxhighlight lang="basic"> 10 IF RND>=.5 THEN GOTO 100 |
||
20 PRINT "YOU PICK FIRST." |
20 PRINT "YOU PICK FIRST." |
||
30 INPUT P$ |
30 INPUT P$ |
||
Line 262: | Line 262: | ||
290 PRINT "YOU WIN" |
290 PRINT "YOU WIN" |
||
300 STOP |
300 STOP |
||
310 PRINT "I WIN"</ |
310 PRINT "I WIN"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>YOU PICK FIRST. |
<pre>YOU PICK FIRST. |
||
Line 273: | Line 273: | ||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
< |
<syntaxhighlight lang="dos">:: |
||
::Penney's Game Task from Rosetta Code Wiki |
::Penney's Game Task from Rosetta Code Wiki |
||
::Batch File Implementation |
::Batch File Implementation |
||
Line 375: | Line 375: | ||
if /i "!you_bet!"=="%cpu_bet%" (echo [Bet something different...]&echo.&goto %1) |
if /i "!you_bet!"=="%cpu_bet%" (echo [Bet something different...]&echo.&goto %1) |
||
for %%i in ("t=T" "h=H") do set "you_bet=!you_bet:%%~i!" |
for %%i in ("t=T" "h=H") do set "you_bet=!you_bet:%%~i!" |
||
goto :EOF</ |
goto :EOF</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Note: The outputs of tosses are 'delayed' just to make the game a little more dramatic. |
Note: The outputs of tosses are 'delayed' just to make the game a little more dramatic. |
||
Line 416: | Line 416: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic">REM >penney |
||
PRINT "*** Penney's Game ***" |
PRINT "*** Penney's Game ***" |
||
REPEAT |
REPEAT |
||
Line 496: | Line 496: | ||
ELSE |
ELSE |
||
= "H" + LEFT$(sequence$, 2) |
= "H" + LEFT$(sequence$, 2) |
||
ENDIF</ |
ENDIF</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>*** Penney's Game *** |
<pre>*** Penney's Game *** |
||
Line 523: | Line 523: | ||
=={{header|C}}== |
=={{header|C}}== |
||
This solution stores the sequences in bit-packed integers. With minor adjustments, this can be extended to allow larger sequence lengths beyond the required 3. However, the ai's algorithm for the perfect choice would need to be altered. More robust methods of input could be chosen, as scanf is generally fairly unsafe. A safer alternative would be to use something like fgets, and parse the input string ourselves. |
This solution stores the sequences in bit-packed integers. With minor adjustments, this can be extended to allow larger sequence lengths beyond the required 3. However, the ai's algorithm for the perfect choice would need to be altered. More robust methods of input could be chosen, as scanf is generally fairly unsafe. A safer alternative would be to use something like fgets, and parse the input string ourselves. |
||
<syntaxhighlight lang="c"> |
|||
<lang C> |
|||
#include <stdio.h> |
#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 645: | Line 645: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 667: | Line 667: | ||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
{{works with|C sharp|7}} |
{{works with|C sharp|7}} |
||
< |
<syntaxhighlight lang="csharp">using static System.Console; |
||
using static System.Threading.Thread; |
using static System.Threading.Thread; |
||
using System; |
using System; |
||
Line 770: | Line 770: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre style="height:30ex;overflow:scroll"> |
<pre style="height:30ex;overflow:scroll"> |
||
Line 798: | Line 798: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <time.h> |
#include <time.h> |
||
#include <iostream> |
#include <iostream> |
||
Line 885: | Line 885: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 904: | Line 904: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(ns penney.core |
||
(:gen-class)) |
(:gen-class)) |
||
Line 967: | Line 967: | ||
(println "Penney's Game.") |
(println "Penney's Game.") |
||
(play-game {:first-player (flip-coin) |
(play-game {:first-player (flip-coin) |
||
:human 0, :computer 0}))</ |
:human 0, :computer 0}))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 999: | Line 999: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
{{trans|UNIX Shell}} |
{{trans|UNIX Shell}} |
||
< |
<syntaxhighlight lang="lisp">(setf *random-state* (make-random-state t)) |
||
(defparameter *heads* #\H) |
(defparameter *heads* #\H) |
||
Line 1,063: | Line 1,063: | ||
(coerce my-list 'string))) |
(coerce my-list 'string))) |
||
(main)</ |
(main)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 1,090: | Line 1,090: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="d">void main() { |
||
import std.stdio, std.random, std.algorithm, std.string, |
import std.stdio, std.random, std.algorithm, std.string, |
||
std.conv, std.range, core.thread; |
std.conv, std.range, core.thread; |
||
Line 1,124: | Line 1,124: | ||
Thread.sleep(1.seconds); |
Thread.sleep(1.seconds); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
The output is the same as the Python entry. |
The output is the same as the Python entry. |
||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="elixir">defmodule Penney do |
||
@toss [:Heads, :Tails] |
@toss [:Heads, :Tails] |
||
Line 1,185: | Line 1,185: | ||
end |
end |
||
Penney.game</ |
Penney.game</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,211: | Line 1,211: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: arrays ascii io kernel math prettyprint random sequences |
||
strings ; |
strings ; |
||
IN: rosetta-code.penneys-game |
IN: rosetta-code.penneys-game |
||
Line 1,298: | Line 1,298: | ||
[ computer-first human-second ] if flip-coins ; |
[ computer-first human-second ] if flip-coins ; |
||
start-game</ |
start-game</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,350: | Line 1,350: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|BBC BASIC}} |
{{trans|BBC BASIC}} |
||
< |
<syntaxhighlight lang="freebasic"> |
||
Sub Jugador_elige(secuencia As String) |
Sub Jugador_elige(secuencia As String) |
||
Dim As String eleccion |
Dim As String eleccion |
||
Line 1,444: | Line 1,444: | ||
Sleep |
Sleep |
||
End |
End |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
<syntaxhighlight lang="go"> |
|||
<lang Go> |
|||
package main |
package main |
||
import "fmt" |
import "fmt" |
||
Line 1,536: | Line 1,536: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,551: | Line 1,551: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import qualified Data.List as L |
||
import System.IO |
import System.IO |
||
import System.Random |
import System.Random |
||
Line 1,641: | Line 1,641: | ||
stdgen <- getStdGen |
stdgen <- getStdGen |
||
let (cpuFirst, genA) = random stdgen |
let (cpuFirst, genA) = random stdgen |
||
game cpuFirst 0 0 genA</ |
game cpuFirst 0 0 genA</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,664: | Line 1,664: | ||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution:''' |
'''Solution:''' |
||
< |
<syntaxhighlight lang="j">require 'format/printf numeric' |
||
randomize '' NB. randomize seed for new session |
randomize '' NB. randomize seed for new session |
||
Line 1,691: | Line 1,691: | ||
'Toss sequence is %s' printf < (3 + <./ Result) {. Tosses |
'Toss sequence is %s' printf < (3 + <./ Result) {. Tosses |
||
echo ('No result';'You win!';'Computer won!') {::~ *-/ Result |
echo ('No result';'You win!';'Computer won!') {::~ *-/ Result |
||
)</ |
)</syntaxhighlight> |
||
'''Usage:''' |
'''Usage:''' |
||
< |
<syntaxhighlight lang="j"> playPenney'' |
||
Computer chose TTT |
Computer chose TTT |
||
Choose a sequence of three coin tosses (H/T): |
Choose a sequence of three coin tosses (H/T): |
||
Line 1,704: | Line 1,704: | ||
Computer chose HHT |
Computer chose HHT |
||
Toss sequence is HTHTT |
Toss sequence is HTHTT |
||
You win!</ |
You win!</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">import java.util.*; |
||
public class PenneysGame { |
public class PenneysGame { |
||
Line 1,758: | Line 1,758: | ||
return s; |
return s; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre>Computer chooses HTH |
<pre>Computer chooses HTH |
||
Line 1,779: | Line 1,779: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
'''Functions''' |
'''Functions''' |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
const SLEN = 3 |
const SLEN = 3 |
||
Line 1,812: | Line 1,812: | ||
return b |
return b |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Game Setup''' |
'''Game Setup''' |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
println("Playing Penney's Game Against the computer.") |
println("Playing Penney's Game Against the computer.") |
||
Line 1,830: | Line 1,830: | ||
print(@sprintf "You bet %s " pgdecode(human)) |
print(@sprintf "You bet %s " pgdecode(human)) |
||
println(@sprintf "and the computer bet %s." pgdecode(mach)) |
println(@sprintf "and the computer bet %s." pgdecode(mach)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Game Play''' |
'''Game Play''' |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
pg = randbool(SLEN) |
pg = randbool(SLEN) |
||
pgtail = copy(pg) |
pgtail = copy(pg) |
||
Line 1,851: | Line 1,851: | ||
println("so you won.") |
println("so you won.") |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{output}} |
{{output}} |
||
Line 1,904: | Line 1,904: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.2.10 |
||
import java.util.Random |
import java.util.Random |
||
Line 1,973: | Line 1,973: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Sample game where computer goes first: |
Sample game where computer goes first: |
||
Line 2,010: | Line 2,010: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua"> |
||
function penny_game() |
function penny_game() |
||
local player, computer = "", "" |
local player, computer = "", "" |
||
Line 2,099: | Line 2,099: | ||
r = io.read() |
r = io.read() |
||
until( r == "N" or r == "n" ) |
until( r == "N" or r == "n" ) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,125: | Line 2,125: | ||
=={{header|MiniScript}}== |
=={{header|MiniScript}}== |
||
< |
<syntaxhighlight lang="miniscript">randomTorH = function() |
||
if rnd < 0.5 then return "T" else return "H" |
if rnd < 0.5 then return "T" else return "H" |
||
end function |
end function |
||
Line 2,154: | Line 2,154: | ||
end if |
end if |
||
wait |
wait |
||
end while</ |
end while</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,180: | Line 2,180: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="nim">import os, random, sequtils, strutils |
||
randomize() |
randomize() |
||
Line 2,212: | Line 2,212: | ||
echo "\n I win!" |
echo "\n I win!" |
||
break |
break |
||
sleep(1000)</ |
sleep(1000)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,223: | Line 2,223: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
< |
<syntaxhighlight lang="pascal">PROGRAM Penney; |
||
TYPE |
TYPE |
||
Line 2,418: | Line 2,418: | ||
readln |
readln |
||
END.</ |
END.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,480: | Line 2,480: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl"> |
||
#!usr/bin/perl |
#!usr/bin/perl |
||
use 5.020; |
use 5.020; |
||
Line 2,591: | Line 2,591: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,614: | Line 2,614: | ||
{{trans|C}} |
{{trans|C}} |
||
Robert's robot's name is Robort. |
Robert's robot's name is Robort. |
||
< |
<syntaxhighlight lang="phix">function trio(integer pick) |
||
return substitute_all(sprintf("%03b",pick),"10","HT") |
return substitute_all(sprintf("%03b",pick),"10","HT") |
||
end function |
end function |
||
Line 2,693: | Line 2,693: | ||
end procedure |
end procedure |
||
main()</ |
main()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,742: | Line 2,742: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(seed (in "/dev/urandom" (rd 8))) |
||
(setq *S (list 0 0)) |
(setq *S (list 0 0)) |
||
Line 2,786: | Line 2,786: | ||
(= "N" (uppc (in NIL (char)))) ) ) ) ) |
(= "N" (uppc (in NIL (char)))) ) ) ) ) |
||
(go)</ |
(go)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,824: | Line 2,824: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
< |
<syntaxhighlight lang="prolog">play :- rand1(R), game(R). |
||
game(h) :- |
game(h) :- |
||
Line 2,860: | Line 2,860: | ||
coin(0,h). coin(1,t). |
coin(0,h). coin(1,t). |
||
coin_s(h, 'H'). coin_s(t, 'T'). |
coin_s(h, 'H'). coin_s(t, 'T'). |
||
opp(h, t). opp(t, h).</ |
opp(h, t). opp(t, h).</syntaxhighlight> |
||
Output: |
Output: |
||
Line 2,880: | Line 2,880: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">from __future__ import print_function |
||
import random |
import random |
||
from time import sleep |
from time import sleep |
||
Line 2,909: | Line 2,909: | ||
print('\n I win!') |
print('\n I win!') |
||
break |
break |
||
sleep(1) # For dramatic effect</ |
sleep(1) # For dramatic effect</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,928: | Line 2,928: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="rsplus">#=============================================================== |
||
# Penney's Game Task from Rosetta Code Wiki |
# Penney's Game Task from Rosetta Code Wiki |
||
# R implementation |
# R implementation |
||
Line 2,991: | Line 2,991: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Human first. |
Human first. |
||
Line 3,032: | Line 3,032: | ||
This does what's required of it from the task... just don't input anything outside the alphabet "htHT" for the human move. |
This does what's required of it from the task... just don't input anything outside the alphabet "htHT" for the human move. |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
;; Penney's Game. Tim Brown 2014-10-15 |
;; Penney's Game. Tim Brown 2014-10-15 |
||
(define (flip . _) (match (random 2) (0 "H") (1 "T"))) |
(define (flip . _) (match (random 2) (0 "H") (1 "T"))) |
||
Line 3,068: | Line 3,068: | ||
(printf "~a chooses: ~a~%" (car p1) (flips->string (cdr p1))) |
(printf "~a chooses: ~a~%" (car p1) (flips->string (cdr p1))) |
||
(values p1 (cons 'Hom-Sap (get-human-sequence)))])) |
(values p1 (cons 'Hom-Sap (get-human-sequence)))])) |
||
(printf "~a wins!~%" (game-on player-1 player-2)))</ |
(printf "~a wins!~%" (game-on player-1 player-2)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,107: | Line 3,107: | ||
{{works with|Rakudo|2018.02}} |
{{works with|Rakudo|2018.02}} |
||
<lang |
<syntaxhighlight lang="raku" line>enum Coin <Heads Tails>; |
||
enum Yay <Yay Good Super Hah Ooh Yipee Sweet Cool Yes Haha>; |
enum Yay <Yay Good Super Hah Ooh Yipee Sweet Cool Yes Haha>; |
||
enum Boo <Drat Darn Crumb Oops Rats Bah Criminy Argh Shards>; |
enum Boo <Drat Darn Crumb Oops Rats Bah Criminy Argh Shards>; |
||
Line 3,178: | Line 3,178: | ||
print "$_ "; |
print "$_ "; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Note: the actual run displays a little coin-flipping animation, but that won't show up here: |
Note: the actual run displays a little coin-flipping animation, but that won't show up here: |
||
Line 3,228: | Line 3,228: | ||
A feature also added was to allow a seed for the '''random''' BIF to allow repeatability for a game. |
A feature also added was to allow a seed for the '''random''' BIF to allow repeatability for a game. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program plays/simulates Penney's Game, a two─player coin toss sequence game. */ |
||
__= copies('─', 9) /*literal for eye─catching fence. */ |
__= copies('─', 9) /*literal for eye─catching fence. */ |
||
signal on halt /*a clean way out if CLBF quits. */ |
signal on halt /*a clean way out if CLBF quits. */ |
||
Line 3,295: | Line 3,295: | ||
do until g\==@.CBLF!; g= r(#); end /*otherwise, generate a choice. */ |
do until g\==@.CBLF!; g= r(#); end /*otherwise, generate a choice. */ |
||
@.comp= translate(g, 'HT', 10) |
@.comp= translate(g, 'HT', 10) |
||
return</ |
return</syntaxhighlight> |
||
{{out|output|text= of a six-game session (ended by user entering a '''quit'''):}} |
{{out|output|text= of a six-game session (ended by user entering a '''quit'''):}} |
||
<pre> |
<pre> |
||
Line 3,405: | Line 3,405: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">Toss = [:Heads, :Tails] |
||
def yourChoice |
def yourChoice |
||
Line 3,439: | Line 3,439: | ||
print " #{seq[-1]}" |
print " #{seq[-1]}" |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,461: | Line 3,461: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust"> |
||
extern crate rand; |
extern crate rand; |
||
Line 3,568: | Line 3,568: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,585: | Line 3,585: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{works with|Tcl|8.6}} |
{{works with|Tcl|8.6}} |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.6 |
||
oo::class create Player { |
oo::class create Player { |
||
Line 3,682: | Line 3,682: | ||
} |
} |
||
game 3 HumanPlayer RobotPlayer</ |
game 3 HumanPlayer RobotPlayer</syntaxhighlight> |
||
{{out|Sample Game}} |
{{out|Sample Game}} |
||
<!-- Huh! This was a long one… --> |
<!-- Huh! This was a long one… --> |
||
Line 3,715: | Line 3,715: | ||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
{{works with|Bash}} |
{{works with|Bash}} |
||
< |
<syntaxhighlight lang="sh">#!/bin/bash |
||
main() { |
main() { |
||
printf $'Penney\'s Game\n\n' |
printf $'Penney\'s Game\n\n' |
||
Line 3,781: | Line 3,781: | ||
main "$@" |
main "$@" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
Line 3,818: | Line 3,818: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
Option Explicit |
Option Explicit |
||
Line 3,899: | Line 3,899: | ||
End If |
End If |
||
End If |
End If |
||
End Function</ |
End Function</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>You start |
<pre>You start |
||
Line 3,924: | Line 3,924: | ||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{libheader|Wren-str}} |
{{libheader|Wren-str}} |
||
< |
<syntaxhighlight lang="ecmascript">import "random" for Random |
||
import "io" for Stdin, Stdout |
import "io" for Stdin, Stdout |
||
import "timer" for Timer |
import "timer" for Timer |
||
Line 3,993: | Line 3,993: | ||
} |
} |
||
Timer.sleep(2000) // wait two seconds for next flip |
Timer.sleep(2000) // wait two seconds for next flip |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,029: | Line 4,029: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn coinToss{ (0).random(2) and "H" or "T" } // (0).random(2) --> 0<=r<2 |
||
reg myBet, yourBet; |
reg myBet, yourBet; |
||
if(coinToss()=="H"){ // The toss says I go first |
if(coinToss()=="H"){ // The toss says I go first |
||
Line 4,046: | Line 4,046: | ||
if(Void!=coins.find(myBet)) { println(" I win!"); break; } |
if(Void!=coins.find(myBet)) { println(" I win!"); break; } |
||
// ignore we both won |
// ignore we both won |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |