How to resolve the algorithm 100 prisoners step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 100 prisoners step by step in the Ada programming language

Table of Contents

Problem Statement

Show and compare the computed probabilities of success for the two strategies, here, on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm 100 prisoners step by step in the Ada programming language

Source code in the ada programming language

package Prisoners is

   type Win_Percentage is digits 2 range 0.0 .. 100.0;
   type Drawers is array (1 .. 100) of Positive;

   function Play_Game
     (Repetitions : in Positive;
      Strategy    :    not null access function
        (Cupboard     : in Drawers; Max_Prisoners : Integer;
         Max_Attempts :    Integer; Prisoner_Number : Integer) return Boolean)
      return Win_Percentage;
   -- Play the game with a specified number of repetitions, the chosen strategy
   -- is passed to this function

   function Optimal_Strategy
     (Cupboard : in Drawers; Max_Prisoners : Integer; Max_Attempts : Integer;
      Prisoner_Number :    Integer) return Boolean;

   function Random_Strategy
     (Cupboard : in Drawers; Max_Prisoners : Integer; Max_Attempts : Integer;
      Prisoner_Number :    Integer) return Boolean;

end Prisoners;


pragma Ada_2012;
with Ada.Numerics.Discrete_Random;
with Ada.Text_IO; use Ada.Text_IO;

package body Prisoners is

   subtype Drawer_Range is Positive range 1 .. 100;
   package Random_Drawer is new Ada.Numerics.Discrete_Random (Drawer_Range);
   use Random_Drawer;
   
   -- Helper procedures to initialise and shuffle the drawers
   
   procedure Swap (A, B : Positive; Cupboard : in out Drawers) is
      Temp : Positive;
   begin
      Temp         := Cupboard (B);
      Cupboard (B) := Cupboard (A);
      Cupboard (A) := Temp;
   end Swap;

   procedure Shuffle (Cupboard : in out Drawers) is
      G : Generator;
   begin
      Reset (G);
      for I in Cupboard'Range loop
         Swap (I, Random (G), Cupboard);
      end loop;
   end Shuffle;

   procedure Initialise_Drawers (Cupboard : in out Drawers) is
   begin
      for I in Cupboard'Range loop
         Cupboard (I) := I;
      end loop;
      Shuffle (Cupboard);
   end Initialise_Drawers;
   
   -- The two strategies for playing the game

   function Optimal_Strategy
     (Cupboard : in Drawers; Max_Prisoners : Integer; Max_Attempts : Integer;
      Prisoner_Number :    Integer) return Boolean
   is
      Current_Card : Positive;
   begin
      Current_Card := Cupboard (Prisoner_Number);
      if Current_Card = Prisoner_Number then
         return True;
      else
         for I in Integer range 1 .. Max_Attempts loop
            Current_Card := Cupboard (Current_Card);
            if Current_Card = Prisoner_Number then
               return True;
            end if;
         end loop;
      end if;
      return False;
   end Optimal_Strategy;

   function Random_Strategy
     (Cupboard : in Drawers; Max_Prisoners : Integer; Max_Attempts : Integer;
      Prisoner_Number :    Integer) return Boolean
   is
      Current_Card : Positive;
      G            : Generator;
   begin
      Reset (G);
      Current_Card := Cupboard (Prisoner_Number);
      if Current_Card = Prisoner_Number then
         return True;
      else
         for I in Integer range 1 .. Max_Attempts loop
            Current_Card := Cupboard (Random (G));
            if Current_Card = Prisoner_Number then
               return True;
            end if;
         end loop;
      end if;
      return False;
   end Random_Strategy;

   function Prisoners_Attempts
     (Cupboard : in Drawers; Max_Prisoners : Integer; Max_Attempts : Integer;
      Strategy :    not null access function
        (Cupboard     : in Drawers; Max_Prisoners : Integer;
         Max_Attempts :    Integer; Prisoner_Number : Integer) return Boolean)
      return Boolean
   is
   begin
      for Prisoner_Number in Integer range 1 .. Max_Prisoners loop
         if not Strategy
             (Cupboard, Max_Prisoners, Max_Attempts, Prisoner_Number)
         then
            return False;
         end if;
      end loop;
      return True;
   end Prisoners_Attempts;
   
   -- The function to play the game itself

   function Play_Game
     (Repetitions : in Positive;
      Strategy    :    not null access function
        (Cupboard     : in Drawers; Max_Prisoners : Integer;
         Max_Attempts :    Integer; Prisoner_Number : Integer) return Boolean)
      return Win_Percentage
   is
      Cupboard            : Drawers;
      Win, Game_Count     : Natural          := 0;
      Number_Of_Prisoners : constant Integer := 100;
      Max_Attempts        : constant Integer := 50;
   begin
      loop
         Initialise_Drawers (Cupboard);
         if Prisoners_Attempts
             (Cupboard     => Cupboard, Max_Prisoners => Number_Of_Prisoners,
              Max_Attempts => Max_Attempts, Strategy => Strategy)
         then
            Win := Win + 1;
         end if;
         Game_Count := Game_Count + 1;
         exit when Game_Count = Repetitions;
      end loop;
      return Win_Percentage ((Float (Win) / Float (Repetitions)) * 100.0);
   end Play_Game;

end Prisoners;


with Prisoners;   use Prisoners;
with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   Wins : Win_Percentage;
   package Win_Percentage_IO is new Float_IO (Win_Percentage);
begin
   Wins := Play_Game (100_000, Optimal_Strategy'Access);
   Put ("Optimal Strategy = ");
   Win_Percentage_IO.Put (Wins, 2, 2, 0);
   Put ("%");
   New_Line;
   Wins := Play_Game (100_000, Random_Strategy'Access);
   Put ("Random Strategy = ");
   Win_Percentage_IO.Put (Wins, 2, 2, 0);
   Put ("%");
end Main;


  

You may also check:How to resolve the algorithm Fibonacci word/fractal step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm String matching step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the V programming language
You may also check:How to resolve the algorithm Show the epoch step by step in the Tcl programming language
You may also check:How to resolve the algorithm Calendar step by step in the AWK programming language