<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><br></div><div>Consider a random testing approach. Throw the random tester at the main function, Think of 'streams' of input that the main function can absorb, and generate random streams .</div><div><br></div><br><div><div>On Jun 3, 2013, at 5:14 PM, Sean Kanaley wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr">Just kidding there are more parse bugs, but I'm doing my best to fix them.&nbsp; The consolation is that the other implementations are either far longer, incorrect, or both.&nbsp; Still... I shall post it shortly and hope for the best.<br>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jun 3, 2013 at 4:26 PM, Sean Kanaley <span dir="ltr">&lt;<a href="mailto:skanaley@gmail.com" target="_blank">skanaley@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Thank you both for the replies.&nbsp; I have incorporated all of your suggestions.&nbsp; The thin board wrapper over vector2 proved useful to quickly switch everything over to arrays.&nbsp; The program should no longer crash due to user input aside from any kind of internal buffer issues that may or may not be possible, which now exceeds the spec of allowing malformed input (though bounds checking was still lacking).<br>

<br></div><div>I also found a few important bugs in the process, so forgive me but I shall repost the code in its entirety.&nbsp; The critical changes are the macro to process adjacent positions, since clearing recursively seems to require not using diagonals but summing the number of adjacent mines obviously does; try-clear! wasn't returning the proper values to stop recursion; assume! should be toggleable (not actually in the spec though); and some other minor things.<br>

<br>It's surprising how difficult a seemingly easy task like cloning minesweeper can be.&nbsp; Ultimately this took several hours for what I thought would be 30-60 minutes.<br></div><div class="gmail_extra"><br></div><div class="gmail_extra">

Also I couldn't find a way to make a mutable-array immutable with anything resembling mutable-array-&gt;array so I left the board as mutable.<br></div><div class="gmail_extra"><br>#lang racket<br>(require math)<br>;board uses arrays directly, but maintaining an abstraction is nice<br>

(define (board-ref b row col) (array-ref b (vector row col)))<br>(define (board-rows b) (vector-ref (array-shape b) 0))<br>(define (board-cols b) (vector-ref (array-shape b) 1))<div class="im"><br>(define (on-board? b row col)<br>
</div>&nbsp; (and (&lt;= 0 row (sub1 (board-rows b)))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (&lt;= 0 col (sub1 (board-cols b)))))<br>(define (board-&gt;lists b) (array-&gt;list* b))<div class="im"><br>;run on adjacent board positions<br></div>(define-syntax (for-adj stx)<br>&nbsp; (syntax-case stx ()<br>&nbsp;&nbsp;&nbsp; [(_ b (r row) (c col) diag? body ...)<br>

&nbsp;&nbsp;&nbsp;&nbsp; (with-syntax ([is (if (syntax-&gt;datum #'diag?) #''(0 0 1 1 1 -1 -1 -1) #''(0 0 1 -1))]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [js (if (syntax-&gt;datum #'diag?) #''(1 -1 0 -1 1 0 -1 1) #''(1 -1 0 0))])<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #'(for ([i is] [j js])<div class="im"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (let ([r (+ row i)]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [c (+ col j)])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (when (on-board? b r c)<br></div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; body ...))))]))<div class="im"><br>;mark is either hidden, assume-mine, or clear<br>

;n is int equal to # adj mines or -1 for mine<br></div>(struct pos ([mark #:mutable] n) #:transparent)<div class="im"><br>(define (mine? p) (= (pos-n p) -1))<br>;hidden0? is needed because only spaces with no mines in them and no mines adjacent<br>
</div>
;to them are cleared recursively<div class="im"><br>(define (hidden0? p)<br>&nbsp; (and (symbol=? (pos-mark p) 'hidden)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (zero? (pos-n p))))<br>(define (show-pos p)<br>&nbsp; (match-let ([(pos m n) p])<br>&nbsp;&nbsp;&nbsp; (case m<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [(hidden) "."]<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [(assume-mine) "?"]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [(clear) (if (zero? n) " " (number-&gt;string n))]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [else (error "illegal mark" m)])))<br>;put "|" around positions<br>(define (show-board b)<br>

&nbsp; (for ([row (board-&gt;lists b)])<br>&nbsp;&nbsp;&nbsp; (displayln (format "|~a|" (string-join (map show-pos row) "|")))))<br><br>;winning = every position is either cleared or a hidden mine<br>(define (win? b)<br>
&nbsp; (for*/and ([r (range 0 (board-rows b))]<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [c (range 0 (board-cols b))])<br>&nbsp;&nbsp;&nbsp; (let ([p (board-ref b r c)])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (or (symbol=? (pos-mark p) 'clear)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (mine? p)))))<br><br></div><div class="im">(define (init-board rows cols)<br>&nbsp; (let ([chance (+ (/ (random) 10) 0.1)]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;empty board<br></div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [b (array-&gt;mutable-array (build-array (vector rows cols)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (λ (x) (pos 'hidden 0))))])<div class="im"><br>&nbsp;&nbsp;&nbsp; ;loop whole board<br>
&nbsp;&nbsp;&nbsp; (for* ([row (range 0 rows)]<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [col (range 0 cols)])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (when (&lt; (random) chance)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;put a mine<br></div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (array-set! b (vector row col) (pos 'hidden -1))<div class="im"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;increment adjacent mine counts unless that adjacent position is a mine<br>
</div>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (for-adj b (r row) (c col) #t<div class="im"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (let ([p (board-ref b r c)])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (unless (mine? p)<br></div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (array-set! b (vector r c) (pos 'hidden (add1 (pos-n p)))))))))<br>

&nbsp;&nbsp;&nbsp; b))<br><br>;only clear position if it's not a mine<br>;only continue recursing when it's a hidden0?<br>(define (try-clear! p)<br>&nbsp; (cond [(mine? p) #f]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [(hidden0? p) (set-pos-mark! p 'clear) #t]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [else (set-pos-mark! p 'clear) #f]))<div class="im"><br><br>;the following player move functions return boolean where #f = lose, #t = still going<br></div>;assuming can never directly lose ((void) == #t from the set!)<br>
;make sure to not allow overwriting an already cleared position<br>
(define (toggle-assume! b row col)<div class="im"><br>&nbsp; (let ([p (board-ref b row col)])<br></div>&nbsp;&nbsp;&nbsp; (set-pos-mark! p (case (pos-mark p)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [(assume-mine) 'hidden]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [(hidden) 'assume-mine]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [(clear) 'clear]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [else (error "invalid mark" (pos-mark p))]))))<div class="im"><br><br>;clearing loses when the chosen position is a mine<br>;void = #t as far as if works, so no need to return #t<br>

(define (clear! b row col)<br>&nbsp; (let ([p (board-ref b row col)])<br>&nbsp;&nbsp;&nbsp; (and (not (mine? p))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;not a mine, so recursively check adjacents, and maintain list of visited positions<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;to avoid infinite loops<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (let ([seen '()])<br></div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;clear the chosen position first, only continuing if it's a 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (when (try-clear! p)<div class="im"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (let clear-adj ([row row] [col col])<br>
</div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (for-adj b (r row) (c col) #f<div class="im"><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;make sure its not seen<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (when (and (not (member (list r c) seen))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (try-clear! (board-ref b r c)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;it was cleared, so loop after saving this position as being seen<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (set! seen (cons (list r c) seen))<br></div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (clear-adj r c)))))))))<br><br>(define (parse-and-do-move! b s)<br>&nbsp; (match (string-split s)<br>&nbsp;&nbsp;&nbsp; [(list type row col)<br>
&nbsp;&nbsp;&nbsp;&nbsp; (let ([row (string-&gt;number row)]<div class="im"><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [col (string-&gt;number col)])<br></div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (on-board? b row col)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (case type<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [("?") (toggle-assume! b row col)]<div class="im"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [("!") (clear! b row col)]<br>
</div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [else (parse-and-do-move! b (read-line))])<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (parse-and-do-move! b (read-line))))]<br>&nbsp;&nbsp;&nbsp; [else (parse-and-do-move! b (read-line))]))<br>(define (run)<br>&nbsp; (displayln (string-append "--- Enter one of:\n"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "--- \"! &lt;row&gt; &lt;col&gt;\" to clear at (row,col), or\n"<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "--- \"? &lt;row&gt; &lt;col&gt;\" to flag a possible mine at (row,col).\n"))<div class="im"><br>&nbsp; (let ([b (init-board 8 8)])<br>&nbsp;&nbsp;&nbsp; (let run ()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (show-board b)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (display "enter move: ")<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (parse-and-do-move! b (read-line))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (win? b) (displayln "CLEAR!") (run))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (displayln "BOOM!")))))<br></div></div></div>
</blockquote></div><br></div>
____________________<br> &nbsp;Racket Users list:<br> &nbsp;<a href="http://lists.racket-lang.org/users">http://lists.racket-lang.org/users</a><br></blockquote></div><br></body></html>