Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Web Based Chess Server

by wombat (Curate)
on Sep 15, 2000 at 23:38 UTC ( #32724=sourcecode: print w/replies, xml ) Need Help??
Category: Fun Stuff
Author/Contact Info wombat
Description: A friend of mine recently learned that I suck at chess. (Only three people on the planet play worse than me) So she took it upon herself to try and teach me. Cordially, she offered to bring a chess set to a local coffeehouse where we could play amid the distraught oil paintings and the low jazz music playing in the background, but I had already gone and built this instead.

What this will do, is allow two people to play a single game of chess, but it could be easily adapted to support multiple boards, (i.e a hidden field in the form code). It's not too hard to set up, and all one needs to do is provide images for the squares and the pieces. These are infinitely customizable. I crafted the pieces from pics of members of the Scifi club I'm involved in, (and it was immensly popular). To see how I set it up on my home machine, go here. I do however ask that you be kind and not move any of the pieces around. This is our game we're playing. Besides, I'll be able to tell if you do.

There is limited error checking. Currently this version will check to see that it really is your turn to move, and that you're trying to actually move a piece (not empty space) and that you're not moving it off the board. However, it does not check for move legality. Thus, a person can in fact move the queen all the way across the board, jump the two rows of pawns, step once to the right, and take the opposing king on the very first move. I chose not to include error checking, because there are lots of different variations of chess thatdo not follow the rules. Most notably, "Fairy Chess" where the board wraps on the sides, (or is played on a 2D torus depending how you think about it.) So moves checking was left out to allow variations like that in.

Again, I'm notoriously bad at chess. And the notation in the logs shows this. People have explained it all to me before, but it never took. I know about castleing, but you'll have to ask whoever the owner of the machine is to change the board when you want to do it. I've heard of "En Passant" passing by pawns, same thing, 'cept I have no clue about the rules surrounding that. In any case I hope you like it!

This file is the ascii representation of the board that the program uses to keep track of the pieces. It's simply...
Where 0=black square, 1=white square, letter=piece, capital=white piece. The last digit indicates turn, 0=black, 1=white. The CGI code is as follows.

#!/usr/bin/perl -w
use CGI;
$q = new CGI;

$fx = $q->param('fx');
$fy = $q->param('fy');
$tx = $q->param('tx');
$ty = $q->param('ty');
$mesg = $q->param('mesg');

$fx--;   #Because the average non-geek doesn't like thinking in
$tx--;   #terms of 0-7.

print "Content-type: text/html\n\n";
$dir[0] = '/home/httpd/html/chess/board1';
$dir[1] = '/home/httpd/html/chess/board1.intransit';
$dir[2] = '/home/httpd/html/chess/log.html';
$dir[3] = '/home/httpd/html/chess/newboard1.html';
$dir[4] = '/home/httpd/html/chess/board1.html';

open (BRD, "$dir[0]") or die "Can't open that one sir!$!\n";
open (nBRD, ">$dir[1]") or die "Can't write to that one sir$!!\n";
open (LOG, ">>$dir[2]") or die "Can't write the log!$!\n";
open (NB, ">$dir[3]") or die "Can't write the new board!$!\n";

for (0..8)          #Read in the ACSII version of the board
  {                 #and assemble the structure.
    $in = <BRD>;
    chop $in;
    push @board, split //, $in;

$piece = $board[$fy*8+$fx];   #Sanity Czechs.
if (($piece =~ /[bknpqr]/)&&($board[64]==1)) {&not_your_turn;}
if (($piece =~ /[BKNPRQ]/)&&($board[64]==0)) {&not_your_turn;}
if ($piece =~ /[01]/) {&no_piece_there;}
if (($fx>7)||($fy>7)||($tx>7)||($ty>7)) {&out_of_bounds;}
if (($fx<0)||($fy<0)||($tx<0)||($ty<0)) {&out_of_bounds;}
$board[$ty*8+$tx] = $piece;
if (($fy%2)xor($fx%2))
 {$board[$fy*8+$fx] = 0;}
else  {$board[$fy*8+$fx] = 1;}
if ($board[64]==1){print LOG "<font color=#ffffff>White ";}   
elsif ($board[64]==0){print LOG "<font color=#ff0000>Black ";}

print NB  <<"endhere";
<html><body bgcolor=#000000 text=ff0000 link=ffff00 vlink=ffff00>
<table border=0 cellpadding=0 cellspacing=0>
<tr><td align=absmiddle>1</td>

for(0..63)     #Build the html of the new board.
  $_ = $board[$_];
  print nBRD $_;
  if ($_ eq '0'){print NB "<td><img src=b.JPG></td>\n"}
  elsif ($_ eq '1'){print NB "<td><img src=w.JPG></td>\n"}
  elsif ($_ eq 'p'){print NB "<td><img src=bp.JPG></td>\n"}
  elsif ($_ eq 'P'){print NB "<td><img src=wp.JPG></td>\n"}
  elsif ($_ eq 'n'){print NB "<td><img src=bk.JPG></td>\n"}
  elsif ($_ eq 'N'){print NB "<td><img src=wk.JPG></td>\n"}
  elsif ($_ eq 'r'){print NB "<td><img src=br.JPG></td>\n"}
  elsif ($_ eq 'R'){print NB "<td><img src=wr.JPG></td>\n"}
  elsif ($_ eq 'b'){print NB "<td><img src=bb.JPG></td>\n"}
  elsif ($_ eq 'B'){print NB "<td><img src=wb.JPG></td>\n"}
  elsif ($_ eq 'q'){print NB "<td><img src=bq.JPG></td>\n"}
  elsif ($_ eq 'Q'){print NB "<td><img src=wq.JPG></td>\n"}
  elsif ($_ eq 'k'){print NB "<td><img src=bg.JPG></td>\n"}
  elsif ($_ eq 'K'){print NB "<td><img src=wg.JPG></td>\n"}
  if ($count==8)
    if($linecount<9){print nBRD "\n";
     print NB "</tr><tr><td align=absmiddle>$linecount</td>";}
if (!$board[64]){$board[64]=1;}
elsif ($board[64]){$board[64]=0;}
{print nBRD "\n$board[64]\n";}
print NB <<"end_of_text";

<td valign=top>
<font size=+2>PERLMONKS CHESS</font><br>
<font size=-2>
"I once held the opinion that magnificent pieces allowed a person to p
a magnificent game.  I quickly learned that my opinion was wrong."

<table border=2>
<tr><td><img align=absmiddle src=b.JPG>Black square</td>
<td><img align=absmiddle src=w.JPG>White square</td></tr>
<tr><td><img align=absmiddle src=bp.JPG>Pawn</td>
<td><img align=absmiddle src=br.JPG>Rook</td></tr>
<tr><td><img align=absmiddle src=bk.JPG>Knight</td>
<td><img align=absmiddle src=bb.JPG>Bishop</td></tr>
<tr><td><img align=absmiddle src=bq.JPG>Queen</td>
<td><img align=absmiddle src=bg.JPG>King</td></tr>
<a href=log.html>See the log of moves and dialogue.</a>

<form method=POST action=
<tr><td>From Y:</td><td>
<input type=text name="fy" size=1 maxlength=1></td>
<td>From X:</td><td>
<input type=text name="fx" size=1 maxlength=1></td></tr><br>
<tr><td>To Y:</td><td>
<input type=text name="ty" size=1 maxlength=1></td>
<td>To X:</td><td>
<input type=text name="tx" size=1 maxlength=1></td></tr><br>
<tr><td colspan=4>
Do you have anything to say to your opponent?<br>
<textarea name=mesg cols=40 rows=5></textarea></td></tr>
<tr><td colspan=4><input type=submit value="Move it or lose it!">
<input type=reset></td></tr>

if ($piece =~ '[pP]'){print LOG "Pawn --";}
if ($piece =~ '[bB]'){print LOG "Bishop --";}
if ($piece =~ '[kK]'){print LOG "King --";}
if ($piece =~ '[nN]'){print LOG "Knight --";}
if ($piece =~ '[rR]'){print LOG "Rook --";}
if ($piece =~ '[qQ]'){print LOG "Queen --";}
print LOG "$fy x $fx to $ty x $tx<br>\n";
print LOG "$mesg</font><br><br><br>\n";

$cmd = "mv $dir[3] $dir[4]";
system ($cmd);
$cmd = "mv $dir[1] $dir[0]";
system ($cmd);

print <<"HTML";
document.location = '';
<body bgcolor=#000000 text=ffffff>
Okay! You moved, have a nice day. <br>
<a href=>Click here to re
to the board</a>

sub no_piece_there
  print "There isn't any piece there.  Do not smoke crack while playin
+g chess.\$
  exit (0);

sub not_your_turn
  print "Not your turn, go away. :-P\n";
  exit (0);
sub out_of_bounds
  print "Invalid move, try again.";
  exit (0);
Replies are listed 'Best First'.
RE: Web Based Chess Server
by isotope (Chaplain) on Sep 16, 2000 at 04:11 UTC
    You might want to remove the absolute links you've embedded in your code so people who download and install without reading the source won't hit your server 8-)

    I like the idea.
Re: Web Based Chess Server
by Anonymous Monk on Apr 27, 2004 at 22:55 UTC
    I also such at chess, so naturally i'm interested in this. Great idea btw. I think I will modify it a bit so I can have multiple games going. That OK w/ you? Thanks
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://32724]
NodeReaper tries to shade his eye-sockets with his hands

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2017-09-25 20:24 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (290 votes). Check out past polls.