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


program Pig;
 
const
	WinningScore = 100;
 
type
	DieRoll = 1..6;
	Score = integer;
	Player = record
		Name: string;
		Points: score;
		Victory: Boolean
	end;
 
{ Assume a 2-player game. }
var Player1, Player2: Player;
 
function RollTheDie: DieRoll;
  { Return a random number 1 thru 6. }
	begin
		RollTheDie := random(6) + 1
	end;
 
procedure TakeTurn (var P: Player);
  { Play a round of Pig. }
	var
		Answer: char;
		Roll: DieRoll;
		NewPoints: Score;
		KeepPlaying: Boolean;
	begin
		NewPoints := 0;
		writeln ;
		writeln('It''s your turn, ', P.Name, '!');
		writeln('So far, you have ', P.Points, ' points in all.');
		writeln ;
		{ Keep playing until the user rolls a 1 or chooses not to roll. }
		write('Do you want to roll the die (y/n)? ');
		readln(Answer);
		KeepPlaying := upcase(Answer) = 'Y';
		while KeepPlaying do
		 begin
			Roll := RollTheDie;
			if Roll = 1 then
			 begin
				NewPoints := 0;
				KeepPlaying := false;
				writeln('Oh no! You rolled a 1! No new points after all.')
			 end
			else
			 begin
				NewPoints := NewPoints + Roll;
				write('You rolled a ', Roll:1, '. ');
				writeln('That makes ', NewPoints, ' new points so far.');
				writeln ;
				write('Roll again (y/n)? ');
				readln(Answer);
				KeepPlaying := upcase(Answer) = 'Y'
			 end
		 end;
		{ Update the player's score and check for a winner. }
		writeln ;
		if NewPoints = 0 then
			writeln(P.Name, ' still has ', P.Points, ' points.')
		else
		 begin
			P.Points := P.Points + NewPoints;
			writeln(P.Name, ' now has ', P.Points, ' points total.');
			P.Victory := P.Points >= WinningScore
		 end
	end;
 
procedure Congratulate(Winner: Player);
	begin
		writeln ;
		write('Congratulations, ', Winner.Name, '! ');
		writeln('You won with ', Winner.Points, ' points.');
		writeln
	end;
 
begin
	{ Greet the players and initialize their data. }
	writeln('Let''s play Pig!');
 
	writeln ;
	write('Player 1, what is your name? ');
	readln(Player1.Name);
	Player1.Points := 0;
	Player1.Victory := false;
 
	writeln ;
	write('Player 2, what is your name? ');
	readln(Player2.Name);
	Player2.Points := 0;
	Player2.Victory := false;
 
	{ Take turns until there is a winner. }
	randomize;
	repeat
		TakeTurn(Player1);
		if not Player1.Victory then TakeTurn(Player2)
	until Player1.Victory or Player2.Victory;
 
	{ Announce the winner. }
	if Player1.Victory then
		Congratulate(Player1)
	else
		Congratulate(Player2)
end.