Hits and Misses

Let’s go through each module we’ve defined so far, and write the functions we need to play the game.

We'll cover the following

Let’s define the functions we need to play the game in each module we’ve covered so far. Coordinates don’t need any more functionality.

Let’s begin with guesses.

Keep track of guesses

As players guess coordinates in the game, we need to keep track of those guesses to accurately represent their opponent’s board. We’ll need to add the guessed coordinates but not remove them.

We need a function to add the guessed coordinates to a Guesses struct and determine whether they go into the set of the hits or of the misses. We’ll create two function clauses for an add/3 function. Each clause will pattern match on whether the guess is a :hit or a :miss and put the coordinate in the appropriate set.

In order for this to work, we need to change the alias function slightly to include the Coordinate module:

alias IslandsEngine.{Coordinate, Guesses}

Get hands-on with 1200+ tech skills courses.