Merge pull request #28421 from geekpi/translating

translating
This commit is contained in:
geekpi 2023-01-13 08:33:58 +08:00 committed by GitHub
commit 159d6ad37c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 179 additions and 179 deletions

View File

@ -1,179 +0,0 @@
[#]: 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: "geekpi"
[#]: 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

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: "geekpi"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
通过编写一个简单的游戏来学习 Ada 编程语言
======
当你想[学习一种新的编程语言][1]时,把注意力放在编程语言的共同点上是很好的。
- 变量
- 表达式
- 语句
这些概念是大多数编程语言的基础。一旦你理解了它们,你就可以开始琢磨其他的东西了。因为编程语言通常有相似之处,一旦你知道一种语言,你就可以通过了解其差异来学习另一种语言的基础知识。
学习新语言的一个好方法是用一个标准程序进行练习。这使你能够专注于语言,而不是程序的逻辑。在这个系列文章中,我使用了一个“猜数字”的程序,在这个程序中,计算机在 1 到 100 之间挑选一个数字,并要求你猜出来。程序循环进行,直到你猜对数字为止。
这个程序锻炼了编程语言中的几个概念:
- 变量
- 输入
- 输出
- 条件判断
- 循环
这是一个学习新的编程语言的很好的实践实验。
### 安装 Ada
[Ada 编程语言][2]是一种独特的、高度结构化的语言有专门的开发者基础。Ada 的工具链是 GNU Ada 发环境,更多的是被称为 GNAT。
你可以使用你的发行版的包管理器在 Linux 上安装 GNAT。在 Fedora、CentOS 或类似系统上:
```
$ sudo dnf install gcc-gnat
```
在 Debian, Linux Mint 及衍生版上:
```
$ sudo apt install gnat
```
在 macOS 和 Windows 上,你可以从 [Adacore 网站][3]下载一个安装程序(从下拉菜单中选择你的平台)。
### 在 Ada 中猜数字
创建一个名为 `game.adb` 的文件。
这个程序使用的两个内置 Ada 库:`Text_IO` 和 `Numerics.Discrete_Random`
```
with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
```
#### 过程头
过程procedure的名称必须与文件的名称一致。第一部分是定义变量。
注意,`discrete_random` 是专门针对特定范围的。在这里,允许数字范围:
```
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;
```
#### 过程逻辑
该逻辑由 `reset(gen)` 开始。这将初始化随机数发生器,确保每次运行程序时,用 `random(gen)` 初始化的数字将是不同的。
下一步是运行循环:
- 输出猜测的指令
- 读取该行
- 将其转换为 `randRange`
- 将其与数字进行核对
如果数字匹配incorrect 被设置为 **False**,导致循环的下一次迭代退出。
最后,程序在退出前会打印出对猜测正确性的确认:
```
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;
```
### 编译程序
编译 Ada 程序的最简单方法是使用 `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
```
这将生成一个名为 `game` 的二进制文件。
### 运行程序
程序的每次运行都会有一些不同。这是一个例子:
```
$ ./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
```
### 学习 Ada
这个“猜数字”游戏是学习新的编程语言的一个很好的入门程序,因为它以一种相当直接的方式锻炼了几个常见的编程概念。通过在不同的编程语言中实现这个简单的游戏,你可以展示这些语言的一些核心概念,并比较它们的细节。
你有喜欢的编程语言吗?你会如何用它来写“猜数字”的游戏?请关注本系列文章,看看你可能感兴趣的其他编程语言的例子吧!
--------------------------------------------------------------------------------
via: https://opensource.com/article/23/1/learn-ada-simple-game
作者:[Moshe Zadka][a]
选题:[lkxed][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者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