Again, it's not the type of program NaaLaa has been designed for, but it's one from my long list of simple programs I test every single language I try with. Some the members here may remember the word count challenge we had on basicprogramming.org. N6 was pretty useless at this task, but N7 is a completely different beast. I was impressed by how easy (thanx to tables, the split function and the free val procedure) was to write this program, considering the fact that NaaLaa doesn't have sorting algorithms built-in.
The task is to read a text file, remove all non-word characters, count all words, count unique words, count how many times each word appears in the text, sort the words by occurrence an save the result to the file. And here is the code:
Code:
open file 1, "Hamlet.txt"
whole_text = ""
x = freadc(1)
while x
if x < 48 or (x > 57 and x < 65) or (x > 90 and x < 97) or x > 122
whole_text = whole_text + chr(32)
elseif x > 64 and x < 91 then
whole_text = whole_text + chr(x + 32)
else
whole_text = whole_text + chr(x)
endif
x = freadc(1)
wend
all_words = split(whole_text, " ")
words_number = 0
unique_words = []
foreach n in all_words
if len(n) > 1 or n = "a" or n ="i"
unique_words[n] = unique_words[n] + 1
words_number = words_number + 1
endif
next
create file 2, "Words.txt"
wln file 2, "All words - " + words_number
wln file 2, "Unique words - " + sizeof(unique_words)
wln file 2, ""
y = 1
while sizeof(unique_words)
foreach a, b in unique_words
if b = y then
wln file 2, a + " - " + b
endif
next
free val unique_words, y
y = y + 1
wend
OK. And now let's move to graphics and games. I won't bore you with this kind of programs, anymore.
OK. I know this is not the kind of program NaaLaa has been created for, but I use it to test the speed of any language I try. Well, NaaLaa is doing a pretty good job here and runing this example as fast as other fast interpreters.
Code:
n = 35001
a = 10000
d = 0
k = 0
g = 0
l = 0
f = []
for x = 0 to n
f[x] = 2000
next
for c = n to 14 step -14
d = 0
for b = c to 1 step -1
d = d * b
g = b * 2 - 1
d = d + f[b] * a
f[b] = d % g
d = floor(d / g)
k = l + floor(d / a)
next
k2 = right("0000" + k, 4)
write k
l = d % a
next
EDIT: On my machine it takes about 1 min. to complete the task and this is the speed I'm expecting from an interpreted language. Python is slightly faster, while Perl being 25% faster than Python. Tcl is 10+ times slower than NaaLaa. It's worth to remember that NaaLaa runs through Wine, while Python, Perl and Tcl run natively.
Here is the Bubble Universe example. It seems that alpha element doesn't affect drawing single pixels:
Code:
set window "Bubble Universe", 512, 512
r = 6.283185307179586 / 235
mw = 512
mh = 512
hw = mw / 2
hh = mh / 2
x = 0
v = 0
u = 0
t = 0
k = rnd(190) + 64
set redraw 0
while 1
set color 0, 0, 0, 255
draw rect 0, 0, 512, 512, 1
for i = 0 to 255
for j= 0 to 255
u = sin(i + v) + sin(r * i + x)
v = cos(i + v) + cos(r * i + x)
x = u + t
set color i, j, 255 - i, k
draw pixel int(hw + u * hw * 0.4), int(hh + v * hh * 0.4)
next
next
redraw
t = t + 0.01
k = rnd(190) + 64
wend
Is this a bug? I've tried draw pixel, set pixel and even both.
Recently, I have been banned from a forum and my nickname has been added to censored words, so other users can't even mention my name. So, is it OK to talk here about other languages, projects etc.?
Sorry, I must have had a temporary brain malfunction. There is already a section for discussing other languages and programming in general.
So, I've tried to run NaaLaa 7 on Linux, but didn't have much success. NED runs, but the text is overlapping. That wouldn't be a big problem (as I use Geany with everything), but more complex examples fail to compile an run due to some .dlls missing. I'm aware that NaaLaa is Windows only and the problems have more to do with Wine than NaaLaa itself, but I wonder why much more complex applications run under Wine and NaaLaa doesn't.
Well, it has taken until 4th of February, for us to get what I call a warm Melbourne summer's day... A very nice 38C... at last... It is rare that we get hot days, 45C+, but it does happen... How warm does it get for other Forum members?
(Well, the forum IS listed as "Everything Else"... I suppose the weather would be included in that category... Moo Ha Ha Ha)
The game starts on the title screen by pressing the return key to start, the "p" key to pause, the arrow keys to move the ship and the "z" key to shoot. You have to shoot the central square of the enemies to destroy them.
This comment is for Marcus, the game has an error that I have not been able to solve because I don't know what the error is, I have left you a screenshot with the error.
The game sometimes works well and other times the error occurs, it usually happens more times when you beat the game and start a second game again, although it can also happen in the first game and there are times when it doesn't happen and you can beat the game without let nothing happen.
I don't remember if I ever posted something about the json library in the old forum - the one that crashed. But it's very easy to use and can be used for saving and loading most n7 tables. You can read more about the file format here: https://en.wikipedia.org/wiki/JSON
Code:
' json_example.n7
' ---------------
' Using the json library you can turn "any" table into a json string. You can save this string to
' file and load it again to get an exact replica of the original table. So it's a very handy library
' and file format.
include "json.n7"
' Let's say you want to save the player's progress in a game.
data = []
data.lastSaved = time()
data.score = 1349
data.name = "Marcus"
data.level = 5
data.levelScores = [1439, 5131, 1486, 394]
data.position = [x: 10, y: 15]
' Create a file for saving.
f = createfile("savedata.json")
if file(f)
' Use JSON_ToString to create a json string and write it to the file.
write file f, JSON_ToString(data)
free file f
' Use JSON_FromFile (there's also JSON_FromString) to load the data into a variable.
data = JSON_FromFile("savedata.json")
' Print out the information loaded from file.
t = datetime(data.lastSaved)
pln "lastSaved = " + t.year + "-" + str(t.month, 2) + "-" + str(t.day, 2) +
" " + str(t.hour, 2) + ":" + str(t.minute, 2) + ":" + str(t.second)
pln "score = " + data.score
pln "name = " + data.name
pln "level = " + data.level
pln "levelScores[2] = " + data.levelScores[2]
pln "position = " + data.position.x + ", " + data.position.y
else
pln "Couldn't create file!"
endif
system "pause"
Edit I wrote "most tables". You can assign functions to variabels in an n7 table, and json doesn't support that ofcourse.