A year ago, I had to write two programs in Ada for my university project. I was allowed to write whatever I wanted as long as I implemented some sort of algorithm. So I did. This is one of the two I wrote. It took a bit to grasp the syntax of Ada since tutorials for it were a little scarce. It was very fun and challenging to write though.
with Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
use Ada.Text_IO;
procedure GuessTheWord is
Input : File_Type;
counter : Integer;
wordLength : Integer;
charArray, answerCharArray, clueCharArray : array (Integer range 0 .. 99) of Character;
temp, temp2 : Integer;
tempChar : Character;
userInput : String (1 .. 80);
last : Natural;
isCorrect : Boolean;
lives : Integer;
function generateRandomLineCount return Integer is
type Rand_Range is range 0..54670;
package Rand_Int is new Ada.Numerics.Discrete_Random(Rand_Range);
seed : Rand_Int.Generator;
Num : Rand_Range;
begin
Rand_Int.Reset(seed);
Num:= Rand_Int.Random(seed);
return Integer(Num);
end generateRandomLineCount;
function generateRandomCounter return Integer is
type Rand_Range is range 1..12;
package Rand_Int is new Ada.Numerics.Discrete_Random(Rand_Range);
seed : Rand_Int.Generator;
Num : Rand_Range;
begin
Rand_Int.Reset(seed);
Num:= Rand_Int.Random(seed);
return Integer(Num);
end generateRandomCounter;
function generateRandomCounterSmaller return Integer is
type Rand_Range is range 1..5;
package Rand_Int is new Ada.Numerics.Discrete_Random(Rand_Range);
seed : Rand_Int.Generator;
Num : Rand_Range;
begin
Rand_Int.Reset(seed);
Num:= Rand_Int.Random(seed);
return Integer(Num);
end generateRandomCounterSmaller;
begin
counter := 1;
Open(File => Input,
Mode => In_File,
Name => "dict.txt");
temp := generateRandomLineCount;
Set_Line(Input, To => Count(temp));
declare
Line : String := Get_Line(Input);
begin
while counter <= Line'Length loop
answerCharArray(counter) := Line(counter);
charArray(counter) := Line(counter);
clueCharArray(counter) := Line(counter);
counter := counter + 1;
end loop;
wordLength := Line'Length;
end;
counter := 1;
--Shuffles the contents of the Character array. This applies the Fisher-Yates algorithm.
while counter <= wordLength loop
temp := generateRandomCounter;
if (temp > wordLength) then
temp:= generateRandomCounterSmaller;
end if;
temp2 := generateRandomCounter;
if (temp2 > wordLength) then
temp2:= generateRandomCounterSmaller;
end if;
tempChar := charArray(temp);
charArray(temp) := charArray(temp2);
charArray(temp2) := tempChar;
counter := counter + 1;
end loop;
New_Line;
-- Add blanks in the letters of the word as a clue
for ctr in Integer range 1 .. 5 loop
clueCharArray(generateRandomCounterSmaller) := '_';
end loop;
-- Display clue array
for ctr in Integer range 1 .. wordLength loop
Put(clueCharArray(ctr) & ' ');
end loop;
New_Line;
-- Display jumbled letters
for counter in Integer range 1 .. wordLength loop
Put(charArray(counter) & ' ');
end loop;
New_Line;
counter := 1;
lives := 3;
while lives > 0 loop
Put("What's the word: ");
Get_Line(userInput,last);
while counter <= wordLength loop
if (answerCharArray(counter) = userInput(counter)) then
isCorrect := True;
else
isCorrect := False;
exit;
end if;
counter := counter + 1;
end loop;
if isCorrect = True then
Put_Line("You are correct!");
exit;
else
Put_Line("You are wrong!");
end if;
lives := lives - 1;
if (lives = 0) then
New_Line;
Put_Line("Game Over!!!");
end if;
end loop;
Close(Input);
end GuessTheWord;