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


 
Module GamePig {
      Print "Game of Pig"
      dice=-1
      res$=""
      Player1points=0
      Player1sum=0
      Player2points=0
      Player2sum=0
      HaveWin=False
      score()
      \\ for simulation, feed the keyboard buffer with R and H
      simulate$=String$("R", 500)
      For i=1 to 100
            Insert Random(1,Len(simulate$)) simulate$="H"
      Next i
      Keyboard simulate$
      \\ end simulation
      while res$<>"Q" {
            Print "Player 1 turn"
            PlayerTurn(&Player1points, &player1sum)
            if res$="Q" then exit
            Player1sum+=Player1points
            Score()
            Print "Player 2 turn"
            PlayerTurn(&Player2points,&player2sum)
            if res$="Q" then exit
            Player2sum+=Player2points
            Score()
      }
      If HaveWin then {
            Score()
            If Player1Sum>Player2sum then {
                  Print "Player 1 Win"
            } Else Print "Player 2 Win"
      }
 
      Sub Rolling()
            dice=random(1,6)
            Print "dice=";dice
      End Sub
      Sub PlayOrQuit()
            Print "R -Roling Q -Quit"
            Repeat {
                  res$=Ucase$(Key$)
            } Until Instr("RQ", res$)>0
      End Sub
      Sub PlayAgain()
            Print "R -Roling H -Hold Q -Quit"
            Repeat {
                  res$=Ucase$(Key$)
            } Until Instr("RHQ", res$)>0
      End Sub
      Sub PlayerTurn(&playerpoints, &sum)
            PlayOrQuit()
            If res$="Q" then Exit Sub
            playerpoints=0
            Rolling()
            While dice<>1 and res$="R" {
                  playerpoints+=dice
                  if dice>1 and playerpoints+sum>100 then {
                        sum+=playerpoints
                        HaveWin=True
                        res$="Q"
                  } Else {
                        PlayAgain()
                        if res$="R" then Rolling()
                  }
            }
            if dice=1 then playerpoints=0
      End Sub
      Sub Score()
            Print "Player1 points="; Player1sum
            Print "Player2 points="; Player2sum
      End Sub     
}
GamePig
 