lots of structural changes, cleaning up to make ready for real dev
This commit is contained in:
		
							parent
							
								
									331c626ca8
								
							
						
					
					
						commit
						40040c4461
					
				|  | @ -0,0 +1,66 @@ | |||
| (in-package :board) | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| (defun make-2d-board (size &optional (initial nil)) | ||||
|   (let ((array (make-array size))) | ||||
|     (dotimes (i size) | ||||
|       (setf (aref array i) (make-array size :initial-element initial))) | ||||
|     array)) | ||||
| 
 | ||||
| (defun copy-2d-board (board) | ||||
|   (let ((copy (make-array (length board)))) | ||||
|     (dotimes (i (length board)) | ||||
|       (setf (aref copy i) (copy-seq (aref board i)))) | ||||
|     copy)) | ||||
| 
 | ||||
| 
 | ||||
| 	       | ||||
| 
 | ||||
| (defun filter-i-number (number) | ||||
|   (if (> number 8)  | ||||
|       (1- number) | ||||
|       number)) | ||||
| 
 | ||||
| (defun str-to-coord (str) | ||||
|   `( ,(filter-i-number (- (char-code (char (string-upcase str) 0)) 65)) ,(- (parse-integer (subseq str 1)) 1))) | ||||
| 
 | ||||
| (defun filter-i-char (number) | ||||
|   (if (>= number 8) | ||||
|       (1+ number) | ||||
|       number)) | ||||
| 
 | ||||
| (defun coord-to-str (coord) | ||||
|   (concatenate 'string (string (code-char (+ 65 (filter-i-char (first coord))))) | ||||
| 		(write-to-string (+ (second coord) 1)))) | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| (defun get-stone (board coord) | ||||
|   (aref (aref board (first coord)) (second coord))) | ||||
| 
 | ||||
| (defun set-stone (board coord val) | ||||
|   (setf (aref (aref board (first coord)) (second coord)) val)) | ||||
| 
 | ||||
|    | ||||
| 
 | ||||
| (defclass  board () | ||||
|   ((boardsize | ||||
|     :initarg boardsize | ||||
|    ; :initform *boardsize* | ||||
|     :accessor boardsize) | ||||
|    (board-def-type | ||||
|     :initarg board-def-type | ||||
|     :initform nil | ||||
|     :accessor board-def-type) | ||||
|    (board | ||||
|     :accessor board))) | ||||
| 
 | ||||
| (defmethod initialize-instance :after ((board board) &key (from-board nil)) | ||||
|   (if (eql from-board nil) | ||||
|       (setf (board-def-type board) (make-board (boardsize board) (board-def-type board))) | ||||
|       (progn | ||||
| 	(setf (boardsize board) (boardsize from-board)) | ||||
| 	(setf (board-def-type board) (board-def-type from-board)) | ||||
| 	(setf (board board) (copy-2d-board (board from-board)))))) | ||||
							
								
								
									
										2
									
								
								env.lisp
								
								
								
								
							
							
						
						
									
										2
									
								
								env.lisp
								
								
								
								
							|  | @ -12,6 +12,8 @@ | |||
| (defparameter *src-root* "/home/dan/src/my/gobot/") | ||||
| 
 | ||||
| (load (compile-file (concatenate 'string *src-root* "packages.lisp"))) | ||||
| (load (compile-file (concatenate 'string *src-root* "netpipe.lisp"))) | ||||
| (load (compile-file (concatenate 'string *src-root* "board.lisp"))) | ||||
| (load (compile-file (concatenate 'string *src-root* "gobot.lisp"))) | ||||
| (load (compile-file (concatenate 'string *src-root* "gtp.lisp"))) | ||||
| 
 | ||||
|  |  | |||
|  | @ -0,0 +1,9 @@ | |||
| (in-package :common-lisp) | ||||
| 
 | ||||
| (defparameter *src-root* "/home/dan/src/my/gobot/") | ||||
| 
 | ||||
| (load (concatenate 'string *src-root* "packages.fasl")) | ||||
| (load (concatenate 'string *src-root* "netpipe.fasl")) | ||||
| (load (concatenate 'string *src-root* "board.fasl")) | ||||
| (load (concatenate 'string *src-root* "gobot.fasl")) | ||||
| (load (concatenate 'string *src-root* "gtp.fasl")) | ||||
							
								
								
									
										31
									
								
								gobot.lisp
								
								
								
								
							
							
						
						
									
										31
									
								
								gobot.lisp
								
								
								
								
							|  | @ -1,8 +1,9 @@ | |||
| (in-package :go-bot) | ||||
| 
 | ||||
| (defparameter *name* "gobot") | ||||
| (defparameter *version* "0.01") | ||||
| (defparameter *name* "fink") | ||||
| (defparameter *version* "0.2.0-dev") | ||||
| (defparameter *author* "Dan Ballard") | ||||
| 
 | ||||
| (defparameter *default-komi* 5.5) | ||||
| (defparameter *komi* *default-komi*) | ||||
| (defparameter *default-boardsize* 19) | ||||
|  | @ -11,16 +12,10 @@ | |||
| (defparameter *board* nil) | ||||
| 
 | ||||
| (defparameter *score-functions* '( (score-unused 1))) | ||||
| 
 | ||||
| (defparameter *passed* nil) | ||||
| (defparameter *player* nil) | ||||
| (defparameter *last-player* nil) | ||||
| 
 | ||||
| (defun make-board (size &optional (initial nil)) | ||||
|   (let ((array (make-array size))) | ||||
|     (dotimes (i size) | ||||
|       (setf (aref array i) (make-array size :initial-element initial))) | ||||
|     array)) | ||||
| 
 | ||||
| (defun set-komi (new-komi) | ||||
|   (setf *komi* new-komi)) | ||||
|  | @ -37,30 +32,10 @@ | |||
|   ;(init other game specific stuff) | ||||
|   (init-board)) | ||||
| 
 | ||||
| (defun filter-i-number (number) | ||||
|   (if (> number 8)  | ||||
|       (1- number) | ||||
|       number)) | ||||
| 
 | ||||
| (defun str-to-coord (str) | ||||
|   `( ,(filter-i-number (- (char-code (char (string-upcase str) 0)) 65)) ,(- (parse-integer (subseq str 1)) 1))) | ||||
| 
 | ||||
| (defun filter-i-char (number) | ||||
|   (if (>= number 8) | ||||
|       (1+ number) | ||||
|       number)) | ||||
| 
 | ||||
| (defun coord-to-str (coord) | ||||
|   (concatenate 'string (string (code-char (+ 65 (filter-i-char (first coord))))) | ||||
| 		(write-to-string (+ (second coord) 1)))) | ||||
|      | ||||
| 
 | ||||
| (defun get-stone (board coord) | ||||
|   (aref (aref board (first coord)) (second coord))) | ||||
| 
 | ||||
| (defun set-stone (board coord val) | ||||
|   (setf (aref (aref board (first coord)) (second coord)) val)) | ||||
|    | ||||
| 
 | ||||
| (defun play (player coord-str) | ||||
|   (setf *last-player* player) | ||||
|  |  | |||
							
								
								
									
										5
									
								
								gobot.sh
								
								
								
								
							
							
						
						
									
										5
									
								
								gobot.sh
								
								
								
								
							|  | @ -1,4 +1,7 @@ | |||
| #!/bin/sh  | ||||
| #echo $1 $2 | ||||
| /usr/bin/sbcl --noinform  --load /home/dan/src/my/gobot/packages.fasl --load /home/dan/src/my/gobot/gobot.fasl /home/dan/src/my/gobot/gtp.fasl --eval "(progn (gtp-handler:gtp-net-client \"$1\" $2) (quit))"  2>&1 /dev/null | ||||
| #/usr/bin/sbcl --noinform  --load /home/dan/src/my/gobot/packages.lisp --load /home/dan/src/my/gobot/gobot.lisp /home/dan/src/my/gobot/gtp.lisp --eval "(progn (gtp-handler:gtp-net-client \"$1\" $2) (quit))"  2>&1 /dev/null | ||||
| 
 | ||||
| /usr/bin/sbcl --noinform --load /home/dan/src/my/gobot/fink.lisp --eval "(progn (gtp-handler:gtp-net-client \"$1\" $2) (quit))" | ||||
| 
 | ||||
| #/usr/bin/sbcl --noinform --load /home/dan/src/my/gobot/env.lisp --eval "(progn (gtp-handler:gtp-client) (quit))" | ||||
|  |  | |||
							
								
								
									
										41
									
								
								gtp.lisp
								
								
								
								
							
							
						
						
									
										41
									
								
								gtp.lisp
								
								
								
								
							|  | @ -1,51 +1,10 @@ | |||
| (in-package :gtp-handler) | ||||
| 
 | ||||
| ;(require :sb-bsd-sockets) | ||||
| 
 | ||||
| 
 | ||||
| (defparameter *quit?* nil) | ||||
| ;(defparameter *cputime*) | ||||
| 
 | ||||
| 
 | ||||
| (defun nslookup (hostname) | ||||
|    "Performs a DNS look up for HOSTNAME and returns the address as a | ||||
|    four element array, suitable for socket-connect.  If HOSTNAME is | ||||
|    not found, a host-not-found-error condition is thrown." | ||||
|    (if hostname | ||||
|        (sb-bsd-sockets:host-ent-address (sb-bsd-sockets:get-host-by-name hostname)) | ||||
|        nil))  | ||||
| 
 | ||||
| (defun tcp-connect (server port &optional (timeout 10)) | ||||
|   (handler-case | ||||
|       (let ((socket (make-instance 'sb-bsd-sockets:inet-socket :type :stream :protocol :tcp)))  | ||||
| 	(sb-bsd-sockets:socket-connect socket  (nslookup server) port)  | ||||
| 	socket) | ||||
|     (sb-bsd-sockets:CONNECTION-REFUSED-ERROR ()  | ||||
|       (progn  | ||||
| 	(format t "Error: Connection refused~%")  | ||||
| 	nil)))) | ||||
| 		 | ||||
| 
 | ||||
| (defun tcp-print-raw (socket line) | ||||
|   (when (and socket line) | ||||
|     (sb-bsd-sockets:socket-send socket line nil))) | ||||
| 
 | ||||
| (defun tcp-print (socket line) | ||||
|   (tcp-print-raw socket (concatenate 'string (format nil "~04d" (length line)) line))) | ||||
| 
 | ||||
| (defun tcp-read-raw (socket &key (maxsize 65536) (timeout 10)) | ||||
|    (when socket | ||||
|      (values (sb-bsd-sockets:socket-receive socket nil maxsize)))) | ||||
| 
 | ||||
| ;(if-timeout (timeout (format t "socket-receive timed out after ~A seconds.~%" timeout) (force-output) nil) | ||||
| 
 | ||||
| (defun tcp-read (socket &key (timeout 10)) | ||||
|    (when socket | ||||
|      (let ((len (parse-integer (tcp-read-raw socket :maxsize 4 :timeout timeout)))) | ||||
|        (tcp-read-raw socket :maxsize len :timeout timeout)))) | ||||
| 	 | ||||
|         | ||||
| 
 | ||||
| 
 | ||||
| (defun gtp-net-client (server port) | ||||
|   (go-bot:init) | ||||
|  |  | |||
|  | @ -172,7 +172,7 @@ int main(int argc,char **argv) | |||
| 	pid = fork(); | ||||
| 	if (pid == 0) { | ||||
| 		//sprintf(buf, "/home/dan/src/my/gobot/gobot.sh 127.0.0.1 %d 2>&1 /dev/null\n",  TCPPORT);
 | ||||
| 		sprintf(buf, "/usr/bin/sbcl --noinform --load /home/dan/src/my/gobot/packages.fasl  --load /home/dan/src/my/gobot/gobot.fasl  --load /home/dan/src/my/gobot/gtp.fasl --eval '(progn (gtp-handler:gtp-net-client \"127.0.0.1\" %d) (quit))' \n", TCPPORT); | ||||
| 		sprintf(buf, "/usr/bin/sbcl --noinform --load /home/dan/src/my/gobot/fink.fasl --eval '(progn (gtp-handler:gtp-net-client \"127.0.0.1\" %d) (quit))' \n", TCPPORT); | ||||
| 
 | ||||
| 		//printf("%s\n", buf);
 | ||||
| 		system(buf); | ||||
|  |  | |||
|  | @ -0,0 +1,41 @@ | |||
| (in-package :netpipe) | ||||
| 
 | ||||
| (defun nslookup (hostname) | ||||
|    "Performs a DNS look up for HOSTNAME and returns the address as a | ||||
|    four element array, suitable for socket-connect.  If HOSTNAME is | ||||
|    not found, a host-not-found-error condition is thrown." | ||||
|    (if hostname | ||||
|        (sb-bsd-sockets:host-ent-address (sb-bsd-sockets:get-host-by-name hostname)) | ||||
|        nil))  | ||||
| 
 | ||||
| (defun tcp-connect (server port); &optional (timeout 10)) | ||||
|   (handler-case | ||||
|       (let ((socket (make-instance 'sb-bsd-sockets:inet-socket :type :stream :protocol :tcp)))  | ||||
| 	(sb-bsd-sockets:socket-connect socket  (nslookup server) port)  | ||||
| 	socket) | ||||
|     (sb-bsd-sockets:CONNECTION-REFUSED-ERROR ()  | ||||
|       (progn  | ||||
| 	(format t "Error: Connection refused~%")  | ||||
| 	nil)))) | ||||
| 		 | ||||
| 
 | ||||
| (defun tcp-print-raw (socket line) | ||||
|   (when (and socket line) | ||||
|     (sb-bsd-sockets:socket-send socket line nil))) | ||||
| 
 | ||||
| (defun tcp-print (socket line) | ||||
|   (tcp-print-raw socket (concatenate 'string (format nil "~04d" (length line)) line))) | ||||
| 
 | ||||
| (defun tcp-read-raw (socket &key (maxsize 65536)); (timeout 10)) | ||||
|    (when socket | ||||
|      (values (sb-bsd-sockets:socket-receive socket nil maxsize)))) | ||||
| 
 | ||||
| ;(if-timeout (timeout (format t "socket-receive timed out after ~A seconds.~%" timeout) (force-output) nil) | ||||
| 
 | ||||
| (defun tcp-read (socket &key (timeout 10)) | ||||
|    (when socket | ||||
|      (let ((len (parse-integer (tcp-read-raw socket :maxsize 4 :timeout timeout)))) | ||||
|        (tcp-read-raw socket :maxsize len :timeout timeout)))) | ||||
| 	 | ||||
|         | ||||
| 
 | ||||
|  | @ -4,13 +4,32 @@ | |||
| ;(asdf:oos 'asdf:load-op :cl-ppcre) | ||||
| (require :sb-bsd-sockets) | ||||
| 
 | ||||
| (defpackage gtp-handler | ||||
| (defpackage netpipe | ||||
|   (:use :common-lisp) | ||||
|   (:export :tcp-connect | ||||
| 	   :nslookup | ||||
| 	   :tcp-print | ||||
| 	   :tcp-read)) | ||||
| 
 | ||||
| 
 | ||||
| (defpackage gtp-handler | ||||
|   (:use :common-lisp | ||||
| 	:netpipe) | ||||
|   (:export :gtp-client | ||||
| 	   :gtp-net-client)) | ||||
| 
 | ||||
| (defpackage go-bot | ||||
| (defpackage board | ||||
|   (:use :common-lisp) | ||||
|   (:export :board | ||||
| 	   :get-stone | ||||
| 	   :set-stone | ||||
| 	   :make-board | ||||
| 	   :coord-to-str | ||||
| 	   :str-to-coord)) | ||||
| 
 | ||||
| (defpackage go-bot | ||||
|   (:use :common-lisp | ||||
| 	:board) | ||||
|   (:export :*name* | ||||
| 	    :*version* | ||||
| 	    :*author* | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue