[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Golf: chessboard



On Sat, Mar 03, 2001 at 09:34:34PM -0500, Keith Calvert Ivey wrote:
> Since some of us had fun with a bridge-related golf challenge 
> a couple of weeks ago, I thought I'd try to come up with 
> something about chess.
> 
> The challenge is to write a Perl program with the fewest
> characters (the shebang line doesn't count) that takes as a
> command-line argument a chess position in Forsythe-Edwards
> notation (FEN) and prints out an ASCII representation of a 
> chessboard like that produced by the Position->to_text() method 
> in Gareth Rees's Games::Chess module.
> 
> That is, write the shortest possible program "chessboard" such 
> that, for example,
> 
>     perl chessboard rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
> 
> produces this output (not indented):
> 
>     r n b q k b n r
>     p p p p p p p p
>       .   .   .   .
>     .   .   .   .  
>       .   .   .   .
>     .   .   .   .  
>     P P P P P P P P
>     R N B Q K B N R
> 
> As another example,
> 
>     perl chessboard 8/3p4/p1bk3p/Pp6/1Kp1PpPp/2P2P1P/2P5/5B2
> 
> produces
> 
>       .   .   .   .
>     .   . p .   .  
>     p . b k   .   p
>     P p .   .   .  
>       K p . P p P p
>     .   P   . P . P
>       . P .   .   .
>     .   .   . B .  
> 
> In FEN, the digits represent runs of open squares, the 
> lowercase letters represent black pieces, and the uppercase 
> letters represent white pieces.  In the chessboard drawings, 
> periods represent open black squares (that's the tricky bit), 
> and spaces represent open white squares; there are spaces 
> between the columns and a newline at the end of each row, 
> including the last.
> 
> My solution is less than 80 characters.  Good luck!


72 characters, without the shebang line. (^I is counted as one char):

    #!/opt/perl/bin/perl -li1
    map{s/\d/$"x$&/eg;$^I++;print map{$^I++%2&&/$"/?'.':$_,$"}/./g}pop=~/\w+/g


It does print a space before the newline though. If that's not allowed, 
I might need three more characters.

I can also go down to 72 characters without using control char variables,
but then you have to allow for command line switches:

    #!/opt/perl/bin/perl -ls
    map{s/\d/$"x$&/eg;$i++;print map{$i++%2&&/$"/?'.':$_,$"}/./g}pop=~/\w+/g

which should be called as:

    chessboard -i=1 <position>


Abigail

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe