mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-03 23:40:14 +08:00
commit
066175a8e5
@ -2,7 +2,7 @@
|
||||
[#]: via: "https://itsfoss.com/rnote/"
|
||||
[#]: author: "Ankush Das https://itsfoss.com/author/ankush/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: " "
|
||||
[#]: translator: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
@ -1,132 +0,0 @@
|
||||
[#]: subject: "Learn Basic by coding a game"
|
||||
[#]: via: "https://opensource.com/article/23/2/learn-basic-coding-game"
|
||||
[#]: author: "Moshe Zadka https://opensource.com/users/moshez"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Learn Basic by coding a game
|
||||
======
|
||||
|
||||
Writing the same application in multiple languages is a great way to learn new ways to program. Most programming languages have certain things in common, such as:
|
||||
|
||||
- Variables
|
||||
- Expressions
|
||||
- Statements
|
||||
|
||||
These concepts are the basis of most programming languages. Once you understand them, you can start figuring the rest out.
|
||||
|
||||
Programming languages usually share some similarities. Once you know one programming language, you can learn the basics of another by recognizing its differences.
|
||||
|
||||
Practicing with a standard program is a good way of learning a new language. It 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. This article focuses on Basic.
|
||||
|
||||
### Guess the number in (Bywater) Basic
|
||||
|
||||
There is no real standard for the Basic programming language. Wikipedia says, "BASIC (Beginners' All-purpose Symbolic Instruction Code) is a family of general-purpose, high-level programming languages designed for ease of use." The [BWBasic][1] implementation is available under the GPL.
|
||||
|
||||
You can explore Basic by writing a version of the "guess the number" game.
|
||||
|
||||
### Install Basic on Linux
|
||||
|
||||
In Debian or Ubuntu, you can install Basic with the following:
|
||||
|
||||
```
|
||||
$ apt install -y bwbasic
|
||||
```
|
||||
|
||||
Download the latest release tarball for Fedora, CentOS, Mageia, and any other Linux distribution. Extract it, make it executable, and then run it from a terminal:
|
||||
|
||||
```
|
||||
$ tar --extract --file bwbasic*z
|
||||
|
||||
$ chmod +x bywater
|
||||
|
||||
$ ./bywater
|
||||
```
|
||||
|
||||
On Windows, [download the .exe release][2].
|
||||
|
||||
### Basic code
|
||||
|
||||
Here is my implementation:
|
||||
|
||||
```
|
||||
10 value$ = cint(rnd * 100) + 1
|
||||
20 input "enter guess"; guess$
|
||||
30 guess$ = val(guess$)
|
||||
40 if guess$ < value$ then print "Too low"
|
||||
50 if guess$ > value$ then print "Too high"
|
||||
60 if guess$ = value$ then 80
|
||||
70 goto 20
|
||||
80 print "That's right"
|
||||
```
|
||||
|
||||
Basic programs can be numbered or unnumbered. Usually, it is better to write programs unnumbered, but writing them with numbered lines makes it easier to refer to individual lines.
|
||||
|
||||
By convention, coders write lines as multiples of 10. This approach allows interpolating new lines between existing ones for debugging. Here's an explanation of my method above:
|
||||
|
||||
- Line 10: Computes a random value between 1 and 100 using the built-in **rnd** function, which generates a number between 0 and 1, not including 1.
|
||||
- Line 20: Asks for a guess and puts the value in the **guess$ scalar** variable. Line 30 converts the value to a numeric one.
|
||||
- Lines 40 and 50: Give the guesser feedback, depending on the comparison.
|
||||
- Line 70: Goes to the beginning of the loop.
|
||||
- Line 60: _Breaks_& the loop by transferring control to line 80. Line 80 is the last line, so the program exits after that.
|
||||
|
||||
### Sample output
|
||||
|
||||
The following is an example of the program after putting it in `program.bas`:
|
||||
|
||||
```
|
||||
$ bwbasic program.bas
|
||||
Bywater BASIC Interpreter/Shell, version 2.20 patch level 2
|
||||
Copyright (c) 1993, Ted A. Campbell
|
||||
Copyright (c) 1995-1997, Jon B. Volkoff
|
||||
|
||||
enter guess? 50
|
||||
Too low
|
||||
enter guess? 75
|
||||
Too low
|
||||
enter guess? 88
|
||||
Too high
|
||||
enter guess? 80
|
||||
Too low
|
||||
enter guess? 84
|
||||
Too low
|
||||
enter guess? 86
|
||||
Too high
|
||||
enter guess? 85
|
||||
That's right
|
||||
```
|
||||
|
||||
### Get started
|
||||
|
||||
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/2/learn-basic-coding-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://yeolpishack.net/repos/ChipMaster/bwBASIC
|
||||
[2]: https://github.com/nerun/bwbasic/releases
|
132
translated/tech/20230202.1 ⭐️ Learn Basic by coding a game.md
Normal file
132
translated/tech/20230202.1 ⭐️ Learn Basic by coding a game.md
Normal file
@ -0,0 +1,132 @@
|
||||
[#]: subject: "Learn Basic by coding a game"
|
||||
[#]: via: "https://opensource.com/article/23/2/learn-basic-coding-game"
|
||||
[#]: author: "Moshe Zadka https://opensource.com/users/moshez"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
通过游戏学习 Basic
|
||||
======
|
||||
|
||||
用多种语言编写同一个应用是学习新的编程语言的好方法。大多数编程语言都有某些共同点,如:
|
||||
|
||||
- 变量
|
||||
- 表达式
|
||||
- 语句
|
||||
|
||||
这些概念是大多数编程语言的基础。当你理解了它们,你就可以开始琢磨其他的东西了。
|
||||
|
||||
编程语言通常有一些相似之处。当你了解了一种编程语言,你就可以通过认识其差异来学习另一种语言的基础知识。
|
||||
|
||||
用标准程序进行练习是学习新语言的一个好方法。它使你能够专注于语言,而不是程序的逻辑。在这个系列文章中,我使用了一个“猜数字”的程序,在这个程序中,计算机在 1 到 100 之间挑选一个数字,并要求你猜出来。程序循环进行,直到你猜对数字为止。
|
||||
|
||||
这个程序锻炼了编程语言中的几个概念:
|
||||
|
||||
- 变量
|
||||
- 输入
|
||||
- 输出
|
||||
- 条件判断
|
||||
- 循环
|
||||
|
||||
这是学习一种新的编程语言的很好的实践。本文主要介绍 Basic。
|
||||
|
||||
### 在(Bywater)Basic 中猜数字
|
||||
|
||||
对于 Basic 编程语言,没有真正的标准。维基百科说:“BASIC(初学者通用符号指令代码)是一个通用的高级编程语言系列,旨在方便使用”。[BWBasic][1] 的实现是在 GPL 下提供的。
|
||||
|
||||
你可以通过编写一个“猜数字”游戏来探索 Basic。
|
||||
|
||||
### 在 Linux 上安装 Basic
|
||||
|
||||
在 Debian 或 Ubuntu 中,你可以用以下方法安装Basic:
|
||||
|
||||
```
|
||||
$ apt install -y bwbasic
|
||||
```
|
||||
|
||||
下载 Fedora、CentOS、Mageia 和其他任何 Linux 发行版的最新版本 tarball。解压并设置可执行,然后从终端运行它:
|
||||
|
||||
```
|
||||
$ tar --extract --file bwbasic*z
|
||||
|
||||
$ chmod +x bywater
|
||||
|
||||
$ ./bywater
|
||||
```
|
||||
|
||||
在 Windows 上,[下载 .exe 版本][2]。
|
||||
|
||||
### Basic 代码
|
||||
|
||||
下面是我的实现:
|
||||
|
||||
```
|
||||
10 value$ = cint(rnd * 100) + 1
|
||||
20 input "enter guess"; guess$
|
||||
30 guess$ = val(guess$)
|
||||
40 if guess$ < value$ then print "Too low"
|
||||
50 if guess$ > value$ then print "Too high"
|
||||
60 if guess$ = value$ then 80
|
||||
70 goto 20
|
||||
80 print "That's right"
|
||||
```
|
||||
|
||||
Basic 程序可以是编号的,也可以是不编号的。通常情况下,写程序时最好不编号,但用编号的行来写,可以更容易地引用各个行。
|
||||
|
||||
按照惯例,编码者将行写成 10 的倍数。这种方法允许在现有的行之间插入新的行,以便进行调试。下面是我对上述方法的解释:
|
||||
|
||||
- 10 行:使用内置的 **rnd** 函数计算一个 1 到 100 之间的随机值,该函数生成一个 0 到 1 之间的数字,不包括 1。
|
||||
- 20 行:询问一个猜测,并将该值放入 **guess$ 标量**变量。30 行将该值转换为一个数字。
|
||||
- 40 行和 50 行:根据比较结果,给猜测者以反馈。
|
||||
- 70 行:回到循环的起点。
|
||||
- 60 行:通过将控制权转移到 80 行来打破循环。80 行是最后一行,所以程序在这之后退出。
|
||||
|
||||
### 输出示例
|
||||
|
||||
下面是将该程序放入 `program.bas` 后的一个例子:
|
||||
|
||||
```
|
||||
$ bwbasic program.bas
|
||||
Bywater BASIC Interpreter/Shell, version 2.20 patch level 2
|
||||
Copyright (c) 1993, Ted A. Campbell
|
||||
Copyright (c) 1995-1997, Jon B. Volkoff
|
||||
|
||||
enter guess? 50
|
||||
Too low
|
||||
enter guess? 75
|
||||
Too low
|
||||
enter guess? 88
|
||||
Too high
|
||||
enter guess? 80
|
||||
Too low
|
||||
enter guess? 84
|
||||
Too low
|
||||
enter guess? 86
|
||||
Too high
|
||||
enter guess? 85
|
||||
That's right
|
||||
```
|
||||
|
||||
### 开始学习
|
||||
|
||||
这个“猜数字”游戏是学习新的编程语言的一个很好的入门程序,因为它以一种相当直接的方式锻炼了几个常见的编程概念。通过在不同的编程语言中实现这个简单的游戏,你可以展示这些语言的一些核心概念,并比较它们的细节。
|
||||
|
||||
你有喜欢的编程语言吗?你会如何用它来写“猜数字”的游戏?请关注本系列文章,看看你可能感兴趣的其他编程语言的例子吧!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/23/2/learn-basic-coding-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://yeolpishack.net/repos/ChipMaster/bwBASIC
|
||||
[2]: https://github.com/nerun/bwbasic/releases
|
Loading…
Reference in New Issue
Block a user