The   game of Pig   is a multiplayer game played with a single six-sided die.   The
object of the game is to reach   100   points or more.  
Play is taken in turns.   On each person's turn that person has the option of either:

 Rolling the dice:   where a roll of two to six is added to their score for that turn and the player's turn continues as the player is given the same choice again;   or a roll of   1   loses the player's total points   for that turn   and their turn finishes with play passing to the next player.
 Holding:   the player's score for that round is added to their total and becomes safe from the effects of throwing a   1   (one).   The player's turn finishes with play passing to the next player.


Task

Create a program to score for, and simulate dice throws for, a two-person game.



Related task

   Pig the dice game/Player


 
﻿﻿class Pig {
  function : Main(args : String[]) ~ Nil {
    player_count := 2;
    max_score := 100;
    safe_score := Int->New[player_count];
    player := 0; score := 0;
 
    while(true) {
      safe := safe_score[player];
      " Player {$player}: ({$safe}, {$score}) Rolling? (y/n) "->PrintLine();
      rolling := IO.Console->ReadString();
      if(safe_score[player] + score < max_score & (rolling->Equals("y") | rolling->Equals("yes"))) {
        rolled := ((Float->Random() * 100.0)->As(Int) % 6) + 1;
        " Rolled {$rolled}"->PrintLine();
        if(rolled = 1) {
          safe := safe_score[player];
          "  Bust! you lose {$score} but still keep your previous {$safe}\n"->PrintLine();
          score := 0;
          player := (player + 1) % player_count;
        }
        else {
          score += rolled;
        };
      }
      else {
        safe_score[player] += score;
        if(safe_score[player] >= max_score) {
          break;
        };
        safe := safe_score[player];
        " Sticking with {$safe}\n"->PrintLine();
        score := 0;
        player := (player + 1) % player_count;
      };
    };
    safe := safe_score[player];
    "\n\nPlayer {$player} wins with a score of {$safe}"->PrintLine();
  }
}
 