2011 – New Year’s Resolutions

I have two, software related, New Year’s Resolutions that I will try to follow during 2011. These are “Do some Lisp Programming” and “Do some Squeak/Pharo Programming”.

In terms of Lisp I have purchased the e-book version of Land of Lisp after seeing many good reviews of it on the net. So far it has been an entertaining re-introduction to Common Lisp.

As for Squeak/Pharo, I have been re-visiting Morphic and finding it much more interesting that I remembered. I definitely intend to do some more with Morphic over the coming months.

Now to go and starting putting this into practice ….

Python Adventure Game – Finished

I have now finished my Python Adventure Game by adding some puzzles and a win algorithm. Its a very simple game but was great fun to write.

Source code is below.

#
# Barry's simple adventure game
#
 
# Room data indexs
SHORT_DESC = 0
LONG_DESC = 1
N_EXIT = 2
S_EXIT = 3
E_EXIT = 4
W_EXIT = 5
U_EXIT = 6
D_EXIT = 7
OBJECT = 8
 
# Room Data
rooms = [
['dummy'],
['the entrance', 'the entrance to a rather large looking cave', 2, 0, 0, 0, 0, 0, ''], # 1
['a coridor', 'a coridor that stretches off to the North', 3, 1, 0, 0, 0, 0, 'Lamp'], #2
['a round room', 'a round room with many exits', 7, 2, 4, 0, 20, 6, ''], #3
['a crawlway', 'a narrow east / west crawlway', 0, 0, 5, 3, 0, 0, ''], #4
['a low cave', 'a low dead end cave', 0, 0, 0, 4, 0, 0, 'Knife'], #5
['a well', 'a deep well with no way out', 0, 0, 0, 0, 0, 0, ''], #6
['a crystal cave', 'a beautifull cave with crystal walls', 8, 3, 9, 0, 0, 0, ''], #7
['an alcove', 'a small alcove in the cave wall', 0, 7, 0, 0, 0, 0, ''], #8
['a passage', 'a long east / west passage', 0, 0, 10, 7, 0, 0, ''], #9
['an anteroom', 'a richly decorated anteroom', 13, 11, 12, 9, 0, 0, ''], #10
['a wardrobe', 'a large wardrobe with space for lots of clothes', 10, 0, 0, 0, 0, 0, 'Key'], #11
['the throne room', 'a very large room with a golden throne on a dais', 0, 0, 0, 10, 0, 0, 'Crown'], #12
['the view room', 'a cave with a small hole giving a view along a green valley', 0, 10, 14, 0, 0, 0, ''], #13
['the guard room', 'a room with chairs and a table that looks like it was used as a guard room', 0, 0, 15, 13, 0, 0, ''], #14
['the ballroom', ' a magnificent ballroom with purple and gold walls', 0, 17, 18, 14, 16, 0, ''], #15
['the organ loft', 'a small room containing the controls for a large pipe organ', 0, 0, 0, 0, 0, 15, ''], #16
['the vault', 'a small airtight vault with shelves around the sides', 15, 0, 0, 0, 0, 0, 'Diamond'], #17
['the dining room', 'a long room with a huge dining table and chairs', 0, 0, 19, 15, 0, 0, ''], #18
['the kitchen', 'a large dusty kitchen', 0, 0, 0, 18, 0, 0, ''], #19
['a gallery', 'a high gallery leading off in a number of directions', 24, 22, 23, 21, 0, 3, ''], #20
['the waverfall cave', 'a large cave with a dark lake and a waterfall at the far end', 0, 0, 20, 0, 0, 0, ''], #21
['the echo cave', 'a small cave where sound echoes all around', 20, 0, 0, 0, 0, 0, ''], #22
['the assasin room', 'a small room bare room', 0, 0, 25, 20, 0, 0, 'a dead Masked Man'], #23
['a dark cave', 'a dark cave', 24, 24, 20, 24, 24, 24, ''], #24
['the exit', 'the exit of the cave', 0, 0, 0, 23, 0, 0, ''] #25
]
 
objects = ['dummy']
visits = [0]
for room in range(1, rooms.__len__()):
    objects.append(rooms[room][OBJECT])
    visits.append(0)
 
current_room = 1
visits[current_room] = 1
 
inventory = []
 
battery = 100
 
# Code
 
def describe_room(room):
    print
    if visits[room] > 1:
        print 'You are in ' + rooms[room][SHORT_DESC]
    else:
        print 'You are in ' + rooms[room][LONG_DESC]
    print
    if rooms[room][N_EXIT] != 0:
        print 'There is an exit to the North'
    if rooms[room][S_EXIT] != 0:
        print 'There is an exit to the South'
    if rooms[room][E_EXIT] != 0:
        print 'There is an exit to the East'
    if rooms[room][W_EXIT] != 0:
        print 'There is an exit to the West'
    if rooms[room][U_EXIT] != 0:
        print 'There is an exit leading upwards'
    if rooms[room][D_EXIT] != 0:
        print 'There is an exit leading downwards'
    if objects[room] != '':
        print
        print 'There is a ' + objects[room] + ' here'
    print
 
def move(exit):
    global current_room
    if rooms[current_room][exit] != 0:
        # Can't enter the vault without the key
        if ((current_room == 15) and (rooms[current_room][exit] == 17)):
            if 'Key' in inventory:
                print
                print
                print "You use the key to open the door to the vault"
                current_room = rooms[current_room][exit]
                visits[current_room] += 1
            else:
                print
                print
                print "You can't open the heavy door but it does have a key hole"
        else:
            current_room = rooms[current_room][exit]
            visits[current_room] += 1
    else:
        print
        print
        print '>>> There is no exit in that direction'
 
def north():
    move(N_EXIT)
 
def south():
    move(S_EXIT)
 
def east():
    move(E_EXIT)
 
def west():
    move(W_EXIT)
 
def up():
    move(U_EXIT)
 
def down():
    move(D_EXIT)
 
def get():
    if objects[current_room] == command.split()[1]:
        objects[current_room] = ''
        inventory.append(command.split()[1])
    else:
        print
        print "There is not a " + command.split()[1] + " here"
 
def drop():
    if command.split()[1] in inventory:
        inventory.remove(command.split()[1])
        objects[current_room] = command.split()[1]
    else:
        print
        print "You are not carrying a " + command.split()[1]
 
def inv():
    print("Inventory: ")
    for item in inventory:
        print item
 
commands = {
'north' : north,
'n' : north,
'south' : south,
's' : south,
'east' : east,
'e' : east,
'west' : west,
'w' : west,
'up' : up,
'u' : up,
'down' : down,
'd' : down,
'get' : get,
'inventory' : inv,
'inv' : inv,
'drop' : drop
}
 
# Main game loop
exit = False
while (not exit):
    print
    print
    describe_room(current_room)
    command = raw_input( "What do you want to do: " )
    if command != "":
        command_name = command.split()[0]
    else:
        command_name = "none"
    if command_name in commands.keys():
        commands[command_name]()
    else:
        print
        print
        print '>>> I don\'t understand that'
    # Check lamp
    if 'Lamp' in inventory:
        battery = battery - 1
        if battery == 50:
            print
            print
            print "The battery in your lamp is half used"
        else:
            if battery == 25:
                print
                print
                print "The battery in your lamp is three quarters used"
            else:
                if battery == 10:
                    print
                    print
                    print "The battery in your lamp is almost exhausted"
                else:
                    if battery == 0:
                        print
                        print "Your lamp has failed"
                        print "You fell in a pit and died!!"
                        print
                        exit = True
    else:
        if current_room == 3:
            print
            print
            print "It's dark here, you should find a light source or you might get hurt"
        else:
            if current_room > 3:
                print
                print "You fell in a pit and died!!"
                print
                exit = True
    # Assasin puzzle
    if current_room == 23:
        if 'Knife' in inventory:
            print
            print
            print "You killed a masked man with the knife you were carrying, well done"
        else:
            print
            print "A masked man killed you, you should have looked for a weapon!"
            print
            exit = True
    # Win check
    if current_room == 25:
        if (('Crown' in inventory) and ('Diamond' in inventory)):
            print
            print "Well done, you found all of the treasure"
            print
            exit = True
        else:
            print
            print "There is more treasure to find, go back and keep looking"
            print

wp-syntax

I have just noticed the full list of languages supported by wp-syntax. Below is the list from the wp-syntax WordPress.org Plugin Directory page.

abap, actionscript, actionscript3, ada, apache, applescript, apt_sources, asm, asp, autoit, avisynth, bash, bf, bibtex, blitzbasic, bnf, boo, c, c_mac, caddcl, cadlisp, cil, cfdg, cfm, cmake, cobol, cpp-qt, cpp, csharp, css, d, dcs, delphi, diff, div, dos, dot, eiffel, email, erlang, fo, fortran, freebasic, genero, gettext, glsl, gml, bnuplot, groovy, haskell, hq9plus, html4strict, idl, ini, inno, intercal, io, java, java5, javascript, kixtart, klonec, klonecpp, latex, lisp, locobasic, lolcode lotusformulas, lotusscript, lscript, lsl2, lua, m68k, make, matlab, mirc, modula3, mpasm, mxml, mysql, nsis, oberon2, objc, ocaml-brief, ocaml, oobas, oracle11, oracle8, pascal, per, pic16, pixelbender, perl, php-brief, php, plsql, povray, powershell, progress, prolog, properties, providex, python, qbasic, rails, rebol, reg, robots, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, tcl, teraterm, text, thinbasic, tsql, typoscript, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xml, xorg_conf, xpp, z80

This gives me some pointers to programming languages to take another (or first in some cases) look at. Should keep me busy for a while 🙂

Python Adventure Game

As well as the space invaders game mentioned in an earlier post I thought that I would also implement a textual adventure for the nostalgia value.

I sketched the map of the game on paper as shown below.

Map 1

Map 2

And then implemented the data structure in python.

# Room data indexes
SHORT_DESC = 0
LONG_DESC = 1
N_EXIT = 2
S_EXIT = 3
E_EXIT = 4
W_EXIT = 5
U_EXIT = 6
D_EXIT = 7
OBJECT = 8
 
# Room Data
rooms = [
['dummy'],
['the entrance', 'the entrance to a rather large looking cave', 2, 0, 0, 0, 0, 0, ''], # 1
['a corridor', 'a corridor that stretches off to the North', 3, 1, 0, 0, 0, 0, 'Lamp'], #2
['a round room', 'a round room with many exits', 7, 2, 4, 0, 20, 6, ''], #3
['a crawlway', 'a narrow east / west crawlway', 0, 0, 5, 3, 0, 0, ''], #4
['a low cave', 'a low dead end cave', 0, 0, 0, 4, 0, 0, 'Knife'], #5
['a well', 'a deep well with no way out', 0, 0, 0, 0, 0, 0, ''], #6
['a crystal cave', 'a beautiful cave with crystal walls', 8, 3, 9, 0, 0, 0, ''], #7
['an alcove', 'a small alcove in the cave wall', 0, 7, 0, 0, 0, 0, ''], #8
['a passage', 'a long east / west passage', 0, 0, 10, 7, 0, 0, ''], #9
['an anteroom', 'a richly decorated anteroom', 13, 11, 12, 9, 0, 0, ''], #10
['a wardrobe', 'a large wardrobe with space for lots of clothes', 10, 0, 0, 0, 0, 0, 'Key'], #11
['the throne room', 'a very large room with a golden throne on a dais', 0, 0, 0, 10, 0, 0, 'Crown'], #12
['the view room', 'a cave with a small hole giving a view along a green valley', 0, 10, 14, 0, 0, 0, ''], #13
['the guard room', 'a room with chairs and a table that looks like it was used as a guard room', 0, 0, 15, 13, 0, 0, ''], #14
['the ballroom', ' a magnificent ballroom with purple and gold walls', 0, 17, 18, 14, 16, 0, ''], #15
['the organ loft', 'a small room containing the controls for a large pipe organ', 0, 0, 0, 0, 0, 15, ''], #16
['the vault', 'a small airtight vault with shelves around the sides', 15, 0, 0, 0, 0, 0, 'Bag of Money'], #17
['the dining room', 'a long room with a huge dining table and chairs', 0, 0, 19, 15, 0, 0, ''], #18
['the kitchen', 'a large dusty kitchen', 0, 0, 0, 18, 0, 0, ''], #19
['a gallery', 'a high gallery leading off in a number of directions', 24, 22, 23, 21, 0, 0, ''], #20
['the waterfall cave', 'a large cave with a dark lake and a waterfall at the far end', 0, 0, 20, 0, 0, 0, ''], #21
['the echo cave', 'a small cave where sound echoes all around', 20, 0, 0, 0, 0, 0, ''], #22
['the assassin room', 'a small room bare room', 0, 0, 25, 20, 0, 0, 'Masked Man'], #23
['a dark cave', 'a dark cave', 24, 24, 20, 24, 24, 24, ''], #24
['the exit', 'the exit of the cave', 0, 0, 0, 23, 0, 0, ''] #25
]

The game is now mostly working. Only the puzzles left to implement.

Squeak 4.1

Squeak 4.1 was released this week. After all of the problems with the community splitting after the Pharo fork, Squeak appears to be back on track. Squeak 4.0 was a licence clean release without any new functionality but 4.1 includes all of the updates that were made during the re-licencing process.

Hightlights include a new look and feel based on the Newspeak UI, new anti-aliased fonts, the closure enabled compiler from Cog, core library improvements and modularity advances. More details can be found on www.squeak.org.

I hope that I can find the time to check it out more fully soon.

Open University

I will have less time for my own programming projects for the next few months because I have started a new Open University course. I felt that I had done enough maths after two courses and so now I am doing a software one, M255 – Object-oriented programming with Java.

This will be the first time that I have been formally taught object-oriented programming or Java. Should be interesting!

Best laid plans…

After stating that I would use Ruby for all of my personal programming I did something that changed all of that. I downloaded VisualWorks 7.7 NC and started to play with it. Smalltalk has always held a dangerous attraction for me since the fist time I saw Squeak many years ago.

The latest version of VisualWorks looks good and there is so much material around the Internet to support it both from Cincom and third parties. I am now going to port some of my personal applications to VW to see how I getting on using it as a development platform.

Ruby is my language of choice (for now!)

As could be guessed from my last post, I have chosen the language that I will concentrate on for a while. And it is Ruby!

However I have had limited time to get any coding done in the last couple of weeks. There is DIY work around the house that has to be done and now we have the #uksnow to contend with as well 🙂

I hope to get on with some serious Ruby coding soon.

Syntax highlighting test

This is a test post for the wp-syntax plug-in. Here is some simple Ruby.

# The Greeter class
class Greeter
  def initialize(name)
    @name = name.capitalize
  end
 
  def salute
    puts "Hello #{@name}!"
  end
end
 
# Create a new object
g = Greeter.new("world")
 
# Output "Hello World!"
g.salute

Hey look it works 🙂

Programming language choice

I have noticed lately that when I think about starting a programming project I get bogged down with choosing which language to implement it in. This takes up time and sometimes even leads to the project being abandoned.

I really must decide on one language and use it on a number of projects. This will increase my fluency with the language avoid the initial choosing that I find so difficult.

All I need to do now is decide on the language to use 🙁

More of this anon ….