1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: reversi -*-
2 ;;;;***************************************************************************
4 ;;;; FILE IDENTIFICATION
7 ;;;; Purpose: Basic Input-Output for reversi
8 ;;;; Programer: Kevin Rosenberg based on code by Peter Norvig
9 ;;;; Date Started: 1 Nov 2001
11 ;;;; $Id: io.lisp,v 1.2 2002/10/25 09:23:39 kevin Exp $
13 ;;;; This file is Copyright (c) 2001-2002 by Kevin M. Rosenberg
14 ;;;; and Copyright (c) 1998-2002 Peter Norvig
16 ;;;; Reversi users are granted the rights to distribute and use this software
17 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
18 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
19 ;;;;***************************************************************************
22 (declaim (optimize (safety 1) (debug 3) (speed 3)))
25 (eval-when (:compile-toplevel :load-toplevel :execute)
27 (cross-product #'concat-symbol
28 '(? A B C D E F G H ?)
29 '(? 1 2 3 4 5 6 7 8 ?))))
32 "Convert from alphanumeric to numeric square notation."
33 (or (position (string str) square-names :test #'string-equal)
37 "Convert from numeric to alphanumeric square notation."
39 (elt square-names num)
42 (defun moves-to-string (moves)
44 (dotimes (i (length moves))
45 (push (format nil "~2d: ~a ~a~%"
47 (title-of (nth 1 (elt moves i)))
48 (symbol-name (88->h8 (nth 0 (elt moves i)))))
50 (setq move-list (nreverse move-list))
51 (list-to-delimited-string move-list #\space))))
53 (defun human (player board)
54 "A human player for the game of Reversi"
55 (format t "~&~c to move ~a: " (name-of player)
56 (mapcar #'88->h8 (legal-moves player board)))
60 (defun print-board (&optional (board *board*) clock)
61 "Print a board, along with some statistics."
62 ;; First print the header and the current score
63 (format t "~2& A B C D E F G H [~c=~2a ~c=~2a (~@d)]"
64 (name-of black) (count black board)
65 (name-of white) (count white board)
66 (count-difference black board))
67 ;; Print the board itself
68 (loop for row from 1 to 8 do
69 (format t "~& ~d " row)
70 (loop for col from 1 to 8
71 for piece = (bref board (+ col (* 10 row)))
72 do (format t "~c " (name-of piece))))
73 ;; Finally print the time remaining for each player
75 (format t " [~c=~a ~c=~a]~2&"
76 (name-of black) (time-string (elt clock black))
77 (name-of white) (time-string (elt clock white)))))
80 (defun time-string (time)
81 "Return a string representing this internal time in min:secs."
82 (multiple-value-bind (min sec)
83 (floor (round time internal-time-units-per-second) 60)
84 (format nil "~2d:~2,'0d" min sec)))