Hello! This is my first publication here. I appreciate any advice to improve my content. Thanks!
Overview
I first learned about Scala more than ten years ago in university. I couldn’t understand how much powerful it is, but I kept a good feeling. This summer I decided to give it another chance. I got Programming in Scala, Fifth Edition. I really recommend this book if you are interested in learning Scala.
Once I’ve read this book, I wanted to consolidate my knowledge. One of my preferred hobbies is chess. I've been wanting to code something related to it for a while. So let’s see how easy (or difficult) is implementing a chess domain with Scala!
I should clarify that it isn't a step-by-step tutorial. It’s more like a journal of my experience in which I’ll explain every concept I use. I’m going to prioritize my coding driven by the domain, not by the language. So maybe you can find this a bit chaotic. I promise to be as clear as possible.
Find the code
You will find all the code in my GitHub repository. It is licensed under GPL v3, so feel free!
Chess main concepts
Color or side
The first thing we think about chess is that one player moves the white pieces and the other one moves the black pieces. OK, this is a basic concept that we can model as an enumeration. Let's do it!
OK, so we have some common keywords such as package or enum. These are not new if you know Java or other languages. Attending to basic syntax, we notice that there aren’t any curly brace () nor semicolon (;). These are not mandatory in Scala, because it is sensible to the indentation and the completion of the expressions.
Files and ranks
Another main concept of chess are the coordinates. These are used to refer to any of the squares in the board. Maybe you’ve seen some numbers and letters around the board. These are the identifiers for files (letters) and ranks (numbers). A square is identified by the combination of both. There are many options to represent them, but I’m going to use enumerations too. This decision allows us to treat them as a complete algebraic type.
The enum Color was so easy. File and Rank are a bit more complex. They have a parameter. As you see, each value of the enumeration is an extension of the enumeration indeed, specifying the value of the parameter.
The enum types can also have methods. I defined next and previous to be able to navigate by the board. Scala doesn’t have control of null values, but it uses Option instead. Option is a trait that has two different implementations: Some and None. In these methods, you can see how you can return them.
Talking about that, Scala doesn’t need the return word! It simply returns the last expression in the code block. Moreover, control flow expressions such as if/else, also return values. This allow us to write the code like in these previous and next methods.
Coordinate
Finally, I combined files and ranks in the Coordinate case class. A case class in Scala is like records in Java. It defines a class with a default constructor that uses the parameters provided, equals and hashCode methods… In this case, I override the toString method to return the combination of file and rank. Note that Scala can use string interpolation. These allow us to use a reference to any variable in the scope with ${}, but we have to prefix the string with the character ‘s’.
I implemented methods to navigate around the board. They are based in the previous and next methods we implemented previously. Look how Scala provides a complete pattern matching feature so much more powerful than a simple switch structure. The north, south, east and west methods match the Option resulting of the application of next and previous methods on a file or a rank. It matches with Some(value) and we can use that value directly, or with None.
However, we can deal with more complex conditions. In the remaining methods, we match against a tuple of Option. Just imagine the kind of things we can easily do with this feature.
Conclusions
We saw some powerful features of Scala in this main concepts of chess. In the next chapter, I will present objects and traits to improve this design a bit more. See you!
Top comments (0)