I wrote my first computer game

battleshipsIn a long ago post I wrote about learning computer programming, and how I think it’s a good thing for everyone to explore at least a little. I finished my first real program the other day. It’s a very simple game. I call it Battleship (no relation to the board game).

Here’s how it works:

  • You select a famous battleship from a list (stats are different for each ship)
  • The program randomly selects an opponent
  • Your ship “fights” the opponent in a series of text-based combat rounds (I haven’t learned anything to do with graphics yet)
  • The combat module uses a random number generator to determine the probability of a “hit” during each combat round.
  • If the random number generator registers a “hit,” the code divides the “attack” rating of the shooter by the “defense” rating of the defender to determine the amount of damage.
  • Once a ship reaches 100% damage the event triggers a change in a Boolean value, which in turn triggers the end of the battle and announcement of the winner.
  • Finally, just before exit a pithy bit of text appears describing the outcome

Clearly this isn’t exactly going to set the world on fire. Still, it was a valuable learning experience. I really enjoyed coming up with creative solutions for the problems that cropped up along the way. More importantly, as I worked on Battleship I began to see some of the same principles at work in (much) more complex mainstream games. When you strip away all the fanciest features every videogame in existence boils down to a bit of code running on a loop.

I don’t have nearly as much time or energy right now as I’d like to code. I’ve started sketching out a plan for a new text-based game – one that people might actually enjoy playing. I will work on it as time permits. When I finish this next game I hope to put it online for all to enjoy.

Everyone should learn computer programming (to some extent)

I think everyone should learn a bit of computer programming. Computers and computer-like devices (tablets, smartphones) are just too important to our everyday lives for us to bumble around treating them like black boxes with incomprehensible innards. I recently started to learn a bit of basic programming myself. While Python is a pretty basic language and I am clearly a beginner, it’s already given me some insight into what the hell it is my laptop/phone is doing when I load up a program or app. It’s also helped me get a batter handle on the technology-related aspects of my fiction writing.

Here are the two beginner resources I would recommend if you want to learn to code:

Another great thing about learning coding and programming is there are plenty of helpful folks out there willing to share their expertise. This means there are a whole slew of free resources available on the web in addition to the two listed above. Give it a try. You won’t be disappointed.

Who knows, maybe it’ll help you start a tech company that you can sell for billions of dollars despite running an operating loss (oops! digression…).

Learning Code the Hard Way

I like learning things. I mean, really like learning things, to the point of masochism (you have to be to attempt the CFA exams). Lately I have been fascinated by computer programming — generally the nuts and bolts of what makes the electronic would go round. As such I’m trying a crash course in coding. So far I am relying on Learn Python the Hard Way, by Zed Shaw.

It’s a whole course in coding, available online for free (if you prefer you can pay for an ebook version or instructional videos).

In his preface, Shaw describes the program thusly:

This simple book is meant to get you started in programming. The title says it’s the hard way to learn to write code but it’s actually not. It’s only the “hard” way because it uses a technique called instruction. Instruction is where I tell you to do a sequence of controlled exercises designed to build a skill through repetition. This technique works very well with beginners who know nothing and need to acquire basic skills before they can understand more complex topics. It’s used in everything from martial arts to music to even basic math and reading skills.

This book instructs you in Python by slowly building and establishing skills through techniques like practice and memorization, then applying them to increasingly difficult problems. By the end of the book you will have the tools needed to begin learning more complex programming topics. I like to tell people that my book gives you your “programming black belt.” What this means is that you know the basics well enough to now start learning programming.

So far I’ve done about a third of the exercises and I’m really enjoying myself. If you have any interest in computers you would do well to check it out.