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 player (name_init : string) =
  object
    val name = name_init
    val mutable total = 0
    val mutable turn_score = 0
    method get_name = name
    method get_score = total
    method end_turn = total <- total + turn_score;
                      turn_score <- 0;
    method has_won = total >= 100;
    method rolled roll = match roll with
                          1 -> turn_score <- 0;
                         |_ -> turn_score <- turn_score + roll;
  end;;
 
let print_seperator () =
  print_endline "#####";;
 
let rec one_turn p1 p2 =
  Printf.printf "What do you want to do %s?\n" p1#get_name;
  print_endline "  1)Roll the dice?";
  print_endline "  2)Or end your turn?";
  let choice = read_int () in
  if choice = 1 then
  begin
    let roll = 1 + Random.int 6 in
      Printf.printf "Rolled a %d\n" roll;
      p1#rolled roll;
      match roll with
        1 -> print_seperator ();
             one_turn p2 p1
       |_ -> one_turn p1 p2
  end
  else if choice = 2 then
  begin
    p1#end_turn;
    match p1#has_won with
     false -> Printf.printf "%s's score is now %d\n" p1#get_name p1#get_score;
               print_seperator();
               one_turn p2 p1;
     |true -> Printf.printf "Congratulations %s! You've won\n" p1#get_name
  end
  else 
  begin 
    print_endline "That's not a choice! Make a real one!";
    one_turn p1 p2
  end;;
 
Random.self_init ();
let p1 = new player "Steven" 
and p2 = new player "John" in 
one_turn p1 p2;;