From 94aee9e706e4e1b1ed98c44be46a3cdf1fc3ceb6 Mon Sep 17 00:00:00 2001 From: Dan Lidral-Porter Date: Fri, 29 Mar 2013 06:54:21 -0700 Subject: [PATCH] parallelize playing games --- src/crosscram/engine.clj | 14 +++++++------- src/crosscram/main.clj | 3 ++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/crosscram/engine.clj b/src/crosscram/engine.clj index 55fdddd..03fa914 100644 --- a/src/crosscram/engine.clj +++ b/src/crosscram/engine.clj @@ -21,7 +21,7 @@ gives the second player a transposed view of the board.)" ;; there or otherwise be up to no good. (def over? - "Returns true if there are no horizontal moves possible, false otherwise. + "Returns true if there are no horizontal moves possible, false otherwise. Takes a board as argument." (complement game/can-move?)) @@ -45,9 +45,9 @@ Takes a board as argument." (recur new-game (rest bot-funs)))))) (defn play-symmetric [game bot-a bot-b games-to-play] - (loop [scoreboard {}] - (if (= games-to-play (apply + (vals scoreboard))) - scoreboard - (let [g1 (winner (play game bot-a bot-b)) - g2 (winner (play game bot-b bot-a))] - (recur (merge-with + scoreboard (score g1 g2))))))) + (let [play-round (fn [_] + (->> [(play game bot-a bot-b) (play game bot-b bot-a)] + (map winner) + (apply score)))] + (->> (pmap play-round (range games-to-play)) + (reduce (partial merge-with +))))) diff --git a/src/crosscram/main.clj b/src/crosscram/main.clj index c4ecae7..fd23f9b 100644 --- a/src/crosscram/main.clj +++ b/src/crosscram/main.clj @@ -26,4 +26,5 @@ (:make-move fns-a) (:make-move fns-b) num-games)] - (println "Scores:" scores))))) + (println "Scores:" scores) + (shutdown-agents)))))