r5106: Automatic commit for debian_version_1_0_6-1
[reversi.git] / io.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: reversi -*-
2 ;;;;***************************************************************************
3 ;;;;
4 ;;;; FILE IDENTIFICATION
5 ;;;; 
6 ;;;;  Name:           io.lisp
7 ;;;;  Purpose:        Basic Input-Output for reversi
8 ;;;;  Programer:      Kevin Rosenberg based on code by Peter Norvig
9 ;;;;  Date Started:   1 Nov 2001
10 ;;;;
11 ;;;; $Id: io.lisp,v 1.4 2003/06/12 12:42:13 kevin Exp $
12 ;;;;
13 ;;;; This file is Copyright (c) 2001-2003 by Kevin M. Rosenberg 
14 ;;;; and Copyright (c) 1998-2002 Peter Norvig
15 ;;;;
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 ;;;;***************************************************************************
20
21 (in-package #:reversi)
22
23
24 (eval-when (:compile-toplevel :load-toplevel :execute)
25 (let ((square-names 
26         (cross-product #'concat-symbol
27                        '(? A B C D E F G H ?)
28                        '(? 1 2 3 4 5 6 7 8 ?))))
29
30   (defun h8->88 (str)
31     "Convert from alphanumeric to numeric square notation."
32     (or (position (string str) square-names :test #'string-equal)
33         str))
34
35   (defun 88->h8 (num)
36     "Convert from numeric to alphanumeric square notation."
37     (if (valid-p num)
38         (elt square-names num)
39       num)))
40
41 (defun moves-to-string (moves)
42   (let (move-list)
43     (dotimes (i (length moves))
44       (push (format nil "~2d: ~a ~a~%"
45                     (1+ i)
46                     (title-of (nth 1 (elt moves i)))
47                     (symbol-name (88->h8 (nth 0 (elt moves i)))))
48             move-list))
49     (setq move-list (nreverse move-list))
50     (list-to-delimited-string move-list #\space))))
51
52 (defun human (player board)
53   "A human player for the game of Reversi"
54   (format t "~&~c to move ~a: " (name-of player)
55           (mapcar #'88->h8 (legal-moves player board)))
56   (h8->88 (read)))
57
58
59 (defun print-board (&optional (board *board*) clock)
60   "Print a board, along with some statistics."
61   ;; First print the header and the current score
62   (format t "~2&    A B C D E F G H   [~c=~2a ~c=~2a (~@d)]"
63           (name-of black) (count black board)
64           (name-of white) (count white board)
65           (count-difference black board))
66   ;; Print the board itself
67   (loop for row from 1 to 8 do
68         (format t "~&  ~d " row)
69         (loop for col from 1 to 8
70               for piece = (bref board (+ col (* 10 row)))
71               do (format t "~c " (name-of piece))))
72   ;; Finally print the time remaining for each player
73   (when clock
74     (format t "  [~c=~a ~c=~a]~2&"
75             (name-of black) (time-string (elt clock black))
76             (name-of white) (time-string (elt clock white)))))
77
78
79 (defun time-string (time)
80   "Return a string representing this internal time in min:secs."
81   (multiple-value-bind (min sec)
82       (floor (round time internal-time-units-per-second) 60)
83     (format nil "~2d:~2,'0d" min sec)))
84
85
86        
87