Implementing Players

Your interaction with the player so far has been represented by a single function, next-guess. It’s time to fill out that idea a bit more. There are many potential ways to implement either a human or computer player. Any time you determine a function is open for extension, you should strongly consider using a multimethod or protocol to make that possible.

In this case, you only have a single function, so either choice is viable. There might be a need for players to keep state (remembering what they’ve guessed), so protocols are a bit easier to use, by encapsulating that state in a record which extends the protocol.

So replace that function with a protocol:

 (defprotocol Player
  (next-guess [player ...

Get Programming Clojure, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.