Jump to content

Penney's game: Difference between revisions

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

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.