Since I've got some time on my hands (long-term sick leave, chronic pain, yay! 😝), I decided to learn some more programming languages. To make the endeavor a little more interesting, I also decided to make it into a series about computer programming. Because, well, why not. And because I miss work plus my doctor says I have to use my fingers even though my hands hurt.
A little background
Programming and software development, or the art of making a computer do stuff, is neither magic or reserved for sure me special group of tech-savvy geeks. Anybody can do it, and in a gazillion different ways at that.
This is primarily a guide to programming. In this guide, we are going to approach a number of different programming languages, paradigms and styles to solve the same thing: we’re going to build ”Game of Life” while adhering to the recommended styles and leveraging the different perks in different programming languages.
First things first
What is Game of Life?
Game of Life, or
Conway's Game of Life or simply
Life, is a zero-player game and cellular automata that dictates a set of rules for survival, birth and death of artificial
cells on a two-dimensional grid. These rules are quite simple:
- Any live cell with fewer than two live neighbours dies, as if caused by under-population.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overcrowding.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
With these rules applied to each cell, the
game moves forward in time by inspecting each cell on the current
board, then applying the rules to each cell on the board to form the next step in time. This is repeated indefinitely or to a given maximum number of generations.
By performing these iteration on a board and displaying each iteration in sequence, you get an animated presentation with different shapes forming, such as this
If you already have a development environment in place, feel free to skip right past this section.
This section is for you if you have little or no previous experience with computer programming. It’s meant to give you a (very) brief intro to what you will need and some tools that might come in handy. We will assume you have these tools in place (or something similar) for the rest of the series.
Computer programming is writing instructions (code) in a file (source) that can later be compiled or interpreted2.
This means that you need an application to write code in and save to files. We’re going to use the
Atom editor by Github since it’s free and runs on Mac, Windows and Linux. You can download Atom from here: https://atom.io/
So go ahead, download and install it now.
A common way to work which has probably been pretty much the same although tools vary, is to write your code in your editor and then compile, test and run it in a terminal. This because the earliest computers didn’t have anything more than a terminal.
Thanks to this, among other things, most computers still have a terminal if you dig around a little. It differs a little from one operating system to the next though.
We won’t cover any customisations here, but for the sake of stuff-to-do-someday, here are a couple of links for you for a rainy day:
- Homebrew -
The missing package manager for Mac OS X: http://brew.sh/ (Mac only)
- Chocolatey -
Machine Package Manager, somewhat like apt-get, but built with Windows in mind: https://chocolatey.org/ (Windows only)
Terminal app found in
Applications/Utilities (easiest way to find it is probably to just search for
Terminal with Spotlight).
It’s called the ”Command Prompt” and can be found in the depths of the Start Menu, or you can just run it by pressing Win+R and enter
HOWEVER, the standard
cmd is a bit clumsy so we’re going to go with another one called
Cmder available from here: http://cmder.net/. It’s free and generally great. So go download that. This one does not come with an installer, it’s ”portable” in that it runs from wherever you put it. So make a folder at, say,
c:\tools\cmder and put it there. Then make a shortcut for starting it on your desktop or in the task bar.
Whichever Linux distro you’ve got installed, you have a terminal somewhere. If it’s Ubuntu, then bring up Unity and search for
This is starting to become a long post but I have a couple of things I want to do before closing up for today. We're going to choose a programming language and make sure I've got everything I need for it.
So, for the first programming language, I'm going to choose something that I'm already familiar with and that is pretty easy to get started with: Ruby.
Why Ruby then? Ruby is an object-oriented, interpreted, imperative programming language that's fairly easy to get started with even with little previous experience.
Ruby is also very mature, at about the same age as the Java programming language (they've both been around since somewhere in the mid-nineties), with a strong community of developers working to make it better, faster, more stable, more versatile etc.
You can read all about ruby on the official website: https://ruby-lang.org
So there you go, we're going to build
Game of Life with Ruby.
Before we get around to it, we'll need to install Ruby, this differs a little from one operating system to the next.
On Mac, you most likely have Ruby tucked away somewhere. This will do fine for us. To see if you have it, start the terminal and type
ruby -v. This should look something like this:
$ ruby -v ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]
The version might be something different, it depends on what version of Mac OS X you've got installed.
Windows doesn't come with Ruby so you will have to install it. Go to http://rubyinstaller.org/ and download the latest version. Run the installer and make sure to select
Add Ruby to PATH (or similar), this way your ruby installation will be available in your Command Prompt (or Cmder-window). This should look something like this:
C:\Users\jan>ruby -v ruby 2.2.2p95 (2015-04-13 revision 50295) [x64-mingw32]
You may or may not have Ruby in your Linux installation. It's fairly easy to install though, with Ubuntu or Debian you can do:
$ apt-get install ruby
in a Terminal to get it.
Now we have everything in place to actually start programming! We've got an editor, we know where and how to find and run a terminal and we've installed Ruby (in the case there wasn't one installed already). That's all for now, in the next post we're going to start doing some actual programming!
Go ahead and read the next episode here: Episode 2: Ruby
Source Wikimedia Commons, BY-SA 3.0, see https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life#/media/File:Gospers_glider_gun.gif ↩
compiled means that your code is transformed into a format that’s closer to what your computer actually understands. This generates runnable program files that you can run on your computer as they are, without further need of your source code. Interpreted means that your source is fed through an interpreter in order to run, this will not create runnable program files so your source code is required every time you want to run your program. ↩