From 40040c4461c48de1fb73c0259c42acec1a3dc679 Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 16 May 2008 15:17:38 -0700 Subject: [PATCH] lots of structural changes, cleaning up to make ready for real dev --- board.lisp | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ env.lisp | 2 ++ fink.lisp | 9 +++++++ gobot.lisp | 31 +++--------------------- gobot.sh | 5 +++- gtp.lisp | 41 -------------------------------- ip2gtp.cpp | 2 +- netpipe.lisp | 41 ++++++++++++++++++++++++++++++++ packages.lisp | 23 ++++++++++++++++-- 9 files changed, 147 insertions(+), 73 deletions(-) create mode 100644 board.lisp create mode 100644 fink.lisp create mode 100644 netpipe.lisp diff --git a/board.lisp b/board.lisp new file mode 100644 index 0000000..7a8e39e --- /dev/null +++ b/board.lisp @@ -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)))))) \ No newline at end of file diff --git a/env.lisp b/env.lisp index f0195f5..39c12d7 100644 --- a/env.lisp +++ b/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"))) diff --git a/fink.lisp b/fink.lisp new file mode 100644 index 0000000..bd9bd3c --- /dev/null +++ b/fink.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")) \ No newline at end of file diff --git a/gobot.lisp b/gobot.lisp index 3a2a65f..82c661d 100644 --- a/gobot.lisp +++ b/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) diff --git a/gobot.sh b/gobot.sh index 0dce325..2088398 100755 --- a/gobot.sh +++ b/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))" diff --git a/gtp.lisp b/gtp.lisp index fc77d31..449c238 100644 --- a/gtp.lisp +++ b/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) diff --git a/ip2gtp.cpp b/ip2gtp.cpp index b1e5e5c..255ab4b 100644 --- a/ip2gtp.cpp +++ b/ip2gtp.cpp @@ -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); diff --git a/netpipe.lisp b/netpipe.lisp new file mode 100644 index 0000000..60e4ff9 --- /dev/null +++ b/netpipe.lisp @@ -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)))) + + + diff --git a/packages.lisp b/packages.lisp index ca14f22..e451141 100644 --- a/packages.lisp +++ b/packages.lisp @@ -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*