AI: Implementing Score4 (Connect four) in functional and imperative languages
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
C# Updated with new results / languages (e.g. Rust!) Nov 27, 2015
C++ Don't recurse in the Makefile Oct 7, 2012
C Spans of 4 are caught as special cases, don't multiply with them Sep 21, 2012
Go Updated with new results / languages (e.g. Rust!) Nov 27, 2015
Lisp Removed CMUCL and added Rust in the benchmark results Nov 21, 2015
OCaml Using Merlin, and annotations. Jul 7, 2014
Rust An imperative Rust version that uses incremental scoring by James Mil… Nov 30, 2015
interfaces Be specific about which Python version we use. Nov 4, 2018
.gitignore Autodetect which languages to benchmark - and also test with D Sep 21, 2012
COPYING Gnu license Jul 10, 2011
Makefile Makefile typo in detecting F# compiler. Nov 4, 2018
Makefile.common Migrated site to my own server Jun 1, 2016 Now works for both Python2 and Python3 Aug 21, 2012

This is the code accompanying my blog post about implementing the game of Score4 (also known as Connect4). The goal of the game is to form horizontal, vertical or diagonal series of 4 of your chips (green). The computer tries to do the same, using red chips.

I coded implementations of the game engine in C, C++, OCaml, F#, C#, Lisp and Rust, using both functional and imperative styles of coding. Ports also came from all over the Web to many more languages (so far: Java, Python, Haskell, Go, D - see Hacker News discussion here and Reddit/programming discussion here). Update, March 2012: pypy did an amazing job optimizing the Python version, bringing it up to performance levels similar to the rest of them.

I also prepared a standalone windows binary of the game - using py2exe for the PyGame Python GUI, and a MinGW-compiled binary of the C version of the game engine.

To fiddle with the sources:

  1. Checkout

  2. Play or benchmark:

    • "make play" to play a graphics game of score4 (via PyGame)
    • "make playSimple" to play a console game of score4
    • "make benchmark" to benchmark the languages
  3. The "make play" controls:

    • Click with mouse to drop a green chip on a column
    • ESCAPE to exit
    • SPACE to start a new game.
  4. To run the benchmark in only one of the languages, e.g. OCaml...

    cd OCaml ; make ; make test

  5. To benchmark in all available languages:

    make benchmark

These are the results I get on my Celeron E3400 under Arch Linux:

= Running benchmarks =
Benchmarking imperative memoized C++ ... 0.087 sec
Benchmarking imperative C++ ... 0.115 sec
Benchmarking imperative C ... 0.120 sec
Benchmarking imperative Lisp (SBCL) ... 0.272 sec
Benchmarking imperative LISP (CMUCL) ... 0.270 sec
Benchmarking imperative Java ... 0.385 sec
Benchmarking imperative OCaml ... 0.306 sec
Benchmarking imperative Python (Pypy) ... 0.638 sec
Benchmarking functional OCaml ... 0.693 sec
Benchmarking imperative C# ... 0.863 sec
Benchmarking imperative F# ... 0.792 sec
Benchmarking functional F# ... 1.958 sec

Update, 3 years later - Rust entered the game with furious speed ; it's almost as fast as the imperative group but the code is functional-style - ported from the OCaml version. CMUCL is gone, too (can't find it in Arch's repos anymore). Go and D are there too...

Results below from my MBair running Arch Linux:

Benchmarking imperative memoized C++ ...: 0.072000
Benchmarking imperative C ...: 0.085000
Benchmarking imperative C++ ...: 0.088000
Benchmarking imperative D ...: 0.139000
Benchmarking imperative Lisp (SBCL) ...: 0.143000
Benchmarking functional Rust ...: 0.155000
Benchmarking imperative Java ...: 0.220000
Benchmarking imperative OCaml ...: 0.230000
Benchmarking functional OCaml ...: 0.434000
Benchmarking imperative C# ...: 0.568000
Benchmarking imperative F# ...: 0.590000
Benchmarking imperative Go ...: 0.697000
Benchmarking functional F# ...: 1.295000