[手动选题][tech]: 20230109.0 ️ Learn the Ada programming language by writing a simple game.md

This commit is contained in:
六开箱 2023-01-10 00:47:48 +08:00
parent 5dba280521
commit bdcd68f13a

View File

@ -0,0 +1,179 @@
[#]: subject: "Learn the Ada programming language by writing a simple game"
[#]: via: "https://opensource.com/article/23/1/learn-ada-simple-game"
[#]: author: "Moshe Zadka https://opensource.com/users/moshez"
[#]: collector: "lkxed"
[#]: translator: " "
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
Learn the Ada programming language by writing a simple game
======
When you want to [learn a new programming language][1], it's good to focus on the things programming languages have in common:
- Variables
- Expressions
- Statements
These concepts are the basis of most programming languages. Once you understand them, you can start figuring out the rest. Because programming languages usually share similarities, once you know one language, you can learn the basics of another by understanding its differences.
A good way to learn new languages is practicing with a standard program. This allows you to focus on the language, not the program's logic. I'm doing that in this article series using a "guess the number" program, in which the computer picks a number between one and 100 and asks you to guess it. The program loops until you guess the number correctly.
This program exercises several concepts in programming languages:
- Variables
- Input
- Output
- Conditional evaluation
- Loops
It's a great practical experiment to learn a new programming language.
### Install Ada
The [Ada programming language][2] is a unique and highly structured language with a dedicated developer base. The toolchain for Ada is the GNU Ada Development Environment, better known as GNAT.
You can install GNAT on Linux using your distribution's package manager. On Fedora, CentOS, or similar:
```
$ sudo dnf install gcc-gnat
```
On Debian, Linux Mint, and derivatives:
```
$ sudo apt install gnat
```
On macOS and Windows, you can download an installer from the [Adacore website][3] (choose your platform from the drop-down menu).
### Guess the number in Ada
Create a file called `game.adb`.
The two built-in Ada libraries this program uses are `Text_IO` and `Numerics.Discrete_Random`:
```
with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
```
#### Procedure head
The name of the procedure must match the name of the file. The first part is defining the variables.
Note that the `discrete_random` is specialized to a specific range. In this case, the range of numbers allowed:
```
procedure Game is
   type randRange is range 1..100;
   package Rand_Int is new ada.numerics.discrete_random(randRange);
   use Rand_Int;
   gen : Generator;
   num : randRange;
   incorrect: Boolean := True;
   guess: randRange;
```
#### Procedure logic
The logic starts by `reset(gen)`. This initializes the random number generator, ensuring the number, initialized with `random(gen)`, will be different each time you run the program.
The next step is to run the loop:
- Output the instructions for a guess
- Read the line
- Convert it to `randRange`
- Check it against the number
If the number matches, incorrect is set to **False**, causing the next iteration of the loop to exit.
Finally, the program prints a confirmation of the guess correctness before exiting:
```
begin
   reset(gen);
   num := random(gen);
   while incorrect loop
       Put_Line ("Guess a number between 1 and 100");
       declare
          guess_str : String := Get_Line (Current_Input);
       begin
          guess := randRange'Value (guess_str);
       end;
       if guess < num then
           Put_line("Too low");
       elsif guess > num then
           Put_line("Too high");
       else
           incorrect := False;
       end if;
   end loop;
   Put_line("That's right");
end Game;
```
### Build the program
The easiest way to compile an Ada program is to use `gnatmake`:
```
$ gnatmake game.adb
aarch64-linux-gnu-gcc-10 -c game.adb
aarch64-linux-gnu-gnatbind-10 -x game.ali
aarch64-linux-gnu-gnatlink-10 game.ali
```
This generates a binary called `game`.
### Run the program
Each run of the program will be a little different. This is one example:
```
$ ./game 
Guess a number between 1 and 100
50
Too low
Guess a number between 1 and 100
75
Too low
Guess a number between 1 and 100
82
Too low
Guess a number between 1 and 100
90
Too high
Guess a number between 1 and 100
87
Too low
Guess a number between 1 and 100
88
That's right
```
### Learn Ada
This "guess the number" game is a great introductory program for learning a new programming language because it exercises several common programming concepts in a pretty straightforward way. By implementing this simple game in different programming languages, you can demonstrate some core concepts of the languages and compare their details.
Do you have a favorite programming language? How would you write the "guess the number" game in it? Follow this article series to see examples of other programming languages that might interest you!
--------------------------------------------------------------------------------
via: https://opensource.com/article/23/1/learn-ada-simple-game
作者:[Moshe Zadka][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/moshez
[b]: https://github.com/lkxed
[1]: https://opensource.com/article/20/10/learn-any-programming-language
[2]: https://opensource.com/article/21/10/learn-ada-2021
[3]: https://www.adacore.com/download/more