Skip to content

Instantly share code, notes, and snippets.

@kmicinski
Created January 28, 2026 20:39
Show Gist options
  • Select an option

  • Save kmicinski/e9df2994580d7d6d76ce82e4ca27cdcb to your computer and use it in GitHub Desktop.

Select an option

Save kmicinski/e9df2994580d7d6d76ce82e4ca27cdcb to your computer and use it in GitHub Desktop.
#lang racket
;; Project 0 Tic-tac-toe with Racket
;;
;; Please immediately read README.md
(provide board?
next-player
valid-move?
make-move
winner?
calculate-next-move)
;;
;; Useful utility functions
;;
;; Returns the number of elements in l for which the predicate f
;; evaluates to #t. For example:
;;
;; (count (lambda (x) (> x 0)) '(-5 0 1 -3 3 4)) => 3
;; (count (lambda (x) (= x 0)) '(-5 0 1 -3 3 4)) => 1
;;
;; This function is useful for later parts, especially winner?, where
;; you can use it to count if the length of the row is equal to the
;; number of elements in the row containing 'X (or 'O).
(define (count f l)
(cond [(empty? l) 0]
[(f (car l)) (add1 (count f (cdr l)))]
[else (count f (cdr l))]))
;;
;; Your solution begins here
;;
;; Check whether a list is a valid board
(define (board? lst)
(define xcount (count (lambda (x) (equal? x 'X)) lst))
(define ocount (count (lambda (x) (equal? x 'O)) lst))
(define ecount (count (lambda (x) (equal? x 'E)) lst))
(and
(integer? (sqrt(length lst)))
(or (= xcount ocount)(= xcount (add1 ocount))); if x count = ocount, good, if ocount + 1
;;equals xcount also good
(= (+ xcount ocount ecount) (length lst))))
;; From the board, calculate who is making a move this turn
(define (next-player board)
(if (> (count (lambda (x) (equal? x 'X)) board)
(count (lambda (e) (equal? e 'O)) board))
'O
'X))
(define (get-cell board r c)
(define size (sqrt (length board)))
(list-ref board (+ (* r size) c)))
;; If player ('X or 'O) want to make a move, check whether it's this
;; player's turn and the position on the board is empty ('E)
(define (valid-move? board row col player)
(and (list? board)
(number? row)
(number? col)
(or (equal? player 'X) (equal? player 'O))
;; using (next-player board), how do we know when it's player's turn?
(equal? (next-player board) player)
(< row (sqrt (length board)))
(< col (sqrt (length board)))
(>= row 0)
(>= col 0)
(equal? (get-cell board row col) 'E)))
;; To make a move, replace the position at (row,col) to the player's
;; (either 'X or 'O).
;;
;; Hint: the board is a one-dimensional list. Figure out a formula for
;; (row,col) in terms of the board and use list-set.
(define (make-move board row col player)
'todo)
;; Determine whether or not there is a winner
;;
;; Hint: write a function to grab all rows, all columns, and the
;; diagonals. Then check if any of them (consider using `ormap`) has
;; the property that the count of cells with 'X (or 'O) is equal to
;; its length.
(define (winner? board)
'todo)
;;
;; BONUS part! Do not do *attempt* this part until you have 100% on
;; the autograder! (Reach out to kkmicins@syr.edu to grade)
;;
;; The board is the list containing E O X
;; Player will always be 'O
;; returns a pair of x and y
(define (calculate-next-move board player)
'todo)
#;(define (list-set l i x)
(if (equal? i 0)
(cons x (rest l))
(cons (first l) (list-set (rest l) (- i 1) x))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment