Ruby Warrior - learn to program AI the fun way

(Daniel Hollands) #1

Following on from last night’s WMRUG meetup, in which we were introduced to the game Ruby Warrior, I figured it might be a good idea to use this thread to share what we did, how successful it was, and to provide encouragement for those of us who maybe didn’t do as well as others :frowning:

For anyone that missed it, in Ruby Warrior you…

play as a warrior climbing a tall tower to reach the precious Ruby at the top level. On each floor you need to write a Ruby script to instruct the warrior to battle enemies, rescue captives, and reach the stairs. You have some idea of what each floor contains, but you never know for certain what will happen. You must give the Warrior enough artificial intelligence up-front to find his own way.

If you’re interested in playing, you have the option of installing the Gem and running it locally, or you can play the web-based edition which features graphics and sound. (I believe the gameplay of them both is exactly the same, just the way it’s presented is different, and as such, any code you write for one should work exactly the same in the other).

Strategy sandbox MMO game for programmers
(Daniel Hollands) #2

Anyway, to get us started, I’ve been playing the web-based one at work, and have completed level 3. This has been pretty easy, seeing as we did level 4 last night - the difference being that I wrote (and even understood) the code this time, so I’m feeling quite proud of myself for that :smile:

This is what I have so far:

I think level 4 is going to be a bit of a challenge (it was last night), but I have hope that I can beat it (and do so without a bug that caused our warrior to keep going backwards)

watch this space

(Nick Goodall) #3

+1, I’ll be carrying on with this; it’s a great, fun way to hone coding and logic skills!

(Stuart Langridge) #4

What a clever idea! Although I wish there were JavaScript Warrior, since I hate Ruby. Still, got to level 3 before I got annoyed with having to look up variable syntax again :slight_smile:

(Stuart Langridge) #5

Hahahahaha :slight_smile:

(Daniel Hollands) #6

I love that this exists - and of course there’s no reason why it should’t have been ported to many other languages (although I doubt we’ll ever get a PHP one).

I do have another programming game which you might be interested in, it supports 6 different languages (none of which are Ruby)… I’ll be making a new thread about it a bit later today, so keep your eyes open for it.

(Daniel Hollands) #7

I’d be keen on seeing the code you’ve got.

I plan on keeping the gist linked to above updated with any enhancements I make to the class (I love gists).

(Nick Goodall) #8

I’m very interested to see!

Think I may as well create a gist then! Although, my code is in the ‘green’ stage (read: messy), I have yet to refactor it…

(Daniel Hollands) #9

I’ll post it later :wink:

That’s what I like about gists - they’re mini Git repos, meaning you can make changes to the original gist, and it’ll keep track of all the revisions (you can even clone them down to your local machine for said changes).

(Nick Goodall) #10

It’s about time I did this (at 1:30 on a Sunday morning…): my Ruby Warrior gist; I won’t be rushing to complete levels, but want decent-looking code.

(Daniel Hollands) #11

My code for level 5 is a lot longer (and probably more confusing) than yours. Hhmm, I shall have to take another look at it.

(Daniel Hollands) #12

Just comparing our code, @nick. This is your score:

Turns: 28
Level Score: 78
Time Bonus: 17
Clear Bonus: 19
Total Score: 163 + 114 = 277

And this is mine:

Turns: 31
Level Score: 78
Time Bonus: 14
Clear Bonus: 18
Total Score: 163 + 110 = 273

I think the main difference is that I’ve set mine to fully recover to 20 HP when able to, where as you’re only healing up to 15 - and it seems to be worth a couple more points, because you’re able to save a couple of turns.

It’ll be interesting to see what we’re both like in a couple of levels :smile:

(Nick Goodall) #13

I’ll be keeping a close eye on yours, @LimeBlast :wink:

After completing level six, here’s my score:

Level Score: 58
Time Bonus: 18
Clear Bonus: 15
Total Score: 299 + 91 = 390

Unfortunately, I think warrior.listen could prevent the unnecessary heal when all enemies are dead, but undefined method 'listen'causes problems…

(Daniel Hollands) #14

I’ve created a git repo to track the changes I make to the edition on my laptop - but I think I’ll probably be playing the browser-based on, because I’ll be able to do it in my downtime at work. I’ve not attempted level 6 yet, I will soon :smile:

(Rob Williams) #15

This is how far @nick and myself got at the end of meetup.

I branched off and managed to get through beginner epic mode.

Changes on each level are here:
& the final result:

It will be interesting to see if this pattern can be applied to the intermediate levels as well.

(Nick Goodall) #16

That looks far too complicated for me… I completed level 9 and failed on epic, but level after level I trimmed seemingly-unnecessary code, so I’m working through again…

@robwilliams I’m tempted to reverse-engineer it… Where does the delegate file come in?

(Rob Williams) #17

My intention wasn’t to complicate things, just to show what makes Ruby and using objects so powerful. It does need cleaning up (a lot :smile: ).

The file ‘delegate’ is part of the ruby standard lib. It allows you to decorate an object by extending from the SimpleDelegator class.

I want to ask the Warrior if he is ‘being_attacked?’ rather than the Player ‘being_attacked?(warrior)’.

Maybe you could add a decorator to your existing code and see if it makes it easier to read and change.

(Nick Goodall) #18

Oh awesome, that explains so much! Thank you, I’ll see what I can do…

(Daniel Hollands) #19

Here’s another JavaScript flavoured version: Warrior.js.