musings of a Software Developer

Seven languages in seven weeks: My solutions for scala day 1


This post will present my solutions for day 1 of the Scala chapter in the book Seven languages in seven weeks:

I really looked forward to the Scala chapter, also because Scala has generated quite some buzz lately. My first encounter with Scala was some 3 years ago on a NL-Jug conference. Since then I’m not a fan of scala, but I doubt that I gave it a fair chance. My dislike especially came from people’s reasoning that in Scala you could do things in less code and so this must be better than Java.

Of course Java brings some verbosity, but it also brings a syntax that is understandable for almost any programmer (so also programmers that never learned Java), and I clearly missed this in a lot of the examples the scala people came up with. Fibonacci in one line of code may seem cool for the writer, but most certainly is not cool for the readers, and seen that code is read at least 10 times as much as written, the argument that you have to write less code should never prevail the argument of readability.

But I might have been blinded by my dislike of this reasoning and programming style, and didn’t give Scala a fair chance. So I will try to give Scala a chance in the coming 3 blog entries, trying to keep a Beginner’s mind as mentioned by Patrick Kua in this presentation. BTW if you like the presentation you will probably also like the this book: Pragmatic Thinking and Learning: Refactor Your Wetware (Pragmatic Programmers).

The Solution

The exercise consisted of programming a TicTacToe game. The first part of the exercise is just implementing functionality so that the program can determine who won. As a bonus exercise you should implement it so 2 players can play it. In my solution the bonus exercise is implemented.

As a side note, the exercise is for the object oriented part of Scala, not the functional part. So probably a much cleaner and better implementation could be used to implement this in Scala, but I tried to focus on the OO part. Please post any suggestions on how to improve the code.

The sign class

For this exercise I choose to do a little bit of OO over engineering and started of with a Sign class in stead of using build-in string or char. I was interested to see if enums existed in Scala and what they looked like.

The player class 

A simple player class to be able to print the winner and place a marker.

The board class

Well in the board class there is the logic to deduct the status. So basically; is there a winner, is the board full or can we continue. Here is an enum for the status.
First a board consists of cells, here is a simple cell class:

Then there is the status enum constuct.

Here is the board class. I decided to use the Option mechanism in Scala to simply extract some methods. I think it is not really a good way to use it because it is more or less a goto statement, but I heard from it of some colleagues and wanted to give it a try.

The TicTacToe class

This class contains the logic to play a game. It will ask the current player for an input between 1-9, and place a marker in the corresponding cell (1-3 from left to right is first row, 4-6 etc.).
Due to time constrains I choose not to add validation for the input etc, but normally you should off course.

Conclusion day 1

I still haven’t decided wether I like Scala or not. Mainly because until now I basically programmed Scava, Java with a Scala syntax. I think the TicTacToe game exercise makes for a great software-kata, and I might redo it with scala-test some day in the future.

Leave a Reply

Required fields are marked *.