hangman_1.0.0_be628ad5/hangman.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
With Ada.Text_IO; Use Ada.Text_IO;
With Ada.Integer_Text_IO; Use Ada.Integer_Text_IO;
With Ada.Characters.Handling; Use Ada.Characters.Handling;
With Ada.Strings.Unbounded; Use Ada.Strings.Unbounded;
With Ada.Integer_Text_IO; Use Ada.Integer_Text_IO;
with ada.integer_text_io;
use ada.integer_text_io;
With Ada.Numerics.Discrete_Random;
Package body hangman is
Package random_num is new Ada.Numerics.Discrete_Random (Result_subtype => rng);

    Procedure StackInit (Stk: in out SType) is
    begin
        Stk.Top := 0;
    end StackInit;
    

Function StkTop (Stk: in SType) return Character is 
   Begin
	Return Stk.Store(Stk.Top);
End StkTop;

Procedure Push (Stk : in out SType; new_e : in Character) is
    Begin
		if Stk.Top /= Stk.Size then
			Stk.Top := Stk.Top + 1;
			Stk.Store (Stk.Top) := new_e;
        End if;
End Push;

Procedure Pop (Stk: in out SType) is
    Begin
		loop
			if Stk.Top /= 0 then
				
				Stk.Top := Stk.Top - 1;
			end if;
			exit when Stk.Top = 0;
		end loop;
	end Pop;
	
Procedure Print (Stk: in SType) is
    Begin
        for i in 1 .. Stk.top loop
	  put(item => Stk.Store(i));
	  put(item => "  ");
        end loop;
    end print;

	
------------ List Functions ---------------------------------

   procedure Search_Linked_List (List :  in List_Type;
                                 Key :  in Unbounded_String;
                                 Found : out Boolean;
                                 Pred_Loc : out Node_Ptr;
                                 Location : out Node_Ptr) is

   begin
      Location := List.Head;  -- Start at the beginning of the linked list
      Pred_Loc := null;  -- No predecessor for 1st element in the list
      -- Search the linked list
      -- Each iteration, check one node
      loop
         exit when Location = null or else -- Reached the end
                   Key < (Location.all.Info) or else -- Passed by
                   Key = (Location.all.Info);  -- Found it
         Pred_Loc := Location ;  
         Location := Location.all.Next;
      end loop; -- Found := Location /= null and then Key = Location.all.Info;
      if Location /= null then
         Found := Key = (Location.all.Info);
      else
         Found := False;
      end if;
   end Search_Linked_List;
   
----------------------------------------------------------------------------
  
   procedure Insert (List : in out List_Type;
                     Word : in Unbounded_String) is
      Have_Duplicate : Boolean;
      Pred_Loc : Node_Ptr;
      Location : Node_Ptr;
   begin
      Search_Linked_List (List => List,
                          Key => Word,
                          Found => Have_Duplicate,
                          Pred_Loc => Pred_Loc,
                          Location => Location);
      if Have_Duplicate then
         raise DUPLICATE_KEY;
      elsif Pred_Loc = null then
         -- Add Item to the head of the linked list
         List.Head := new Node_Type'(Info => Word, Next => List.Head);
      else -- Add at the middle or end of the linked list
         Pred_Loc.all.Next := new Node_Type'(Info => Word, Next => 
Location);
      end if;
   exception
      when STORAGE_ERROR => -- Overflow when no storage available
         raise OVERFLOW;
   end Insert;
   
   
----------------------------------------------------------------------------
   procedure Traverse(List : in out List_Type; RandomNum : in Integer;  Element : out Unbounded_String) is
      Location : Node_Ptr;  -- Designates current node
   begin
      Location := List.Head;  -- Start with the first node in the linked 
      for i in 1..RandomNum loop
         exit when Location = null;
         Element := Location.all.info;
         Location := Location.all.Next; -- Move to next node
      end loop;
   end Traverse;
  
--------------------------------------------------------------------------
Procedure InitList(x: out List_Type) is

subtype File_Type is Ada.Text_IO.File_Type; -- creates a subtype File_Type
File_1: File_Type;
Test_File : File_Type;
c : character;
line_data2 : Unbounded_String;
Begin
    Ada.Text_IO.Open(File => File_1, mode => Ada.Text_IO.IN_File, name => "wordbank.txt");
    While not Ada.Text_IO.End_of_File(File_1) loop
        loop
			Get(File => File_1, Item => c);
			exit when c = ' ';
			line_data2 := line_data2 & to_lower(c);
        end loop;
		Insert(x, line_data2);
        line_data2 := to_unbounded_string("");
    end loop;
    Ada.Text_IO.Close(File => File_1);
End InitList;


Function RandomNumber Return Integer is
R: random_num.Generator;

Begin
    random_num.Reset (Gen => R);
    Return random_num.random(Gen => R);
End RandomNumber;


Function GetWord (x: In List_Type) Return Unbounded_String is
	randnum: integer;
	retword: Unbounded_String;
        retrieve : List_Type := x;
Begin 
	randnum:= RandomNumber;
	Traverse(retrieve, randnum, retword);
	Return retword;
End GetWord;


------------ Game Functions ---------------------------------
Function CreateMan (WrongCount: integer) Return ManArray is 
	HangMan: ManArray;
	One: Line;
	Two: Line;
	Three: Line;
	Four: Line;
	Five: Line;
	Six: Line;
	Seven: Line;
	Eight: Line;
	Nine: Line;
	Ten: Line;
	Eleven: Line;
Begin 

	
	if (WrongCount = 0) Then 

		One   := "******************";
		Two   := "     _ _ _ _     *";
		Three := "    |      |     *";
		Four  := "    |            *";
		Five  := "    |            *";
		Six   := "    |            *";
		Seven := "    |            *";
		Eight := "    |            *";
		Nine  := "    |            *";
		Ten   := " --------        *";
		Eleven:= "******************";
		
	elsif (WrongCount = 1) Then
		One   := "******************";
		Two   := "     _ _ _ _     *";
		Three := "    |      |     *";
		Four  := "    |      O     *";
		Five  := "    |            *";
		Six   := "    |            *";
		Seven := "    |            *";
		Eight := "    |            *";
		Nine  := "    |            *";
		Ten   := " --------        *";
		Eleven:= "******************";
		
	elsif (WrongCount = 2) Then
		One   := "******************";
		Two   := "     _ _ _ _     *";
		Three := "    |      |     *";
		Four  := "    |      O     *";
		Five  := "    |      |     *";
		Six   := "    |      |     *";
		Seven := "    |            *";
		Eight := "    |            *";
		Nine  := "    |            *";
		Ten   := " --------        *";
		Eleven:= "******************";
	
	elsif (WrongCount = 3) Then
		One   := "******************";
		Two   := "     _ _ _ _     *";
		Three := "    |      |     *";
		Four  := "    |    \ O     *";
		Five  := "    |     \|     *";
		Six   := "    |      |     *";
		Seven := "    |            *";
		Eight := "    |            *";
		Nine  := "    |            *";
		Ten   := " --------        *";
		Eleven:= "******************";
		
	elsif (WrongCount = 4) Then
		One   := "******************";
		Two   := "     _ _ _ _     *";
		Three := "    |      |     *";
		Four  := "    |    \ O /   *";
		Five  := "    |     \|/    *";
		Six   := "    |      |     *";
		Seven := "    |            *";
		Eight := "    |            *";
		Nine  := "    |            *";
		Ten   := " --------        *";
		Eleven:= "******************";
		
	elsif (WrongCount = 5) Then
		One   := "******************";
		Two   := "     _ _ _ _     *";
		Three := "    |      |     *";
		Four  := "    |    \ O /   *";
		Five  := "    |     \|/    *";
		Six   := "    |      |     *";
		Seven := "    |     /      *";
		Eight := "    |    /       *";
		Nine  := "    |            *";
		Ten   := " --------        *";
		Eleven:= "******************";
		
	elsif (WrongCount = 6) Then 
		One   := "******************";
		Two   := "     _ _ _ _     *";
		Three := "    |      |     *";
		Four  := "    |    \ O /   *";
		Five  := "    |     \|/    *";
		Six   := "    |      |     *";
		Seven := "    |     / \    *";
		Eight := "    |    /   \   *";
		Nine  := "    |            *";
		Ten   := " --------        *";
		Eleven:= "******************";
	
	end if;
	
	HangMan(1) := One;
	HangMan(2) := Two;
	HangMan(3) := Three;
	HangMan(4) := Four;
	HangMan(5) := Five;
	HangMan(6) := Six;
	HangMan(7) := Seven;
	HangMan(8) := Eight;
	HangMan(9) := Nine;
	HangMan(10) := Ten;
	HangMan(11) := Eleven;
	
	Return HangMan;

End CreateMan;
-----------------------------------------------------------------------------------------

Procedure DrawMan(Man: IN ManArray) is
Begin
	For i in 1..11 Loop
		Ada.Text_IO.Put(Item => Man(i));
		New_Line;
	End Loop;
End DrawMan;

-------------------------------------------------------------------------------------------

Procedure ShowMessage(WrongCount: integer; WrongPrev: integer) is

Begin
	if (WrongCount = WrongPrev) Then
		Put("Good Guess!");
	else 
		if (WrongCount = 1) Then 
			Put("Head added to Hang Man!");
		
		elsif (WrongCount = 2) Then 
			Put("Body added to Hang Man!");
		
		elsif (WrongCount = 3) Then
			Put("First arm added to Hang Man!");
		
		elsif (WrongCount = 4) Then
			Put("Second arm added to Hang Man!");
			
		elsif (WrongCount = 5) Then 
			Put("First leg added to Hang Man! Be Careful!!");
		
		else  -- (WrongCount = 6) Then
			Put("Second leg added to Hang Man!");
			New_Line;
			Put("You hung the man! YOU LOSE!");
			
		end if;
		
	end if;
  end ShowMessage;

  
-------------------------------------------------------------
end hangman;