Tuesday, May 10, 2016

Using Access Pointers

Oh, Ada.

So, after a longer time that expected, I finished the program that would utilize specific access pointers! To clarify, access pointers are the "arrows" that point at the object being referenced.

To demonstrate access pointers, I created a game called "Pyramid Loot".

Explanation of the game:

Pyramid Loot is a text-based game where your goal is to get the treasure in the pyramid. To get there, you must battle 4 different monsters of increasing difficulty.

You start off being asked what kind of blessing you want. There are 3 options: health, attack, and dexterity.

You are given 2 heals.

Each battle scene is simulated in text.

At the end of each battle, the monster you killed will drop an item if it was carrying one. You will have the option to pick it up or leave it, and if you already have 2 items (because you only have 2 hands), you must decide which item you want to replace OR just leave it.

Each room has an item that simulates the same decision making as picking up a monster's item.

Swords give you attack, shields give you damage reduction.

Using heals at the right time and strategically building your character is how you beat the game and get the treasure.


How the game uses access pointers:

Anytime an object is created in Ada, an access pointer must be made for it.
Player, Monsters, Items, and Rooms are all objects that require access pointers to allow for instances of these objects!



Here is the code if you want to try it yourself. At the bottom is a sample run through of the game.






































Sample of the Game:








































If I would have lost a battle, I would have been prompted to play again or not.

Example of this:



So, this is a program that utilizes access pointers.

Even though Ada was developed for very serious purposes, it can still be used to create fun games.

Good luck everyone.







Friday, May 6, 2016

Interesting features of Ada

Interesting features of Ada


Ada has a couple of interesting features that make it stand out. They tie back to why it was created. It is no surprise that its special features are directly related to ensuring proper functionality of the programs created, for Ada was originally created for military purposes where a malfunction in the program could cause accidental injury or death. Things in Ada must be very specific to avoid error, and this is done through high-level typing.

Specifically, Ada uses explicitly declared access types.

It also supports run-time checks to protect against access to unallocated memory, buffer overflow errors, range violations, off-by-one errors, array access errors, and other detectable bugs.

These make Ada relevant in the coding of avionics, ATC, railways, banking, and military and space technology.

In my next posts, I will present some programs that will demonstrate these things!


Monday, March 21, 2016

Methods/Functions and the Classic Binary Search

One main use of methods in Ada, or in any programming language, is to prevent redundancy in code.

There are two types of methods in Ada:

1. procedures
2. functions

A distinct feature of function or procedure parameters is the ability to specify what exactly the parameter will do regarding the variables passed in.

These specifications are:

in
     the default specification for parameters of methods. This makes the method use the variable passed in as a CONSTANT and thus only allows reading. Changes will not happen to the actual variable passed in.

out
     the value of the variable passed in as a parameter does not matter. It has no value when passed in and needs to be assigned a value in the method. The variable will receive a value in the method and that will be saved as the value of the variable.

in out
     the value of the variable passed in as a parameter can be read and written to. It can be used as a constant or changed. This is a mixture of IN and OUT.

Procedures

Procedures are methods without return values. They are similar to a function in Java with return value of VOID.

The syntax with 2 parameters:
procedure name(parameterName :  in/out/in out   parameterType; parameterName2 :  in/out/in out   parameterType2is

          [insert declarations]

         begin

end name;                 

Calling the procedure syntax:

name(parameter1, parameter2);

Here is an example of a procedure in use using the in parameter type:


And the output:




Notice how the variables passed in were used as constants and read.

Here is an example of a procedure in use using the out parameter type:



And the output:


Notice how the method disregarded the values of the variables passed in and changed them.


Functions

Functions in Ada must use return values and therefore must be used in expressions.

The syntax of a function with 1 parameter:

function name(parameter1 : parameter1Type) return returnType is

            [declarations]

           begin

                   [stuff to do]

end name;


Calling the function syntax:

name(parameter1)

^ MUST BE USED IN AN EXPRESSION

Here is an example of a function with a parameter of type IntArray:



This function adds up all the values in an array passed in as a parameter. In this case, it is used the array anArray to do its calculation.

This is the output:



I can call these procedures and functions as many times as needed without writing the same code over and over. This is what makes them so useful.

The Binary Search Example

A binary search is searching for a number in an array with numbers ordered from lowest to highest. Although it seems simple, we want to do this in the most efficient way possible:

1) take the mid point index of the array and get the value. Compare the value being search with it. If it equals the mid point index's value, the number has been found. If not, check what half of the array it is on.
         1a) if the number being searched is larger than the mid point index's value, search the upper half of the array.
         1b) if the number being searched is smaller than the mid point index's value, search the lower half of the array.

2) each time a new sector of the array is chosen, a new mid point and a new start or end index value will be set according to whether the upper or lower half of the sector of the array is being checked, respectively.

3) this will continue until the number has been found or there are no more numbers to check -- the number is not found in the array.


A little on Arrays...

To do this in Ada, you must also know how to create arrays in Ada.

First, you must create and declare the type of array you will use for your actual array.

Syntax of an INTEGER array:

type nameOfTypeOfArray is array (Positive range <>) of Integer;

Next, you must declare the actual array with its values and start and end index.

The start index should be 1 (yes, not 0.) and the end index will be the last index of your array.

Syntax for an Integer array with 3 values:

arrayName nameOfTypeOfArray (1.. 3) := (50, 70, 80);

Back to the Binary Search Problem:


Here is my solution in code to this problem using methods:





num is the number being searched for in the method findIndex.
anArray is the array being searched through.

Here is the output when num = 93:
 
I expose the mid values to demonstrate where the mid-index is at each check. 
I also expose the number of checks to demonstrate the efficiency of this method.
Instead of 13 checks, only 3 are done.

Here is another example when num = 150, a number not in the array and larger than all the numbers in the array:



And the output:



Another example when num = 0, a number not in the array and smaller than all the numbers in the array:



And the output:











Tuesday, March 8, 2016

Loops!

Ada uses loops to run through code several times, either forever or until a condition is met or a test fails.

The different types:

1. Endless loop
2. Loops with condition
3. For loop


Endless Loop


This loop runs forever.
Syntax:

             loop

               [insert code to be repeated];

             end loop;


Loops with Condition


There are three types of condition placement that a loop can use to continue running its code.

Condition in the:

                  1. beginning
                  2. middle
                  3. end

Condition in the beginning uses a test in the beginning to check if it should run a certain looping code. If the condition is met, it will run the code. If not, the code is never executed or if it has already run, will not loop through again.

Syntax:

while [condition] loop

[code to be repeated]

end loop;


Condition in the end uses a test to check if it should run a certain looping code more times. If the condition is met, the code will be repeated. The code in the loop will be ran at least once.

Syntax:



       loop

             [code to be repeated]

               exit when [condition];

       end loop;

Condition in the middle uses a test to check if it should continue to run a looping code or not. If the condition is met, the code will continue. If not, it will exit the loop. It is pretty much the same as the Until loop except the exit appears in the middle instead of the end.

Syntax:

       loop

             [code to be repeated]

               exit when [condition];

             [more code to be repeated]

       end loop;


For Loop



This is used for incrementing a specific variable to control the number of times a looping code is ran.

The syntax:

for I in [low value] .. [high value] loop

         [looping code]

end loop;

OR

for I in reverse [low value] .. [high value] loop

         [looping code]

end loop;


The variable can be incremented up or down to any end value. Normally, the variable to be incremented is type Integer

The starting, end, and incrementing is specified using a Range.

The way in which the variable will increment (up or down) will depend if reverse is used.

The low value and high value MUST be come in the order specified. Low value first, then high value.

You can also loop on Arrays! Syntax:


for I in X'Range loop

           [looping code]
           [to use the current X, you would use X(I)]

end loop;

where X is the Array name.


Examples


Endless Loop:




The output:






Conditional Loops

Condition in beginning:




The output:




Condition in middle or end:




The output:







For Loops


Normal For loop:



The output:




Reverse For loop:



The output:































Wednesday, February 17, 2016

Ada Conditionals

I will cover conditional statements in this post. Conditionals are how code makes choices.


The basic syntax of a conditional statement is:

if condition1 then

               what happens if condition1 passes

end if;

In Ada, any conditional must come after the begin statement and before the end [procedure name] statement.

Choice Between Two Things - If and Else


There is also a way to make a choice between two things - if one condition is not met, then such and such will happen.

The syntax of this is:

if condition1 then

               what happens if condition1 passes

else

               what happens if condition1 did not pass

end if;

Nested Conditional


The next scenario describes how code will make choices that lead to further choices. These are nested conditionals.

This is the syntax of a nested conditional:

if condition1 then

                  --condition1  passed, now check if condition2 will
           
                  if condition2 then
                                       --condition2 passed

                                       what happens if condition 2 passes


                  end if;


else

                 --what happens if condition1 did not pass

                  if condition3 then
                                       --condition3 passed

                                       what happens if condition 3 passes

                  end if;


end if;


Compound Conditional

A compound conditional is another way code makes choices. It involves giving two conditions in one, such as checking if number1 is greater than zero AND if number1 is less than two.

There are two ways two write these: using and or or. I will describe the use of these operators in the Logical Operators section.

It would look like this:


if condition1 and condition2 then

               what happens if condition1  AND condition2 passes

else

               what happens if condition1 OR condition2 did not pass

end if;

Elsif

Another important way of testing is using elsif. This is like an extension if else in that you can provide a condition that you want to be tested if the initial if or previous elsif in the same conditional tree had failed.

The syntax is as follows:


if condition1 then

               what happens if condition1 passes

elsif condition2 --IF CONDITION1  FAILED

               what happens if condition2 passes

elsif condition3 --IF CONDITION2  FAILED

               what happens if condition3 passes
end if;


Logical Operators

The most common operators are:

1) and 
2) or
3) not

and is used to test if BOTH conditions are being met. If so, the test will pass, and if not, the test will fail.

or is the opposite in that it checks of EITHER of the conditions are being met. If so, the test will pass. If neither are, it will fail.

not is used for turning a condition in the opposite direction. If the condition is num1 = num2, then using not will turn the condition into num1 /= num2 (num1 does not equal num2).


Code Examples

If-Then

The first is an example of a simple conditional to see if a piece of code is either done or not done (if - then):



The output:


If-Else

This example gives you an idea of how an if-else works in Ada.



The initial condition will fail, causing the else-code to run.
The output:




If-Elsif-Else


The output:



Nested, Compound, Logical Operators




The output: