Author: Anthony Cozzie
Date: 10:56:20 02/22/05
Go up one level in this thread
On February 22, 2005 at 13:25:09, Uri Blass wrote: >On February 22, 2005 at 12:40:52, Anthony Cozzie wrote: > >>On February 22, 2005 at 08:35:07, Andrew Wagner wrote: >> >>>Hi all. >>>I got sidetracked this morning by an interesting chess programming problem. It >>>took me a couple hours, but I think I have a working algorithm -- haven't tested >>>yet though. Anyway, I got to wondering if others would approach it the same way. >>>So I thought I'd make a little competition of it. Post your code here, and I'll >>>pick the program I like best and shower praise and adulation on its author. If >>>people like this challenge, maybe I'll do one each month or something. Anyway, >>>here's the one I did this morning: >>>There are 64 x 63 = 4032 ways to put a black knight and white knoght both on a >>>chess board. Write a program -- from scratch -- to generate FENs for each of >>>these positions. The FENs should look something like: Nn6/8/8/8/8/8/8/8 w - - 0 >>>1. >>> >>>I think my code will wind up weighing in at around 60-70 lines of C. Can you do >>>better? >> >>100% untested. >> >>#include "stdio.h" >> >>int main(void) >>{ >> char board[64] = >>{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, >>fentable = {0, 'n', 'N'}; >> >> for(int i = 0; i < 4096; i++) { >> int wn = i & 0x3F, bn = i >> 6; >> if(wn == bn) >> continue; >> board[wn] = 1; >> board[bn] = 2; >> >> for(int rank = 7; rank >= 0; rank--) { >> for(int file = 0, empty = 0; file < 8; file++) { >> if(empty && (board[rank*8+file] || file == 7)) { >> printf("%d", empty); >> empty = 0; >> } >> else >> empty++; >> if(board[rank*8+file]) >> printf("%c", fentable[board[rank*8+file]]); >> } >> printf("/"); >> } >> >> printf(" - - 0 1\n"); >> board[wn] = board[bn] = 0; >> } >>} > >Your code seems to be with the best basis > >Here are correction to some bugs and hopefully there are no bugs left > >int main(void) >{ > char board[64] = >{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, >fentable[3] = {0, 'n', 'N'}; > > for(int i = 0; i < 4096; i++) { > int wn = i & 0x3F, bn = i >> 6; > if(wn == bn) > continue; > board[wn] = 1; > board[bn] = 2; > > for(int rank = 7; rank >= 0; rank--) { > for(int file = 0, empty = 0; file < 8; file++) { > if (board[rank*8+file]) > { > if (empty>0) > printf("%d", empty); > printf("%c", fentable[board[rank*8+file]]); > empty = 0; > } > else > { > empty++; > if (file==7) > printf("%d", empty); > } > > } > if (rank>0) printf("/"); > } > > printf(" - - 0 1\n"); > board[wn] = board[bn] = 0; > } > return 0; >} Better? #include "stdio.h" int main(void) { char board[64] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, fentable = {0, 'n', 'N'}; for(int i = 0; i < 4096; i++) { int wn = i & 0x3F, bn = i >> 6; if(wn == bn) continue; board[wn] = 1; board[bn] = 2; for(int rank = 7; rank >= 0; rank--) { for(int file = 0, empty = 0; file < 8; file++) { if(empty && (board[rank*8+file] || file == 7)) { printf("%d", empty+!board[rank*8+file]); empty = 0; } else empty++; if(board[rank*8+file]) printf("%c", fentable[board[rank*8+file]]); } printf("/"); } printf(" - - 0 1\n"); board[wn] = board[bn] = 0; } }
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.