Pig the dice game: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 20:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V playercount = 2
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]))</langsyntaxhighlight>
 
{{out}}
Line 50:
 
=={{header|ActionScript}}==
<syntaxhighlight lang="actionscript">
<lang ActionScript>
package {
Line 477:
}
</syntaxhighlight>
</lang>
 
=={{header|Ada}}==
Line 491:
Also, there is a procedure Play to play the game, following whatever the actors do.
 
<langsyntaxhighlight Adalang="ada">package Pig is
type Dice_Score is range 1 .. 6;
Line 513:
end record;
 
end Pig;</langsyntaxhighlight>
 
The implementation of Pig is as follows:
 
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Discrete_Random;
package body Pig is
Line 567:
begin
RND.Reset(Gen);
end Pig;</langsyntaxhighlight>
 
===Solving the Task===
Line 574:
class Hand from the class Actor to implement manually playing the game.
 
<langsyntaxhighlight Adalang="ada">with Pig, Ada.Text_IO;
 
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;</langsyntaxhighlight>
 
{{out}}
Line 635:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">Gui, Font, s12, Verdana
Gui, Add, Text, vPlayer0, Player 0
Gui, Add, Text, vSum0, 000
Line 681:
 
ButtonReload:
Reload</langsyntaxhighlight>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PIG_THE_DICE_GAME.AWK
# converted from LUA
Line 723:
exit(0)
}
</syntaxhighlight>
</lang>
 
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
<lang BASIC256>
numjugadores = 2
maxpuntos = 100
Line 771:
Next jugador
Until false
</syntaxhighlight>
</lang>
 
=={{header|C}}==
<syntaxhighlight lang="c">
<lang c>
#include <stdio.h>
#include <stdlib.h>
Line 859:
}
 
</syntaxhighlight>
</lang>
 
Output:
Line 901:
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">
using System;
using System.IO;
Line 1,017:
}
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <windows.h>
#include <iostream>
Line 1,156:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,176:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(def max 100)
 
(defn roll-dice []
Line 1,216:
(defn -main [& args]
(println "Pig the Dice Game.")
(play-game {:player1 0, :player2 0} :player1))</langsyntaxhighlight>
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">% This program uses the RNG included in PCLU's "misc.lib".
 
pig = cluster is play
Line 1,318:
start_up = proc()
pig$play()
end start_up</langsyntaxhighlight>
{{out}}
<pre style='height:50ex'>Game of Pig
Line 1,469:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defconstant +max-score+ 100)
(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)))</langsyntaxhighlight>
{{out}}
<pre>Player 0: (0, 0). Rolling? (Y)
Line 1,535:
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang="d">void main() {
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]);
}</langsyntaxhighlight>
{{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].
<langsyntaxhighlight Delphilang="delphi">program Pig_the_dice_game;
 
{$APPTYPE CONSOLE}
Line 1,687:
writeln(format('Player %d wins!!!', [(turn - 1) mod Length(playerScores)]));
Readln;
end.</langsyntaxhighlight>
{{out}}
<pre>Player 0 has: 96 points
Line 1,700:
=={{header|Eiffel}}==
 
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
PLAYER
Line 1,752:
points: INTEGER
end
</syntaxhighlight>
</lang>
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
PIG_THE_DICE
Line 1,799:
winner: PLAYER
end
</syntaxhighlight>
</lang>
Test:
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 1,818:
pig: PIG_THE_DICE
end
</syntaxhighlight>
</lang>
{{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">
<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>
</lang>
{{out}}
Start of game:
Line 1,998:
 
=={{header|FOCAL}}==
<langsyntaxhighlight lang="focal">01.10 T "GAME OF PIG"!
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"!</langsyntaxhighlight>
 
=={{header|Forth}}==
{{works with|4tH|3.62.1}}
<langsyntaxhighlight lang="forth">include lib/choose.4th
include lib/yesorno.4th
 
Line 2,043:
; \ show the results
 
pigthedice</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
Const numjugadores = 2
Const maxpuntos = 100
Line 2,089:
Next jugador
Loop
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
<langsyntaxhighlight Golang="go">package main
 
import (
Line 2,146:
}
fmt.Printf("Player %v wins!!!\n", (turn-1)%len(playerScores))
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
Currently hard coded for 2 players, but will work for multiple players
<syntaxhighlight lang="groovy">
<lang Groovy>
class PigDice {
 
Line 2,245:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">
<lang Haskell>
import System.Random (randomRIO)
 
Line 2,288:
putStrLn "\nInvalid input - please try again."
askPlayer $ Score stack score
</syntaxhighlight>
</lang>
 
Example output:
Line 2,333:
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "Pig.bas"
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</langsyntaxhighlight>
 
=={{header|J}}==
 
<langsyntaxhighlight lang="j">require'general/misc/prompt' NB. was require'misc' in j6
 
status=:3 :0
Line 2,423:
smoutput 'player scores now: ',":scores end. end.
smoutput 'player ',(":1+I.scores>:100),' wins'
)</langsyntaxhighlight>
 
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</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|D}}
<langsyntaxhighlight lang="java">import java.util.*;
 
public class PigDice {
Line 2,533:
player, safeScore[player]);
}
}</langsyntaxhighlight>
 
{{works with|Java|8+}}
<langsyntaxhighlight lang="java5">import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
Line 2,602:
;
}
}</langsyntaxhighlight>
 
<pre> Player 0: (0, 0) Rolling? (y/n) y
Line 2,641:
 
=={{header|JavaScript}}==
<langsyntaxhighlight Javascriptlang="javascript">let players = [
{ name: '', score: 0 },
{ name: '', score: 0 }
Line 2,684:
`);
}
</syntaxhighlight>
</lang>
 
=={{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">
<lang Julia>
type PigPlayer
name::String
Line 2,746:
 
pig_game([PigPlayer("Alice"), PigPlayer("Bob")])
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,792:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
fun main(Args: Array<String>) {
Line 2,836:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,938:
{{works with|lua|5.1}}
Supports any number of players
<langsyntaxhighlight lang="lua">local numPlayers = 2
local maxScore = 100
local scores = { }
Line 2,972:
end
end
</syntaxhighlight>
</lang>
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module GamePig {
Print "Game of Pig"
Line 3,052:
}
GamePig
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
<langsyntaxhighlight lang="maple">pig := proc()
local Points, pointsThisTurn, answer, rollNum, i, win;
randomize();
Line 3,099:
end proc;
 
pig();</langsyntaxhighlight>
{{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]]}]</langsyntaxhighlight>
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">// Pig the Dice for two players.
Player = {}
Player.score = 0
Line 3,193:
if player.score >= goal then break
end for
end while</langsyntaxhighlight>
 
{{out}}
Line 3,237:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import random, strformat, strutils
 
randomize()
Line 3,285:
 
else:
echo " Must be 'r' or 'h', try again"</langsyntaxhighlight>
 
{{out}}
Line 3,343:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
class Pig {
function : Main(args : String[]) ~ Nil {
Line 3,383:
}
}
</syntaxhighlight>
</lang>
<pre>
Player 0: (0, 0) Rolling? (y/n)
Line 3,418:
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">class player (name_init : string) =
object
val name = name_init
Line 3,469:
let p1 = new player "Steven"
and p2 = new player "John" in
one_turn p1 p2;;</langsyntaxhighlight>
 
=={{header|Pascal}}==
{{works with|Free Pascal|2.6.4}}
 
<langsyntaxhighlight lang="pascal">program Pig;
 
const
Line 3,582:
else
Congratulate(Player2)
end.</langsyntaxhighlight>
 
=={{header|Perl}}==
You can have as many players as you want, simply provide their names on the command line.
<langsyntaxhighlight lang="perl">#!perl
use strict;
use warnings;
Line 3,626:
}
__END__
</syntaxhighlight>
</lang>
 
=={{header|Phix}}==
Initially a translation of [[Pig_the_dice_game#Lua|Lua]], but now quite different.
<langsyntaxhighlight Phixlang="phix">constant numPlayers = 2,
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})</langsyntaxhighlight>
{{out}}
<pre>
Line 3,677:
=={{header|PHP}}==
{{trans|D}}
<langsyntaxhighlight lang="php">error_reporting(E_ALL & ~ ( E_NOTICE | E_WARNING ));
 
define('MAXSCORE', 100);
Line 3,709:
printf('\n\nPlayer %d wins with a score of %d ',
$player, $safeScore[$player]);
</syntaxhighlight>
</lang>
<pre>C:\UniServer\usr\local\php\php pig.php
Player 0: (0, 0) Rolling? (Yn)
Line 3,748:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">#!/usr/bin/python3
 
'''
Line 3,784:
score, player = 0, (player + 1) % playercount
print('\nPlayer %i wins with a score of %i' %(player, safescore[player]))</langsyntaxhighlight>
 
;Samples from a game:
Line 3,825:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 3,854:
 
(pig-the-dice #:print? #t human human)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015-09-30}}
<syntaxhighlight lang="raku" perl6line>constant DIE = 1..6;
sub MAIN (Int :$players = 2, Int :$goal = 100) {
Line 3,890:
say " Sticking with $safe." if $ante;
}
}</langsyntaxhighlight>
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)
<langsyntaxhighlight lang="rexx">/*REXX program plays "pig the dice game" (any number of CBLFs and/or silicons or HALs).*/
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</langsyntaxhighlight>
This REXX program makes use of &nbsp; '''LINESIZE''' &nbsp; REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).
<br>The &nbsp; '''LINESIZE.REX''' &nbsp; REXX program is included here &nbsp; ──► &nbsp; [[LINESIZE.REX]].
Line 4,178:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Pig the dice game
 
Line 4,207:
end
see "Player " + player + " wins with a score of " + safeScore[player]
</syntaxhighlight>
</lang>
Output:
<pre>
Line 4,252:
* transparent game logic
 
<langsyntaxhighlight lang="ruby">class PigGame
Player = Struct.new(:name, :safescore, :score) do
def bust!() self.score = safescore end
Line 4,294:
end
 
PigGame.new( %w|Samuel Elizabeth| )</langsyntaxhighlight>
 
;Samples from a game:
Line 4,332:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">numPlayers = 2
maxScore = 100
dim safeScore(numPlayers)
Line 4,357:
goto [loop]
[winner]
print "Player ";plater;" wins with a score of ";safeScore(player)</langsyntaxhighlight>
 
=={{header|Rust}}==
{{works with|Rust|2018}}
<langsyntaxhighlight Rustlang="rust">use rand::prelude::*;
 
fn main() {
Line 4,491:
self.score <= 100 || self.status == TurnStatus::Continue
}
}</langsyntaxhighlight>
 
=={{header|Scala}}==
Line 4,501:
{{libheader|Scala 100% Immutable variables}}
{{libheader|Scala Concise}}
<langsyntaxhighlight Scalalang="scala">object PigDice extends App {
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))
}</langsyntaxhighlight>
 
=={{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! -->
<langsyntaxhighlight lang="tcl">package require TclOO
 
oo::class create Player {
Line 4,627:
rotateList scores
}
}</langsyntaxhighlight>
Demonstrating with human players:
<langsyntaxhighlight lang="tcl">oo::class create HumanPlayer {
variable me
superclass Player
Line 4,660:
}
 
pig [HumanPlayer new "Alex"] [HumanPlayer new "Bert"]</langsyntaxhighlight>
{{out|Sample output}}
<pre>
Line 4,685:
=={{header|VBA}}==
 
<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
 
Line 4,736:
MsgBox Replace(WIN, "¤¤¤¤", Cp)
End Sub
</syntaxhighlight>
</lang>
 
=={{header|Vlang}}==
{{trans|go}}
<langsyntaxhighlight lang="vlang">import rand
import rand.seed
import os
Line 4,789:
}
println("Player ${(turn-1)%player_scores.len} wins!!!", )
}</langsyntaxhighlight>
 
=={{header|Wren}}==
Line 4,795:
{{libheader|Wren-ioutil}}
{{libheader|Wren-str}}
<langsyntaxhighlight lang="ecmascript">import "/ioutil" for Input
import "/str" for Str
import "random" for Random
Line 4,834:
System.print(" Your score for the round is now %(score)")
}
}</langsyntaxhighlight>
 
{{out}}
Line 4,954:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
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!!!");
]</langsyntaxhighlight>
 
Output:
Line 5,017:
=={{header|zkl}}==
{{trans|D}}
<langsyntaxhighlight lang="zkl">const WIN=100, PLAYERS=2;
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]));</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits