Rock Paper Scissor Game through Actor Model in Scala

4 minute read

Published:

I had an opportunity to attend a wonderful course during my first year of Ph.D. at the GSSI. The course was offered by Prof. Rocco De Nicola. It was on The principles of Concurrent and Distributed Programming. One of the projects for the course was to develop a program to simulate a three-player distributed Rock/ Paper/Scissor Game. It’s a game we all would have played at some point in our lives.

For people who don’t know the game, please refer here. It is a hand game where each player simultaneously forms the shape of a rock, paper or scissor and based on the rules of the game, the points are assigned and the game goes on.

</pE> An easy way to do develop this game is to have 3 processes where each process represents a player. Every process will select their moves one by one and based on the rules the score can be computed. However one of the important constraints of the game is that the moves cannot be made in a sequential way rather it should happen at the same time. This can be seen as a classic example where concurrency plays an important role. One more additional challenge that was given for the project was that there should not be any central coordinator. For example, if there was a central coordinator then the three processes could send their moves concurrently to a central process which can then compute the score and communicate to each process. This means that the processes should be able to concurrently send their moves to each other as well as have a view of the total scores ### Game Rules 1. Each player should be able to randomly select a move from the set of available moves {rock,paper,Scissor} 2. The user should be able to specify the number of rounds for which the game has to be conducted. 3. Every player shall be able to send and receive moves to and from other players 4. Every player should have means by which it can send it's move to the other two players 5. Each player should have a view of the global score of other players such that at any instant, a player will have the score information of all the three and it should be consistent 6. Only when the round has finished in each of the players, the next round can be started The choice of language was given to the students and I selected Scala for my implementation. The reason was pretty simple, I wanted to try out the Actor model (Many languages like Erlang, Haskell. Java support Actor model) and also to get my hands on some Scala code. Scala is one of the most used programming languages for implementing concurrent and distributed systems. This is more clear from the fact that one of the key big data processing frameworks like Apache Spark is completely written in Scala. Due to the functional as well as object-oriented behavior provided by Scala, it is also one of the most used programming languages in the world. There are several other features provided by Scala that makes it more popular for writing distributed and parallel applications [1] ### Scala and AKKA Even though Scala can uses threads to enable concurrency as similar to Java, Scala has also a powerful way of enabling concurrency using Actor models provided by the [AKKA](https://akka.io/) framework. Akka uses the [Actor model](https://en.wikipedia.org/wiki/Actor_model) for handling concurrency. In the actor-based system, everything is an actor and it resembles an object in the Object-oriented design. The actor model was created as a model for concurrency. Every actor has a state and behavior. Actors run on real threads and more than one actor can run on a single thread and this is handled by the framework. Even though Scala can uses threads to enable concurrency as similar to Java, Scala has also a powerful way of enabling concurrency using Actor models provided by the [AKKA](https://akka.io/) framework. Akka uses the [Actor model](https://en.wikipedia.org/wiki/Actor_model) for handling concurrency. In the actor-based system, everything is an actor and it resembles an object in the Object-oriented design. The actor model was created as a model for concurrency. Every actor has a state and behavior. Actors run on real threads and more than one actor can run on a single thread and this is handled by the framework.