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
1 changed files with 33 additions and 44 deletions

View File

@ -12,9 +12,9 @@
(define ggg-size 3) ;; tic tac toe with more than 3x3 grid?
(define (^ggg-controller bcom initiator? peer)
(define mark (if initiator? "x" "o"))
(define peer-mark (if initiator? "o" "x"))
(define board (selfish-spawn ^peer-board))
(define mark (if initiator? 'x 'o))
(define peer-mark (if initiator? 'o 'x))
(define board (make-board))
(define my-turn? (not initiator?))
(methods
@ -22,55 +22,44 @@
[(peer-turn! coords)
(if (not my-turn?)
(begin
($ board 'choose! coords peer-mark)
(set! my-turn? (not my-turn?)))
(board-choose! board peer-mark coords)
(set! my-turn? (not my-turn?))
(board-display board))
(error "It's my turn!"))]
;; TODO: This needs to go somewhere else so the peer can't move for us!
[(my-turn! coords)
(if my-turn?
(begin
($ board 'choose! coords mark)
(set! my-turn? (not my-turn?)))
(board-choose! board mark coords)
(set! my-turn? (not my-turn?))
(board-display board))
(error "It's not my turn."))]))
;; Board logic
(define (^peer-board bcom self)
;; Define the array with unspecified values, then fill
(define arr (make-array *unspecified* ggg-size ggg-size))
(array-map! arr (lambda () (spawn ^mark)))
(define (make-board)
(make-array #f ggg-size ggg-size))
(methods
;; Switch coords for clarity
[(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 (board-ref board coords)
(match coords ((x y) (array-ref board y x))))
(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)))]))
(define (board-choose! board val coords)
(match coords
((x y)
(define ref (board-ref board coords))
(if ref
(error "That space is already occupied with:" ref)
(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))