Board refactor to not have each square be actor and not be actor
This commit is contained in:
parent
fbdf8129f5
commit
3040c402b8
1 changed files with 33 additions and 44 deletions
|
@ -12,9 +12,9 @@
|
||||||
(define ggg-size 3) ;; tic tac toe with more than 3x3 grid?
|
(define ggg-size 3) ;; tic tac toe with more than 3x3 grid?
|
||||||
|
|
||||||
(define (^ggg-controller bcom initiator? peer)
|
(define (^ggg-controller bcom initiator? peer)
|
||||||
(define mark (if initiator? "x" "o"))
|
(define mark (if initiator? 'x 'o))
|
||||||
(define peer-mark (if initiator? "o" "x"))
|
(define peer-mark (if initiator? 'o 'x))
|
||||||
(define board (selfish-spawn ^peer-board))
|
(define board (make-board))
|
||||||
(define my-turn? (not initiator?))
|
(define my-turn? (not initiator?))
|
||||||
|
|
||||||
(methods
|
(methods
|
||||||
|
@ -22,55 +22,44 @@
|
||||||
[(peer-turn! coords)
|
[(peer-turn! coords)
|
||||||
(if (not my-turn?)
|
(if (not my-turn?)
|
||||||
(begin
|
(begin
|
||||||
($ board 'choose! coords peer-mark)
|
(board-choose! board peer-mark coords)
|
||||||
(set! my-turn? (not my-turn?)))
|
(set! my-turn? (not my-turn?))
|
||||||
|
(board-display board))
|
||||||
(error "It's my turn!"))]
|
(error "It's my turn!"))]
|
||||||
;; TODO: This needs to go somewhere else so the peer can't move for us!
|
;; TODO: This needs to go somewhere else so the peer can't move for us!
|
||||||
[(my-turn! coords)
|
[(my-turn! coords)
|
||||||
(if my-turn?
|
(if my-turn?
|
||||||
(begin
|
(begin
|
||||||
($ board 'choose! coords mark)
|
(board-choose! board mark coords)
|
||||||
(set! my-turn? (not my-turn?)))
|
(set! my-turn? (not my-turn?))
|
||||||
|
(board-display board))
|
||||||
(error "It's not my turn."))]))
|
(error "It's not my turn."))]))
|
||||||
|
|
||||||
;; Board logic
|
;; Board logic
|
||||||
|
|
||||||
(define (^peer-board bcom self)
|
(define (make-board)
|
||||||
;; Define the array with unspecified values, then fill
|
(make-array #f ggg-size ggg-size))
|
||||||
(define arr (make-array *unspecified* ggg-size ggg-size))
|
|
||||||
(array-map! arr (lambda () (spawn ^mark)))
|
|
||||||
|
|
||||||
(methods
|
(define (board-ref board coords)
|
||||||
;; Switch coords for clarity
|
(match coords ((x y) (array-ref board y x))))
|
||||||
[(ref coords)
|
|
||||||
(match coords ((x y) (array-ref arr y x)))]
|
(define (board-choose! board val coords)
|
||||||
[(chosen? coords) (not (not ($ ($ self 'ref coords) 'get)))]
|
(match coords
|
||||||
[(choose! coords mark-char)
|
((x y)
|
||||||
(let* ((mark ($ self 'ref coords))
|
(define ref (board-ref board coords))
|
||||||
(char ($ mark 'get)))
|
(if ref
|
||||||
(if char
|
(error "That space is already occupied with:" ref)
|
||||||
(error "coords already chosen:" coords)
|
(array-set! board val y x)))))
|
||||||
(begin
|
|
||||||
($ mark 'choose! mark-char)
|
(define (board-display board)
|
||||||
($ self 'display))))]
|
|
||||||
[(display)
|
|
||||||
(define (print-mark mark)
|
(define (print-mark mark)
|
||||||
(let ((mark ($ mark 'get)))
|
(if mark mark " "))
|
||||||
(or mark " ")))
|
|
||||||
;; this is .. probably messy?
|
;; this is .. probably messy?
|
||||||
(array-slice-for-each-in-order
|
(array-slice-for-each-in-order
|
||||||
1
|
1
|
||||||
(lambda (x)
|
(lambda (x)
|
||||||
(map (lambda (i) (format #t "[~a]" (print-mark i)))
|
(map (lambda (i) (format #t "[~a]" (print-mark i)))
|
||||||
(array->list x))
|
(array->list x))
|
||||||
(format #t "\n")) arr)]))
|
(format #t "\n"))
|
||||||
;; Do we need to use SRFI 158, or newra library, to do this?
|
board))
|
||||||
;; [(get-winner) ???]
|
|
||||||
|
|
||||||
(define* (^mark bcom #:optional [mark #f])
|
|
||||||
(methods
|
|
||||||
[(get) mark]
|
|
||||||
[(choose! mark-char)
|
|
||||||
(if mark
|
|
||||||
(error "this mark is already chosen")
|
|
||||||
(bcom (^mark bcom mark-char)))]))
|
|
||||||
|
|
Loading…
Reference in a new issue