From 8a98223d67a54e074983fc666cfb86343aa6d565 Mon Sep 17 00:00:00 2001 From: nihilazo Date: Mon, 4 Apr 2022 14:12:17 +0100 Subject: [PATCH] make players into a struct so they can have GUI names --- main.rkt | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/main.rkt b/main.rkt index 18704ca..b711e8c 100644 --- a/main.rkt +++ b/main.rkt @@ -21,6 +21,12 @@ ; scores is a list of numbers for the score of each player. (struct GameState (grid player scores) #: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)) + ; width and height of the playing grid. (define GRID-WIDTH 6) (define GRID-HEIGHT 6) @@ -122,9 +128,12 @@ ; 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. @@ -167,7 +176,7 @@ (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 players p)) g p)] [new-grid (append-move g move)] [boxes (completes-boxes g move)] [box (not (= 0 boxes))]