mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
translating by tt67wq
This commit is contained in:
parent
5919e9a0ee
commit
c7b858144e
@ -1,141 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Program a simple game with Elixir)
|
||||
[#]: via: (https://opensource.com/article/20/12/elixir)
|
||||
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
|
||||
|
||||
Program a simple game with Elixir
|
||||
======
|
||||
Learn Elixir by programming a "guess the number" game and comparing the
|
||||
language against ones you know.
|
||||
![A die with rainbow color background][1]
|
||||
|
||||
To you learn a new programming language, it's good to focus on the things most programming languages have in common:
|
||||
|
||||
* Variables
|
||||
* Expressions
|
||||
* Statements
|
||||
|
||||
|
||||
|
||||
These concepts are the basis of most programming languages. Because of these similarities, once you know one programming language, you can start figuring another one out by recognizing its differences.
|
||||
|
||||
Another good tool for learning a new language is starting with a standard program. This allows you to focus on the language, not the program's logic. We're 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.
|
||||
|
||||
The "guess the number" 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.
|
||||
|
||||
### Guess the number in Elixir
|
||||
|
||||
The [Elixir][2] programming language is a dynamically typed functional language designed for building stable and maintainable applications. It runs on top of the same virtual machine as [Erlang][3] and shares many of its strengths—but with slightly easier syntax.
|
||||
|
||||
You can explore Elixir by writing a version of the "guess the number" game.
|
||||
|
||||
Here is my implementation:
|
||||
|
||||
|
||||
```
|
||||
defmodule Guess do
|
||||
def guess() do
|
||||
random = Enum.random(1..100)
|
||||
IO.puts "Guess a number between 1 and 100"
|
||||
Guess.guess_loop(random)
|
||||
end
|
||||
def guess_loop(num) do
|
||||
data = IO.read(:stdio, :line)
|
||||
{guess, _rest} = Integer.parse(data)
|
||||
cond do
|
||||
guess < num ->
|
||||
IO.puts "Too low!"
|
||||
guess_loop(num)
|
||||
guess > num ->
|
||||
IO.puts "Too high!"
|
||||
guess_loop(num)
|
||||
true ->
|
||||
IO.puts "That's right!"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Guess.guess()
|
||||
```
|
||||
|
||||
To assign a value to a variable, list the variable's name followed by the `=` sign. For example, the statement `random = 0` assigns a zero value to the `random` variable.
|
||||
|
||||
The script starts by defining a **module**. In Elixir, only modules can have named functions in them.
|
||||
|
||||
The next line defines the function that will serve as the entry point, `guess()`, which:
|
||||
|
||||
* Calls the `Enum.random()` function to get a random integer
|
||||
* Prints the game prompt
|
||||
* Calls the function that will serve as the loop
|
||||
|
||||
|
||||
|
||||
The rest of the game logic is implemented in the `guess_loop()` function.
|
||||
|
||||
The `guess_loop()` function uses [tail recursion][4] to loop. There are several ways to do looping in Elixir, but using tail recursion is a common one. The last thing `guess_loop()` does is call _itself_.
|
||||
|
||||
The first line in `guess_loop()` reads the input from the user. The next line uses `parse()` to convert the input to an integer.
|
||||
|
||||
The `cond` statement is Elixir's version of a multi-branch statement. Unlike `if/elif` or `if/elsif` in other languages, Elixir does not treat the first nor the last branch in a different way.
|
||||
|
||||
This `cond` statement has a three-way branch: The guess can be smaller, bigger, or equal to the random number. The first two options output the inequality's direction and then tail-call `guess_loop()`, looping back to the beginning. The last option outputs `That's right`, and the function finishes.
|
||||
|
||||
### Sample output
|
||||
|
||||
Now that you've written your Elixir program, you can run it to play the "guess the number" game. Every time you run the program, Elixir will pick a different random number, and you can guess until you find the correct number:
|
||||
|
||||
|
||||
```
|
||||
$ elixir guess.exs
|
||||
Guess a number between 1 and 100
|
||||
50
|
||||
Too high
|
||||
30
|
||||
Too high
|
||||
20
|
||||
Too high
|
||||
10
|
||||
Too low
|
||||
15
|
||||
Too high
|
||||
13
|
||||
Too low
|
||||
14
|
||||
That's right!
|
||||
```
|
||||
|
||||
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/20/12/elixir
|
||||
|
||||
作者:[Moshe Zadka][a]
|
||||
选题:[lujun9972][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/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/dice_tabletop_board_gaming_game.jpg?itok=y93eW7HN (A die with rainbow color background)
|
||||
[2]: https://elixir-lang.org/
|
||||
[3]: https://www.erlang.org/
|
||||
[4]: https://en.wikipedia.org/wiki/Tail_call
|
137
translated/tech/20201209 Program a simple game with Elixir.md
Normal file
137
translated/tech/20201209 Program a simple game with Elixir.md
Normal file
@ -0,0 +1,137 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (tt67wq)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Program a simple game with Elixir)
|
||||
[#]: via: (https://opensource.com/article/20/12/elixir)
|
||||
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
|
||||
|
||||
使用 Elixir 语言编写一个小游戏
|
||||
======
|
||||
通过编写"猜数字"游戏来学习 Elixir 编程语言并将它与一个你熟知的语言做对比。
|
||||
|
||||
![A die with rainbow color background][1]
|
||||
|
||||
为了更好的学习一门新的编程语言,最好的方法是去关注主流语言的一些共有特征:
|
||||
|
||||
* 变量
|
||||
* 表达式
|
||||
* 声明
|
||||
|
||||
|
||||
这些概念是大多数编程语言的基础。因为这些相似性,只要你通晓了一门编程语言,你可以通过对比差异来熟知另一门编程语言。
|
||||
|
||||
另外一个学习新编程语言的好方法是开始编写一个简单标准的程序。它可以让你集中精力在语言上而非程序的逻辑本身。在这个系列的文章中,我们使用"猜数字"程序来实现,计算机会选择一个介于 1 到 100 之间的数字,并要求你来猜测它。程序会循环执行,直到您正确猜出该数字为止。
|
||||
|
||||
"猜数字"这个小程序使用了编程语言的以下概念:
|
||||
|
||||
* 变量
|
||||
* 输入
|
||||
* 输出
|
||||
* 条件判断
|
||||
* 循环
|
||||
|
||||
|
||||
这是一个学习新编程语言的绝佳实践。
|
||||
|
||||
### 猜数字的 Elixir 实现
|
||||
|
||||
[Elixir][2] 是一门被设计用于构建稳定可维护应用的动态类型函数式编程语言。它与 [Erlang][3] 运行于同一虚拟机之上,吸纳了 Erlang 的众多长处的同时拥有更加轻便的语法。
|
||||
|
||||
你可以编写一个 Elixir 版本的"猜数字"游戏来体验这门语言。
|
||||
|
||||
这是我的实现方法:
|
||||
|
||||
|
||||
```
|
||||
defmodule Guess do
|
||||
def guess() do
|
||||
random = Enum.random(1..100)
|
||||
IO.puts "Guess a number between 1 and 100"
|
||||
Guess.guess_loop(random)
|
||||
end
|
||||
def guess_loop(num) do
|
||||
data = IO.read(:stdio, :line)
|
||||
{guess, _rest} = Integer.parse(data)
|
||||
cond do
|
||||
guess < num ->
|
||||
IO.puts "Too low!"
|
||||
guess_loop(num)
|
||||
guess > num ->
|
||||
IO.puts "Too high!"
|
||||
guess_loop(num)
|
||||
true ->
|
||||
IO.puts "That's right!"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Guess.guess()
|
||||
```
|
||||
|
||||
Elixir 通过列出变量的名称后面跟一个`=`号来为了给变量分配一个数值。举个例子,表达式 `random = 0` 给 `random` 变量分配一个数值 0。
|
||||
|
||||
代码以定义一个 **module** 开始。在 Elixir 语言中,只有 module 可以包含非匿名函数。
|
||||
|
||||
紧随其后的这行代码定义了入口函数 `guess()`,这个函数:
|
||||
|
||||
* 调用 `Enum.random()` 函数来获取一个随机数
|
||||
* 打印游戏提示
|
||||
* 调用循环执行的函数
|
||||
|
||||
剩余的游戏逻辑实现在 `guess_loop()` 函数中。
|
||||
|
||||
`guess_loop()` 函数利用[尾递归 ][4] 来实现循环。Elixir 中有好几种实现循环的方法,尾递归是比较常用的一种方式。`guess_loop()` 函数做的最后一件事就是调用自身。
|
||||
|
||||
`guess_loop()` 函数的第一行读取用户输入。下一行调用 `parse()` 函数将输入转换成一个整数。
|
||||
|
||||
`cond` 表达式是 Elixir 版本的多重分支表达式。与其他语言中的 `if/elif` 或者 `if/elsif` 表达式不同,Elixir 对于的首个分支或者最后一个没有分支并没有区别对待。
|
||||
|
||||
`cond` 表达式有三路分支:猜测的结果可以比随机数大、小或者相等。前两个选项先输出不等式的方向然后递归调用 `guess_loop()`,循环返回至函数开始。最后一个选项输出 `That's right`,然后这个函数就完成了。
|
||||
|
||||
### 输出例子
|
||||
|
||||
现在你已经编写了你的 Elixir 代码,你可以运行它来玩"猜数字"的游戏。每次你执行这个程序,Elixir 会选择一个不同的随机数,你可以一直猜下去直到你找到正确的答案:
|
||||
|
||||
|
||||
```
|
||||
$ elixir guess.exs
|
||||
Guess a number between 1 and 100
|
||||
50
|
||||
Too high
|
||||
30
|
||||
Too high
|
||||
20
|
||||
Too high
|
||||
10
|
||||
Too low
|
||||
15
|
||||
Too high
|
||||
13
|
||||
Too low
|
||||
14
|
||||
That's right!
|
||||
```
|
||||
|
||||
"猜数字"游戏是一个学习一门新编程语言的绝佳入门程序,因为它用了非常直接的方法实践了常用的几个编程概念。通过用不同语言实现这个简单的小游戏,你可以实践各个语言的核心概念并且比较它们的细节。
|
||||
|
||||
你是否有你最喜爱的编程语言?你将怎样用它来编写"猜数字"这个游戏?关注这个系列的文章来看看其他你可能感兴趣的语言实现。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/12/elixir
|
||||
|
||||
作者:[Moshe Zadka][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[tt67wq](https://github.com/tt67wq)
|
||||
校对:[校对者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/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/dice_tabletop_board_gaming_game.jpg?itok=y93eW7HN (A die with rainbow color background)
|
||||
[2]: https://elixir-lang.org/
|
||||
[3]: https://www.erlang.org/
|
||||
[4]: https://en.wikipedia.org/wiki/Tail_call
|
Loading…
Reference in New Issue
Block a user