Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
37 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
(ns battleship.ai | ||
(:use battleship.core)) | ||
|
||
; A Monte Carlo solver for Battleship. | ||
|
||
; An infinite sequence of random boards | ||
(def infinite-boards | ||
(repeatedly #(place-all-pieces (newboard)))) | ||
|
||
; 1 if we might want to shoot that square, 0 otherwise | ||
(defn is-target [square] | ||
(if (and (not (nil? (:piece square))) | ||
(= :unstruck (:state square))) | ||
1 0)) | ||
|
||
; Gets the (not normalized) distribution of targetable squares | ||
; in a (finite) boardseq. | ||
(defn get-distribution [boardseq] | ||
(reduce (fn [running-count board] | ||
(map (fn [running-count-row row] | ||
(map + running-count-row | ||
(map #(if (is-target %) 1 0) row))) | ||
running-count board)) | ||
(repeat (repeat 0.0)) boardseq)) | ||
|
||
#_(defn get-normalized-probability-distribution [boardseq] | ||
(let [magnitude (apply #(apply + %) boardseq)] | ||
(map #(map / (float %) magnitude) | ||
(get-distribution boardseq)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters