Penney's game: Difference between revisions
m
syntax highlighting fixup automation
Alextretyak (talk | contribs) (Added 11l) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 33:
{{trans|Python}}
<
V you = ‘’
Line 60:
print("\n I win!")
L.break
sleep(1)</
{{out}}
Line 72:
=={{header|AutoHotkey}}==
<
Gui, add, text, w90, Computer:
loop, 3
Line 152:
gosub, RandomStart
return
;-----------------------------------------------</
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
global jugador, computador, secuencia
jugador = "" : computador = ""
Line 227:
print "¡Gracias por jugar!"
end
</syntaxhighlight>
==={{header|Sinclair ZX81 BASIC}}===
Works with 1k of RAM.
<
20 PRINT "YOU PICK FIRST."
30 INPUT P$
Line 262:
290 PRINT "YOU WIN"
300 STOP
310 PRINT "I WIN"</
{{out}}
<pre>YOU PICK FIRST.
Line 273:
=={{header|Batch File}}==
<
::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</
{{out}}
Note: The outputs of tosses are 'delayed' just to make the game a little more dramatic.
Line 416:
=={{header|BBC BASIC}}==
<
PRINT "*** Penney's Game ***"
REPEAT
Line 496:
ELSE
= "H" + LEFT$(sequence$, 2)
ENDIF</
{{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">
#include <stdio.h>
#include <stdlib.h>
Line 645:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
Line 667:
=={{header|C sharp}}==
{{works with|C sharp|7}}
<
using static System.Threading.Thread;
using System;
Line 770:
}
</syntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">
Line 798:
=={{header|C++}}==
<
#include <time.h>
#include <iostream>
Line 885:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
Line 904:
=={{header|Clojure}}==
<
(:gen-class))
Line 967:
(println "Penney's Game.")
(play-game {:first-player (flip-coin)
:human 0, :computer 0}))</
{{out}}
Line 999:
=={{header|Common Lisp}}==
{{trans|UNIX Shell}}
<
(defparameter *heads* #\H)
Line 1,063:
(coerce my-list 'string)))
(main)</
{{Out}}
Line 1,090:
=={{header|D}}==
{{trans|Python}}
<
import std.stdio, std.random, std.algorithm, std.string,
std.conv, std.range, core.thread;
Line 1,124:
Thread.sleep(1.seconds);
}
}</
The output is the same as the Python entry.
=={{header|Elixir}}==
{{trans|Ruby}}
<
@toss [:Heads, :Tails]
Line 1,185:
end
Penney.game</
{{out}}
Line 1,211:
=={{header|Factor}}==
<
strings ;
IN: rosetta-code.penneys-game
Line 1,298:
[ computer-first human-second ] if flip-coins ;
start-game</
{{out}}
<pre>
Line 1,350:
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
<
Sub Jugador_elige(secuencia As String)
Dim As String eleccion
Line 1,444:
Sleep
End
</syntaxhighlight>
=={{header|Go}}==
<syntaxhighlight lang="go">
package main
import "fmt"
Line 1,536:
}
}
</syntaxhighlight>
Output:
<pre>
Line 1,551:
=={{header|Haskell}}==
<
import System.IO
import System.Random
Line 1,641:
stdgen <- getStdGen
let (cpuFirst, genA) = random stdgen
game cpuFirst 0 0 genA</
{{out}}
<pre>
Line 1,664:
=={{header|J}}==
'''Solution:'''
<
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
)</
'''Usage:'''
<
Computer chose TTT
Choose a sequence of three coin tosses (H/T):
Line 1,704:
Computer chose HHT
Toss sequence is HTHTT
You win!</
=={{header|Java}}==
<
public class PenneysGame {
Line 1,758:
return s;
}
}</
<pre>Computer chooses HTH
Line 1,779:
=={{header|Julia}}==
'''Functions'''
<syntaxhighlight lang="julia">
const SLEN = 3
Line 1,812:
return b
end
</syntaxhighlight>
'''Game Setup'''
<syntaxhighlight 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>
'''Game Play'''
<syntaxhighlight lang="julia">
pg = randbool(SLEN)
pgtail = copy(pg)
Line 1,851:
println("so you won.")
end
</syntaxhighlight>
{{output}}
Line 1,904:
=={{header|Kotlin}}==
<
import java.util.Random
Line 1,973:
}
}
</syntaxhighlight>
Sample game where computer goes first:
Line 2,010:
=={{header|Lua}}==
<
function penny_game()
local player, computer = "", ""
Line 2,099:
r = io.read()
until( r == "N" or r == "n" )
</syntaxhighlight>
{{out}}
<pre>
Line 2,125:
=={{header|MiniScript}}==
<
if rnd < 0.5 then return "T" else return "H"
end function
Line 2,154:
end if
wait
end while</
{{out}}
Line 2,180:
=={{header|Nim}}==
{{trans|Python}}
<
randomize()
Line 2,212:
echo "\n I win!"
break
sleep(1000)</
{{out}}
Line 2,223:
=={{header|Pascal}}==
<
TYPE
Line 2,418:
readln
END.</
{{out}}
Line 2,480:
=={{header|Perl}}==
<
#!usr/bin/perl
use 5.020;
Line 2,591:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 2,614:
{{trans|C}}
Robert's robot's name is Robort.
<
return substitute_all(sprintf("%03b",pick),"10","HT")
end function
Line 2,693:
end procedure
main()</
{{out}}
<pre>
Line 2,742:
=={{header|PicoLisp}}==
<
(setq *S (list 0 0))
Line 2,786:
(= "N" (uppc (in NIL (char)))) ) ) ) )
(go)</
{{out}}
<pre>
Line 2,824:
=={{header|Prolog}}==
<
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).</
Output:
Line 2,880:
=={{header|Python}}==
<
import random
from time import sleep
Line 2,909:
print('\n I win!')
break
sleep(1) # For dramatic effect</
{{out}}
Line 2,928:
=={{header|R}}==
<
# Penney's Game Task from Rosetta Code Wiki
# R implementation
Line 2,991:
}
}
</syntaxhighlight>
{{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.
<
;; 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)))</
{{out}}
Line 3,107:
{{works with|Rakudo|2018.02}}
<syntaxhighlight lang="raku"
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 "$_ ";
}
}</
{{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 '''random''' BIF to allow repeatability for a 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</
{{out|output|text= of a six-game session (ended by user entering a '''quit'''):}}
<pre>
Line 3,405:
=={{header|Ruby}}==
<
def yourChoice
Line 3,439:
print " #{seq[-1]}"
end
end</
{{out}}
Line 3,461:
=={{header|Rust}}==
<
extern crate rand;
Line 3,568:
}
</syntaxhighlight>
{{out}}
<pre>
Line 3,585:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<
oo::class create Player {
Line 3,682:
}
game 3 HumanPlayer RobotPlayer</
{{out|Sample Game}}
<!-- Huh! This was a long one… -->
Line 3,715:
=={{header|UNIX Shell}}==
{{works with|Bash}}
<
main() {
printf $'Penney\'s Game\n\n'
Line 3,781:
main "$@"
</syntaxhighlight>
{{Output}}
Line 3,818:
=={{header|VBA}}==
<syntaxhighlight lang="vb">
Option Explicit
Line 3,899:
End If
End If
End Function</
{{out}}
<pre>You start
Line 3,924:
{{trans|Kotlin}}
{{libheader|Wren-str}}
<
import "io" for Stdin, Stdout
import "timer" for Timer
Line 3,993:
}
Timer.sleep(2000) // wait two seconds for next flip
}</
{{out}}
Line 4,029:
=={{header|zkl}}==
<
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
}</
{{out}}
<pre>
|