Pig the dice game: Difference between revisions
m
syntax highlighting fixup automation
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 20:
{{trans|Python}}
<
V maxscore = 100
V safescore = [0] * playercount
Line 44:
(score, player) = (0, (player + 1) % playercount)
print("\nPlayer #. wins with a score of #.".format(player, safescore[player]))</
{{out}}
Line 50:
=={{header|ActionScript}}==
<syntaxhighlight lang="actionscript">
package {
Line 477:
}
</syntaxhighlight>
=={{header|Ada}}==
Line 491:
Also, there is a procedure Play to play the game, following whatever the actors do.
<
type Dice_Score is range 1 .. 6;
Line 513:
end record;
end Pig;</
The implementation of Pig is as follows:
<
package body Pig is
Line 567:
begin
RND.Reset(Gen);
end Pig;</
===Solving the Task===
Line 574:
class Hand from the class Actor to implement manually playing the game.
<
procedure Play_Pig is
Line 605:
Play(A1, A2, Alice);
Ada.Text_IO.Put_Line("Winner = " & (if Alice then "Alice!" else "Bob!"));
end Play_Pig;</
{{out}}
Line 635:
=={{header|AutoHotkey}}==
<
Gui, Add, Text, vPlayer0, Player 0
Gui, Add, Text, vSum0, 000
Line 681:
ButtonReload:
Reload</
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f PIG_THE_DICE_GAME.AWK
# converted from LUA
Line 723:
exit(0)
}
</syntaxhighlight>
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
numjugadores = 2
maxpuntos = 100
Line 771:
Next jugador
Until false
</syntaxhighlight>
=={{header|C}}==
<syntaxhighlight lang="c">
#include <stdio.h>
#include <stdlib.h>
Line 859:
}
</syntaxhighlight>
Output:
Line 901:
=={{header|C sharp}}==
<
using System;
using System.IO;
Line 1,017:
}
}
</syntaxhighlight>
=={{header|C++}}==
<
#include <windows.h>
#include <iostream>
Line 1,156:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
Output:
<pre>
Line 1,176:
=={{header|Clojure}}==
<
(defn roll-dice []
Line 1,216:
(defn -main [& args]
(println "Pig the Dice Game.")
(play-game {:player1 0, :player2 0} :player1))</
=={{header|CLU}}==
<
pig = cluster is play
Line 1,318:
start_up = proc()
pig$play()
end start_up</
{{out}}
<pre style='height:50ex'>Game of Pig
Line 1,469:
=={{header|Common Lisp}}==
<
(defconstant +n-of-players+ 2)
Line 1,500:
(setf current-player (mod (1+ current-player) +n-of-players+)))))
(format t "~%Player ~d wins with a score of ~d~%" current-player
(nth current-player scores)))</
{{out}}
<pre>Player 0: (0, 0). Rolling? (Y)
Line 1,535:
=={{header|D}}==
{{trans|Python}}
<
import std.stdio, std.string, std.algorithm, std.random;
enum maxScore = 100;
Line 1,571:
writefln("\n\nPlayer %d wins with a score of %d",
player, safeScore[player]);
}</
{{out}}
<pre> Player 0: (0, 0). Rolling? (y/n)
Line 1,606:
{{Trans|Go}}
Thanks JensBorrisholt for library [https://github.com/JensBorrisholt/DelphiConsole System.Console].
<
{$APPTYPE CONSOLE}
Line 1,687:
writeln(format('Player %d wins!!!', [(turn - 1) mod Length(playerScores)]));
Readln;
end.</
{{out}}
<pre>Player 0 has: 96 points
Line 1,700:
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
class
PLAYER
Line 1,752:
points: INTEGER
end
</syntaxhighlight>
<syntaxhighlight lang="eiffel">
class
PIG_THE_DICE
Line 1,799:
winner: PLAYER
end
</syntaxhighlight>
Test:
<syntaxhighlight lang="eiffel">
class
APPLICATION
Line 1,818:
pig: PIG_THE_DICE
end
</syntaxhighlight>
{{out}}
<pre>
Line 1,844:
=={{header|Erlang}}==
Some of the code (ex: quit/2) is only there to make play during development easier. Some (ex: player_name/1) is only there to make [[Pig_the_dice_game/Player]] easier.
<syntaxhighlight lang="erlang">
-module( pig_dice ).
Line 1,941:
[io:fwrite("~p with ~p~n", [N, T]) || {N, T} <- Rest];
task_display( Result ) -> io:fwrite( "Result: ~p~n", [Result] ).
</syntaxhighlight>
{{out}}
Start of game:
Line 1,998:
=={{header|FOCAL}}==
<
01.20 S S(1)=0;S S(2)=0;S P=1
01.30 T !"PLAYER",%1,P," TURN BEGINS"!
Line 2,019:
03.55 T "- TOO BAD!"!;S T=0
03.60 S S(P)=S(P)+T
03.65 T "PLAYER",%1,P," SCORE",%3,S(P)," TURN FINISHED"!</
=={{header|Forth}}==
{{works with|4tH|3.62.1}}
<
include lib/yesorno.4th
Line 2,043:
; \ show the results
pigthedice</
=={{header|FreeBASIC}}==
<
Const numjugadores = 2
Const maxpuntos = 100
Line 2,089:
Next jugador
Loop
</syntaxhighlight>
=={{header|Go}}==
<
import (
Line 2,146:
}
fmt.Printf("Player %v wins!!!\n", (turn-1)%len(playerScores))
}</
=={{header|Groovy}}==
Currently hard coded for 2 players, but will work for multiple players
<syntaxhighlight lang="groovy">
class PigDice {
Line 2,245:
}
}
</syntaxhighlight>
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">
import System.Random (randomRIO)
Line 2,288:
putStrLn "\nInvalid input - please try again."
askPlayer $ Score stack score
</syntaxhighlight>
Example output:
Line 2,333:
=={{header|IS-BASIC}}==
<
110 RANDOMIZE
120 STRING PLAYER$(1 TO 2)*28
Line 2,386:
610 LOOP UNTIL K$="y" OR K$="n"
620 LET ANSWER$=K$
630 END DEF</
=={{header|J}}==
<
status=:3 :0
Line 2,423:
smoutput 'player scores now: ',":scores end. end.
smoutput 'player ',(":1+I.scores>:100),' wins'
)</
Example game:
<syntaxhighlight lang="text"> pigsim 2
2 player game of pig
begining of turn for player 1
Line 2,488:
..Roll the dice or Hold or Quit? [R or H or Q]: H
player scores now: 84 102
player 2 wins</
=={{header|Java}}==
{{trans|D}}
<
public class PigDice {
Line 2,533:
player, safeScore[player]);
}
}</
{{works with|Java|8+}}
<
import java.util.Random;
import java.util.Scanner;
Line 2,602:
;
}
}</
<pre> Player 0: (0, 0) Rolling? (y/n) y
Line 2,641:
=={{header|JavaScript}}==
<
{ name: '', score: 0 },
{ name: '', score: 0 }
Line 2,684:
`);
}
</syntaxhighlight>
=={{header|Julia}}==
The game is built around the <tt>PigPlayer</tt> type, which contains the player information, including a reference to the strategy function, <tt>strat</tt>, that is to be used to determined whether a player is going to continue to roll. In this incarnation of the game, there is only one strategy function available, <tt>pig_manual</tt>, which gets this decision from user input.
<syntaxhighlight lang="julia">
type PigPlayer
name::String
Line 2,746:
pig_game([PigPlayer("Alice"), PigPlayer("Bob")])
</syntaxhighlight>
{{out}}
Line 2,792:
=={{header|Kotlin}}==
<
fun main(Args: Array<String>) {
Line 2,836:
}
}
}</
{{out}}
Line 2,938:
{{works with|lua|5.1}}
Supports any number of players
<
local maxScore = 100
local scores = { }
Line 2,972:
end
end
</syntaxhighlight>
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module GamePig {
Print "Game of Pig"
Line 3,052:
}
GamePig
</syntaxhighlight>
=={{header|Maple}}==
<
local Points, pointsThisTurn, answer, rollNum, i, win;
randomize();
Line 3,099:
end proc;
pig();</
{{out}}
<pre>
Line 3,130:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="text">DynamicModule[{score, players = {1, 2}, roundscore = 0,
roll}, (score@# = 0) & /@ players;
Panel@Dynamic@
Line 3,147:
players = RotateLeft@players]]},
Button["Play again.",
roundscore = 0; (score@# = 0) & /@ players]]}]</
=={{header|MiniScript}}==
<
Player = {}
Player.score = 0
Line 3,193:
if player.score >= goal then break
end for
end while</
{{out}}
Line 3,237:
=={{header|Nim}}==
{{trans|Kotlin}}
<
randomize()
Line 3,285:
else:
echo " Must be 'r' or 'h', try again"</
{{out}}
Line 3,343:
=={{header|Objeck}}==
<
class Pig {
function : Main(args : String[]) ~ Nil {
Line 3,383:
}
}
</syntaxhighlight>
<pre>
Player 0: (0, 0) Rolling? (y/n)
Line 3,418:
=={{header|OCaml}}==
<
object
val name = name_init
Line 3,469:
let p1 = new player "Steven"
and p2 = new player "John" in
one_turn p1 p2;;</
=={{header|Pascal}}==
{{works with|Free Pascal|2.6.4}}
<
const
Line 3,582:
else
Congratulate(Player2)
end.</
=={{header|Perl}}==
You can have as many players as you want, simply provide their names on the command line.
<
use strict;
use warnings;
Line 3,626:
}
__END__
</syntaxhighlight>
=={{header|Phix}}==
Initially a translation of [[Pig_the_dice_game#Lua|Lua]], but now quite different.
<
maxScore = 100
sequence scores = repeat(0,numPlayers)
Line 3,657:
end if
end while
printf(1,"\nPlayer %d wins with a score of %d!\n",{player,scores[player]+points})</
{{out}}
<pre>
Line 3,677:
=={{header|PHP}}==
{{trans|D}}
<
define('MAXSCORE', 100);
Line 3,709:
printf('\n\nPlayer %d wins with a score of %d ',
$player, $safeScore[$player]);
</syntaxhighlight>
<pre>C:\UniServer\usr\local\php\php pig.php
Player 0: (0, 0) Rolling? (Yn)
Line 3,748:
=={{header|Python}}==
<
'''
Line 3,784:
score, player = 0, (player + 1) % playercount
print('\nPlayer %i wins with a score of %i' %(player, safescore[player]))</
;Samples from a game:
Line 3,825:
=={{header|Racket}}==
<
#lang racket
Line 3,854:
(pig-the-dice #:print? #t human human)
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015-09-30}}
<syntaxhighlight lang="raku"
sub MAIN (Int :$players = 2, Int :$goal = 100) {
Line 3,890:
say " Sticking with $safe." if $ante;
}
}</
The game defaults to the specified task, but we'll play a shorter game with three players for our example:
{{out}}
Line 3,955:
:::* names are used for the die faces (in addition to the pip value)
:::* a simple (but aggressive) strategy is used (that favors a human player)
<
sw= linesize() - 1 /*get the width of the terminal screen,*/
parse arg hp cp win die _ . '(' names ")" /*obtain optional arguments from the CL*/
Line 4,054:
/*──────────────────────────────────────────────────────────────────────────────────────*/
err: say; say; say center(' error! ', max(40, linesize() % 2), "*"); say
do j=1 for arg(); say arg(j); say; end; say; exit 13</
This REXX program makes use of '''LINESIZE''' REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).
<br>The '''LINESIZE.REX''' REXX program is included here ──► [[LINESIZE.REX]].
Line 4,178:
=={{header|Ring}}==
<
# Project : Pig the dice game
Line 4,207:
end
see "Player " + player + " wins with a score of " + safeScore[player]
</syntaxhighlight>
Output:
<pre>
Line 4,252:
* transparent game logic
<
Player = Struct.new(:name, :safescore, :score) do
def bust!() self.score = safescore end
Line 4,294:
end
PigGame.new( %w|Samuel Elizabeth| )</
;Samples from a game:
Line 4,332:
=={{header|Run BASIC}}==
<
maxScore = 100
dim safeScore(numPlayers)
Line 4,357:
goto [loop]
[winner]
print "Player ";plater;" wins with a score of ";safeScore(player)</
=={{header|Rust}}==
{{works with|Rust|2018}}
<
fn main() {
Line 4,491:
self.score <= 100 || self.status == TurnStatus::Continue
}
}</
=={{header|Scala}}==
Line 4,501:
{{libheader|Scala 100% Immutable variables}}
{{libheader|Scala Concise}}
<
private val (maxScore, nPlayers) = (100, 2)
private val rnd = util.Random
Line 4,545:
loop(Game(gameOver = false, 0, 0, Array.ofDim[Int](nPlayers).toVector))
}</
=={{header|Tcl}}==
<table><tr><td>{{works with|Tcl|8.6}}</td><td>or alternatively with Tcl 8.5 and</td><td>{{libheader|TclOO}}</td></tr></table><!-- dirty trick! -->
<
oo::class create Player {
Line 4,627:
rotateList scores
}
}</
Demonstrating with human players:
<
variable me
superclass Player
Line 4,660:
}
pig [HumanPlayer new "Alex"] [HumanPlayer new "Bert"]</
{{out|Sample output}}
<pre>
Line 4,685:
=={{header|VBA}}==
<syntaxhighlight lang="vb">
Option Explicit
Line 4,736:
MsgBox Replace(WIN, "¤¤¤¤", Cp)
End Sub
</syntaxhighlight>
=={{header|Vlang}}==
{{trans|go}}
<
import rand.seed
import os
Line 4,789:
}
println("Player ${(turn-1)%player_scores.len} wins!!!", )
}</
=={{header|Wren}}==
Line 4,795:
{{libheader|Wren-ioutil}}
{{libheader|Wren-str}}
<
import "/str" for Str
import "random" for Random
Line 4,834:
System.print(" Your score for the round is now %(score)")
}
}</
{{out}}
Line 4,954:
=={{header|XPL0}}==
<
integer Player, Die, Points, Score(2);
[Score(0):= 0; Score(1):= 0; \starting scores for each player
Line 4,978:
until Score(Player) >= 100;
Text(0, "Player "); IntOut(0, Player+1); Text(0, " WINS!!!");
]</
Output:
Line 5,017:
=={{header|zkl}}==
{{trans|D}}
<
players,safeScores:=Walker.cycle([0..PLAYERS-1]), PLAYERS.pump(List(),0);
rollDie:=(1).random.fp(7);
Line 5,040:
player,score,S=players.next(),0,safeScores[player];
}
println("\n\nPlayer %d wins with a score of %d".fmt(player+1, safeScores[player]));</
{{out}}
<pre>
|