Board refactor to not have each square be actor and not be actor

This commit is contained in:
Vivianne 2023-07-02 22:57:40 -07:00
parent fbdf8129f5
commit 3040c402b8

View file

@ -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)))]
[(chosen? coords) (not (not ($ ($ self 'ref coords) 'get)))]
[(choose! coords mark-char)
(let* ((mark ($ self 'ref coords))
(char ($ mark 'get)))
(if char
(error "coords already chosen:" coords)
(begin
($ mark 'choose! mark-char)
($ self 'display))))]
[(display)
(define (print-mark mark)
(let ((mark ($ mark 'get)))
(or mark " ")))
;; this is .. probably messy?
(array-slice-for-each-in-order
1
(lambda (x)
(map (lambda (i) (format #t "[~a]" (print-mark i)))
(array->list x))
(format #t "\n")) arr)]))
;; Do we need to use SRFI 158, or newra library, to do this?
;; [(get-winner) ???]
(define* (^mark bcom #:optional [mark #f]) (define (board-choose! board val coords)
(methods (match coords
[(get) mark] ((x y)
[(choose! mark-char) (define ref (board-ref board coords))
(if mark (if ref
(error "this mark is already chosen") (error "That space is already occupied with:" ref)
(bcom (^mark bcom mark-char)))])) (array-set! board val y x)))))
(define (board-display board)
(define (print-mark mark)
(if mark mark " "))
;; this is .. probably messy?
(array-slice-for-each-in-order
1
(lambda (x)
(map (lambda (i) (format #t "[~a]" (print-mark i)))
(array->list x))
(format #t "\n"))
board))