with Text_IO; with Ada.Text_IO; with Ada.Integer_Text_IO; with Ada.Strings.Fixed; with Ada.Strings.Maps.Constants; with Ada.Strings.Unbounded; with Ada.Numerics.Discrete_Random; with GNAT.IO; use GNAT.IO; use Ada.Strings.Unbounded; procedure Guessanumberada is package Boolean_IO is new Text_IO.Enumeration_IO(Boolean); use Boolean_IO; function IsAnInt(Input: in String) return Boolean is Val: Integer; begin Val := Integer'Value(Input); return TRUE; exception when others => return FALSE; end IsAnInt; function GetIntInput(Prompt: in String) return Integer is GotNum: Boolean := FALSE; Val: Integer; begin <> Ada.Text_IO.Put(Prompt & ": "); Ada.Integer_Text_IO.Get(Val); Text_IO.Put_Line(""); if Val < 0 then Text_IO.Put_Line("No negative values allowed."); goto gii_Loop; else GotNum := TRUE; end if; if GotNum = FALSE then Text_IO.Put_Line("Please eneter an integer."); goto gii_Loop; end if; return Val; end GetIntInput; function AskToPlayAgain return Boolean is Ans: String(1..3); Len: Integer; Answer: Boolean; GotAns: Boolean := FALSE; begin <> Ada.Text_IO.Skip_Line; Ada.Text_IO.Put_Line("Would you like to play again? (""YES"" or ""NO"")"); Ada.Text_IO.Get_Line(Ans, Len); Ans := Ada.Strings.Fixed.Translate(Ans, Ada.Strings.Maps.Constants.Upper_Case_Map); if Ans(1..1) = "Y" or Ans = "YES" then Answer := TRUE; GotAns := TRUE; end if; if Ans(1..1) = "N" or Ans(1..2) = "NO" then Answer := FALSE; GotAns := TRUE; end if; if GotAns = FALSE then Ada.Text_IO.Put_Line("Please respond with with ""YES"" or ""NO"""); goto atpa_Loop; end if; return Answer; end AskToPlayAgain; function GenerateRandomNumber(Min: Integer; Max: Integer; Show: Boolean) return Integer is type rndRange is new Integer range Min..Max; package Rand_Int is new Ada.Numerics.Discrete_Random(rndRange); use Rand_Int; Gen: Generator; Num: rndRange; begin reset(Gen); Num := Random(Gen); if Show = TRUE then Put_Line(rndRange'Image(Num)); end if; return Integer'Value(rndRange'Image(Num)); end GenerateRandomNumber; Running : Boolean := TRUE; GotMaxNumber : Boolean := FALSE; GameOver : Boolean := FALSE; MaxNumber : Integer; Answer : Integer; Tries : Integer; Guess : Integer; begin Put_Line("*** GUESS A NUMBER : Ada ***"); <> if Running = TRUE then if GotMaxNumber = FALSE then MaxNumber := GetIntInput("Please enter the maximum number you wish to use"); if MaxNumber > 0 then GotMaxNumber := TRUE; Tries := 0; GameOver := FALSE; Answer := GenerateRandomNumber(1,MaxNumber,FALSE); Put_Line("Ok, I am thinking of a number between 1 and" & Integer'Image(MaxNumber)); else Put_Line("The maximum number should be greater than 0"); end if; end if; Guess := GetIntInput("Guess a number between 1 and" & Integer'Image(MaxNumber)); Tries := Tries + 1; if Guess > Answer then Put_Line("Too High."); goto Running_Loop; end if; if Guess < Answer then Put_Line("Too Low."); goto Running_Loop; end if; Put("You got it in" & Integer'Image(Tries)); if Tries = 1 then Put_Line(" try!"); else Put_line(" tries!"); end if; GameOver := TRUE; Running := AskToPlayAgain; if Running = TRUE then GotMaxNumber := FALSE; goto Running_Loop; end if; end if; Put_Line("Thanks for playing!"); Put_Line("*** GAME OVER ***"); end Guessanumberada;