ColdFusion PrintFEN Function

A ColdFusion function for printing chess diagrams from FEN notation. Try it out here.

function IsWhiteSquare(row,column)
{ // function determines square color
 // odd rows start on white - odd columns are white evens are black
 // even rows on black - odd columns are black and evens are white
 if((row MOD 2) NEQ 0) // odd row
 {
  if((column MOD 2) NEQ 0) // odd column
   return true;
  else
   return false;  
 }
 else // even row
 {
  if((column MOD 2) NEQ 0) // odd column
   return false;
  else
   return true;  
 } 
}


function PrintFEN(fen,orient) {
chessBoard = StructNew();
chessBoard.piecesFEN = 'p,P,r,R,n,N,b,B,q,Q,k,K'; // black is lowercase, white uppercase
chessBoard.FEN = ListFirst(fen,' ');
chessBoard.orientation = orient; // w or b

if(chessBoard.orientation EQ "b")
chessBoard.FEN = Reverse(chessBoard.FEN);

for(row=1; row LTE 8; row=row+1) // parse rows
{
 thisrow = ListGetAt(chessBoard.FEN,row,'/');
 offset = 1; // used to determine column in FEN rows with numbers (empty squares)
 for(col=1; col LTE Len(thisrow); col=col+1) // process FEN row
 {
  thischar = Mid(thisrow,col,1); 
  
  if(IsNumeric(thischar))
  {
   // numeric vals are empty squares
   for(square=1; square LTE thischar; square=square+1)
   {
    if(IsWhiteSquare(row,offset+square))
	 WriteOutput('_');
	else
	 WriteOutput(' ');
   } // close for j
  offset = offset + thischar;
  }
  else // isNumeric(thischar)
  {
   if(IsWhiteSquare(row,offset))
    WriteOutput(thischar);
   else
    WriteOutput(Chr(Asc(thischar) + 8));
    offset = offset + 1;
  } // isNumeric(thischar)
 } // end process column - for i = 1
WriteOutput('
'); } // end process row - for o = 1 }
Posted by nagrom on 03/25/2009 at 8:46 PM | Categories: Code - ColdFusion -

2 Comments

Eric Belair wrote on 03/26/09 7:23 AM

NICE. I love seeing someone make good use of MOD(). Now do it in ActionScript. Joe G says hi - from stall 1.

nagrom wrote on 03/31/09 10:04 AM

Y'know, I could make it simpler by adding the row to the column and doing a mod on that:

row 1 + col 1 = 2 (even)

row 2 + col 1 = 3 (odd)