Lua programming

Lua is a programming language like C, but it’s a lot easier to learn. Lua is one of the simplest programming languages and was made in 1993. Me and my dad have started to learn it and we’re really enjoying experimenting with it.

We made a simple ‘guess the number’ game. Even though Lua is a lot simpler than C, it was still quite hard to create it. Firstly, the random number function didn’t work so we had to call it twice and add a seed which was, in this case, the time in seconds. The reason for this was because the random number was only random once because its starting number (seed) stayed the same each round. Next, the random number stayed the same because the seed didn’t change much when it was the time. Therefore, we had to call the random number again to make it random enough (nothing is totally random).

It looked like this (I have included a screenshot from SciTe which is a Lua editor because WordPress cannot highlight Lua code properly):

guess the number lua code screenshot

--[[  Guess the number game, by Jeevan

computer thinks of a number between 1 and arg[1]
if no arg then 1 to 100
computer asks user "guess my number"
it then tells the user whether the computer's number is higher or lower (or equal) than the guess
once guessed computer tells user the number of guesses

]]--

-- variables
local maxNumber, theNumber, userGuess, numTries

numTries = 0

-- check arguments
if (#arg == 1)
then
maxNumber = tonumber(arg[1])
else
maxNumber = 100
end
-- check we actually got a number
if (maxNumber == nil or maxNumber <= 1 or maxNumber > 1000000)
then
print("That was not a valid number (for this game), using default of 100")
maxNumber = 100
end

math.randomseed(os.time())
theNumber = math.random()
theNumber = math.random(maxNumber) -- the first random() always comes back the same or is predictable even with randomseed, so we use the 2nd one
print("I thought of a number between 1 and " .. maxNumber .. "; can you guess it?")

repeat
userGuess = tonumber(io.read())
numTries = numTries + 1

if (userGuess == nil or userGuess < 1 or userGuess > maxNumber) -- check we actually got a number
then
print("That was not a valid number (but it does count as a go so be careful fat fingers).")
elseif (userGuess < theNumber)
then
print("Higher")
elseif (userGuess > theNumber)
then
print("Lower")
end

if (numTries == 5 or numTries >= 10)
then
print ("Come on, you're rubbish at this game.")
end

until userGuess == theNumber

print("You got it in " .. numTries .. " goes.")

 

Scratch

Scratch is an object oriented programming language that is easy to learn. Everything is done graphically. I have made 2 projects already. One is a multi-player game called “The Chase” and the other is a music making app called “Piano on Stage”.

It is easy and fun but can be quite repetitive at times. For instance, I had to record all the piano sounds in LMMS and export them individually as wav files. Then I had to assign the sounds to each keyboard key sprite. Finally I had to add the code to play the sound when the key is pressed. Clicking on the laptop sprite changes the sound from piano to special effects.

 

The Chase - a screenshot showing the Scratch project

The Chase - a screenshot showing the Scratch project

 

Piano on Stage - a screenshot showing the Scratch project

Piano on Stage - a screenshot showing the Scratch project

Edubuntu

A few months ago I used to have GNU (GNU not UNIX) / Linux Edubuntu 10.11 Maverick Meerkat on my mum’s old computer that I used. About a month later it started to crash a lot. I do not know what was causing it to crash but it was probably Adobe Flash Player in Mozilla Firefox. Now I share a computer with my mum and my sister that already has her own computer. I have Windows 7 now which is good, but not as good as Linux so my dad set up a Virtual Box with Linux. I tried to install Edubuntu 11.04 Natty Narwhal alpha version because I wanted to check for bugs but it had errors when I installed it. I guess I have to wait till the full version is out. So now I installed 10.11 in the Virtual Box and I use it a lot. I have got lots of free games on it and one is a football management game and is quite good but not as good as SWOS or Championship Manager. Software Centre is similar to App Store which is great. Windows doesn’t have anything like it.

Also, I have been learning to use commands in the terminal. The first commands my dad told me were ls, cd, pwd and grep. Linux commands can have options to change what they do. So you can do “ls -lFart” to have details of all the files in the directory and most recent files at the bottom of the list”. Another one I made is “ls -pooFart”. Try it for yourself.

🙂   😎   o-{

My first Excel formula

Me and my Dad wanted a spreadsheet that lists the date and the number of house points I got on that date.  My Dad wrote most of the instructions here but I helped find out what to do at each stage and also I can recreate this spreadsheet whenever I want to.

Here’s our first attempt.

Date House Points
01 February 2010 1
02 February 2010 2
03 February 2010 1
04 February 2010 8
05 February 2010 3
06 February 2010 0
07 February 2010 0
08 February 2010 2
09 February 2010 1
10 February 2010 3

This is how we did the dates:

A
1 01 February 2010
2 = A1+1
3 = A2+1

And so on…

But this meant we had all the weekends included and we would be putting zeroes in the weekends.  This is ok for working out the total number of house points but it massively lowers the average as we are always counting weekends but we always have zeroes there.  For example, in 15 school days I got 30 house points.  The average is therefore 30/15 = 2.  But in our sheet we would have 3 weekends included so our average would be 30/21 = 1.43.

It is clear that we don’t want to include weekends.  This is what we did next.

In column A we start off with a date in cell A1.  Cells A2 and below will be 1 day later than the previous cell in the column.  But we only want weekdays.  Here is the formula:

A
1 01 February 2010
2 =IF(WEEKDAY(A1+1,2) =6, A1+3, A1+1)

First we need to use the WEEKDAY function.  This is defined as:

WEEKDAY(serial_number,return_type)

Return_type is a number that determines the type of return value.

Return_type Number returned
1 or omitted Numbers 1 (Sunday) through 7 (Saturday). Behaves like previous versions of Microsoft Excel.
2 Numbers 1 (Monday) through 7 (Sunday).
3 Numbers 0 (Monday) through 6 (Sunday).

We want to use return type 2 as we want Monday to Friday and as soon as we get a day number of 6 we know we have reached Saturday.  We could also use a return type of 3.  But using 1 would mean we need to change the formula to look out for 1 meaning Sunday and 7 meaning Saturday.

The formula says that if by adding 1 to the previous day we would get a Saturday then we should add 3 to the previous day and make it a Monday.  Otherwise we just add 1 because adding 1 is still a weekday.

The condition is:

WEEKDAY(A1+1,2) =6

If this is true then we use the value:

A1+3

If the condition is false we use the value:

A1+1

We then wanted to see the name of the weekday, this is easy…

A B
1 01 February 2010 =TEXT(A1,”ddd”)
2 =IF(WEEKDAY(A1+1,2) =6, A1+3, A1+1) =TEXT(A2,”ddd”)

We are showing the shorthand day name.  Our final sheet looks like this:

01 February 2010 Mon 2
02 February 2010 Tue 2
03 February 2010 Wed 1
04 February 2010 Thu 1
05 February 2010 Fri 1
08 February 2010 Mon 3
09 February 2010 Tue 2
10 February 2010 Wed 1
11 February 2010 Thu 3
12 February 2010 Fri 1
15 February 2010 Mon 3
16 February 2010 Tue 0
17 February 2010 Wed 7
18 February 2010 Thu 1
19 February 2010 Fri 2