Skip to content

Commit 2e4e10d

Browse files
committed
Bounds checking on moves
1 parent d58713d commit 2e4e10d

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

src/gomoku/core.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
(>= (count @board-state) (* cells-horizontal cells-vertical)))
3232

3333
(defn make-move! [player move-pos]
34-
(if (gameplay/is-cell-occupied? @board-state move-pos)
34+
(if (or (gameplay/is-cell-occupied? @board-state move-pos)
35+
(gameplay/is-cell-outside-bounds? move-pos cells-horizontal cells-vertical))
3536
(println "Can't place move for player " player " on " move-pos)
3637
(swap! board-state conj (gameplay/create-move player move-pos))))
3738

src/gomoku/gameplay.clj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
(some true? (for [cell board]
1212
(= (:pos cell) pos))))
1313

14+
(defn is-cell-outside-bounds? [[x y] w h]
15+
(not (and (<= x 0)
16+
(<= y 0)
17+
(< x w)
18+
(< y h))))
19+
1420
(defn get-moves-for-player [board player]
1521
(filter #(= player (:player %)) board))
1622

0 commit comments

Comments
 (0)