From: David Trudgett
Subject: Toy noughts and crosses program in Common Lisp
Date: 
Message-ID: <4137FA0D.1000809@yahoo.com>
Hi there all,

I'm just a newbie in Lisp myself, but on the off chance there is anyone 
here who may be interested, newbie or not, I've been playing around with 
a GUI + text-based noughts and crosses in Common Lisp, and have made the 
source code available at:

http://www.zeta.org.au/~wpower/dkt/programs/tictactoe.lisp

There is also a screenshot here:

http://www.zeta.org.au/~wpower/tic.png

should you wish to see what the GUI looks like. By the way, the GUI part 
of my program uses Peter Herth's cool LTk library, which has had some 
nice enhancements made to it for version 0.8.6 (see 
http://www.peter-herth.de/ltk/). Onya, Peter! ;-)

I don't have any instructions for Windows users yet, sorry, but the text 
mode is just standard Common Lisp, so should work anywhere. I've read 
elsewhere that people have got clisp and tcl working with LTk on 
Windows, which means that this game should, in theory, be playable on 
Windows.

It is with some pride that I point out that my program now plays to 
International Grand Master level. Impressive, eh? :-)

Catch ya

David Trudgett



-- 

We come here upon what, in a large proportion of cases, forms the
source of the grossest errors of mankind. Men on a lower level of
understanding, when brought into contact with phenomena of a higher
order, instead of making efforts to understand them, to raise
themselves up to the point of view from which they must look at the
subject, judge it from their lower standpoint, and the less they
understand what they are talking about, the more confidently and
unhesitatingly they pass judgment on it.

     -- Leo Tolstoy, "The Kingdom of God is Within You"

From: Alan Crowe
Subject: Re: Toy noughts and crosses program in Common Lisp
Date: 
Message-ID: <86fz5zxqw4.fsf@cawtech.freeserve.co.uk>
David Trudgett wrote:
> I've been playing around with a GUI + text-based noughts
> and crosses in Common Lisp

That was fun. I've not played noughts and crosses for twenty
years :-)

I've only tried the text-based version. It wouldn't load,
for lack of the ltk package, so I commented out the whole of
the defun starting

(defun tic-gui ()
  (use-package :ltk)...

Load also choked on

(defun gui-clear-canvas ()
  "Clear the canvas drawing area"
  ;; The definition of clearCanvas has already been sent to wish
  (ltk::send-wish "clearCanvas"))

so I also commented that out, then it loaded and ran, and
would not let me beat it :-(

Does it need something like

(when (find-package :ltk)
  (defun tic-gui () ...

to make both gui and text work without hacking the source?

Alan Crowe
Edinburgh
Scotland
From: Peter Herth
Subject: Re: Toy noughts and crosses program in Common Lisp
Date: 
Message-ID: <cha63o$ele$1@newsreader2.netcologne.de>
You can find Ltk here:
http://www.peter-herth.de/ltk/
(Should run almost everywhere)

Peter

-- 
pet project: http://dawn.netcologne.de
homepage:    http://www.peter-herth.de
lisp stuff:  http://www.peter-herth.de/lisp.html
get Ltk here: http://www.peter-herth.de/ltk/
From: Alan Crowe
Subject: Re: Toy noughts and crosses program in Common Lisp
Date: 
Message-ID: <86d613x9j0.fsf@cawtech.freeserve.co.uk>
Peter Herth wrote
> You can find Ltk here:
> http://www.peter-herth.de/ltk/
> (Should run almost everywhere)

Amazing! It even runs in the distant past. I am still
running CMUCL 18d on FreeBSD 4.1 from August 2000. Once I
had lied to ltk about my obsolete version of wish, with

ln -s wish8.2 wish8.4 

it sprang to life. I really must upgrade, I get filthy
shoveling coal into my computer's boiler. However, Ltk looks
really interesting, I'll just spend a little while studying
Ltk first ...

http://www.peter-herth.de/ltk/ltkdoc/node8.html tells me
that I should have set *wish-pathname* to "wish8.2" instead
of my awful hack with link.

Peter, I got a little confused because *wish-pathname* is
exported from the LTK package, but *wish-args* is NOT
exported. Is that really what you want?
(*ltk-version* => 0.861)

Alan
From: Peter Herth
Subject: Re: Toy noughts and crosses program in Common Lisp
Date: 
Message-ID: <chbr7c$7j2$1@newsreader2.netcologne.de>
Alan Crowe wrote:

> Amazing! It even runs in the distant past. I am still
> running CMUCL 18d on FreeBSD 4.1 from August 2000. Once I
> had lied to ltk about my obsolete version of wish, with

Yes the time-warp thingy was especially difficult to implement :)

> it sprang to life. I really must upgrade, I get filthy
> shoveling coal into my computer's boiler. However, Ltk looks
> really interesting, I'll just spend a little while studying
> Ltk first ...

I hope you have fun with it. If you have any questions or
suggestions, do not heasitate to contact me.
 
> http://www.peter-herth.de/ltk/ltkdoc/node8.html tells me
> that I should have set *wish-pathname* to "wish8.2" instead
> of my awful hack with link.
> 
> Peter, I got a little confused because *wish-pathname* is
> exported from the LTK package, but *wish-args* is NOT
> exported. Is that really what you want?
> (*ltk-version* => 0.861)

Oh, uhm, seems that I just overlooked it. I have added
*wish-args* to the exported variables now. 

Peter

-- 
pet project: http://dawn.netcologne.de
homepage:    http://www.peter-herth.de
lisp stuff:  http://www.peter-herth.de/lisp.html
get Ltk here: http://www.peter-herth.de/ltk/
From: David Trudgett
Subject: Re: Toy noughts and crosses program in Common Lisp
Date: 
Message-ID: <4138EF9F.3080601@yahoo.com>
Hi Alan!

Alan Crowe wrote:

> 
> That was fun. I've not played noughts and crosses for twenty
> years :-)

Glad you liked it! :-)

> 
> I've only tried the text-based version. It wouldn't load,
> for lack of the ltk package, so I commented out the whole of
> the defun starting

Sorry about that. I had a little similar trouble when I tried it in GCL, 
so I just created a dummy LTk package with nothing in it. Seemed to fix 
the problem. But I will follow your suggestion below for the next code 
revision. I didn't know how to do it before (or I'd forgotten, because I 
think I may actually have seen something like that). Thanks for the tip!


> 
> (defun tic-gui ()
>   (use-package :ltk)...
> 
> Load also choked on
> 
> (defun gui-clear-canvas ()
>   "Clear the canvas drawing area"
>   ;; The definition of clearCanvas has already been sent to wish
>   (ltk::send-wish "clearCanvas"))
> 
> so I also commented that out, then it loaded and ran, and
> would not let me beat it :-(


:-) It defaults to 'nightmare' mode, you know! ;-) The comments at the 
top of the file do suggest the following:

;;; (play-round)
;;; (play-round :num-games 5 :difficulty 'nightmare :size 'small)
;;; (play-round :size 'small :difficulty 'can-i-play)
;;; (play-round :num-games 15 :difficulty 'bring-it-on :size 'large)

The three difficulty levels are 'CAN-I-PLAY, 'BRING-IT-ON' AND 
'NIGHTMARE. The first is easy to beat, though you won't beat it every 
time. The second can be beaten, but you have to find out how. The third 
is unbeatable, I'm afraid. :-)


> 
> Does it need something like
> 
> (when (find-package :ltk)
>   (defun tic-gui () ...
> 
> to make both gui and text work without hacking the source?

I'll do that. Thanks.


By the way, LTk is just one Lisp source file, so it's not hard to try 
out. You'll want the 0.8.6 version from Peter Herth's site. If your 
system's TCL version is less than 8.4, you'll want to use a slightly 
modified version, which you can find at my website here:

http://www.zeta.org.au/~wpower/dkt/programs/ltk.lisp


That incorporates a one-liner fix to a fragment of TCL that didn't work 
under TCL 8.3. Peter has been good enough to incorporate the fix into 
the next release of LTk (which isn't available, yet).


Hope that helps!

David Trudgett
From: Vassil Nikolov
Subject: Re: Toy noughts and crosses program in Common Lisp
Date: 
Message-ID: <lzy8jpsj2u.fsf@janus.vassil.nikolov.names>
Alan Crowe <····@cawtech.freeserve.co.uk> writes:

> [...]
> (when (find-package :ltk)
>   (defun tic-gui () ...


  It would be better to use #+ syntax for the DEFUN form instead: in
  this way, there would be no problems with any references to a
  non-existent LTK package when the form is read, and the DEFUN form
  would stay at top level where it belongs.

  ---Vassil.


-- 
Vassil Nikolov <········@poboxes.com>

Hollerith's Law of Docstrings: Everything can be summarized in 72 bytes.
From: David Trudgett
Subject: Re: Toy noughts and crosses program in Common Lisp
Date: 
Message-ID: <413A5722.90908@yahoo.com>
Vassil Nikolov wrote:

> Alan Crowe <····@cawtech.freeserve.co.uk> writes:
> 
> 
>>[...]
>>(when (find-package :ltk)
>>  (defun tic-gui () ...
> 
> 
> 
>   It would be better to use #+ syntax for the DEFUN form instead: in
>   this way, there would be no problems with any references to a
>   non-existent LTK package when the form is read, and the DEFUN form
>   would stay at top level where it belongs.
> 
>   ---Vassil.
> 
> 

Good point, Vassil. So, I would do:

#+:ltk
(defun tic-gui ()  ....

I guess.

David Trudgett
From: Vassil Nikolov
Subject: Re: Toy noughts and crosses program in Common Lisp
Date: 
Message-ID: <lzpt51shnw.fsf@janus.vassil.nikolov.names>
David Trudgett <·····················@yahoo.com> writes:

> [...]
> #+:ltk
> (defun tic-gui ()  ....


  Whatever loading Ltk adds to *FEATURES* (I don't know what exactly
  it is), and if it adds none, you can easily do that for it, of
  course.

  ---Vassil.

-- 
Vassil Nikolov <········@poboxes.com>

Hollerith's Law of Docstrings: Everything can be summarized in 72 bytes.
From: Kalle Olavi Niemitalo
Subject: Re: Toy noughts and crosses program in Common Lisp
Date: 
Message-ID: <87eklhb7h9.fsf@Astalo.kon.iki.fi>
David Trudgett <·····················@yahoo.com> writes:

> #+:ltk

#+ltk would do the same thing: #+ and #- bind *PACKAGE* to the
KEYWORD package when they read the feature expression.
From: Christophe Rhodes
Subject: Re: Toy noughts and crosses program in Common Lisp
Date: 
Message-ID: <sq656pkfjp.fsf@cam.ac.uk>
[ Sorry for the late followup ]

Vassil Nikolov <········@poboxes.com> writes:

> Alan Crowe <····@cawtech.freeserve.co.uk> writes:
>
>> [...]
>> (when (find-package :ltk)
>>   (defun tic-gui () ...
>
>   It would be better to use #+ syntax for the DEFUN form instead: in
>   this way, there would be no problems with any references to a
>   non-existent LTK package when the form is read, and the DEFUN form
>   would stay at top level where it belongs.

I disagree, at least in part.  Used naively, #+ syntax has a tendency
to render source code unmaintainable and unreadable in the long run,
because the *FEATURES*, due both to implementations and libraries
changing, is a time-varying quantity.

Fortunately, a nice trick due to Helmut Eller allows read-time
conditionalization in a much more maintainable fashion:

#+#.(cl:if (cl:find-package :ltk) '(and) '(or))
(defun tic-gui () 
  ...)

The test for (find-package :ltk) could be refined to a more
fine-grained test if you are using one particular piece of
functionality, say just one operator: that way if new versions of the
ltk package were to float around with incompatible API -- or indeed if
someone completely different implements some completely different
functionality in an independent package named :ltk -- the code could
be conditionally compiled in in only the right circumstances
nontheless.

Christophe
From: David Trudgett
Subject: Re: Toy noughts and crosses program in Common Lisp
Date: 
Message-ID: <413A3D71.7040609@yahoo.com>
I've now posted an updated version incorporating a couple of handy 
suggestions from Alan Crowe and Bj�rn Lindberg.

The URLs are:

source: http://www.zeta.org.au/~wpower/dkt/programs/tictactoe.lisp
screenshot: http://www.zeta.org.au/~wpower/tic.png
LTk: http://www.peter-herth.de/ltk/ltk-0.8.6.tgz

If your TCL is earlier than version 8.4, use the following version of 
LTk for the time being (it incorporates a one-line patch that will be in 
the next release of LTk, thanks to Peter Herth):

http://www.zeta.org.au/~wpower/dkt/programs/ltk.lisp


Enjoy!

David Trudgett
From: Peter Herth
Subject: Re: Toy noughts and crosses program in Common Lisp
Date: 
Message-ID: <chefht$k44$1@newsreader2.netcologne.de>
David Trudgett wrote:

> If your TCL is earlier than version 8.4, use the following version of
> LTk for the time being (it incorporates a one-line patch that will be in
> the next release of LTk, thanks to Peter Herth):
> 
> http://www.zeta.org.au/~wpower/dkt/programs/ltk.lisp

*cough* I already incorperated your patch in the "official" 0.8.6 version
(which is actually 0.8.6.1) :)

Peter 

-- 
pet project: http://dawn.netcologne.de
homepage:    http://www.peter-herth.de
lisp stuff:  http://www.peter-herth.de/lisp.html
get Ltk here: http://www.peter-herth.de/ltk/
From: David Trudgett
Subject: Re: Toy noughts and crosses program in Common Lisp
Date: 
Message-ID: <413B8E55.90206@yahoo.com>
Peter Herth wrote:
> David Trudgett wrote:
> 
> 
>>If your TCL is earlier than version 8.4, use the following version of
>>LTk for the time being (it incorporates a one-line patch that will be in
>>the next release of LTk, thanks to Peter Herth):
>>
>>http://www.zeta.org.au/~wpower/dkt/programs/ltk.lisp
> 
> 
> *cough* I already incorperated your patch in the "official" 0.8.6 version
> (which is actually 0.8.6.1) :)
> 
> Peter 
> 

Sorry, I didn't look because the file name was the same. You're too 
efficient! Thanks again.

David



--
David Trudgett
From: David Trudgett
Subject: Re: Toy noughts and crosses program in Common Lisp
Date: 
Message-ID: <41496CC6.1000403@yahoo.com>
David Trudgett wrote:
> 
> I'm just a newbie in Lisp myself, but on the off chance there is anyone 
> here who may be interested, newbie or not, I've been playing around with 
> a GUI + text-based noughts and crosses in Common Lisp, and have made the 
> source code available at:
> 
> http://www.zeta.org.au/~wpower/dkt/programs/tictactoe.lisp
> 
> There is also a screenshot here:
> 
> http://www.zeta.org.au/~wpower/tic.png
> 

Since my last post, I have tested the program under Windows and added 
instructions for Windows users. I've also made a few incremental 
improvements here and there.

Thanks to Alan Crowe, Bjorn Lindberg, Vassil Nikolov and Christophe 
Rhodes for your helpful suggestions. I've included your names in the 
credits. If you don't want it there, let me know. Thanks also to Peter 
Herth for incorporating a couple of my truly, genuinely and utterly 
trivial and inconsequential suggestions into LTk. (There's no smiley 
there, because I'm not joking :-)

David