Wednesday, September 1, 2010

Creating a simple simulation - The HarvesterSpec

In my previous post, I introduced the Producer actor. Now it's time to introduce the Harvester.

In this post I will define the specifications of the Harvester and show how to test the concurrency of the eventual implementation.

In order to do this, I will also dive into some of the cooler features of Scala, especially foreach, map, list-comprehensions and futures.

Adding Scala unit tests

In my previous post, I showed how the Producer worked by adding some example code to my Simulation object. This might be very suitable for small projects, but in the long run I always prefer to have unit tests. In this post I will show how I added Scala unit tests to my project.

Creating a simple simulation - Producers

Now that the project has been set up, it's time to start creating the first steps in our simulation.

Every city simulation game I've played begins the same: getting workers to obtain raw materials. So let's  create a text-only simulation that does just that.

This post is about Scala syntax, Scala Actors, Scala messages, and examples of pattern matching.

Creating a Java/Scala Maven project

In this post, I will explain how I set up my first program, allowing me to create and run an extremely simple Scala program in my favorite IDE using Maven.

Welcome

I'm currently in the process of learning Scala, and decided to use this blog to share my learning curve with the rest of the world.

Goal

I am going to create a large-scale simulation as experienced in the games Ceasar, Settlers or CivCity. I do not pretend this will result in a full-blown game, but it will contain many of the aspects found in those games.

For those not knowing those games, the concept is simple: you, the player, are expected to turn a piece of land into a thriving city. This is done by attracting citizens, having them harvest food and resources, having them process those resources into essential and/or luxury goods, and not going broke in the process.

I chose this goal for two reasons: first, one of the selling points of Scala is the support for event-based Actors and the possibility to run thousands of them in a small JVM. To me, this sounds exciting. Secondly, creating simulations is just a lot of fun.

Prerequisites

I am not planning to write a tutorial for absolute beginners; the best way to understand each step I make is to be me. As it turns out, being me is a not a transferable skill, so let's assume the following requisites:

Knowledge

When writing the blog entries, I will assume the following knowledge:

  • Experience with programming Java;
  • Knowledge of functional programming constructs as experienced in e.g. Python or Haskell – especially higher-order functions and type inference;
  • Scala basics as explained in A Brief Scala Tutorial and Scala by Example.

Software

Furthermore, I will assume the reader has the following software installed: