Jump to content

Pig the dice game: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 825:
What do you want to do (1)roll or (2)hold:
</pre>
 
=={{header|C sharp}}==
<lang csharp>
using System;
using System.IO;
 
namespace Pig {
 
class Roll {
public int TotalScore{get;set;}
public int RollScore{get;set;}
public bool Continue{get;set;}
}
 
class Player {
public String Name{get;set;}
public int Score {get;set;}
Random rand;
 
public Player() {
Score = 0;
rand = new Random();
}
 
public Roll Roll(int LastScore){
Roll roll = new Roll();
roll.RollScore = rand.Next(6) + 1;
 
if(roll.RollScore == 1){
roll.TotalScore = 0;
roll.Continue = false;
return roll;
}
 
roll.TotalScore = LastScore + roll.RollScore;
roll.Continue = true;
return roll;
}
 
public void FinalizeTurn(Roll roll){
Score = Score + roll.TotalScore;
}
}
 
public class Game {
public static void Main(String[] argv){
String input = null;
Player[] players = new Player[2];
 
// Game loop
while(true){
Console.Write("Greetings! Would you like to play a game (y/n)?");
while(input == null){
input = Console.ReadLine();
if(input.ToLowerInvariant() == "y"){
players[0] = new Player();
players[1] = new Player();
Console.Write("Player One, what's your name?");
input = Console.ReadLine();
players[0].Name = input;
Console.Write("Player Two, what's your name?");
input = Console.ReadLine();
players[1].Name = input;
Console.WriteLine(players[0].Name + " and " + players[1].Name + ", prepare to do battle!");
} else if (input.ToLowerInvariant() == "n"){
goto Goodbye; /* Not considered harmful */
} else {
input = null;
Console.Write("I'm sorry, I don't understand. Play a game (y/n)?");
}
}
 
// Play the game
int currentPlayer = 0;
Roll roll = null;
bool runTurn = true;
while(runTurn){
Player p = players[currentPlayer];
roll = p.Roll( (roll !=null) ? roll.TotalScore : 0 );
if(roll.Continue){
if(roll.TotalScore + p.Score > 99){
Console.WriteLine("Congratulations, " + p.Name + "! You rolled a " + roll.RollScore + " for a final score of " + (roll.TotalScore + p.Score) + "!");
runTurn = false;
} else {
Console.Write(p.Name + ": Roll " + roll.RollScore + "/Turn " + roll.TotalScore + "/Total " + (roll.TotalScore + p.Score) + ". Roll again (y/n)?");
input = Console.ReadLine();
if(input.ToLowerInvariant() == "y"){
// Do nothing
} else if (input.ToLowerInvariant() == "n"){
p.FinalizeTurn(roll);
currentPlayer = Math.Abs(currentPlayer - 1);
Console.WriteLine();
Console.WriteLine(players[0].Name + ": " + players[0].Score + " " + players[1].Name + ": " + players[1].Score);
Console.WriteLine(players[currentPlayer].Name + ", your turn begins.");
roll = null;
} else {
input = null;
Console.Write("I'm sorry, I don't understand. Play a game (y/n)?");
}
}
} else {
Console.WriteLine(p.Name + @", you rolled a 1 and lost your points for this turn.
Your current score: " + p.Score);
Console.WriteLine();
Console.WriteLine(players[0].Name + ": " + players[0].Score + " " + players[1].Name + ": " + players[1].Score);
currentPlayer = Math.Abs(currentPlayer - 1);
}
}
 
 
input = null;
}
Goodbye:
Console.WriteLine("Thanks for playing, and remember: the house ALWAYS wins!");
System.Environment.Exit(0);
}
}
}
</lang>
 
=={{header|C++}}==
Line 981 ⟶ 1,100:
What do you want to do (H)old or (R)oll?
</pre>
 
=={{header|C sharp}}==
<lang csharp>
using System;
using System.IO;
 
namespace Pig {
 
class Roll {
public int TotalScore{get;set;}
public int RollScore{get;set;}
public bool Continue{get;set;}
}
 
class Player {
public String Name{get;set;}
public int Score {get;set;}
Random rand;
 
public Player() {
Score = 0;
rand = new Random();
}
 
public Roll Roll(int LastScore){
Roll roll = new Roll();
roll.RollScore = rand.Next(6) + 1;
 
if(roll.RollScore == 1){
roll.TotalScore = 0;
roll.Continue = false;
return roll;
}
 
roll.TotalScore = LastScore + roll.RollScore;
roll.Continue = true;
return roll;
}
 
public void FinalizeTurn(Roll roll){
Score = Score + roll.TotalScore;
}
}
 
public class Game {
public static void Main(String[] argv){
String input = null;
Player[] players = new Player[2];
 
// Game loop
while(true){
Console.Write("Greetings! Would you like to play a game (y/n)?");
while(input == null){
input = Console.ReadLine();
if(input.ToLowerInvariant() == "y"){
players[0] = new Player();
players[1] = new Player();
Console.Write("Player One, what's your name?");
input = Console.ReadLine();
players[0].Name = input;
Console.Write("Player Two, what's your name?");
input = Console.ReadLine();
players[1].Name = input;
Console.WriteLine(players[0].Name + " and " + players[1].Name + ", prepare to do battle!");
} else if (input.ToLowerInvariant() == "n"){
goto Goodbye; /* Not considered harmful */
} else {
input = null;
Console.Write("I'm sorry, I don't understand. Play a game (y/n)?");
}
}
 
// Play the game
int currentPlayer = 0;
Roll roll = null;
bool runTurn = true;
while(runTurn){
Player p = players[currentPlayer];
roll = p.Roll( (roll !=null) ? roll.TotalScore : 0 );
if(roll.Continue){
if(roll.TotalScore + p.Score > 99){
Console.WriteLine("Congratulations, " + p.Name + "! You rolled a " + roll.RollScore + " for a final score of " + (roll.TotalScore + p.Score) + "!");
runTurn = false;
} else {
Console.Write(p.Name + ": Roll " + roll.RollScore + "/Turn " + roll.TotalScore + "/Total " + (roll.TotalScore + p.Score) + ". Roll again (y/n)?");
input = Console.ReadLine();
if(input.ToLowerInvariant() == "y"){
// Do nothing
} else if (input.ToLowerInvariant() == "n"){
p.FinalizeTurn(roll);
currentPlayer = Math.Abs(currentPlayer - 1);
Console.WriteLine();
Console.WriteLine(players[0].Name + ": " + players[0].Score + " " + players[1].Name + ": " + players[1].Score);
Console.WriteLine(players[currentPlayer].Name + ", your turn begins.");
roll = null;
} else {
input = null;
Console.Write("I'm sorry, I don't understand. Play a game (y/n)?");
}
}
} else {
Console.WriteLine(p.Name + @", you rolled a 1 and lost your points for this turn.
Your current score: " + p.Score);
Console.WriteLine();
Console.WriteLine(players[0].Name + ": " + players[0].Score + " " + players[1].Name + ": " + players[1].Score);
currentPlayer = Math.Abs(currentPlayer - 1);
}
}
 
 
input = null;
}
Goodbye:
Console.WriteLine("Thanks for playing, and remember: the house ALWAYS wins!");
System.Environment.Exit(0);
}
}
}
</lang>
 
=={{header|Clojure}}==
Line 1,277:
 
Player 0 wins with a score of 101</pre>
 
=={{header|Eiffel}}==
 
Line 2,425 ⟶ 2,426:
end
</lang>
 
=={{header|M2000 Interpreter}}==
<lang M2000 Interpreter>
Line 2,971 ⟶ 2,973:
__END__
</lang>
=={{header|Perl 6}}==
{{works with|rakudo|2015-09-30}}
<lang perl6>constant DIE = 1..6;
sub MAIN (Int :$players = 2, Int :$goal = 100) {
my @safe = 0 xx $players;
for |^$players xx * -> $player {
say "\nOK, player #$player is up now.";
my $safe = @safe[$player];
my $ante = 0;
until $safe + $ante >= $goal or
prompt("#$player, you have $safe + $ante = {$safe+$ante}. Roll? [Yn] ") ~~ /:i ^n/
{
given DIE.roll {
say " You rolled a $_.";
when 1 {
say " Bust! You lose $ante but keep your previous $safe.";
$ante = 0;
last;
}
when 2..* {
$ante += $_;
}
}
}
$safe += $ante;
if $safe >= $goal {
say "\nPlayer #$player wins with a score of $safe!";
last;
}
@safe[$player] = $safe;
say " Sticking with $safe." if $ante;
}
}</lang>
The game defaults to the specified task, but we'll play a shorter game with three players for our example:
{{out}}
<pre>> pig help
Usage:
pig [--players=<Int>] [--goal=<Int>]
> pig --players=3 --goal=20
 
OK, player #0 is up now.
#0, you have 0 + 0 = 0. Roll? [Yn]
You rolled a 6.
#0, you have 0 + 6 = 6. Roll? [Yn]
You rolled a 6.
#0, you have 0 + 12 = 12. Roll? [Yn] n
Sticking with 12.
 
OK, player #1 is up now.
#1, you have 0 + 0 = 0. Roll? [Yn]
You rolled a 4.
#1, you have 0 + 4 = 4. Roll? [Yn]
You rolled a 6.
#1, you have 0 + 10 = 10. Roll? [Yn]
You rolled a 6.
#1, you have 0 + 16 = 16. Roll? [Yn] n
Sticking with 16.
 
OK, player #2 is up now.
#2, you have 0 + 0 = 0. Roll? [Yn]
You rolled a 5.
#2, you have 0 + 5 = 5. Roll? [Yn]
You rolled a 1.
Bust! You lose 5 but keep your previous 0.
 
OK, player #0 is up now.
#0, you have 12 + 0 = 12. Roll? [Yn]
You rolled a 1.
Bust! You lose 0 but keep your previous 12.
 
OK, player #1 is up now.
#1, you have 16 + 0 = 16. Roll? [Yn] n
 
OK, player #2 is up now.
#2, you have 0 + 0 = 0. Roll? [Yn]
You rolled a 6.
#2, you have 0 + 6 = 6. Roll? [Yn]
You rolled a 6.
#2, you have 0 + 12 = 12. Roll? [Yn]
You rolled a 4.
#2, you have 0 + 16 = 16. Roll? [Yn]
You rolled a 6.
 
Player #2 wins with a score of 22!</pre>
 
=={{header|Phix}}==
Line 3,284 ⟶ 3,201:
(pig-the-dice #:print? #t human human)
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015-09-30}}
<lang perl6>constant DIE = 1..6;
sub MAIN (Int :$players = 2, Int :$goal = 100) {
my @safe = 0 xx $players;
for |^$players xx * -> $player {
say "\nOK, player #$player is up now.";
my $safe = @safe[$player];
my $ante = 0;
until $safe + $ante >= $goal or
prompt("#$player, you have $safe + $ante = {$safe+$ante}. Roll? [Yn] ") ~~ /:i ^n/
{
given DIE.roll {
say " You rolled a $_.";
when 1 {
say " Bust! You lose $ante but keep your previous $safe.";
$ante = 0;
last;
}
when 2..* {
$ante += $_;
}
}
}
$safe += $ante;
if $safe >= $goal {
say "\nPlayer #$player wins with a score of $safe!";
last;
}
@safe[$player] = $safe;
say " Sticking with $safe." if $ante;
}
}</lang>
The game defaults to the specified task, but we'll play a shorter game with three players for our example:
{{out}}
<pre>> pig help
Usage:
pig [--players=<Int>] [--goal=<Int>]
> pig --players=3 --goal=20
 
OK, player #0 is up now.
#0, you have 0 + 0 = 0. Roll? [Yn]
You rolled a 6.
#0, you have 0 + 6 = 6. Roll? [Yn]
You rolled a 6.
#0, you have 0 + 12 = 12. Roll? [Yn] n
Sticking with 12.
 
OK, player #1 is up now.
#1, you have 0 + 0 = 0. Roll? [Yn]
You rolled a 4.
#1, you have 0 + 4 = 4. Roll? [Yn]
You rolled a 6.
#1, you have 0 + 10 = 10. Roll? [Yn]
You rolled a 6.
#1, you have 0 + 16 = 16. Roll? [Yn] n
Sticking with 16.
 
OK, player #2 is up now.
#2, you have 0 + 0 = 0. Roll? [Yn]
You rolled a 5.
#2, you have 0 + 5 = 5. Roll? [Yn]
You rolled a 1.
Bust! You lose 5 but keep your previous 0.
 
OK, player #0 is up now.
#0, you have 12 + 0 = 12. Roll? [Yn]
You rolled a 1.
Bust! You lose 0 but keep your previous 12.
 
OK, player #1 is up now.
#1, you have 16 + 0 = 16. Roll? [Yn] n
 
OK, player #2 is up now.
#2, you have 0 + 0 = 0. Roll? [Yn]
You rolled a 6.
#2, you have 0 + 6 = 6. Roll? [Yn]
You rolled a 6.
#2, you have 0 + 12 = 12. Roll? [Yn]
You rolled a 4.
#2, you have 0 + 16 = 16. Roll? [Yn]
You rolled a 6.
 
Player #2 wins with a score of 22!</pre>
 
=={{header|REXX}}==
10,333

edits

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