Guesses
Explore how to model opponent guesses in an Elixir application using MapSet to maintain unique coordinates for hits and misses. Learn to create and manage guesses as structs, update sets functionally, and ensure efficient, reliable game state representation.
We'll cover the following...
The opponent’s board
With the coordinates represented, let’s move on to the less complex of the two boards—the opponent’s board.
The opponent’s board is nothing but a group of guessed coordinates separated into those that hit an island and those that missed. There will be a large number of unguessed coordinates as well. However, if we identify all the guesses, we can assume the rest are plain “ocean” coordinates.
This sounds like two lists—one for hits and the other for misses. We could wrap these lists in a struct with :hits and :misses keys, just as we did for coordinates.
There’s something else to consider here, though. We can’t necessarily guarantee that we’ll receive any specific guess only ...