Monday, December 24, 2012

Teaching a computer to play Connect Four, Part 2

In this post, I will briefly describe the Connect Four game play code that I've written to act as the base for my machine learning experimentation. At the moment, I am working with v1.0.0 of the code.

Feel free to checkout the latest version of the code or follow the progress on my GitHub page.

The basic game play part of the package consists of three main components:

Board: the game board object.
C4Bot: the player object.
main: The function used to play a game.

I will now describe each of these briefly.

Teaching a computer to play Connect Four, Part 1

Lately I've been studying Machine Learning from various sources on the web, including the great lecture series by Andrew Ng at Stanford (videoscourse website). Why? It's really cool and people are doing amazing things with it these days (Exhibit A). On top of that it should provides some useful tools to have in my toolbox as an astrophysicist.

So far I've been surprised to learn that I am already pretty well versed in many of the underlying methods of machine learning (the course has mainly involved a bunch of model fitting using Maximum Likelihood estimation to this point and I happen to know a little something about that), although I expect that once we get to the topics of "unsupervised learning" and neural networks I will start swimming in uncharted waters. Regardless, the course already has me thinking about data in different ways and considering a variety of new and interesting applications like robotics, speech-recognition, machine vision, etc.

As I go along I want to have a simple project that I can use to play around with because it's hard to truly learn something without getting some hands-on experience. Taking inspiration from Arthur Samuel's checkers-playing program (thought to be the first self-learning program), why not write an algorithm that would learn how to play a game? I could work with with checkers of course, but I don't have a checkers program lying around and am not keen on writing one. Connect Four (aka Four-In-A-Row) on the other hand is a rather simple game but should still have enough complexity to give me something to work on. OK then, Connect Four it is.

In just a few hours this past weekend I managed to build a "sandbox" for machine learning in the form of a Python based Connect Four application. On top of defining the game rules, I've also developed a database for storing information about board configurations. This is all a work in progress. I haven't come up with a satisfying solution for teaching the software how to play the game yet, but with this foundation in place I can finally start tinkering around.

In the next post, I'll describe the Connect Four software that I've written including the features designed to help write ML code. Future posts will follow my progress in developing an ML algorithm that will learn how to play the game.

Until then, I've put the project up on GitHub, so feel free to check out the code as I go along.

https://github.com/mrbell/connectfour


Sunday, July 24, 2011

Introducing Pyll Popper - A Dr. Mario clone in Python using Pygame

I've always wanted to try writing a simple computer game, but have never found the time to sit down and do it.  Part of the problem is that until recently I have not been very proficient in a suitable programming language.  Not only would I have to spend time learning game programming techniques, but I would also have to brush up on my general programming skills as well. 

Lately, I've been writing a lot of code in Python for work, and as a result I would finally consider the 'lack of general programming skills' hurdle jumped.  But is Python a suitable language?  It's certainly not a good choice of language for a game that requires high performance, e.g. a FPS, but for a 2D side-scroller or puzzle game it should be fine.  To help there is the Pygame library, whose various classes and functions will handle a lot of the dirty work that goes into manipulating graphics objects and handling I/O.

So with my new found Python proficiency, and Pygame to help me out, the time is ripe for making this project happen.  OK, so what game to code?

I've got it! A Dr. Mario clone!  Should be easy enough... simple graphics objects, simple rules, simple everything.  Plus, it's fun as hell, and Dr. Mario clones are relatively rare (at least compared to, say, Tetris clones). 

Fast forward to today.  After only a couple of Sundays worth of work I have written a bare bones, but fully playable, version of the game.  You can see a screen shot above.  Nice graphics, huh?

Now comes the hard part.  What should I call it?  I need to follow the usual convention of putting Py or Python in the name since it was written in Python.  The most obvious choice is Dr. Python, which was my first working title (as you can see in the window title of the screen shot).   Unfortunately there is a text editor that already goes by that name, so I have to try harder.  What about Python, M.D.?  Or Pyll Popper?  Yeah, that's a bit more creative.  Pyll Popper it is. 

In an upcoming series of posts I will describe the development of Pyll Popper, particularly describing how to use Pygame for such a project.  In the meantime, feel free to try the game.  It is cross-platform, or at least it should be (I've only tested it on Linux).  To play you will need to have installed python (obviously), and both the pygame and numpy libraries.  For my code there is no installation necessary.  Just unzip, make the .py file executable, and run it from the command line. 

The controls are simple.  Move a pill left or right using the arrows, and rotate using the 'z' and 'x' keys.  To pause, press 'p' (some diagnostic information will be spit out at you as well).  To quit, simple close the window or hit 'Esc'.  If you find any bugs... no wait.  When you find bugs, be a pal and let me know.

Dev. diary posts to follow...stay tuned.

Download Pyll Popper (right-click and 'Save Link As...')