Skip to content

Commit

Permalink
fix bug that prevented us from finding a lot of single-cell moves
Browse files Browse the repository at this point in the history
  • Loading branch information
jrheard committed Dec 14, 2017
1 parent 52cd8ce commit 2913a90
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 32 deletions.
28 changes: 27 additions & 1 deletion dev-diary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,30 @@ yields score 22, which is incorrect

it also says that placing a 5 above the 3 and a 2 below the 3 yields score 25, which is _also_ incorrect

looks like it was a mistake in the line where i was determining whether the move was horizontal or vertical
nvm, fixed, looks like it was a mistake in the line where i was determining whether the move was horizontal or vertical

===========

12/14/17

currently seeing a bug:
on a board like this:

5
4
3 6 1

with a hand of just [5], the function never tries to place the 5 to the left or right of the preexisting 5
let's investigate that and figure out why

ok awesome, it looks like the bug was that we were throwing away results if you were in a
situation where your hand ran out of tiles. changing

(empty? available-cells-for-move)

to

(or (empty? available-cells-for-move)
(empty? hand))

solved the problem.
36 changes: 11 additions & 25 deletions src/quinto/ai.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

(defn -cell-value-is-definitely-invalid?
"Used when speculatively placing a value at a particular cell. Returns true
if placing this value at this cell would _definitely_ cause the board to end up
in an invalid state, false otherwise."
if the runs created by placing this value at this cell would _definitely_ cause the board
to end up in an invalid state, false otherwise."
[[horizontal-length horizontal-sum] [vertical-length vertical-sum] move-direction]
(or
; A cell value is definitely invalid if it would create a too-long run.
Expand Down Expand Up @@ -47,8 +47,9 @@
(all-moves-for-cells-and-hand grid available-cells-for-move hand move-direction #{} #{}))

([grid available-cells-for-move hand move-direction valid-moves-seen move-so-far]
(if (empty? available-cells-for-move)
; If there aren't any cells left for us to use, that's the end of this particular path of investigation.
(if (or (empty? available-cells-for-move)
(empty? hand))
; If there aren't any cells or values left for us to use, that's the end of this particular path of investigation.
valid-moves-seen

; Otherwise, let's try placing a value at the first cell.
Expand All @@ -69,6 +70,8 @@
(let [grid-with-value (assoc-in grid [x y] value)
[[horizontal-length horizontal-sum]
[vertical-length vertical-sum]] (g/find-runs grid-with-value x y)]
#_(when (= (first available-cells-for-move) [5 6])
(js-debugger))

(cond
; If placing this value here clearly makes for an invalid board state,
Expand Down Expand Up @@ -125,8 +128,8 @@
; A move can only consist of cells that are _on_ the grid in the first place,
; and a move can't create a run that's >= MAX-RUN-LENGTH.
(if (and (g/cell-is-on-grid grid x y)
(< (+ relevant-run-length (count available-cells))
MAX-RUN-LENGTH))
(<= (+ relevant-run-length (count available-cells))
MAX-RUN-LENGTH))
(conj available-cells [x y])
(reduced available-cells)))
[]
Expand All @@ -135,7 +138,6 @@
; valid-cells-for-move should contain _at least_ one cell,
; because it's a precondition of this function that [x y] must be a playable cell.
(assert (seq valid-cells-for-move))

(all-moves-for-cells-and-hand grid
valid-cells-for-move
hand
Expand All @@ -159,27 +161,11 @@
:args (s/cat :grid ::sp/grid :hand ::sp/hand :cell ::sp/cell)
:ret (s/coll-of ::sp/move))

(comment
(select [ALL 0]
[[[1 5] [3 10]] [[1 4] [3 10]] [[3 10] [3 10]]])

(select [ALL 0 (fn [[run-length run-sum]] (> run-length 1)) 1]
[[[1 5] [3 10]] [[1 4] [3 10]] [[3 10] [3 10]]])

(select [ALL (fn [[run-length run-sum]] (> run-length 1)) 1]
[[1 5] [1 4] [3 10]]
)

)

(defn score-move
[grid move]
; TODO can this be written with specter? should it?
(let [grid-with-move (reduce (fn [grid [[x y] value]]
(assoc-in grid [x y] value))
grid
move)]
(let [grid-with-move (reduce (partial apply assoc-in) grid move)]

; XXXX replace this with false and see what happens to single-cell horizontal moves
(if (= (count move) 1)
; If the move's only one cell long, then just add that cell's vertical and horizontal run sums.
; XXX does this actually need to be a special case?
Expand Down
2 changes: 1 addition & 1 deletion src/quinto/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
(count (@app-state :deck))

(ai/score-move (@app-state :grid)
#{[[4 7] 5] [[4 9] 2]})
#{[[7 7] 6]})

(map (fn [move]
[move (ai/score-move (@app-state :grid) move)])
Expand Down
8 changes: 3 additions & 5 deletions src/quinto/deck.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
(:require [clojure.spec.alpha :as s]
[quinto.specs :as sp]))

(def MAX-HAND-SIZE 5)

; Going with the tile distribution reported for the marbellized version I played.
; See https://boardgamegeek.com/thread/24859/tile-distribution
(defn make-deck []
Expand All @@ -27,9 +29,5 @@
(concat hand (take num-tiles deck))])

(s/fdef draw-tiles
:args (s/cat :deck ::sp/deck :hand ::sp/hand :num-tiles (s/and nat-int? #(<= % 5)))
:args (s/cat :deck ::sp/deck :hand ::sp/hand :num-tiles (s/and nat-int? #(<= % MAX-HAND-SIZE)))
:ret (s/cat :new-deck ::sp/deck :new-hand ::sp/hand))

(comment
(count (make-deck))
)

0 comments on commit 2913a90

Please sign in to comment.