Skip to content

Instantly share code, notes, and snippets.

@mistercam
Created April 29, 2012 15:20
Show Gist options
  • Save mistercam/2551175 to your computer and use it in GitHub Desktop.
Save mistercam/2551175 to your computer and use it in GitHub Desktop.
Functions to print a text version of the Reversi board
(def columns
{"A" 0 "B" 1 "C" 2 "D" 3 "E" 4 "F" 5 "G" 6 "H" 7})
(defn print-cols [row]
"Prints the contents of the each square in the specified row to standard
out."
(if (not (empty? row))
(let [c (first row)]
(cond
(= :w c) (print "W")
(= :b c) (print "B")
:else (print "."))
(recur (rest row)))))
(defn print-rows [board]
"Prints each row on the specified board to standard out, along with a
numerical row identifier."
(let [r (first board) row-id (- 8 (count board))]
(if (not (empty? r)) ; if there's still rows to print
(do
(print row-id "[ ") ; prepend the row number
(print-cols r) ; print the row contents
(print " ]" row-id) ; append the row number
(newline) ; print a new line
(recur (rest board))))))
(defn print-board [board]
"Prints the specified board to standard out"
(let [header (str " " (apply str (keys columns)))]
(println header) ; print column labels
(print-rows board) ; print all of the rows
(println header))) ; print column labels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment