The K-Zone: Perl downloads
Not much here at the moment; more to come when time allows, perhaps.
P-Reversi
P-Reversi (Perl reversi) is an implementation in object-oriented Perl
of the classic reversi game. It is implemented as a CGI script (or,
rather, a CGI script and a set of Perl classes) so it can be played with a
Web browser as the user interface. This application was written as a
case study for a course I am developing on object-oriented Perl
programming.
In operation, it looks something like this:
How to use
Perl reversi is very simple, and follows the same rules as the
standard board game. At present, the computer always plays black,
while the user always plays white and gets to move first. The program
displays squares that are legal moves with grey crosses in them.
When all the squares are full, it will tell you who won. You can
get a hint by clicking the `hint' link. You can start a new game by
clicking `new game'. Easy, isn't it?
Installation
To run Perl reversi, you will need a CGI-enabled Web server. You may
need to configure it to be able to run CGI scripts from the
appropriate directories.
Installation will depend on your Web server, and how it is set up to
run CGI scripts, so the following is only an outline.
- Create a directory called
Preversi (the name is
significant, including the upper-case `P') under the directory that
you normally use for CGI scripts. The usual default with Apache on
Linux, for example, is /home/httpd/cgi-bin.
Copy the .pl and .pm files from the Preversi directory
of the distribution into that directory.
- Make sure that
PreversiCgi.pl has execute permissions
for the account that owns the Web server
- Modify the first line of
PreversiCgi.pl so that the
first line references the location of the Preversi
directory. Note that the directory name should not include
Preversi itself, but the directory above it.
- Copy the images from the
images directory of
the distribution to wherever you normally store images on your Web
server.
- Edit the line
IMG_BASE= at the start of
PreversiCgi.pl to supply the base URL that would be
needed to prepend to the image filename to make a valid URL.
For example, if your Web server has images in a directory
images under the document root, then set
IMG_BASE to /images.
- Test by pointing your Web browser at
/cgi-bin/Preversi/PreversiCgi.pl
Download
Click here to download the source.
About the algorithm
Perl reversi uses a relatively simple min-max search algorithm, with
a depth of three levels. It probably won't be able to beat you if you
think about your moves. It probably will beat you if you play
carelessly. The algorithm works by constructing a tree of all possible
moves with the assumption that the players taking turns.
It then finds a move that tends
to optimize the average strength of the computer's position at the end
of three moves. To do
this it determines a `strength' of each board layout for the computer
player. Optimizing the routine that determines the strength is crucial
to the overall success of the algorithm and - guess what? - I haven't
spent much time on this yet. It knows that corners make good moves,
and that the squares next to corners tend to make bad moves, but
doesn't take into account the fact that the value of the square next
to a corner depends on who owns the corner. These enhancements, and
others, are planned for the next version. Because it is written in
Perl, a brute force increase in the search depth will probably not be
practical, but some heuristic pruning of the search tree may make it
possible to get a better effective search depth.
Legal stuff
Preversi is ©2003 Kevin Boone, distributed according to the terms
of the GNU Public Licence. The code is provided free of charge in the
hope that it will prove useful. There is no warranty of any kind. I am
pleased to receive bug reports, requests for additional functionality,
etc., but cannot promise to be able to act on them.
©1994-2006 Kevin Boone, all rights reserved