CSNoobKinda
CSNoobKinda

Reputation: 23

How do I program a short clojure game of Nim?

I'm really lost as to how to continue with my Clojure game of Nim (numbers game where each player removes stones from 3 heaps of stone until the loser is left with a total of one last stone) required for my programming class. I have defined the default number of stones, number of players, etc. but I'm unsure of how to actually define a function of the players taking their turns (they have to take a number of stones from a specific heap) and properly reducing the corresponding heap they took from. Any advice???

I've tried using an atom to switch players, but I keep getting syntax errors, possible because I'm overloading both players into it (it only gets more complicated when I try to split it up)

(def starting-heap-one 11)
(def starting-heap-two 11)
(def starting-heap-three 11)
(def min-move-allowed 1)
(def set-players-allowed {:player1 "Gwent 1" :player2 "Gwent 2"})
(def move-order '(:player1 :player2))

(defn heap-1-move
  [x]
  (cond:
    (if (nil?) println "Invalid move: Please take at least ONE stone"))
  :else
  (println (- starting-heap-one x)))

(defn heap-2-move
  [x]
  (cond:
    (if (nil?) println "Invalid move: Please take at least ONE stone"))
  :else
  (println (- starting-heap-two x)))

(defn heap-3-move
  [x]
  (cond:
    (if (nil?) println "Invalid move: Please take at least ONE stone"))
  :else
  (println (- starting-heap-three x)))

(defn switch-player
  [p])

Expected game results should look like:

heap 1   heap 2    heap 3          
11       11        11            Begin Game
10       11        11            Player 1 removed one from heap 1
10        9        11            Player 2 removed two from heap 2

etc.

Upvotes: 1

Views: 108

Answers (1)

Denis Fuenzalida
Denis Fuenzalida

Reputation: 3346

My suggestion would be to think of games as a set of rules between players and the state of the game. The rules you mentioned:

  • players take turns to remove stones from one of three heaps
    • how do the players express which heap and how many stones?
  • if two heaps are empty and a player takes the remaining stones of the last heap, then the game ends, and the player wins
    • How does the game keep track of the remaining stones?

Think of "players take turns" now. You cannot anticipate how many turns there will be, but surely can be modelled as a loop (eg. player-1, then player-2 and so on...).

On each iteration of the loop, each player enters some input (heap number and number of stones to take), the game checks some rules (is the heap number valid? Does it have any stones left?, etc.)

With that in mind, I think you can start with a basic program that has a similar idea: take a name from the user, if the name is not "end", you print "Hello, <name>" and loop again. Otherwise, just let the program end.

Once you have the program above, you can use it to create an outline of the loop and the rules for Nim.

Here there are some links to examples from ClojureDocs which I think can help you:

  • read-line reads a line of text from the user. Note that it's read as a string, you'll need to combine it with something like read-string to turn the string into a number

  • loop and recur (like on this example) will give you a basic looping construct and if you look closely you'll find the state on the example is comprised of the iteration number (iter) and the accumulated sum (acc). You can follow this pattern to express the state of your game of Nim.

Upvotes: 2

Related Questions