{"id":193,"date":"2015-06-25T18:02:26","date_gmt":"2015-06-25T18:02:26","guid":{"rendered":"http:\/\/arnoldvanhofwegen.com\/blog\/?p=193"},"modified":"2015-06-25T18:43:59","modified_gmt":"2015-06-25T18:43:59","slug":"a-new-red-released-now-we-make-a-program","status":"publish","type":"post","link":"https:\/\/arnoldvanhofwegen.com\/blog\/a-new-red-released-now-we-make-a-program\/","title":{"rendered":"A new Red released, now we make a program"},"content":{"rendered":"<p>(This article was largely automatically translated)<br \/>\nIt&#8217;s been a little while ago I found time to write a nice article on this blog.<br \/>\nFortunately, there&#8217;s another good reason to get something to post.<\/p>\n<p><strong>Namely the 0.5.4 release of Red.<\/strong><\/p>\n<p>Red is a new programming language. Only in development since 2012, but now has a firm foundation on which can be built considerably.<\/p>\n<p>And besides Red already is pretty useful for small programs.<\/p>\n<p>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.<\/p>\n<p>Let&#8217;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.<\/p>\n<p>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.<\/p>\n<p><strong>What do we need?<\/strong><\/p>\n<p>First, a Rebol runtime (the Rebol program) for our computer.<br \/>\nPlease download a rebol core of the site Rebol.com<br \/>\n<a href=\"http:\/\/www.rebol.com\/download-core.html\">http:\/\/www.rebol.com\/download-core.html<\/a><br \/>\nand if you&#8217;re at a view also similar version<br \/>\n<a href=\"http:\/\/www.rebol.com\/download-view.html\">http:\/\/www.rebol.com\/download-view.html<\/a><br \/>\nRed to compile programs you have enough core version.<\/p>\n<p>Download the source code for the Red Github site<br \/>\n<a href=\"https:\/\/github.com\/red\/red\">https:\/\/github.com\/red\/red<\/a><br \/>\nAt the bottom of the right column is a button that says &#8220;Download ZIP&#8221;<br \/>\nUnzip this file to the folder where you want it.<\/p>\n<p>Also download the Red program from the official site<br \/>\n<a href=\"http:\/\/www.red-lang.org\/p\/download.html\" target=\"_blank\">http:\/\/www.red-lang.org\/p\/download.html<\/a><br \/>\nIf all is well you will see a big red button that links to the program for your operating system of your computer is.<br \/>\nYou make myfolder in a folder, for example, the name &#8220;project&#8221; herein can again create new folders for each program that you make one.<\/p>\n<p>I&#8217;m going to just assume that you now have a few folders:<\/p>\n<pre>\/Mydir\r\n\trebol-core.exe\r\n\trebol-view.exe\r\n\tred.exe\r\n\/Mydir\/project\r\n\/Mydir\/red<\/pre>\n<p>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<\/p>\n<p><strong>Let&#8217;s create a program file.<\/strong><br \/>\nThat&#8217;s just a text file that can be opened with Notepad or Notepad.<br \/>\nThe file is called &#8220;guess.red&#8221; and we put it in the folder myfolder\/project\/guess\/<\/p>\n<p>On the first line is established that this is a program Red.<\/p>\n<pre>Red []<\/pre>\n<p>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).<\/p>\n<pre>\r\nRed [\r\n\tfile: %guess.red\r\n\tAuthor: \"I\"\r\n\tDescription: {\r\n\t\tThis program selects a number from 1 to 99, you have to guess.\r\n\t}\r\n]\r\n<\/pre>\n<p>As you can see, the file is guess.red preceded by a percent sign &#8220;%&#8221;. This tells Red that it is a file, not a name of a variable or something else.<\/p>\n<p>We want to be able to ask the player to fill in a number. How do we do that? We use the ASK mode.<br \/>\nASK is still not a standard feature in Red but the console program that comes with red makes already use.<br \/>\nWe need at this time so here itself the source code for this function to give to our program. Fortunately, this is easy.<\/p>\n<pre>\r\nRed []\r\n#include %..\/..\/red\/environment\/console\/input.red\r\n<\/pre>\n<p>The dots and the slash indicate that we first from this program up a folder in the folder tree should steps.<\/p>\n<p>What should the program?<br \/>\n1. Choose a random integer from 1 to 99<br \/>\n2. Ask for a number<br \/>\n3. see if the number is guessed, or it is advisable number is larger or smaller.<br \/>\n4. If the number is guessed or the player wants to stop or attempting to stop often than not the right answer<br \/>\n5. Otherwise, we go to step 2<\/p>\n<p>We call the number that the computer chooses secret-number<br \/>\nWith a colon after secret-number we indicate that the value of secrecy number will be filled with what haunts.<br \/>\nChoosing a random number is the random function, which of course also arbitrary means in English.<\/p>\n<pre>\r\nStep 1 then translates to\r\nSecret Number: random 99\r\n<\/pre>\n<p>The rest of the program now as I write differently<br \/>\nDIY until player wants to stop or often guessed or guessed<br \/>\nask for a number<br \/>\ncount on a guess at the number of times<br \/>\ncheck if the number is guessed<br \/>\nif not give a clue, the player has to guess higher or lower?<\/p>\n<p>End of game<\/p>\n<p>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.<\/p>\n<p>We can help do until the Red Console (red.exe on Windows computers)<\/p>\n<pre>- Red == 0.5.4 == -\r\nType HELP for starting information.<\/pre>\n<p>&gt;&gt; help save until<\/p>\n<p>USAGE:<br \/>\nuntil body<\/p>\n<p>DESCRIPTION:<br \/>\nEvaluates body until it is true.<br \/>\nuntil or type: Native!<\/p>\n<p>ARGUMENTS:<br \/>\nbody [block!]<\/p>\n<p>Refinements:<\/p>\n<p>So until is followed by a block !. And a block! is nothing more or less than a block code. And that&#8217;s all the code inside a pair of square brackets state. Ie until []. We write our code.<\/p>\n<pre>until [\r\n    do-here-the-things-you-want-to-do\r\n    and-perhaps-have-you-there-very-few-rules-for-needed\r\n    until-the-line-where-is\r\n]<\/pre>\n<p>So we have to keep going and how many guesses were made.<\/p>\n<pre>\r\ntimes-guessed: 0\r\nnumber-guessed false\r\n<\/pre>\n<p>If the player wants to quit, we want to also keep in mind that, right?<br \/>\nSo there are 3 reasons why our program to stop. Every reason in itself is enough reason to stop.<br \/>\nWe have a Red ANY function of more than 1 conditions return or 1 or more true. Put each condition in a neat line.<\/p>\n<p>Consulting the Red help for ANY<\/p>\n<pre>\r\nred>> help any\r\n\r\nUSAGE:\r\n    any conds\r\n\r\nDESCRIPTION:\r\n     Evaluates, returning at the first that is true.\r\n     any is of type: native!\r\n\r\nARGUMENTS:\r\n     conds  [block!]\r\n\r\nREFINEMENTS:\r\n<\/pre>\n<p>So ANY displays true on the very first condition met that is true and false otherwise.<\/p>\n<p>We get now<br \/>\nany [times-guessed &gt; 20<br \/>\n\tresponse = &#8220;stop&#8221;<br \/>\n\tnumber-guessed<br \/>\n]<\/p>\n<p>Together, this will become<\/p>\n<pre>\r\ntimes-guessed: 0\r\nnumber-guessed: false\r\n\r\nuntil [\r\n    response: ask \"Give a number (from 1 up to 99 or stop):\"\r\n    times-guessed: times-guessed + 1\r\n\r\n    is-the-number-guessed?\r\n\r\n    any [times-guessed &gt; 20\r\n\t\tresponse = \"stop\"\r\n\t\tnumber-guessed\r\n\t]\r\n]\r\n<\/pre>\n<p>The piece is-the-number-guessed? We still have to fill.<br \/>\nThe secret number is then equal to the answer. If so then answer guessed.<br \/>\nAs in other languages Red also has an IF<br \/>\nif secret number = response [number-guessed: true]<\/p>\n<p>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.<br \/>\nWe use EITHER followed by two blocks of code. Don&#8217;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.<\/p>\n<p>So we fill in all the pieces, we get a nice program<\/p>\n<pre>Red [\r\n\tfile: %guess.red\r\n\tAuthor: \"I\"\r\n\tDescription: {\r\n\t\tThis program selects a number from 1 to 99, you have to guess.\r\n\t}\r\n]\r\n\r\n#include %..\/..\/red\/environment\/console\/input.red\r\n\r\nsecret-number: random 99\r\n\r\ntimes-guessed: 0\r\nnumber-guessed: false\r\nready: false\r\n\r\nuntil [\r\n\tresponse: ask \"Give a number (from 1 up to 99 or stop):\"\r\n\ttimes-guessed: times-guessed + 1\r\n\t\r\n\tif secret-number = response [\r\n\t\tnumber-guessed: true\r\n\t\tprint [\"Hooray you have guessed the number in\" times-guessed \"times\"]\r\n\t]\r\n\t\r\n\tif secret-number &lt; response [ \r\n\t\tprint [\"The secret number is less than\" answer] \r\n\t] \r\n\tif secret-number &gt; response [\r\n\t\tprint [\"The secret number is greater than\" answer]\r\n\t]\r\n\t\r\n\tany [times-guessed &gt; 20\r\n\t\tanswer = \"stop\"\r\n\t\tnumber-guessed]\r\n\t]\r\n<\/pre>\n<p>But &#8230;.<br \/>\nWe still have to compile the program!<\/p>\n<p>To do that we start rebol core now.<br \/>\nWe go to the directory mydir with CD \/ red<\/p>\n<pre>cd red<\/pre>\n<p>And there<\/p>\n<pre>do red.r%<\/pre>\n<p>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)<\/p>\n<pre>rc \"-c ..\/project\/guess\/guess.red\"<\/pre>\n<p>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<\/p>\n<p>We test to see if the program stops when we stop typing. Okay it&#8217;s fun. Now we will test again and give a number. Oops! An error!<\/p>\n<p>Everything we type is obviously a string and not a number. And if we &#8220;12&#8221; 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.<br \/>\nBoard Number: load response<br \/>\nand we test whether we actually have a number of<br \/>\ninteger? guess-number<br \/>\nIf so we can continue.<\/p>\n<p>How do we make the program better?<br \/>\nRather than answer = &#8220;stop&#8221; we do to stop a flag, we can call this example &#8220;stop&#8221; but &#8220;ready&#8221; is also an option. When we start we are not ready yet, so we take a line with<\/p>\n<pre>ready?: false<\/pre>\n<p>Now we can also help the player &#8220;s&#8221; and &#8220;Stop&#8221; also known as &#8220;stop&#8221; to conceive<\/p>\n<pre>if any [answer = \"s\"\r\n\tanswer = \"stop\"\r\n\tanswer = \"stop\"] [\r\n\tready?: true\r\n]\r\n<\/pre>\n<p>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.<\/p>\n<pre>random\/seed 9999<\/pre>\n<p>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.<\/p>\n<p>Furthermore, the program crashes at the end when you&#8217;re ready. I&#8217;ve already reported to the developers, but it will in due course solve itself as official Red input \/ output supported.<\/p>\n<p>The latter two problems are solved by using the C-library binding by Kaj de Vos, and especially the ANSI bind.<br \/>\nYou can find these binding http:\/\/red.esperconsultancy.nl\/Red-test\/dir?ci=tip<br \/>\n<strong>Tip!: this site contains Red distributions for all kinds of OS&#8217;es complete with the bindings!<\/strong><br \/>\nYou 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<br \/>\nYou put it in the folder with your rebol program and from the console rebol you<\/p>\n<pre>do download.r%<\/pre>\n<p>Put all your new binding files in a folder named bind within the folder Mymap.<\/p>\n<p>Now you replace the #include line for the next<\/p>\n<pre>#include %..\/bind\/C-library\/ANSI.red<\/pre>\n<p>and we determine the time in seconds with now \/ precise we have immediately thanks to the bond.<\/p>\n<p>time: remainder now\/precise 10000; is not really necessary but roomy enough for our program.<\/p>\n<pre>random\/seed time<\/pre>\n<p>For completeness, the whole program<\/p>\n<p>(Do not pay attention to the alignment, indentation should be tabbed with 4 spaces)<\/p>\n<pre>\r\nRed [\r\n\tfile: %guess.red\r\n\tAuthor: \"I\"\r\n\tDescription: {\r\n\t\tThis program selects a number from 1 to 99, you have to guess.\r\n\t}\r\n]\r\n; A comment is all that comes after a semicolon, unless the semicolon\r\n; is in a text in quotes.\r\n\r\n; Add the functions ASK NOW and increases with this binding\r\n#include %..\/bind\/C-library\/ANSI.red\r\n\r\n; Set Reset\r\ntime: remainder now\/precise 10000\r\nrandom\/seed time\r\n\r\nsecret-number: random 99\r\nprint secret-number; This you must change later into a comment!\r\n\r\ntimes-guessed: 0\r\nready?: false\r\n\r\nuntil [\r\n\tresponse: ask \"Give a number (from 1 up to 99 or stop):\"\r\n\tif any [response = \"s\"\r\n\t\tresponse = \"S\"\r\n\t\tresponse = \"stop\"\r\n\t\tresponse = \"STOP\"\r\n\t\tresponse = \"stop\"\r\n\t\tresponse = \"STOP\"] [\r\n\t\tprint \"You want to stop, so until next time!\"\r\n\t\tready?: true\r\n\t]\r\n\t\r\n\ttimes-guessed: times-guessed + 1\r\n\t\r\n\tif not ready? [\r\n\t\tguess-number: load response\r\n\t\teither integer! = type? guess-number [\r\n\t\t\r\n\t\t\tif secret number = guess-number [\r\n\t\t\t\tready?: true\r\n\t\t\t\tprint [\"Hooray! You have guessed the number in\" times-guessed \"times\"]\r\n\t\t\t]\r\n\t\t\t\r\n\t\t\tif not ready? [\r\n\t\t\t\tif times-guessed &gt; 19 [\r\n\t\t\t\t\tready?: true\r\n\t\t\t\t\tprint [\"Sorry. You've guessed 20 times, the number was\" secret-number]\r\n\t\t\t\t]\r\n\t\t\t]\r\n\t\r\n\t\t\tif not ready? [\r\n\t\t\t\tif secret-number &lt; guess-numberr [ \r\n\t\t\t\t\tprint [\"The secret number is less than\" guess-number] \r\n\t\t\t\t] \r\n\t\t\t\tif secret-number &gt; guess-number [\r\n\t\t\t\t\tprint [\"The secret number is greater than\" guess-number]\r\n\t\t\t\t]\r\n\t\t\t]\r\n\t\t] [\r\n\t\t\teither times-guessed &gt; 19 [\r\n\t\t\t\tready?: true\r\n\t\t\t\tprint [\"Sorry. You've guessed 20 times, the number was\" secret-number]\r\n\t\t\t] [\r\n\t\t\t\tprint \"I did not understand you, try again please.\"\r\n\t\t\t]\r\n\t\t]\r\n\t]\r\n\t\r\n\tReady?\r\n]\r\n\r\n; end of program\r\nprint \"Goodbye!\"\r\n<\/pre>\n<p>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. <\/p>\n<p>I hope I helped you a little to get you started with Red.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(This article was largely automatically translated) It&#8217;s been a little while ago I found time to write a nice article on this blog. Fortunately, there&#8217;s another good reason to get something to post. Namely the 0.5.4 release of Red. Red &hellip; <a href=\"https:\/\/arnoldvanhofwegen.com\/blog\/a-new-red-released-now-we-make-a-program\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"gallery","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/arnoldvanhofwegen.com\/blog\/wp-json\/wp\/v2\/posts\/193"}],"collection":[{"href":"https:\/\/arnoldvanhofwegen.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arnoldvanhofwegen.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arnoldvanhofwegen.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/arnoldvanhofwegen.com\/blog\/wp-json\/wp\/v2\/comments?post=193"}],"version-history":[{"count":11,"href":"https:\/\/arnoldvanhofwegen.com\/blog\/wp-json\/wp\/v2\/posts\/193\/revisions"}],"predecessor-version":[{"id":209,"href":"https:\/\/arnoldvanhofwegen.com\/blog\/wp-json\/wp\/v2\/posts\/193\/revisions\/209"}],"wp:attachment":[{"href":"https:\/\/arnoldvanhofwegen.com\/blog\/wp-json\/wp\/v2\/media?parent=193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arnoldvanhofwegen.com\/blog\/wp-json\/wp\/v2\/categories?post=193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arnoldvanhofwegen.com\/blog\/wp-json\/wp\/v2\/tags?post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}