Magic 8 ball in CX

Pavla Zakova
8 min readMay 30, 2019

or how even a total newbie can have some fun coding in CX

If you haven’t heard about CX before, you should know that CX is a programming language that has been developed as part of the Skycoin ecosystem. It has been in development for a few years now and has recently been integrated with the Skycoin blockchain. At the end of this article you can find links to the Skycoin website and telegrams groups where you can find all the information.

But maybe you’ve not only never heard of CX but you’ve also never used any other programming language. There is a lot of people who have never written a code more complex than Hello World or a simple IF statement and guess what — I’m one of them! But I’m intrigued by programming languages and their power to create something fun and useful. If only I knew how to use it I thought. And with that I decided it’s time to stop making excuses, learn and have a stab at creating something simple but fun — the Magic 8 ball!

But what do I need to know to be able to do that?

Data Types

We use computers every day and most of us probably never thought about how computers handle data and how they know what exactly we want them to do with it. All data used by a computer is stored as zeros and ones. Various combinations of these create different data types, which are then treated differently by the computer.

Data types can generally be split into primitive and complex. Primitive data types are very basic data types which can be used to create more complex ones.

CX defines these primitive data types:

byte — byte is comprised of eight bits, i.e. eight zeros and ones, therefore a byte can represent 256 different values.

bool — Boolean data type has only two possible values as they are based on the logic that any data is either True or False.

i32 — or 32-bit integers (whole numbers) are comprised of 32 bits (or 4 bytes). All integers are signed in CX, meaning that they can represent both positive and negative values and therefore i32 can represent values from −2,147,483,648 to 2,147,483,647.

i64 — or 64-bit integers are comprised of 64 bits (or 8 bytes), therefore i64 can represent values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, which frankly is more than you’ll ever need.

f32–or 32-bit/ single precision floats. These are number with decimal point that can float to allow for numbers with different magnitudes.

f64 — or 64-bit/ double precision floats. Same as f32 but allows for even bigger (and smaller) numbers.

str — string is a sequence of characters. It is an equivalent of “text” in excel. String can also contain spaces and numbers. If we assign a string data type to a number, the computer will read the number as a text.

I think that in my Magic 8 ball code I’ll only need to use a string and i32 data types! I may however need a couple complex data types as well.There is quite a few of these and so let’s just look at those we will need to make the magic ball work.

Variables — a variable is a storage location, it is assigned a specific type and has a name. The value of variables change, e.g. we can have a string type variable called Name. The program may then ask the user to enter their name and print out a welcome message. Like this:

1 package main
2 func main () {

3 str.print(“Enter your name: “)
4 var name str
5 name = read()

6 str.print(str.concat(“Hello ,”,name))

7 }

Let’s have a closer look at the code, while we’re at it. As you already know (if you don’t, check out my previous article on how to install CX on Windows and get started), each program in CX needs at least one main package and main function.

On line 3 we use a print function. There is a print variant for every primitive data type. In our case we are printing a string and so we use a str.print. If we wanted to print e.g. an integer, we would use i32.print. As the name of the function suggests, when the program is run, it will print Enter your name: on the screen.

On line 5 we define our variable. We are asking the user for their name and so we’ll call the variable name. It is best to call variables using something meaningful as in a longer code it may get confusing if we have variables called e.g. x instead of name or dog when counting cats. Sooner or later, you’ll be asking yourself, what was x again!? As mentioned before, we need to assign a data type to our variables — in our case name will be a string.

On line 6 we use function read. This function handles input from user and as it’s name suggests, it reads the name the user provided.

And finally on line 7 the program prints out the greeting. We’ve used a concatenate function, which adds together a string Hello and adds the name the user entered. Note that a string will never change and is always in inverted commas while for variables we just use their name and the computer is able to assign a value to them based on our instructions, in this case user’s input.

At this stage the lazy in me kicks in — so what if we are not asking for user’s input but we already have a list of names, do we need to declare a variable for each of them?

I’m relieved to find out we don’t — we can use Arrays — arrays are fixed length collections of elements of the same type. To declare an array you put square brackets before the type in a variable declaration, and enter the number of elements. Like this:

1 package main

2 func main () {

3 var name [3]str=[3]str{“John”,”Mary”,”Peter”}

4 str.print(name[2])

5 }

Note that the code above actually prints out Peter and not Mary as you may have expected. This is because we start at 0 and not 1.

Arrays will definitely come in handy in our magic ball code as all the answers that the ball will give us is basically an array of strings.

Functions

We’ve already come across a few functions in our codes, namely main, print, read and concatenate. Function is an independent section of code that performs a specific task. We may want to perform the task several times in our program and so it helps to define it as a function so that we can just call it by its name and don’t need to write the whole set of instructions every time. All the functions we used already are predefined by the language and so it is generally known what they’ll do but we can also define and name our own, which helps to keep our code nice and tidy.

The last bit to figure out is how we make the ball to give us a randomly picked answer every time we ask a question. Luckily, the rand function is integrated within CX as well, and so no import of additional packages is necessary.

So let’s finally start with our Magic 8 ball!

To be perfectly honest, the code below took a bit of experimenting and trial and error. But it gets to show that CX is quite intuitive as in the end I made it work! The biggest struggle was line 10 as I didn’t realise I have to repeat the [8]str after =. I still came up with a solution where I split all the answers to separate lines, like this..

var answers [8]str

answers[0] = “It is certain.”
answers[1] = “Don’t count on it.”
answers[2] = “Ask again later.”
answers[3] = “Very doubtful.”
answers[4] = “Most likely.”
answers[5] = “Not in million years.”
answers[6] = “You don’t really want to know.”
answers[7] = “Without a doubt.”

..but I didn’t like that as it made the code unnecessarily long and it was too much typing too. And so I posted my question to the CX Skycoin Telegram group and with Skyfleet’s help, we have an elegant, more compact solution:

1 package main

2 func main () {

3 str.print(“Welcome stranger! What is your name?”)

4 var name str
5 name = read()

6 str.print(str.concat(“Hello, “,name))
7 str.print(“What would you like to know?”)

8 var response str

9 response = read()

10 var answers [8]str=[8]str{“It is certain.”,”Don’t count on it.”,”Ask again later.”,”Very doubtful.”,”Most likely.”,”Not in a million years.”,”You don’t really want to know.”,”Without a doubt.”}

11 str.print(answers[i32.rand(0,8)])

12 }

So let’s briefly recap what each line is doing:

Lines 1 and 2 are our usual declarations of main package and main functions. As we already know, each CX code must have at least one of each to run.

Line 3 will print out the welcome message on the screen and will ask the user to enter their name. What we’re printing is a string, therefore we use str.print and the message is in inverted commas.

Line 4 declares a variable that we called a name. This is the input that will be entered by the user.

Line 5 instructs the program to read what the user inputted.

Line 6 will print a personalised greeting using the name that the user entered.

Line 7 will print the prompt for the user to ask their question. As in line 3, we use str.print and the message is in inverted commas.

Line 8 is similar to line 4 — it declares a new variable called response, which will be input by the user. We expect it to be a question, hence the data type assigned is a string.

Line 9 is the same as line 5 and instructs the program to read what the user inputted

Line 10 declares a variable called answers, which is an array of all the possible answers the ball will return. We have 8 possible answers, therefore we assign the length of 8 to the array, this needs to be in square brackets.

Line 11 directs the program to print an answer. If we wanted the same answer each time, e.g. It is certain, which is in the first position in the array, we would simply write str.print(answers[0]). But as we want a different answer every time and we want it random, we need to replace the 0 with a function that will return a random number each time and this is where the i32.rand function comes in. As we have 8 unique possible answers, we ask the function to pick a random number between 0 and 7 and then select the relevant answer from the array based on that.

And that’s it! The Magic 8 ball is done! So let’s see what the ball thinks, shall we?

Seems the ball is working! :)

I hope you’ve learnt something today and maybe I’ve inspired you to attempt to write a little program in CX as well. Trust me, if I can do it, you can too!

Also note that once you get better at programming, you can enter the CX Labs program and get rewarded for your coding efforts. There is a monthly contest with the current awards being $2,500 for 1st place, $1,500 for 2nd place and $300 for participation.

Don’t forget to check the links below to learn more about CX and become a part of the Skycoin community.

Skycoin on Telegram: https://t.me/Skycoin

CX: https://cx.skycoin.net/

CX Labs on Telegram: https://t.me/CXLabs

CX on Reddit: https://reddit.com/r/CX_Language

CX on GitHub: http://github.com/Skycoin/CX

CX Textbook: https://cx.skycoin.net/books/cx-programming-language & https://github.com/skycoin/cx/releases

--

--