And here is the complete code of Redirect_User_Concern;
helps to add logic that navigates the next step where the User
must pause the Double_Threat
creation intent to prevent the Computer
from winning sooner. We can now remove the Put_Line
lines that print the Count_Double_Threats
and predictive sets of values.
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 | with Ada.Text_IO; use Ada.Text_IO; package body AI_Mockup is procedure Redirect_User_Concern ( Computer_Move : in out Digit ; App_State : in State ) is -- local Foreseen_User_Moves : Digit_Array; -- Foreseen_Computer_Moves : Digit_Array; Foreseen_App_State : State; Computer_After_Next_Move: Digit := 0; begin if Count_Double_Threats (App_State) > 1 then Foreseen_User_Moves := (others => 0); Get_Double_Threats (Foreseen_User_Moves, App_State); -- Foreseen_Computer_Moves := (others => 0); Get_Direct_Chance_Inits (Foreseen_Computer_Moves, App_State); -- for Index in Foreseen_Computer_Moves'Range loop if Foreseen_Computer_Moves (Index) /= 0 then Copy (Foreseen_App_State, App_State); Update_Computer_Set (Foreseen_App_State, Foreseen_Computer_Moves (Index)); Get_Direct_Chance (Computer_After_Next_Move, Foreseen_App_State); -- if not Found (Computer_After_Next_Move, Foreseen_User_Moves) then Computer_Move := Foreseen_Computer_Moves (Index); end if; end if; end loop; end if; end Redirect_User_Concern; end AI_Mockup; |
You can test the previous cases to test the operation of the new logic added to AI_Mockup.Get (Computer_Move, App_State);
. On the computer I’m using, the results are pretty good and just a bit unnatural because we haven’t split the potential result sets to do a random selection of possible moves. However, that will require code refactoring for quite some time and you should consider it after you have made sure that the test results are completely fine.
Source Code
: GitHub.IO
Although there are still other features that I have not fully learned about the Ada
language and the GNAT
development environment, this is the right point to end this Sub-Series with the main purpose of learning about the Ada language and the GNAT development environment. Procedural Programming
in a development environment designed specifically for this programming paradigm.
Specifically, before we move on to the previously planned Sub-Series Functional Programming
, I want to highlight a few features of the Procedural
aspect of thinking that have been received by Ada
:
- Always distinguish very clearly the types of
sub-program
according to the purpose of use.procedure
are used to createside-effect
that affect the state data of the external environment.function
are used to support local computations and will never produceside-effect
likeprocedure
. procedure
are used mainly andfunction
are just add-ons.procedure
will play the role of controlling the main operation logic of the program. Utilityfunction
are used to minimize the number oflocal
variables forprocedure
and in places where syntax is required in the form of return-valued expressions, such ascontract
.- The focus of a program is that the
State
state data will be created and stored permanently in aglobal
location, or if saved locally, it will be alocal
variable of importantprocedure
that contribute to the control of the system. control the main processing logic of the program. - Address references to
State
tuples will be shared amongsub-program
, andprocedure
will act as the surrounding objective actions to in turn make changes on theState
. To make it easier to imagine, theState
will act as Mini Databases during the program’s operation and will be updated and changed many times. contract
binding elements are very helpful forsub-program
; For example, the operation modes of thein
,out
,in out
parameters will help to report an error if we write code that implements inappropriate logic; And other self-definedcontract
types can be attached tosub-program
to ensure the validity ofinput/output
data.
Talking about Ada
in particular, the transparency in the language’s syntax and the naming style of elements in the standard libraries also helps me improve my coding habits. However, only the feature of grouping parameters of the same type into lists with semicolons ;
in the code declaring sub-program
is somewhat redundant. I actually had to re-edit the code so many times just because of the habit from other languages to have a single parameter list with the parameters listed with commas ,
.
1 2 3 4 5 | function Area_of_Circle ( R : Float ; X , Y : Integer ) return Float; |
However, that is just the personal opinion of a beginner
who is just learning to code. Because perhaps for engineers who program controls for hardware engine
like commercial airplanes, perhaps this is a useful feature when designing sub-program
with many different parameter groups. .
The only point that I can boldly affirm is that Ada
‘s ability to format the results to print to the Console
window is very limited by the strict style design and there is no option for the coder to redefine the parameters. Type'Attribute
attribute. When using Integer'Image (Value)
with single digit integer values, the result will always be a string with a space character in front. And we also don’t have the feature to use format parameters in C
such as %9s
to print the result string 9
characters long although the data passed to printf
can be much shorter.
If I plan to create another application after this project
, I will definitely only use Ada
to code the main logic. As for the code to draw the user interface, it will definitely be written in C
and import
into Ada
for later use. And such a combination is probably perfect for both Ada
and C
; because Ada
is not very popular in general General Purposes
application programming, so it will need to communicate with the API
of other environments using C
; while C
does not have the common data structures like Map
, Set
, Tree
, etc.. and many features support at the language level and high level programming interface such as contract
, and also generic
in C
It is also very difficult to implement.
Now we will move on to the next programming model in the group of popular programming models. Hopefully you still remember Elm
‘s syntax and the basics of Declarative
. If you’ve forgotten, take a moment to skim through the previous Sub-Series articles [Declarative Programming + Elm]
; Because the language that we are going to learn and example for the next Sub-Series can be seen as Elm
‘s more complete version of the features supported at the syntax level of the language.