;;; random.ss ;;; A connect-5 player who simply moves to a random unoccupied location. ;;; ;; An on/off switch for any debugging statements. (define debugging-on false) (define program-name "random.ss") ; used in debugging. ;; debug: string, strings... --> (void) ;; Print a message (via printf) to stderr, if debugging-on. ;; (We can't print to stdout -- the referee awaits an answer there.) ;; (define (debug msg . args) (when debugging-on (let* {[msg-w-src (string-append program-name " debug: " msg "~n")] [arg-list (cons msg-w-src args)] ; The list of arguments for printf. [full-msg-string (apply format arg-list)]} (fprintf (current-error-port) full-msg-string)))) ;; unoccupied? : symbol --> boolean ;; Is (define (unoccupied? content) (symbol=? content '-)) ;(boolean=? (unoccupied? '-) true) ;(boolean=? (unoccupied? 'X) false) ;(boolean=? (unoccupied? 'O) false) ;(boolean=? (unoccupied? 'kiwi-fruit-with-the-skin-still-on) false) (define check-for-occupied true) (define (a-random-location board) (local [(define rows (length board)) (define cols (length (first board))) (define a-row (random rows)) (define a-col (random cols))] (if (or (not check-for-occupied) (unoccupied? (list-ref (list-ref board a-row) a-col))) (list a-col a-row) (a-random-location board)) )) ; Compute the answer, then print it: ; (printf "~s~n" (a-random-location (read))) #| (define board (read)) (debug "Read the board: ~s." board) (define answer (a-random-location board)) (debug "Returning answer ~s." answer) (printf "~s~n" answer) |#