Pig the dice game/Player: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: syntax coloured)
m (syntax highlighting fixup automation)
Line 35: Line 35:
The implementation reads five parameters from the command line, in that order: (1) N the number of games to play, (2) the Bound for the first player, (3) the Final_Run for the first player, (4) the Bound for the second player and (5) the Final_Run for the second player. After reading these from the command line (or accepting reasonable defaults), it plays the game N times and counts how often either player wins.
The implementation reads five parameters from the command line, in that order: (1) N the number of games to play, (2) the Bound for the first player, (3) the Final_Run for the first player, (4) the Bound for the second player and (5) the Final_Run for the second player. After reading these from the command line (or accepting reasonable defaults), it plays the game N times and counts how often either player wins.


<lang Ada>with Pig; with Ada.Text_IO; with Ada.Command_Line;
<syntaxhighlight lang="ada">with Pig; with Ada.Text_IO; with Ada.Command_Line;


procedure automatic_Pig is
procedure automatic_Pig is
Line 71: Line 71:
Ada.Text_IO.Put_Line(Natural'Image(Win_Count(True)) &
Ada.Text_IO.Put_Line(Natural'Image(Win_Count(True)) &
Natural'Image(Win_Count(False)));
Natural'Image(Win_Count(False)));
end Automatic_Pig;</lang>
end Automatic_Pig;</syntaxhighlight>




Line 88: Line 88:


Requires additional file from [[Pig the dice game/Player/AutoHotkey]]
Requires additional file from [[Pig the dice game/Player/AutoHotkey]]
<lang autohotkey>#NoEnv
<syntaxhighlight lang="autohotkey">#NoEnv
SetBatchLines, -1
SetBatchLines, -1
#SingleInstance, Force
#SingleInstance, Force
Line 161: Line 161:
Roll := Optimal[SumMe,TurnSum,SumOpp+1]
Roll := Optimal[SumMe,TurnSum,SumOpp+1]
Return Roll = "" ? 1 : Roll
Return Roll = "" ? 1 : Roll
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<table border=1 cellpadding=1 cellspacing=0 width=500 style='table-layout:fixed;width:375pt'>
<table border=1 cellpadding=1 cellspacing=0 width=500 style='table-layout:fixed;width:375pt'>
Line 314: Line 314:
Player 3 always tries to score at least 20 points in a round.<br />
Player 3 always tries to score at least 20 points in a round.<br />
Player 4, just like player 3, always tries to score at least 20 points in a round. But as his round score increases, he gets a little "nervous", what increases the chances that he'll hold.
Player 4, just like player 3, always tries to score at least 20 points in a round. But as his round score increases, he gets a little "nervous", what increases the chances that he'll hold.
<lang cpp>
<syntaxhighlight lang="cpp">
#include <windows.h>
#include <windows.h>
#include <iostream>
#include <iostream>
Line 474: Line 474:
}
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 638: Line 638:
=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Just implemented two strategies. One is actually a variable strategy which holds until a specific value is reached and then turns the dice over. The default value is 25. The other is from "Practical Play of the Dice Game Pig" by ToddW. Neller and Clifton G.M. Presser. Their suggested strategy is "If either player’s score is 71 or higher, roll for the goal. Otherwise, hold at 21 + (j - i) / 8" where j is the other player's score and i is the strategizing player's score. The scoring is handled by generic functions on the player type.
Just implemented two strategies. One is actually a variable strategy which holds until a specific value is reached and then turns the dice over. The default value is 25. The other is from "Practical Play of the Dice Game Pig" by ToddW. Neller and Clifton G.M. Presser. Their suggested strategy is "If either player’s score is 71 or higher, roll for the goal. Otherwise, hold at 21 + (j - i) / 8" where j is the other player's score and i is the strategizing player's score. The scoring is handled by generic functions on the player type.
<lang lisp>(defclass player ()
<syntaxhighlight lang="lisp">(defclass player ()
((score :initform 0 :accessor score)
((score :initform 0 :accessor score)
(name :initarg :name :accessor name)))
(name :initarg :name :accessor name)))
Line 698: Line 698:


(defun play-pig-player (player1 player2)
(defun play-pig-player (player1 player2)
(catch 'quit (format t "Hooray! ~A won the game!"</lang>
(catch 'quit (format t "Hooray! ~A won the game!"</syntaxhighlight>
Output:
Output:
<lang>Darrell: Rolled a 4 - Turn: 4 Current Score: 4 Keep rolling (Y, N or Q)?Y
<syntaxhighlight lang="text">Darrell: Rolled a 4 - Turn: 4 Current Score: 4 Keep rolling (Y, N or Q)?Y
Darrell: Rolled a 3 - Turn: 7 Current Score: 7 Keep rolling (Y, N or Q)?Y
Darrell: Rolled a 3 - Turn: 7 Current Score: 7 Keep rolling (Y, N or Q)?Y
Darrell: Rolled a 3 - Turn: 10 Current Score: 10 Keep rolling (Y, N or Q)?Y
Darrell: Rolled a 3 - Turn: 10 Current Score: 10 Keep rolling (Y, N or Q)?Y
Line 802: Line 802:
Darrell rolls a 3 and WINS!
Darrell rolls a 3 and WINS!
Hooray! Darrell won the game!
Hooray! Darrell won the game!
NIL</lang>
NIL</syntaxhighlight>


=={{header|D}}==
=={{header|D}}==
{{trans|C++}}
{{trans|C++}}
<lang d>import std.stdio, std.random;
<syntaxhighlight lang="d">import std.stdio, std.random;


enum nPlayers = 4, maxPoints = 100;
enum nPlayers = 4, maxPoints = 100;
Line 932: Line 932:
writeln("Player III (AL20): ", players[2].getCurrScore);
writeln("Player III (AL20): ", players[2].getCurrScore);
writeln("Player IV (AL20T): ", players[3].getCurrScore, "\n\n");
writeln("Player IV (AL20T): ", players[3].getCurrScore, "\n\n");
}</lang>
}</syntaxhighlight>


The output is similar to the C++ entry.
The output is similar to the C++ entry.
Line 938: Line 938:
=={{header|Erlang}}==
=={{header|Erlang}}==
Four players take turns starting. Their strategy is how long to wait before holding. They aim for 5, 10, 15 and 20 rolls. Player20 managed better than I thought it would.
Four players take turns starting. Their strategy is how long to wait before holding. They aim for 5, 10, 15 and 20 rolls. Player20 managed better than I thought it would.
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( pig_dice_player ).
-module( pig_dice_player ).


Line 993: Line 993:
Result
Result
end.
end.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,004: Line 1,004:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package pig
<syntaxhighlight lang="go">package pig


import (
import (
Line 1,194: Line 1,194:
}
}
pg.PrintStatus(gameOverSummary)
pg.PrintStatus(gameOverSummary)
}</lang>
}</syntaxhighlight>
Sample run, player one just tries to keep ahead, while player two always tries to take three rolls, no more.
Sample run, player one just tries to keep ahead, while player two always tries to take three rolls, no more.
<pre>
<pre>
Line 1,407: Line 1,407:
- player4 rolls 3/4 of the time, 1/4 he holds, but if he gets a score more than 75 he goes for the win
- player4 rolls 3/4 of the time, 1/4 he holds, but if he gets a score more than 75 he goes for the win


<syntaxhighlight lang="haskell">
<lang Haskell>
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE ViewPatterns #-}


Line 1,483: Line 1,483:
p4 = pInfo { name = "Stephen" }
p4 = pInfo { name = "Stephen" }
strat1 [p1, p2, p3, p4]
strat1 [p1, p2, p3, p4]
</syntaxhighlight>
</lang>


Example output:
Example output:
Line 1,542: Line 1,542:
To test the distribution by yourself (in parallel):
To test the distribution by yourself (in parallel):


<syntaxhighlight lang="haskell">
<lang Haskell>


-- add this to the top
-- add this to the top
Line 1,565: Line 1,565:
-- ghc FILENAME.hs -O2 -threaded -with-rtsopts="-N4" -o dice
-- ghc FILENAME.hs -O2 -threaded -with-rtsopts="-N4" -o dice


</syntaxhighlight>
</lang>


Distribution:
Distribution:
Line 1,585: Line 1,585:


If the expected value is positive, it's probably in the best interest of the player to take the roll. That said, a more sophisticated strategy might play cautiously when a player is sufficiently ahead of the other player(s).
If the expected value is positive, it's probably in the best interest of the player to take the roll. That said, a more sophisticated strategy might play cautiously when a player is sufficiently ahead of the other player(s).
<lang j>pigval=:4 :0
<syntaxhighlight lang="j">pigval=:4 :0
(+/%#)(-x),}.(1+i.6)<.100-y+x
(+/%#)(-x),}.(1+i.6)<.100-y+x
)</lang>
)</syntaxhighlight>
Examples:
Examples:
<lang j> 10 pigval 90
<syntaxhighlight lang="j"> 10 pigval 90
_1.66667</lang>
_1.66667</syntaxhighlight>
If we have 10 points from our current rolls and have 90 permanent points, rolling again is a bad idea.
If we have 10 points from our current rolls and have 90 permanent points, rolling again is a bad idea.
<lang j> 0 5 10 15 20 pigval"0/60 65 70 75 80 85 90 95 100
<syntaxhighlight lang="j"> 0 5 10 15 20 pigval"0/60 65 70 75 80 85 90 95 100
3.33333 3.33333 3.33333 3.33333 3.33333 3.33333 3.33333 3.16667 0
3.33333 3.33333 3.33333 3.33333 3.33333 3.33333 3.33333 3.16667 0
2.5 2.5 2.5 2.5 2.5 2.5 2.33333 _0.833333 _5
2.5 2.5 2.5 2.5 2.5 2.5 2.33333 _0.833333 _5
1.66667 1.66667 1.66667 1.66667 1.66667 1.5 _1.66667 _5.83333 _10
1.66667 1.66667 1.66667 1.66667 1.66667 1.5 _1.66667 _5.83333 _10
0.833333 0.833333 0.833333 0.833333 0.666667 _2.5 _6.66667 _10.8333 _15
0.833333 0.833333 0.833333 0.833333 0.666667 _2.5 _6.66667 _10.8333 _15
0 0 0 _0.166667 _3.33333 _7.5 _11.6667 _15.8333 _20</lang>
0 0 0 _0.166667 _3.33333 _7.5 _11.6667 _15.8333 _20</syntaxhighlight>
If we have 70 permanent points (or less) we should probably re-roll when our uncommitted rolls total to less than 20.
If we have 70 permanent points (or less) we should probably re-roll when our uncommitted rolls total to less than 20.
<lang j> (1+i.19) ([,:1+i:~) +/ 0 < pigval"0/~ 1+i.100
<syntaxhighlight lang="j"> (1+i.19) ([,:1+i:~) +/ 0 < pigval"0/~ 1+i.100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
98 97 96 95 93 92 91 90 89 87 86 85 84 82 81 80 78 77 75</lang>
98 97 96 95 93 92 91 90 89 87 86 85 84 82 81 80 78 77 75</syntaxhighlight>
This is a table of decision points. First row represents sum of our current uncommitted rolls. Second row represents the maximum permanent score where you should roll again with that number of uncommitted points, if we are using this estimation mechanism to choose our actions. Note that the first four columns here should have some obvious validity -- for example, if we have 96 permanent points and we have rolled 4 uncommitted points, we have won the game and we gain nothing from rerolling... Note also that this decision mechanism says we should never reroll if we have at least 20 uncommitted points.
This is a table of decision points. First row represents sum of our current uncommitted rolls. Second row represents the maximum permanent score where you should roll again with that number of uncommitted points, if we are using this estimation mechanism to choose our actions. Note that the first four columns here should have some obvious validity -- for example, if we have 96 permanent points and we have rolled 4 uncommitted points, we have won the game and we gain nothing from rerolling... Note also that this decision mechanism says we should never reroll if we have at least 20 uncommitted points.


Line 1,608: Line 1,608:


This is the main file, Pigdice.java
This is the main file, Pigdice.java
<lang Java>import java.util.Scanner;
<syntaxhighlight lang="java">import java.util.Scanner;


public class Pigdice {
public class Pigdice {
Line 1,747: Line 1,747:
}
}
}</lang>
}</syntaxhighlight>


This is the Player.java class file.
This is the Player.java class file.
<lang Java>public class Player {
<syntaxhighlight lang="java">public class Player {


private int points = 0;
private int points = 0;
Line 1,807: Line 1,807:
}
}


}</lang>
}</syntaxhighlight>


This is the Move.java class file.
This is the Move.java class file.
<lang Java>public enum Move { ROLL, HOLD }</lang>
<syntaxhighlight lang="java">public enum Move { ROLL, HOLD }</syntaxhighlight>


This is the Strategy.java class file.
This is the Strategy.java class file.
<lang Java>import java.util.Scanner;
<syntaxhighlight lang="java">import java.util.Scanner;


public interface Strategy {
public interface Strategy {
Line 1,941: Line 1,941:
};
};


}</lang>
}</syntaxhighlight>


And finally, this is the Dice.java class file. It's pretty self-explanatory.
And finally, this is the Dice.java class file. It's pretty self-explanatory.
<lang Java>import java.util.Random;
<syntaxhighlight lang="java">import java.util.Random;


public class Dice {
public class Dice {
Line 1,958: Line 1,958:
return rand.nextInt(sides) + 1;
return rand.nextInt(sides) + 1;
}
}
}</lang>
}</syntaxhighlight>


Here's a small sample output using only bots (even though it fully supports human players too). A full game simulation can obviously be MUCH longer.
Here's a small sample output using only bots (even though it fully supports human players too). A full game simulation can obviously be MUCH longer.
Line 2,011: Line 2,011:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>mutable struct Player
<syntaxhighlight lang="julia">mutable struct Player
score::Int
score::Int
ante::Int
ante::Int
Line 2,067: Line 2,067:


rungames(1000000)
rungames(1000000)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Player 3 rolls a 5.
Player 3 rolls a 5.
Line 2,208: Line 2,208:
Even for 20+20 games strategy 12, 20 wins 8, 10
Even for 20+20 games strategy 12, 20 wins 8, 10


<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module GamePig (games, strategy1, strategy2) {
Module GamePig (games, strategy1, strategy2) {
Print "Game of Pig"
Print "Game of Pig"
Line 2,304: Line 2,304:




</syntaxhighlight>
</lang>


=={{header|Nim}}==
=={{header|Nim}}==
Line 2,314: Line 2,314:
Except for some small differences in the formatting, the output is similar to that of D (or C++) implementation.
Except for some small differences in the formatting, the output is similar to that of D (or C++) implementation.


<lang Nim>import random, strformat
<syntaxhighlight lang="nim">import random, strformat


const MaxPoints = 100
const MaxPoints = 100
Line 2,419: Line 2,419:
for player in playerList:
for player in playerList:
let stratStr = &"({player.strategy}):"
let stratStr = &"({player.strategy}):"
echo &"Player {player.num} {stratStr:8} {player.currentScore:3}"</lang>
echo &"Player {player.num} {stratStr:8} {player.currentScore:3}"</syntaxhighlight>


{{out}}
{{out}}
Line 2,670: Line 2,670:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>my $GOAL = 100;
<syntaxhighlight lang="perl">my $GOAL = 100;


package Player;
package Player;
Line 2,735: Line 2,735:


print ' ----' x @players, "\n";
print ' ----' x @players, "\n";
printf "%5d", $_ for @wins; print "\n";</lang>
printf "%5d", $_ for @wins; print "\n";</syntaxhighlight>
Distribution of wins after 10000 games:
Distribution of wins after 10000 games:
{{out}}
{{out}}
Line 2,741: Line 2,741:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">maxScore</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">100</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">maxScore</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">100</span>
Line 2,820: Line 2,820:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"strategy %d: %d wins\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">wins</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"strategy %d: %d wins\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">wins</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,848: Line 2,848:
Notice how Pythons Counter class from the standard library is used to collate the winning statistics near the end of the program without much additional code.
Notice how Pythons Counter class from the standard library is used to collate the winning statistics near the end of the program without much additional code.


<lang python>#!/usr/bin/python3
<syntaxhighlight lang="python">#!/usr/bin/python3


'''
'''
Line 2,974: Line 2,974:
for i in range(maxgames))
for i in range(maxgames))
print(' Players(position) winning on left; occurrences on right:\n %s'
print(' Players(position) winning on left; occurrences on right:\n %s'
% ',\n '.join(str(w) for w in winners.most_common()))</lang>
% ',\n '.join(str(w) for w in winners.most_common()))</syntaxhighlight>
{{out}}
{{out}}
First is shown the game data for a single game with reduced maxscore then statistics on multiple games.
First is shown the game data for a single game with reduced maxscore then statistics on multiple games.
Line 3,022: Line 3,022:
Same as [[Pig_the_dice_game#Racket]], with three strategy makers, and
Same as [[Pig_the_dice_game#Racket]], with three strategy makers, and
simulation code for trying out strategies.
simulation code for trying out strategies.
<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket


(define (pig-the-dice #:print? [print? #t] . players)
(define (pig-the-dice #:print? [print? #t] . players)
Line 3,067: Line 3,067:
;; (n-runs 1000 (n-random 2) (n-random 3) (n-random 4))
;; (n-runs 1000 (n-random 2) (n-random 3) (n-random 4))
;; (n-runs 1000 (n-rounds 5) (n-points 24))
;; (n-runs 1000 (n-rounds 5) (n-points 24))
;; (n-runs 1000 (n-rounds 5) (n-random 2))</lang>
;; (n-runs 1000 (n-rounds 5) (n-random 2))</syntaxhighlight>
The following example run demonstrates the output from
The following example run demonstrates the output from
<lang Racket>(pig-the-dice #:print? #t (n-points 12) (n-rounds 4))</lang>
<syntaxhighlight lang="racket">(pig-the-dice #:print? #t (n-points 12) (n-rounds 4))</syntaxhighlight>
Where <code>n-points</code> is a strategy where the user continues to roll until the specified number of points is gained and <code>n-rounds</code> is a strategy where the users rolls the specified number of times each round.
Where <code>n-points</code> is a strategy where the user continues to roll until the specified number of points is gained and <code>n-rounds</code> is a strategy where the users rolls the specified number of times each round.
<pre>
<pre>
Line 3,303: Line 3,303:
player 4 randomly chooses to roll again but gets more consrvative as its score get closer to the goal.
player 4 randomly chooses to roll again but gets more consrvative as its score get closer to the goal.


<lang perl6>my $games = @*ARGS ?? (shift @*ARGS) !! 100;
<syntaxhighlight lang="raku" line>my $games = @*ARGS ?? (shift @*ARGS) !! 100;


constant DIE = 1 .. 6;
constant DIE = 1 .. 6;
Line 3,368: Line 3,368:


say "\nSCORES: for $games games";
say "\nSCORES: for $games games";
say join "\t", @wins;</lang>
say join "\t", @wins;</syntaxhighlight>


'''Sample output for 10000 games'''
'''Sample output for 10000 games'''
Line 3,402: Line 3,402:


The (somewhat aggressive) "quarter" strategy was chosen to give the advantage to a human (it was presumed that this dice game would be played with a CBLF).
The (somewhat aggressive) "quarter" strategy was chosen to give the advantage to a human (it was presumed that this dice game would be played with a CBLF).
<lang rexx>/*REXX program plays "pig the dice game" (any number of CBLFs and/or silicons or HALs).*/
<syntaxhighlight 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,*/
sw= linesize() - 1 /*get the width of the terminal screen,*/
parse arg hp cp win die _ . '(' names ")" /*obtain optional arguments from the CL*/
parse arg hp cp win die _ . '(' names ")" /*obtain optional arguments from the CL*/
Line 3,499: Line 3,499:
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
err: say; say; say center(' error! ', max(40, linesize() % 2), "*"); say
err: say; say; say center(' error! ', max(40, linesize() % 2), "*"); say
do j=1 for arg(); say arg(j); say; end; say; exit 13</lang>
do j=1 for arg(); say arg(j); say; end; say; exit 13</syntaxhighlight>
This REXX program makes use of &nbsp; '''LINESIZE''' &nbsp; BIF &nbsp; which returns the terminals width (linesize).
This REXX program makes use of &nbsp; '''LINESIZE''' &nbsp; BIF &nbsp; which returns the terminals width (linesize).
<br>Some REXXes don't have a &nbsp; '''LINESIZE''' &nbsp; BIF, so one is included here &nbsp; ──► &nbsp; [[LINESIZE.REX]].
<br>Some REXXes don't have a &nbsp; '''LINESIZE''' &nbsp; BIF, so one is included here &nbsp; ──► &nbsp; [[LINESIZE.REX]].
Line 3,632: Line 3,632:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>
<syntaxhighlight lang="ruby">
def player1(sum,sm)
def player1(sum,sm)
for i in 1..100
for i in 1..100
Line 3,673: Line 3,673:
sm=0
sm=0
player1(sum,sm)
player1(sum,sm)
return</lang>
return</syntaxhighlight>


=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Raku}}
{{trans|Raku}}
<lang ruby>var (games=100) = ARGV.map{.to_i}...
<syntaxhighlight lang="ruby">var (games=100) = ARGV.map{.to_i}...


define DIE = 1..6;
define DIE = 1..6;
Line 3,736: Line 3,736:


say "\nSCORES: for #{games} games";
say "\nSCORES: for #{games} games";
say wins.join("\t");</lang>
say wins.join("\t");</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,766: Line 3,766:
<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! -->
<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! -->
First the structure of the game (from [[Pig the dice game#Tcl|the parent page]]):
First the structure of the game (from [[Pig the dice game#Tcl|the parent page]]):
<lang tcl>package require TclOO
<syntaxhighlight lang="tcl">package require TclOO


oo::class create Player {
oo::class create Player {
Line 3,872: Line 3,872:
rotateList scores
rotateList scores
}
}
}</lang>
}</syntaxhighlight>
Then the classes that create the various implemented strategies:
Then the classes that create the various implemented strategies:
<lang tcl>oo::class create RoboPlayer {
<syntaxhighlight lang="tcl">oo::class create RoboPlayer {
superclass Player
superclass Player
variable me
variable me
Line 3,934: Line 3,934:
dict set scores $who $score
dict set scores $who $score
}
}
}</lang>
}</syntaxhighlight>
Demonstration, pitting the three of them against each other:
Demonstration, pitting the three of them against each other:
<lang tcl>pig [RandomPlayer new] [To20Player new] [Desperate new]</lang>
<syntaxhighlight lang="tcl">pig [RandomPlayer new] [To20Player new] [Desperate new]</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,037: Line 4,037:
=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Sidef}}
{{trans|Sidef}}
<lang ecmascript>import "random" for Random
<syntaxhighlight lang="ecmascript">import "random" for Random
import "os" for Process
import "os" for Process


Line 4,112: Line 4,112:


System.print("\nSCORES: for %(games) games")
System.print("\nSCORES: for %(games) games")
System.print(wins.join("\t"))</lang>
System.print(wins.join("\t"))</syntaxhighlight>


{{out}}
{{out}}