0

I have created this rank-boards function. It takes a board, ranks it, makes the next move on the board and recurs. How can I stop the recur from happening when the board is full? The make-move-on-board function breaks when there are no moves left, and I get a Key must be integer error.

 (defn rank-boards [board max-mark min-mark depth]
  (cond
    (r/game-in-progress? board max-mark min-mark) in-progress-score
    (r/tie? board max-mark min-mark) tie-score
    (r/winner? board max-mark min-mark)       highest-score)
  (recur 
    (b/make-move-on board (next-space board max-mark min-mark depth) max-mark) 
       min-mark 
       max-mark
       (inc depth)))
0

1 Answer 1

2

Assuming a function called is-full? that returns a truthy value when it's time to stop recursing, and that your desired return value is the score:

(defn rank-boards [board max-mark min-mark depth]
 (if (r/is-full? board)

   ;; board is full, so we return the score
   (cond
     (r/game-in-progress? board max-mark min-mark) in-progress-score
     (r/tie? board max-mark min-mark)              tie-score
     (r/winner? board max-mark min-mark)           highest-score)

   ;; board is not yet full, so we recurse
   (recur
     (b/make-move-on board (next-space board max-mark min-mark depth) max-mark) 
        min-mark 
        max-mark
        (inc depth)))

The code originally threw away the results of the cond -- not a particularly sensible operation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.