with Text_IO; with Ada.Integer_Text_IO; with Ada.Strings.Fixed; with Ada.Strings.Unbounded; with Tools; with GNAT.IO; use GNAT.IO; use Tools; procedure Bagels is type AnArray is Array(1..3) of Integer; Running: Boolean; GameOver: Boolean; TheDigits: AnArray; Score: Integer; Guessed: Boolean; Attempt: Integer; Guess: Integer; function AskForGuess(Attempt: Integer) return Integer is Guess: Integer; begin <> Text_IO.Put("GUESS #" & Integer'Image(Attempt)); Guess := GetIntInput(":"); if Guess < 0 then Text_IO.Put_Line("PLEASE GUESS A NON-NEGATIVE NUMBER."); goto afg_Loop; elsif Guess > 999 or Guess < 100 then Text_IO.Put_Line("PLEASE GUESS A NUMBER BETWEEN 100 AND 999."); goto afg_Loop; end if; return Guess; end AskForGuess; procedure DisplayGameOver is TheNumber: Integer; begin TheNumber := MakeIntFromArray(TheDigits); Text_IO.Put_Line("OH WELL, THAT'S TWENTY GUESSES."); Text_IO.Put_Line("MY NUMBER WAS " & Integer'Image(TheNumber)); Text_IO.Put_Line(""); end DisplayGameOver; procedure DisplayRules is begin Text_IO.Put_Line("I WILL THINK OF A THREE-DIGIT NUMBER. YOU WILL TRY TO"); Text_IO.Put_Line("GUESS MY NUMBER AND I WILL GIVE YOU CLUES AS FOLLOWS:"); Text_IO.Put_Line(" PICO - ONE DIGIT CORRECT BUT IN THE WRONG POSITION"); Text_IO.Put_Line(" FERMI - ONE DIGIT CORRECT AND IN THE RIGHT POSITION"); Text_IO.Put_Line(" BAGELS - NO DIGITS ARE CORRECT"); Text_IO.Put_Line(""); Text_IO.Put_Line("YOU GET TWENTY TRIES BEFORE YOU LOSE."); Text_IO.Put_Line(""); end DisplayRules; function MakeArrayFromInt(valIn: Integer) return AnArray is retArray: AnArray; begin retArray(1) := valIn / 100; retArray(2) := (valIn - (retArray(1) * 100)) / 10; retArray(3) := (valIn - (retArray(1) * 100)) - (retArray(2) * 10); return retArray; end MakeArrayFromInt; function MakeIntFromArray(arrayIn: AnArray) return Integer is retVal: Integer; begin retVal := arrayIn(1) * 100 + arrayIn(2) * 10 + arrayIn(3); return retVal; end MakeIntFromArray; procedure CreateTheDigits is ADigit: Integer; begin TheDigits(1) := GenerateRandomNumber(1,9,FALSE); <> ADigit := GenerateRandomNumber(1,9,FALSE); if ADigit /= TheDigits(1) then TheDigits(2) := ADigit; else goto sn1_Loop; end if; <> ADigit := GenerateRandomNumber(1,9,FALSE); if ADigit /= TheDigits(1) and ADigit /= TheDigits(2) then TheDigits(3) := ADigit; else goto sn2_Loop; end if; end CreateTheDigits; function CheckGuess(Guess: Integer) return Boolean is Temp: AnArray; Vals: AnArray; d: Integer; Result: Integer; RetVal: Boolean; begin RetVal := FALSE; Temp := MakeArrayFromInt(Guess); d := 1; while d < 4 loop Vals(d) := 0; if Temp(d) = TheDigits(d) then Vals(d) := 1; end if; d := d + 1; end loop; Result := MakeIntFromArray(Vals); if Result = 111 then RetVal := TRUE; else d := 1; while d < 4 loop if Vals(d) = 0 then case d is when 1 => if Temp(d) = TheDigits(3) then Vals(d) := 2; elsif Temp(d) = TheDigits(2) then Vals(d) := 2; end if; when 2 => if Temp(d) = TheDigits(1) then Vals(d) := 2; elsif Temp(d) = TheDigits(3) then Vals(d) := 2; end if; when others => if Temp(d) = TheDigits(1) then Vals(d) := 2; elsif Temp(d) = TheDigits(2) then Vals(d) := 2; end if; end case; end if; d := d + 1; end loop; Result := MakeIntFromArray(Vals); if Result = 0 then Text_IO.Put_Line("BAGLES"); else d := 1; while d < 4 loop if Vals(d) = 1 then Text_IO.Put("FERMI "); elsif Vals(d) = 2 then Text_IO.Put("PICO "); end if; d := d + 1; end loop; Text_IO.Put_Line(""); end if; end if; return RetVal; end CheckGuess; begin Text_IO.Put_Line("*** BAGELS : ADA ***"); Running := TRUE; if AskToSeeTheRules then DisplayRules; end if; Score := 0; while Running = TRUE loop Attempt := 1; gameOver := FALSE; CreateTheDigits; Text_IO.Put_Line("O.K. I HAVE A NUMBER IN MIND."); while gameOver = FALSE loop Guess := AskForGuess(Attempt); Guessed := CheckGuess(Guess); if Guessed = TRUE then Text_IO.Put_Line("YOU GOT IT!!!"); Score := Score + 1; GameOver := TRUE; else Attempt := Attempt + 1; if Attempt > 20 then DisplayGameOver; GameOver := TRUE; end if; end if; end loop; Text_IO.Put_Line("*** GAME OVER ***"); Running := AskToPlayAgain; end loop; Text_IO.Put_Line("A" & Integer'Image(Score) & " POINT BAGELS BUFF!"); Text_IO.Put_Line("HOPE YOU HAD FUN. BYE!"); null; end Bagels;