Compare commits

...

3 Commits

2 changed files with 27 additions and 19 deletions

View File

@ -18,6 +18,8 @@
; Make a frame by instantiating the frame% class
(define g (grid-lines (GameState-grid game)))
(define w (grid-width (GameState-grid game)))
(define h (grid-height (GameState-grid game)))
(define t 0)
(define frame (new frame%
[label "Game Analysis"]
@ -30,7 +32,7 @@
[paint-callback
(lambda (canvas dc)
(send dc clear)
(render-grid (grid (take g t) 6 6) dc))]))
(render-grid (grid (take g t) w h) dc))]))
(define log (new list-box%
[label ""]
@ -66,6 +68,6 @@
[label (format "Final Score: R: ~a, B: ~a" (first (GameState-scores game)) (last (GameState-scores game)))]))
(send frame show #t))
(define g (play-game (GameState (grid '() 6 6) 0 '(0 0)) (list random-player random-player)))
(define g (play-game (GameState (grid '() 6 6) 0 '(0 0) (list random-player random-player))))
(print (GameState-grid g))
(analysis-gui g)

View File

@ -19,7 +19,14 @@
; grid is the grid
; player is the current player
; scores is a list of numbers for the score of each player.
(struct GameState (grid player scores) #:transparent)
; players is a list of two players who are playing in the game
(struct GameState (grid player scores players) #:transparent)
; represents a player. func is a function with the signature:
; Grid Number -> Line
; where the grid is the current board state, the Number is if the player is player 1 or 2, and it returns the move it will play that turn.
; name is the player's name.
(struct player (name func) #:transparent)
; width and height of the playing grid.
(define GRID-WIDTH 6)
@ -117,14 +124,13 @@
(not (out-of-bounds? (line-from line)))
(not (out-of-bounds? (line-to line))))))
; a Player is a function with the signature:
; Grid Number -> Line
; where the grid is the current board state, the Number is if the player is player 1 or 2, and it returns the move it will play that turn.
; a Player that plays random moves
(define (random-player g n)
(let ([m (car (random-sample (valid-moves g) 1))])
(line (line-from m) (line-to m) n)))
(define random-player
(player
"Random"
(lambda (g n)
(let ([m (car (random-sample (valid-moves g) 1))])
(line (line-from m) (line-to m) n)))))
; Grid Line -> Grid
; adds line to grid, if it is a valid move. Otherwise skips the move.
@ -156,18 +162,18 @@
([boxes (boxes-for l)]
[g (append-move grid l)])
(length (filter (lambda (b) (= (count-square b g) 4)) boxes))))
; GameState, List of 2 Players -> GameState
; GameState -> GameState
; runs the game.
; If the game is over, returns the final game state
; else, lets a player play a move. If they complete a box, they stay on and earn a point. Else, the other player has a turn.
(define (play-game s players)
(define (play-game s)
(let ([g (GameState-grid s)]
[p (GameState-player s)])
(cond
[(= (length (grid-lines g)) (length (grid-lines FULL-GRID))) s] ; end of the game
[else
(let* ([move ((list-ref players p) g p)]
(let* ([move ((player-func (list-ref (GameState-players s) p)) g p)]
[new-grid (append-move g move)]
[boxes (completes-boxes g move)]
[box (not (= 0 boxes))]
@ -175,7 +181,7 @@
(play-game (GameState
new-grid
(if box p (abs (- p 1)))
(if box (list-set scores p (+ boxes (list-ref scores p))) scores)) players))])))
(if box (list-set scores p (+ boxes (list-ref scores p))) scores) (GameState-players s))))])))
; Grid -> Image
; renders grid to an image for showing humans the game.
@ -189,9 +195,9 @@
; renders grid on the given drawing context.
; TODO setting line color doesn't seem to be always working?
; TODO color in squares
(define (render-grid grid dc)
(define (render-grid g dc)
(define passed '())
(for ([l (grid-lines grid)]) ; reversed for fill-drawing malarkey.
(for ([l (grid-lines g)]) ; reversed for fill-drawing malarkey.
(if (empty? passed) (set! passed (list l)) (set! passed (append passed (list l))))
(cond
[(= (line-player l) 0) (send dc set-pen "red" 4 'solid)] ; player 1 draws in red
@ -203,8 +209,8 @@
(+ 4 (* GRID-SCALE (point-y (line-to l))))) ; draw the line
(let ([boxes (boxes-for l)])
(for ([b boxes])
(if (= (count-square b passed) 4)
(send dc draw-text
(if (= (count-square b (grid passed (grid-width g) (grid-height g))) 4)
(send dc draw-text
(if (= (line-player l) 0) "R" "B")
(+ 6 (* GRID-SCALE (point-x b)))
(+ 3 (* GRID-SCALE (point-y b))))