[bn-perlmongers] Checkers Game in under 1k of perl

Phil Winans pawinan at ilstu.edu
Mon Nov 12 14:31:59 CST 2001


Here is a game of checkers I discovered on perlmonks.org today:
http://perlmonks.org/?node_id=124845

It's slow and for UNIX (requires tput/terminfo), but fascinating that it
is less than 1k.

Here is the code:

@a=0..63;g(o($_),a($_)?$_<24?"w":$_>39?"b":0:0)for at a;$w=$b=12;while($w&&$b)
{$z=!$z if!$r;$i=" 01234567\n";print$i,(join"",map{($x,$y)=o($_);($x?"":$y)
.(a($_)?`tput smso`:`tput rmso`).(g(o($_))||" ").`tput rmso`.($x==7?"$y\n":
"")}@a),$i;$z?&n:&c;g($f,$g,($g==7&&$z)||!($z||$g)?uc&w:g($d,$e));g($d,$e,0
);t(1)}sub n{print"$_[0]xyxy?\n";($d,$e,$f,$g)=split"",<STDIN>;&v||&n}sub
t{ @t=($f>$d?$d+1:$d-1,$g>$e?$e+1:$e-1);if($s==4&&g(@t)&&lc(g(@t))ne&w){if(
@_){g(@t,0);$z?--$b:--$w;&b}1}else{0}}sub v{$s=($d-$f)**2;(lc(g($d,$e))ne&w
||$s!=($e-$g)**2||g($f,$g)||$f>7||$f<0||$g>7||$g<0||(($z?$e-$g:$g-$e)>0)&&!
(g($d,$e)eq uc&w)||!($s==1||t())||($r?$d!=$c||$e!=$h:0))?0:1}sub c{for(@a){
($d,$e)=o($_);if(lc(g(o($_)))eq&w){for(@a){($f,$g)=o($_);&t?return:push at q,[
$d,$e,$f,$g]if&v}}}($d,$e,$f,$g)=@{$q[$#q]}}sub w{$z?"w":"b"}sub
o{($_[0]%8,int($_[0]/8))}sub a{($_[0]%8+int($_[0]/8))%2}sub
g{$n=$_[0]+8*$_[1];defined$_[2]?$j[$n]=$_[2]:$j[$n]}sub b{$d=$c=$f;$e=$h=$g
;$r=(grep{($f,$g)=o($_);&v&&t()}@a)?1:0}die$w?"win":"lose"."\n"

Phil



---
Bloomington-Normal Perl Mongers User's Group
-Send List commands to: majordomo at hfb.pm.org
-Send List posts to: bloomington-normal-pm at hfb.pm.org



More information about the Bloomington-normal-pm mailing list