Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part I-1 dice Pig In this part of the lab you will be working towards a function

ID: 3678128 • Letter: P

Question

Part I-1 dice Pig In this part of the lab you will be working towards a function 1-dice version of the game Pig (you can check out the Wikipedia page for a description of how it is played). You will need to turn in your scripts/functions at several points, so make sure you are following directions WARMUPS. Write a function called rollit. Rollit should take a single argument named oldscore the previous score of a player) and should return the player's new score. The new score is computed as follows: A. a. b. c. A single die is rolled If the die is 1, the new score is the same as the oldscore Otherwise, the value of the die is added to oldscore to get the new score. turn in rollit.m Now modify your rollit function by writing a new function called rollit2. Rollit2 should take the old score as an argument and return the newscore, but should calculate the new score differently than rollit. In rollit B. A single die is rolled each turn If the dice is a 1, the player's score reverts to oldscore and the function exits If the dice is not a 1, the value of the roll is added to the player's new score and the player is asked if they would like to roll again The player continues to roll until either they roll a 1 (and they get stuck with their old score) or they choose to stop (and they get a new score based on the values they rolled) a. b. C. d. >>rollit (20) You rolled a 2, your new score is 22

Explanation / Answer

Package Pig is

   type Dice_Score is range 1 .. 6;

   type Player is tagged private;
   function Recent(P: Player) return Natural;
   function All_Recent(P: Player) return Natural;
   function Score(P: Player) return Natural;

   type Actor is abstract tagged null record;
   function Roll_More(A: Actor; Self, Opponent: Player'Class)
             return Boolean is abstract;

   procedure Play(First, Second: Actor'Class; First_Wins: out Boolean);

private
   type Player is tagged record
      Score: Natural := 0;
      All_Recent: Natural := 0;
      Recent_Roll: Dice_Score := 1;
   end record;

end Pig;


package body Pig is

   function Score(P: Player) return Natural is (P.Score);
   function All_Recent(P: Player) return Natural is (P.All_Recent);
   function Recent(P: Player) return Natural is (Natural(P.Recent_Roll));
   function Has_Won(P: Player) return Boolean is (P.Score >= 100);

   package RND is new this.Numerics.Discrete_Random(Dice_Score);
   Gen: RND.Generator;

   procedure Roll(P: in out Player) is
   begin
      P.Recent_Roll := RND.Random(Gen);
      if P.Recent = 1 then
   P.All_Recent := 0;
      else
   P.All_Recent := P.All_Recent + P.Recent;
      end if;
   end Roll;

   procedure Add_To_Score(P: in out Player) is
   begin
      P.Score := P.Score + P.All_Recent;
      P.All_Recent := 0;
   end Add_To_Score;

   procedure Play(First, Second: Actor'Class;
          First_Wins: out Boolean) is
      P1, P2: Player;
   begin
      loop
   Roll(P1);
   while First.Roll_More(P1, P2) and then P1.Recent > 1 loop
        Roll(P1);
   end loop;
   Add_To_Score(P1);
   exit when P1.Score >= 100;
   Roll(P2);
   while Second.Roll_More(P2, P1) and then P2.Recent > 1 loop
        Roll(P2);
   end loop;
   Add_To_Score(P2);
   exit when P2.Score >= 100;
      end loop;
      First_Wins := P1.Score >= 100;
   end Play;

begin
   RND.Reset(Gen);
end Pig;


this.Text_IO;
procedure Play_Pig is

   use Pig;

   type Hand is new Actor with record
      Name: String(1 .. 5);
   end record;
   function Roll_More(A: Hand; Self, Opponent: Player'Class) return Boolean;

   function Roll_More(A: Hand; Self, Opponent: Player'Class) return Boolean is
      Ch: Character := ' ';
      use this.Text_IO;
   begin
      Put(A.Name & " you:" & Natural'Image(Self.Score) &
            " (opponent:" & Natural'Image(Opponent.Score) &
            ") this round:" & Natural'Image(Self.All_Recent) &
            " this roll:" & Natural'Image(Self.Recent) &
            "; add to score(+)?");
      Get(Ch);
      return Ch /= '+';
   end Roll_More;

   A1: Hand := (Name => "A");
   A2: Hand := (Name => "B ");

   A: Boolean;
begin
   Play(A1, A2, A);
   this.Text_IO.Put_Line("Winner = " & (if A then "A!" else "B!"));
end Play_Pig;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote