Position the Islands

Understand how to position islands in this lesson.

We'll cover the following

Position the islands

When players move their islands around on the board, the UI will send messages back to the server. These messages include a key for the island type as well as the row and column numbers for the island’s upper-left coordinate.

The Board module knows how to position an island with a key and a full island. The Coordinate module turns the row and column value into a coordinate, while the Island module turns the island key and the coordinate into a full island. The Game module coordinates between these, checks to see that all the values are valid and formulates a response.

We start with a public function for the interface as we have before. It takes the game process PID, the player, and the island key. It also takes the row and column of the island’s upper-left coordinate. We use GenServer.call/2 so we can send a response back to the caller.

We want to make sure that the value we get for the player matches either :player1 or :player2. Let’s add a module attribute with both those atoms in a list:

@players [:player1, :player2]

Now we add a guard for it in the function head:

def position_island(game, player, key, row, col) when player in @players, do:
  GenServer.call(game, {:position_island, player, key, row, col})

Get hands-on with 1200+ tech skills courses.