(This article was largely automatically translated)
It’s been a little while ago I found time to write a nice article on this blog.
Fortunately, there’s another good reason to get something to post.
Namely the 0.5.4 release of Red.
Red is a new programming language. Only in development since 2012, but now has a firm foundation on which can be built considerably.
And besides Red already is pretty useful for small programs.
Soon the first version with GUI support. All the more reason to take a brief look at how easy it is to program with Red.
Let’s make a version of a simple board-a-number-under-the-100 game. We will create a working program and meanwhile we make it a little nicer, but it can always be better, it is also to show what it can do.
This English version I treat extensively the steps needed to set up your environment. Because everything on the official sites in English explaining I leave this in the English version I will put omitted, but to those who make less good English too controlling with Red knowledge tell me extensively all delicious.
What do we need?
First, a Rebol runtime (the Rebol program) for our computer.
Please download a rebol core of the site Rebol.com
http://www.rebol.com/download-core.html
and if you’re at a view also similar version
http://www.rebol.com/download-view.html
Red to compile programs you have enough core version.
Download the source code for the Red Github site
https://github.com/red/red
At the bottom of the right column is a button that says “Download ZIP”
Unzip this file to the folder where you want it.
Also download the Red program from the official site
http://www.red-lang.org/p/download.html
If all is well you will see a big red button that links to the program for your operating system of your computer is.
You make myfolder in a folder, for example, the name “project” herein can again create new folders for each program that you make one.
I’m going to just assume that you now have a few folders:
/Mydir rebol-core.exe rebol-view.exe red.exe /Mydir/project /Mydir/red
Start the program once on Red. Red now compiles the console program for you. With this console, you can retrieve all kinds HELP for help. Try it with confidence. You can also try small sums such as 12 * 12 or 4096/16
Let’s create a program file.
That’s just a text file that can be opened with Notepad or Notepad.
The file is called “guess.red” and we put it in the folder myfolder/project/guess/
On the first line is established that this is a program Red.
Red []
Between putting the brackets (or bacon hooks I often use both names interchangeably) that you will see many kinds of information you can about your program (or script).
Red [ file: %guess.red Author: "I" Description: { This program selects a number from 1 to 99, you have to guess. } ]
As you can see, the file is guess.red preceded by a percent sign “%”. This tells Red that it is a file, not a name of a variable or something else.
We want to be able to ask the player to fill in a number. How do we do that? We use the ASK mode.
ASK is still not a standard feature in Red but the console program that comes with red makes already use.
We need at this time so here itself the source code for this function to give to our program. Fortunately, this is easy.
Red [] #include %../../red/environment/console/input.red
The dots and the slash indicate that we first from this program up a folder in the folder tree should steps.
What should the program?
1. Choose a random integer from 1 to 99
2. Ask for a number
3. see if the number is guessed, or it is advisable number is larger or smaller.
4. If the number is guessed or the player wants to stop or attempting to stop often than not the right answer
5. Otherwise, we go to step 2
We call the number that the computer chooses secret-number
With a colon after secret-number we indicate that the value of secrecy number will be filled with what haunts.
Choosing a random number is the random function, which of course also arbitrary means in English.
Step 1 then translates to Secret Number: random 99
The rest of the program now as I write differently
DIY until player wants to stop or often guessed or guessed
ask for a number
count on a guess at the number of times
check if the number is guessed
if not give a clue, the player has to guess higher or lower?
End of game
For do-until we have at Red until the function. Until does everything from the block until the condition comes after the last line has come true.
We can help do until the Red Console (red.exe on Windows computers)
- Red == 0.5.4 == - Type HELP for starting information.
>> help save until
USAGE:
until body
DESCRIPTION:
Evaluates body until it is true.
until or type: Native!
ARGUMENTS:
body [block!]
Refinements:
So until is followed by a block !. And a block! is nothing more or less than a block code. And that’s all the code inside a pair of square brackets state. Ie until []. We write our code.
until [ do-here-the-things-you-want-to-do and-perhaps-have-you-there-very-few-rules-for-needed until-the-line-where-is ]
So we have to keep going and how many guesses were made.
times-guessed: 0 number-guessed false
If the player wants to quit, we want to also keep in mind that, right?
So there are 3 reasons why our program to stop. Every reason in itself is enough reason to stop.
We have a Red ANY function of more than 1 conditions return or 1 or more true. Put each condition in a neat line.
Consulting the Red help for ANY
red>> help any USAGE: any conds DESCRIPTION: Evaluates, returning at the first that is true. any is of type: native! ARGUMENTS: conds [block!] REFINEMENTS:
So ANY displays true on the very first condition met that is true and false otherwise.
We get now
any [times-guessed > 20
response = “stop”
number-guessed
]
Together, this will become
times-guessed: 0 number-guessed: false until [ response: ask "Give a number (from 1 up to 99 or stop):" times-guessed: times-guessed + 1 is-the-number-guessed? any [times-guessed > 20 response = "stop" number-guessed ] ]
The piece is-the-number-guessed? We still have to fill.
The secret number is then equal to the answer. If so then answer guessed.
As in other languages Red also has an IF
if secret number = response [number-guessed: true]
As you probably know from other languages there is often the possibility to do something if the test is not true. They use ELSE. It may seem weird at first but Red does not have IF THEN ELSE.
We use EITHER followed by two blocks of code. Don’t come in and ask for IF THEN ELSE because when you get more accustomed to EITHER you will notice having this difference makes your code better readable.
So we fill in all the pieces, we get a nice program
Red [ file: %guess.red Author: "I" Description: { This program selects a number from 1 to 99, you have to guess. } ] #include %../../red/environment/console/input.red secret-number: random 99 times-guessed: 0 number-guessed: false ready: false until [ response: ask "Give a number (from 1 up to 99 or stop):" times-guessed: times-guessed + 1 if secret-number = response [ number-guessed: true print ["Hooray you have guessed the number in" times-guessed "times"] ] if secret-number < response [ print ["The secret number is less than" answer] ] if secret-number > response [ print ["The secret number is greater than" answer] ] any [times-guessed > 20 answer = "stop" number-guessed] ]
But ….
We still have to compile the program!
To do that we start rebol core now.
We go to the directory mydir with CD / red
cd red
And there
do red.r%
and we compile our program (how it must be done in English extensively here: http://www.red-lang.org/2015/04/053-faster-compilation-and-extended.html)
rc "-c ../project/guess/guess.red"
When this is done, there is a program made so that you can perform. In Windows you must open a cmd.exe (DOS shell) herein, go to the folder (myfolder / red) with the program guess.exe
We test to see if the program stops when we stop typing. Okay it’s fun. Now we will test again and give a number. Oops! An error!
Everything we type is obviously a string and not a number. And if we “12” compared, for example 6, we compare apples and pears. We must make a number of our imports. Fortunately, it can be simple with the LOAD function.
Board Number: load response
and we test whether we actually have a number of
integer? guess-number
If so we can continue.
How do we make the program better?
Rather than answer = “stop” we do to stop a flag, we can call this example “stop” but “ready” is also an option. When we start we are not ready yet, so we take a line with
ready?: false
Now we can also help the player “s” and “Stop” also known as “stop” to conceive
if any [answer = "s" answer = "stop" answer = "stop"] [ ready?: true ]
If you play the game often you will find that the program always chooses the same number. We still have the random generator shake. That must be with the / seed refinement of the random function.
random/seed 9999
The number by which the seed is made should preferably themselves not always be the same. The time stamp for this is a good option. Unfortunately, Red still has no time can be retrieved.
Furthermore, the program crashes at the end when you’re ready. I’ve already reported to the developers, but it will in due course solve itself as official Red input / output supported.
The latter two problems are solved by using the C-library binding by Kaj de Vos, and especially the ANSI bind.
You can find these binding http://red.esperconsultancy.nl/Red-test/dir?ci=tip
Tip!: this site contains Red distributions for all kinds of OS’es complete with the bindings!
You have to download a fossil program from the fossil site fossil http://fossil-scm.org/index.html/doc/trunk/www/index.wiki or rather from https://www.fossil-scm.org/download.html
You put it in the folder with your rebol program and from the console rebol you
do download.r%
Put all your new binding files in a folder named bind within the folder Mymap.
Now you replace the #include line for the next
#include %../bind/C-library/ANSI.red
and we determine the time in seconds with now / precise we have immediately thanks to the bond.
time: remainder now/precise 10000; is not really necessary but roomy enough for our program.
random/seed time
For completeness, the whole program
(Do not pay attention to the alignment, indentation should be tabbed with 4 spaces)
Red [ file: %guess.red Author: "I" Description: { This program selects a number from 1 to 99, you have to guess. } ] ; A comment is all that comes after a semicolon, unless the semicolon ; is in a text in quotes. ; Add the functions ASK NOW and increases with this binding #include %../bind/C-library/ANSI.red ; Set Reset time: remainder now/precise 10000 random/seed time secret-number: random 99 print secret-number; This you must change later into a comment! times-guessed: 0 ready?: false until [ response: ask "Give a number (from 1 up to 99 or stop):" if any [response = "s" response = "S" response = "stop" response = "STOP" response = "stop" response = "STOP"] [ print "You want to stop, so until next time!" ready?: true ] times-guessed: times-guessed + 1 if not ready? [ guess-number: load response either integer! = type? guess-number [ if secret number = guess-number [ ready?: true print ["Hooray! You have guessed the number in" times-guessed "times"] ] if not ready? [ if times-guessed > 19 [ ready?: true print ["Sorry. You've guessed 20 times, the number was" secret-number] ] ] if not ready? [ if secret-number < guess-numberr [ print ["The secret number is less than" guess-number] ] if secret-number > guess-number [ print ["The secret number is greater than" guess-number] ] ] ] [ either times-guessed > 19 [ ready?: true print ["Sorry. You've guessed 20 times, the number was" secret-number] ] [ print "I did not understand you, try again please." ] ] ] Ready? ] ; end of program print "Goodbye!"
There is plenty left to add for testing input being larger than 0 and smaller than 100 and add smart responses for jokers answers like pi, or written out numbers.
I hope I helped you a little to get you started with Red.
Beginners don’t need Rebol or install from source!
Just point to the download page on the official website http://www.red-lang.org/p/download.html
There one can download either a stable or a bleeding edge nightly built binary.
Hi Arie, thank you for your comment. That would be okay for trying your script, but the Red compiler is still written in plain ‘old’ R2 Rebol, so if you want to compile your program you will need Rebol. Furthermore if you want to use Kaj’s bindings with Red then you do it like I showed or download the complete distribution provided by Kaj.
Perhaps beginners could mean absolute beginners or programmers starting with programming in Red or anything in between 😉