moved players into GameState so GameStates can be easily saved as records
This commit is contained in:
parent
8a98223d67
commit
e84ee87d72
2
gui.rkt
2
gui.rkt
|
@ -68,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)
|
19
main.rkt
19
main.rkt
|
@ -19,13 +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))
|
||||
(struct player (name func) #:transparent)
|
||||
|
||||
; width and height of the playing grid.
|
||||
(define GRID-WIDTH 6)
|
||||
|
@ -123,10 +124,6 @@
|
|||
(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
|
||||
(player
|
||||
|
@ -165,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 ((player-func (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))]
|
||||
|
@ -184,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.
|
||||
|
|
Loading…
Reference in New Issue