The following is a guest post by Brendan Manley and originally appeared on his blog. Brendan is currently in the Ruby-003 class at The Flatiron School. You can follow him on Twitter here.
No, not like in the Matrix. Well, kind of. Looking at code is very new to me. Before April ‘13, the only lines of code I had ever seen probably were in the Matrix. Those who know me IRL, know that this has changed drastically. Now, code refuses to share my field of vision with ‘non-essentials’; like the faces of loved ones or the inside of my eyelids.
And the more time I spend looking at code, the more I see it (or at least try to see it). For example, it is happening right now. In fact, I was so curious about the code that forged this site I had to pause for a moment and check out the page source. But this is merely one of the more conventional examples of my new ocular lens. The other day I found myself daydreaming about coding the wind. Laugh if you must! However, wind simulation software is just out of my scope, not out of scope.
Similarly, there is a game I’ve been playing, whenever I’ve been on the subway over the past two weeks, that I have been softly trying to mentally code. Most of the time, I am actually engaged in the game as these thoughts are being rolled out. Thus, no real progress has been made. Game play is simple. There’s a battlefield divided up into seven parallel lines, and as soon as the match begins you and your opponent begin dropping troops on these lines. Once a troop has been spawned, it tries to make its way across the battlefield. If it encounters an enemy soldier coming from the opposite direction, they battle and the winner continues to advance. The outcome of each duel can generally be predicted, as there are different types of troops; the attack and defense of each varies. Ultimately, the purpose of the game is to get more troops across the battlefield than your opponent.
Simple enough. Now I know that I could not write this game. Moreover, I have absolutely no idea how I would even begin to create all of the little soldiers and make them swing their swords, etc. However, I do think I could write a shadow or make-shift version. Over the past few weeks, Avi has emphasized the value of writing pseudo code before writing code. The point is to outline what the program should do, before translating it into proper code.
So, let’s write some pseudo code for my version of Warlords (at least I think that’s the title of the game):
#I need two soldiers hashes, that will represent the available units of each players
#Both hashes should have a key for every unit type, the value of each of these keys should be a nested hash with two key-value pairs (e.g.{ :attack => 8, :defense => 22 })
#The battlefield should be represented by a hash with seven nested hashes within it; one nested hash for each lane (e.g. lanes = { :lane1 => {}…})
#Each lane key within the lanes hash, should accept two keys. Each key will belong to a soldier, and the value each soldier will be a hash containing its attack and defense.
#Only two soldiers should be allowed in a hash at a time (this deviates from the game I’m trying to mimic. However, if I was actually writing this program, I would try to get it to run this way first)
#Whenever two soldiers are in a lane together, the attack of each of these units will be subtracted from the others defense (this arithmetic will be done during an iteration through the lanes hash)
#If a unit’s defense is brought down to zero, it is removed from the lane. Elsif, it remains in the lane with its altered defense total.
#After remaining in a lane for x iterations through the lanes hash, a soldier is removed from this hash and that soldier’s controller is given a point by incrementing the value of a ‘player_points_z_variable’.
#The program will have a standing command, that a player wins once his/her point variable reaches a certain value
#To make things harder, units can be given spawn counters. This will be a new key within soldiers (along with :attack and :defense). If a unit’s spawn counter is 2, then the lanes hash must be iterated through twice, before the unit with the spawn counter of 2 can be put into the lanes hash again.
Now the fun starts. In my next post, I’ll get this thing working. Stay tuned.