CS 2360 Assignment 5: Due Midnight Feb 27

Connect 4 (Part 1)

For this part of the assignment you need to write up the infrastructure necessary to play connect 4. If you are not familiar with the game, please see me. Also, see the newsgroup, as I am going to be providing a sample implementation of assignment for you to experiment with. You will actually be implementing your connect 4 playing program (in terms of the state-space search) next time (assignment 6).

For this assignment you need to:

Assumptions you can and can not make:

You may find the following piece of code a useful starting point in developing yourself a "driver" to test with. This function (from my source) assumes the existance of some functions not mentioned above, but they are clear enough that without much work you can probably get this to go. I issue no warranties either stated or implied about this code...

;; This is a driver function which will allow you to play connect 4 ;; using the computer to detect wins (defun c4-simple-driver () (let ( current-board current-column user-input user-color (first-move t) (turn 'R) (lookahead 2) ;; this is how many tree levels to expand ;;bind this to a larger value if you want a bigger board (board-size 8)) ;; make a new board (setq current-board (c4-gen-board board-size)) ;; ask the user his preference... (if (y-or-n-p "Do you want to be red and go first?") (setq user-color 'R) (setq user-color 'B)) ;; loop until we find a winning board... note we check the PREVIOUS ;; color since we get to the top of this loop at a point when the ;; color has already changed ;; this trick with the setq sets the mrb (most recent board) to ;; the one used in this loop... very convenient for debugging (loop until (or (c4-board-win-p (setq mrb current-board) board-size (c4-invert-color turn)) ;; this check is for stalemate (null (c4-legal-columns current-board board-size))) do ;; display the board (c4-print-board current-board board-size) ;; is it the user 's turn? (if (equal turn user-color) (setq current-column (loop ;; loop until we get a valid response (if (equal turn 'R) (format t "You are RED: ") (format t "You are BLACK: ")) (format t "Enter a column number to drop a piece into ") (format t "(1 to ~S):~%" board-size) (setq user-input (read)) ;; make sure its in the right range a member of the valid ;; set of possible choices (if (and (numberp user-input) (> user-input 0) ;; modify this line if you play on a larger board (<= user-input board-size) (member (1- user-input) (c4-legal-columns current-board board-size) :test #'equal)) (return (1- user-input))))) (progn ;; this implements the searching for the computer's turn (format t "%~%Thinking...~%") ;; if its the first time, just save time by not bothering... (if first-move (setq current-column (random board-size)) (setq current-column (c4-choose-column current-board board-size lookahead turn))) (format t "~%~%Computer chooses: ~S ~%" (1+ current-column)))) ;; make sure we are not on the first turn anymore ;; until you get your searching code done, leave this line ;; commented out so you always pick randomly ;; (setq first-move nil) ;; slide the piece in (setq current-board (c4-slide-in-column current-board board-size current-column turn)) ;; change turns (if (equal turn 'R) (setq turn 'B) (setq turn 'R))) ;; see what result to print (if (c4-legal-columns current-board board-size) ;; its not a stalemate (if (equal (c4-invert-color turn) user-color) (progn (c4-print-board current-board board-size) (format t "YOU WIN!~%")) (progn (c4-print-board current-board board-size) (format t "YOU GOT BEAT!~%"))) ;; it is a stalemate (format t "This game is a tie!~%"))))


Back To The CS2360 Home Page
Ian Smith (iansmith@cc.gatech.edu)

Last Modified 19 Feb 95 by Ian Smith (iansmith@cc.gatech.edu)