N.E.G. 1.0 released

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
hgm
Posts: 27898
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

N.E.G. 1.0 released

Post by hgm »

Just for fun I made a small improvement to my non-searching Chess engine N.E.G.: it had a nasty tendency to draw games where it had reduced the opponent to a bare King, because when there is nothing left to capture it prefers checks, and chasing the bare King with a Queen (which often was the only active piece) of course leads to nothing.

So in this new version it only gets the bonus for checking with a new piece. This usually is enough to get a bare King checkmated.

I also made it WB v2, supporting setboard, and Linux-compatible. This is the source code:

Code: Select all

#include <stdio.h>

#ifdef WIN32 
#    include <windows.h>
#else
#    include <sys/time.h>
#    include <sys/times.h>
#    include <unistd.h>
     int GetTickCount&#40;)
     &#123;	struct timeval t;
	gettimeofday&#40;&t, NULL&#41;;
	return t.tv_sec*1000 + t.tv_usec/1000;
     &#125;
#endif

#define WHITE 8
#define BLACK 16
#define COLOR &#40;WHITE|BLACK&#41;


typedef void Func&#40;int stm, int from, int to, void *closure&#41;;
typedef int Board&#91;128&#93;;

int value&#91;8&#93; = &#123; 0, 100, 100, 10000, 325, 350, 500, 950 &#125;;
int firstDir&#91;&#93; = &#123; 0, 0, 27, 4, 18, 8, 13, 4 &#125;;
int steps&#91;&#93; = &#123; -16, -15, -17, 0, 1, -1, 16, -16, 15, -15, 17, -17, 0, 1, -1, 16, -16, 0, 18, 31, 33, 14, -18, -31, -33, -14, 0, 16, 15, 17, 0 &#125;;
Board PST = &#123;
 0, 2, 4, 6, 6, 4, 2, 0,   0,0,0,0,0,0,0,0,
 2, 8,10,12,12,10, 8, 2,   0,0,0,0,0,0,0,0,
 6,12,16,18,18,16,12, 6,   0,0,0,0,0,0,0,0,
 8,14,18,20,20,18,14, 8,   0,0,0,0,0,0,0,0,
 8,14,18,20,20,18,14, 8,   0,0,0,0,0,0,0,0,
 6,12,16,18,18,16,12, 6,   0,0,0,0,0,0,0,0,
 2, 8,10,12,12,10, 8, 2,   0,0,0,0,0,0,0,0,
 0, 2, 4, 6, 6, 4, 2, 0,   0,0,0,0,0,0,0,0
&#125;;


//              "abcdefghijklmnopqrstuvwxyz"
char pieces&#91;&#93; = ".5........3..4.176........";
Board board, attacks&#91;2&#93;, lva&#91;2&#93;;
int bestScore, bestFrom, bestTo, lastMover, lastChecker, randomize, post;

void
MoveGen &#40;Board board, int stm, Func proc, void *cl&#41;
&#123;
  int from, to, piece, victim, type, dir, step;
  for&#40;from=0; from<128; from = from + 9 & ~8&#41; &#123;
    piece = board&#91;from&#93;;
    if&#40;piece & stm&#41; &#123;
      type = piece & 7;
      dir = firstDir&#91;type&#93;;
      while&#40;&#40;step = steps&#91;dir++&#93;)) &#123;
        to = from;
        do &#123;
          to += step;
          if&#40;to & 0x88&#41; break;
          victim = board&#91;to&#93;;
          (*proc&#41;&#40;piece & COLOR, from, to, cl&#41;;
          victim += type < 5;
          if&#40;!&#40;to - from & 7&#41; && type < 3 && &#40;type == 1 ? to > 79 &#58; to < 48&#41;) victim--;
        &#125; while&#40;!victim&#41;;
      &#125;
    &#125;
  &#125;
&#125;

int InCheck &#40;int stm&#41;
&#123;
  int k, xstm = COLOR - stm;
  for&#40;k=0; k<128; k++) if&#40; board&#91;k&#93; == stm + 3 ) &#123;
    int dir=4, step;
    int f = &#40;stm == WHITE ? -16 &#58; 16&#41;; // forward
    int p = &#40;stm == WHITE ? 2 &#58; 1&#41;;
    if&#40;!&#40;k + f + 1 & 0x88&#41; && board&#91;k + f + 1&#93; == xstm + p&#41; return k + f + 2;
    if&#40;!&#40;k + f - 1 & 0x88&#41; && board&#91;k + f - 1&#93; == xstm + p&#41; return k + f;
    while&#40;&#40;step = steps&#91;dir&#93;)) &#123;
	int from = k + steps&#91;dir + 14&#93;;
	if&#40;!&#40;from & 0x88&#41; && board&#91;from&#93; == xstm + 4&#41; return from + 1;
	from = k + step;
	if&#40;!&#40;from & 0x88&#41; && board&#91;from&#93; == xstm + 3&#41; return from + 1;
	from = k;
	while&#40;!(&#40;from += step&#41; & 0x88&#41;) if&#40;board&#91;from&#93;) &#123; // occupied
	    if&#40;dir < 8 && &#40;board&#91;from&#93; & COLOR + 6&#41; == xstm + 6&#41; return from + 1; // R or Q and orthogonal
	    if&#40;dir > 7 && &#40;board&#91;from&#93; & COLOR + 5&#41; == xstm + 5&#41; return from + 1; // B or Q and diagonal
	    break;
	&#125;
	dir++;
    &#125;
    break;
  &#125;
  return 0;
&#125;

void
Count &#40;int stm, int from, int to, void *cl&#41;
&#123;
  int s = &#40;stm == BLACK&#41;;
  if&#40;!&#40;to - from & 7&#41; && &#40;board&#91;from&#93; & 7&#41; < 3&#41; return; // ignore Pawn non-captures
  attacks&#91;s&#93;&#91;to&#93;++;
  if&#40;lva&#91;s&#93;&#91;to&#93; > value&#91;board&#91;from&#93; & 7&#93;) lva&#91;s&#93;&#91;to&#93; = value&#91;board&#91;from&#93; & 7&#93;;
&#125;

void
Score &#40;int stm, int from, int to, void *cl&#41;
&#123;
  int score = PST&#91;to&#93; - PST&#91;from&#93;;
  int piece = board&#91;from&#93;;
  int victim = board&#91;to&#93;;
  int myVal = value&#91;piece & 7&#93;;
  int hisVal = value&#91;victim & 7&#93;;
  int push = &#40;piece & 7&#41; < 3 && to - from & 7;// Pawn non-capture
  int s = &#40;stm == BLACK&#41;;
  int check;
  if&#40;&#40;piece & 7&#41; < 3 && !&#40;to - from & 7&#41; != !victim&#41; return; // weed out illegal pawn modes
  if&#40;&#40;piece & 7&#41; == 3&#41; score -= score;        // keep King out of center
  else if&#40;myVal > 400&#41; score = 0;             // no centralization for R, Q
  if&#40;&#40;piece & ~7&#41; == &#40;victim & ~7&#41;) return;   // self capture
  board&#91;from&#93; = 0; board&#91;to&#93; = piece;
  if&#40;from != lastChecker && InCheck&#40;COLOR - stm&#41;) score += 50; // bonus for checking with new piece
  check = InCheck&#40;stm&#41;;                       // in check after move?
  board&#91;to&#93; = victim; board&#91;from&#93; = piece;
  if&#40;check&#41; return;                           // illegal
  score += (&#40;rand&#40;)>>8 & 31&#41; - 16&#41;*randomize; // randomize
  if&#40;from == lastMover&#41; score -= 10;          // discourage moving same piece twice
  if&#40;hisVal && hisVal < 400&#41; score += PST&#91;to&#93;;// centralization bonus of victim
  score += hisVal;                            // captured piece
  if&#40;attacks&#91;!s&#93;&#91;to&#93;) &#123;                       // to-square was attacked
    if&#40;attacks&#91;s&#93;&#91;to&#93; - 1 + push < attacks&#91;!s&#93;&#91;to&#93; ) score -= myVal; else // not sufficiently protected
    if&#40;myVal > lva&#91;!s&#93;&#91;to&#93;) score += lva&#91;!s&#93;&#91;to&#93; - myVal; // or protected, but more valuable
  &#125;
  if&#40;&#40;piece & 7&#41; != 3 && attacks&#91;!s&#93;&#91;from&#93;) &#123; // from-square was attacked &#40;and not King&#41;
    if&#40;attacks&#91;s&#93;&#91;from&#93; < attacks&#91;!s&#93;&#91;from&#93; ) score += myVal; else // not sufficiently protected
    if&#40;myVal > lva&#91;!s&#93;&#91;from&#93;) score -= lva&#91;!s&#93;&#91;from&#93; - myVal; // or protected, but more valuable
  &#125;
  if&#40;&#40;piece & 7&#41; == 1 && to < 48&#41; score += 50;
  if&#40;&#40;piece & 7&#41; == 2 && to > 79&#41; score += 50;

  if&#40;score > bestScore&#41; bestScore = score, bestFrom = from, bestTo = to; // remember best move
  if&#40;post&#41; printf&#40;"2 %d 0 1 %c%d%c%d\n", score, &#40;from&7&#41; + 'a', 8 - &#40;from >> 4&#41;, &#40;to&7&#41; + 'a', 8 - &#40;to >> 4&#41;);
&#125;

int
Setup &#40;char *fen&#41;
&#123;
  char c;
  int i;
  for&#40;i=0; i<128; i++) board&#91;i&#93; = 0;
  i = 0;
  while&#40;&#40;c = *fen++)) &#123;
    if&#40;c == 'p') board&#91;i++&#93; = BLACK + 2; else
    if&#40;c >= '0' && c <= '9') i += c - '0'; else
    if&#40;c >= 'a' && c <= 'z') board&#91;i++&#93; = BLACK + pieces&#91;c - 'a'&#93; - '0'; else
    if&#40;c >= 'A' && c <= 'Z') board&#91;i++&#93; = WHITE + pieces&#91;c - 'A'&#93; - '0'; else
    if&#40;c == '/') i = &#40;i | 15&#41; + 1; else break;
  &#125;
  for&#40;i=0; i<128; i = i + 9 & ~8&#41; printf&#40;i&7 ? " %2d" &#58; "\n# %2d", board&#91;i&#93;); printf&#40;"\n");
  return (*fen == 'w' ? WHITE &#58; BLACK&#41;;
&#125;

int
main ()
&#123;
  int stm = WHITE, engineSide = 0;
  char line&#91;256&#93;, command&#91;20&#93;;
  srand&#40;GetTickCount&#40;));
  while&#40;1&#41; &#123;
    int i, c;
    if&#40;stm == engineSide&#41; &#123;
	for&#40;i=0; i<128; i++) lva&#91;0&#93;&#91;i&#93; = lva&#91;1&#93;&#91;i&#93; = 30000, attacks&#91;0&#93;&#91;i&#93; = attacks&#91;1&#93;&#91;i&#93; = 0;
	MoveGen&#40;board, COLOR, &Count, NULL&#41;;
	bestScore = -30000;
	MoveGen&#40;board, stm, &Score, NULL&#41;;
	board&#91;bestTo&#93; = board&#91;bestFrom&#93;; board&#91;bestFrom&#93; = 0; stm ^= COLOR;
	if&#40;&#40;board&#91;bestTo&#93; & 7&#41; < 3 && &#40;stm == WHITE ? bestTo < 16 &#58; bestTo > 111&#41;) board&#91;bestTo&#93; |= 7; // always promote to Q
	lastMover = bestTo;
	lastChecker = InCheck&#40;stm&#41; ? bestTo &#58; -1 ;
	printf&#40;"move %c%d%c%d\n", &#40;bestFrom&7&#41; + 'a', 8 - &#40;bestFrom >> 4&#41;, &#40;bestTo&7&#41; + 'a', 8 - &#40;bestTo >> 4&#41;);
    &#125;
    fflush&#40;stdout&#41;; i = 0;
    while&#40;&#40;line&#91;i++&#93; = c = getchar&#40;)) != '\n') if&#40;c == EOF&#41; printf&#40;"# EOF\n"), exit&#40;1&#41;; line&#91;i&#93; = '\0';
    if&#40;*line == '\n') continue;
    sscanf&#40;line, "%s", command&#41;;
    printf&#40;"# command&#58; %s\n", command&#41;;
    if&#40;!strcmp&#40;command, "usermove")) &#123;
	int from, to; char c, d, promo, ep;
	sscanf&#40;line, "usermove %c%d%c%d%c", &c, &from, &d, &to, &promo&#41;;
	from = &#40;8 - from&#41;*16 + c - 'a'; to = &#40;8 - to&#41;*16 + d - 'a';
	if&#40;&#40;board&#91;from&#93; & 7&#41; == 3 && to - from == 2&#41; board&#91;from + 1&#93; = board&#91;to + 1&#93;, board&#91;to + 1&#93; = 0; // K-side castling
	if&#40;&#40;board&#91;from&#93; & 7&#41; == 3 && from - to == 2&#41; board&#91;from - 1&#93; = board&#91;to - 2&#93;, board&#91;to - 2&#93; = 0; // Q-side castling
	ep = (&#40;board&#91;from&#93; & 7&#41; < 3 && !board&#91;to&#93;); // recognize e.p. capture
	board&#91;to&#93; = board&#91;from&#93;; if&#40;ep&#41; board&#91;from & 0x70 | to & 7&#93; = 0; board&#91;from&#93; = 0;
	if&#40;promo == 'q') board&#91;to&#93; = board&#91;to&#93; | 7; // promote
	stm ^= COLOR;	
    &#125;
    else if&#40;!strcmp&#40;command, "protover")) printf&#40;"feature myname=\"N.E.G. 1.0\" setboard=1 usermove=1 analyze=0 colors=0 sigint=0 sigterm=0 done=1\n");
    else if&#40;!strcmp&#40;command, "new"))      stm = Setup&#40;"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"), randomize = 0, engineSide = BLACK;
    else if&#40;!strcmp&#40;command, "go"))       engineSide = stm;
    else if&#40;!strcmp&#40;command, "result"))   engineSide = 0;
    else if&#40;!strcmp&#40;command, "force"))    engineSide = 0;
    else if&#40;!strcmp&#40;command, "setboard")) stm = Setup&#40;line+9&#41;;
    else if&#40;!strcmp&#40;command, "random"))   randomize = !randomize;
    else if&#40;!strcmp&#40;command, "post"))     post = 1;
    else if&#40;!strcmp&#40;command, "nopost"))   post = 0;
    else if&#40;!strcmp&#40;command, "quit"))     break;
  &#125;
  return 0;
&#125;
User avatar
hgm
Posts: 27898
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: N.E.G. 1.0 released

Post by hgm »

Oops, I already discovered a bug. The 'always promote to Q' line has the colors reversed, and should have read:

Code: Select all

	if&#40;&#40;board&#91;bestTo&#93; & 7&#41; < 3 && &#40;stm == BLACK ? bestTo < 16 &#58; bestTo > 111&#41;) board&#91;bestTo&#93; |= 7; // always promote to Q
As it was, it would never promote its own Pawns, but just leave them as dead wood on the last rank.

Fairy-Max with a depth limit of 1 ply scores about 72% against N.E.G. (measured over 100 games). This doesn't give a proper indication of the strength difference, however, as most points of N.E.G. are due to draws, being stalemated in a totally lost position. (At 1 ply Fairy-Max can see checkmate, due to the check-extension + King capture in QS, but not stalemate which does not start with check.)

Nevertheless, N.E.G. was able to win 8 games out of 100.
Adam Hair
Posts: 3226
Joined: Wed May 06, 2009 10:31 pm
Location: Fuquay-Varina, North Carolina

Re: N.E.G. 1.0 released

Post by Adam Hair »

I wonder how it would do against Usurpator. I have a Pentium IV sitting around that I may load with Winboard and use to run that match.
Roger Brown
Posts: 782
Joined: Wed Mar 08, 2006 9:22 pm

Re: N.E.G. 1.0 released

Post by Roger Brown »

hgm wrote:Just for fun I made a small improvement to my non-searching Chess engine N.E.G.: it had a nasty tendency to draw games where it had reduced the opponent to a bare King, because when there is nothing left to capture it prefers checks, and chasing the bare King with a Queen (which often was the only active piece) of course leads to nothing.

So in this new version it only gets the bonus for checking with a new piece. This usually is enough to get a bare King checkmated.

I also made it WB v2, supporting setboard, and Linux-compatible. This is the source code:

Code: Select all

SNIP
Hello H.G.,

I went to your site but the the N.E.G. version available is 0.3d.exe.

Where is version 1.0 available?

Later.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: N.E.G. 1.0 released

Post by Sven »

hgm wrote:Oops, I already discovered a bug. The 'always promote to Q' line has the colors reversed, and should have read:

Code: Select all

	if&#40;&#40;board&#91;bestTo&#93; & 7&#41; < 3 && &#40;stm == BLACK ? bestTo < 16 &#58; bestTo > 111&#41;) board&#91;bestTo&#93; |= 7; // always promote to Q
As it was, it would never promote its own Pawns, but just leave them as dead wood on the last rank.

Fairy-Max with a depth limit of 1 ply scores about 72% against N.E.G. (measured over 100 games). This doesn't give a proper indication of the strength difference, however, as most points of N.E.G. are due to draws, being stalemated in a totally lost position. (At 1 ply Fairy-Max can see checkmate, due to the check-extension + King capture in QS, but not stalemate which does not start with check.)

Nevertheless, N.E.G. was able to win 8 games out of 100.
Isn't the command[20] array in main() too small for a "setboard" command?
mar
Posts: 2572
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: N.E.G. 1.0 released

Post by mar »

Sven Schüle wrote:Isn't the command[20] array in main() too small for a "setboard" command?
setboard is only 9 chars (including zero terminator). line is 256 bytes, should be enough.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: N.E.G. 1.0 released

Post by Sven »

mar wrote:
Sven Schüle wrote:Isn't the command[20] array in main() too small for a "setboard" command?
setboard is only 9 chars (including zero terminator). line is 256 bytes, should be enough.
Agreed. The sscanf(line, "%s", command) confused me but actually it stores the first word ending at the first whitespace in 'command' (the special handling of whitespace characters in (s)scanf looks inconsistent to me but it is as it is ...). I never use functions from the scanf family in my life, and now once again I see why.
flok

Re: N.E.G. 1.0 released

Post by flok »

I just got an error from cutechess-cli (got cc-c to work eventually) that neg played an invalid move:

Code: Select all

&#91;Event "?"&#93; 
&#91;Site "?"&#93; 
&#91;Date "2014.12.27"&#93; 
&#91;Round "2"&#93; 
&#91;White "neg"&#93; 
&#91;Black "600f"&#93; 
&#91;Result "0-1"&#93; 
&#91;ECO "A00"&#93; 
&#91;Opening "Dunst &#40;Sleipner, Heinrichsen&#41; Opening"&#93; 
&#91;PlyCount "128"&#93; 
&#91;TimeControl "50/90+1"&#93; 
 
1. Nc3 &#123;+0.14/2 0s&#125; d5 &#123;1.5s&#125; 2. Nf3 &#123;+0.14/2 0s&#125; Be6 &#123;1.5s&#125; 3. d4 &#123;0.00/2 0s&#125; 
Bg4 &#123;1.5s&#125; 4. Be3 &#123;0.00/2 0s&#125; Bxf3 &#123;1.6s&#125; 5. gxf3 &#123;0.00/2 0s&#125; c6 &#123;1.6s&#125; 
6. a4 &#123;0.00/2 0s&#125; h5 &#123;1.6s&#125; 7. b4 &#123;0.00/2 0s&#125; Qd7 &#123;1.7s&#125; 8. h4 &#123;0.00/2 0s&#125; 
f5 &#123;1.7s&#125; 9. Bg2 &#123;0.00/2 0s&#125; Qc8 &#123;1.8s&#125; 10. f4 &#123;0.00/2 0s&#125; Kd8 &#123;1.8s&#125; 
11. Bf3 &#123;0.00/2 0s&#125; Rh7 &#123;1.9s&#125; 12. a5 &#123;0.00/2 0s&#125; Nf6 &#123;1.9s&#125; 13. Rb1 &#123;0.00/2 0s&#125; 
Rh6 &#123;2.0s&#125; 14. Qc1 &#123;0.00/2 0s&#125; a6 &#123;2.1s&#125; 15. Ra1 &#123;0.00/2 0s&#125; e6 &#123;2.2s&#125; 
16. b5 &#123;0.00/2 0s&#125; axb5 &#123;2.2s&#125; 17. Rb1 &#123;0.00/2 0s&#125; Rxa5 &#123;2.3s&#125; 
18. Qd1 &#123;0.00/2 0s&#125; Ra3 &#123;2.5s&#125; 19. Nxb5 &#123;0.00/2 0s&#125; cxb5 &#123;2.6s&#125; 
20. Rxb5 &#123;0.00/2 0s&#125; Rxe3 &#123;2.8s&#125; 21. fxe3 &#123;0.00/2 0s&#125; Rh7 &#123;3.0s&#125; 
22. Ra5 &#123;0.00/2 0s&#125; Ne8 &#123;3.2s&#125; 23. Qc1 &#123;0.00/2 0s&#125; Nc7 &#123;3.6s&#125; 24. c3 &#123;0.00/2 0s&#125; 
b6 &#123;4.0s&#125; 25. Ra4 &#123;0.00/2 0s&#125; b5 &#123;4.1s&#125; 26. Ra2 &#123;0.00/2 0s&#125; e5 &#123;1.1s&#125; 
27. fxe5 &#123;0.00/2 0s&#125; Ke8 &#123;1.1s&#125; 28. Rb2 &#123;0.00/2 0s&#125; Qb7 &#123;1.1s&#125; 
29. Qd1 &#123;0.00/2 0s&#125; Kf7 &#123;1.1s&#125; 30. Rc2 &#123;0.00/2 0s&#125; Kg6 &#123;1.1s&#125; 
31. Rg1+ &#123;0.00/2 0s&#125; Kf7 &#123;1.1s&#125; 32. Rd2 &#123;-4.02/2 0s&#125; Nc6 &#123;1.1s&#125; 
33. Qc1 &#123;-3.42/2 0s&#125; Nxe5 &#123;1.1s&#125; 34. dxe5 &#123;-3.42/2 0s&#125; f4 &#123;1.1s&#125; 
35. exf4 &#123;-3.42/2 0s&#125; Ba3 &#123;1.1s&#125; 36. Qxa3 &#123;-3.42/2 0s&#125; Qb6 &#123;1.1s&#125; 
37. Rh1 &#123;+1.58/2 0s&#125; Qc6 &#123;1.1s&#125; 38. e3 &#123;-0.10/2 0s&#125; g6 &#123;1.1s&#125; 
39. Qb3 &#123;0.00/2 0s&#125; Qb7 &#123;1.1s&#125; 40. Bxd5+ &#123;0.00/2 0s&#125; Nxd5 &#123;1.1s&#125; 
41. Qxd5+ &#123;0.00/2 0s&#125; Qxd5 &#123;1.1s&#125; 42. Rxd5 &#123;+5.00/2 0s&#125; b4 &#123;1.1s&#125; 
43. cxb4 &#123;0.00/2 0s&#125; Ke6 &#123;1.1s&#125; 44. Rd6+ &#123;0.00/2 0s&#125; Ke7 &#123;1.1s&#125; 
45. Rxg6 &#123;0.00/2 0s&#125; Kf7 &#123;1.1s&#125; 46. Rf6+ &#123;0.00/2 0s&#125; Ke7 &#123;1.1s&#125; 
47. e4 &#123;0.00/2 0s&#125; Rh8 &#123;1.1s&#125; 48. Rg6 &#123;0.00/2 0s&#125; Rh7 &#123;1.1s&#125; 49. b5 &#123;0.00/2 0s&#125; 
Rh6 &#123;1.1s&#125; 50. Rxh6 &#123;0.00/2 0s&#125; Kf7 &#123;1.1s&#125; 51. Rxh5 &#123;0.00/2 0s&#125; Kg6 &#123;2.1s&#125; 
52. Rg5+ &#123;0.00/2 0s&#125; Kf7 &#123;2.0s&#125; 53. b6 &#123;0.00/2 0s&#125; Ke6 &#123;2.0s&#125; 
54. Rg6+ &#123;0.00/2 0s&#125; Kf7 &#123;2.0s&#125; 55. Rh6 &#123;0.00/2 0s&#125; Kg7 &#123;2.0s&#125; 
56. Rf6 &#123;0.00/2 0s&#125; Kh7 &#123;2.0s&#125; 57. e6 &#123;0.00/2 0s&#125; Kg7 &#123;2.0s&#125; 
58. Rf7+ &#123;0.00/2 0s&#125; Kh6 &#123;2.0s&#125; 59. b7 &#123;0.00/2 0s&#125; Kg6 &#123;2.0s&#125; 
60. f5+ &#123;0.00/2 0s&#125; Kh6 &#123;2.0s&#125; 61. Rf6+ &#123;0.00/2 0s&#125; Kg7 &#123;2.0s&#125; 
62. Rg6+ &#123;0.00/2 0s&#125; Kh7 &#123;1.9s&#125; 63. f6 &#123;0.00/2 0s&#125; Kxg6 &#123;1.9s&#125; 
64. Rg1+ &#123;0.00/2 0s&#125; Kxf6 &#123;1.9s, White makes an illegal move&#58; b7b8&#125; 0-1 
User avatar
hgm
Posts: 27898
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: N.E.G. 1.0 released

Post by hgm »

Oh, I guess cutechess-cli does not assume Q as default promotion choice. I will fix that later today.
User avatar
hgm
Posts: 27898
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: N.E.G. 1.0 released

Post by hgm »

The following version (N.E.G. 1.1) will always print the 'q' suffix on promotion moves:

Code: Select all

#include <stdio.h>

#ifdef WIN32 
#    include <windows.h>
#else
#    include <sys/time.h>
#    include <sys/times.h>
#    include <unistd.h>
     int GetTickCount&#40;)
     &#123;	struct timeval t;
	gettimeofday&#40;&t, NULL&#41;;
	return t.tv_sec*1000 + t.tv_usec/1000;
     &#125;
#endif

#define WHITE 8
#define BLACK 16
#define COLOR &#40;WHITE|BLACK&#41;


typedef void Func&#40;int stm, int from, int to, void *closure&#41;;
typedef int Board&#91;128&#93;;

int value&#91;8&#93; = &#123; 0, 100, 100, 10000, 325, 350, 500, 950 &#125;;
int firstDir&#91;&#93; = &#123; 0, 0, 27, 4, 18, 8, 13, 4 &#125;;
int steps&#91;&#93; = &#123; -16, -15, -17, 0, 1, -1, 16, -16, 15, -15, 17, -17, 0, 1, -1, 16, -16, 0, 18, 31, 33, 14, -18, -31, -33, -14, 0, 16, 15, 17, 0 &#125;;
Board PST = &#123;
 0, 2, 4, 6, 6, 4, 2, 0,   0,0,0,0,0,0,0,0,
 2, 8,10,12,12,10, 8, 2,   0,0,0,0,0,0,0,0,
 6,12,16,18,18,16,12, 6,   0,0,0,0,0,0,0,0,
 8,14,18,20,20,18,14, 8,   0,0,0,0,0,0,0,0,
 8,14,18,20,20,18,14, 8,   0,0,0,0,0,0,0,0,
 6,12,16,18,18,16,12, 6,   0,0,0,0,0,0,0,0,
 2, 8,10,12,12,10, 8, 2,   0,0,0,0,0,0,0,0,
 0, 2, 4, 6, 6, 4, 2, 0,   0,0,0,0,0,0,0,0
&#125;;


//              "abcdefghijklmnopqrstuvwxyz"
char pieces&#91;&#93; = ".5........3..4.176........";
Board board, attacks&#91;2&#93;, lva&#91;2&#93;;
int bestScore, bestFrom, bestTo, lastMover, lastChecker, randomize, post;

void
MoveGen &#40;Board board, int stm, Func proc, void *cl&#41;
&#123;
  int from, to, piece, victim, type, dir, step;
  for&#40;from=0; from<128; from = from + 9 & ~8&#41; &#123;
    piece = board&#91;from&#93;;
    if&#40;piece & stm&#41; &#123;
      type = piece & 7;
      dir = firstDir&#91;type&#93;;
      while&#40;&#40;step = steps&#91;dir++&#93;)) &#123;
        to = from;
        do &#123;
          to += step;
          if&#40;to & 0x88&#41; break;
          victim = board&#91;to&#93;;
          (*proc&#41;&#40;piece & COLOR, from, to, cl&#41;;
          victim += type < 5;
          if&#40;!&#40;to - from & 7&#41; && type < 3 && &#40;type == 1 ? to > 79 &#58; to < 48&#41;) victim--;
        &#125; while&#40;!victim&#41;;
      &#125;
    &#125;
  &#125;
&#125;

int InCheck &#40;int stm&#41;
&#123;
  int k, xstm = COLOR - stm;
  for&#40;k=0; k<128; k++) if&#40; board&#91;k&#93; == stm + 3 ) &#123;
    int dir=4, step;
    int f = &#40;stm == WHITE ? -16 &#58; 16&#41;; // forward
    int p = &#40;stm == WHITE ? 2 &#58; 1&#41;;
    if&#40;!&#40;k + f + 1 & 0x88&#41; && board&#91;k + f + 1&#93; == xstm + p&#41; return k + f + 2;
    if&#40;!&#40;k + f - 1 & 0x88&#41; && board&#91;k + f - 1&#93; == xstm + p&#41; return k + f;
    while&#40;&#40;step = steps&#91;dir&#93;)) &#123;
	int from = k + steps&#91;dir + 14&#93;;
	if&#40;!&#40;from & 0x88&#41; && board&#91;from&#93; == xstm + 4&#41; return from + 1;
	from = k + step;
	if&#40;!&#40;from & 0x88&#41; && board&#91;from&#93; == xstm + 3&#41; return from + 1;
	from = k;
	while&#40;!(&#40;from += step&#41; & 0x88&#41;) if&#40;board&#91;from&#93;) &#123; // occupied
	    if&#40;dir < 8 && &#40;board&#91;from&#93; & COLOR + 6&#41; == xstm + 6&#41; return from + 1; // R or Q and orthogonal
	    if&#40;dir > 7 && &#40;board&#91;from&#93; & COLOR + 5&#41; == xstm + 5&#41; return from + 1; // B or Q and diagonal
	    break;
	&#125;
	dir++;
    &#125;
    break;
  &#125;
  return 0;
&#125;

void
Count &#40;int stm, int from, int to, void *cl&#41;
&#123;
  int s = &#40;stm == BLACK&#41;;
  if&#40;!&#40;to - from & 7&#41; && &#40;board&#91;from&#93; & 7&#41; < 3&#41; return; // ignore Pawn non-captures
  attacks&#91;s&#93;&#91;to&#93;++;
  if&#40;lva&#91;s&#93;&#91;to&#93; > value&#91;board&#91;from&#93; & 7&#93;) lva&#91;s&#93;&#91;to&#93; = value&#91;board&#91;from&#93; & 7&#93;;
&#125;

void
Score &#40;int stm, int from, int to, void *cl&#41;
&#123;
  int score = PST&#91;to&#93; - PST&#91;from&#93;;
  int piece = board&#91;from&#93;;
  int victim = board&#91;to&#93;;
  int myVal = value&#91;piece & 7&#93;;
  int hisVal = value&#91;victim & 7&#93;;
  int push = &#40;piece & 7&#41; < 3 && to - from & 7;// Pawn non-capture
  int s = &#40;stm == BLACK&#41;;
  int check;
  if&#40;&#40;piece & 7&#41; < 3 && !&#40;to - from & 7&#41; != !victim&#41; return; // weed out illegal pawn modes
  if&#40;&#40;piece & 7&#41; == 3&#41; score -= score;        // keep King out of center
  else if&#40;myVal > 400&#41; score = 0;             // no centralization for R, Q
  if&#40;&#40;piece & ~7&#41; == &#40;victim & ~7&#41;) return;   // self capture
  board&#91;from&#93; = 0; board&#91;to&#93; = piece;
  if&#40;from != lastChecker && InCheck&#40;COLOR - stm&#41;) score += 50; // bonus for checking with new piece
  check = InCheck&#40;stm&#41;;                       // in check after move?
  board&#91;to&#93; = victim; board&#91;from&#93; = piece;
  if&#40;check&#41; return;                           // illegal
  score += (&#40;rand&#40;)>>8 & 31&#41; - 16&#41;*randomize; // randomize
  if&#40;from == lastMover&#41; score -= 10;          // discourage moving same piece twice
  if&#40;hisVal && hisVal < 400&#41; score += PST&#91;to&#93;;// centralization bonus of victim
  score += hisVal;                            // captured piece
  if&#40;attacks&#91;!s&#93;&#91;to&#93;) &#123;                       // to-square was attacked
    if&#40;attacks&#91;s&#93;&#91;to&#93; - 1 + push < attacks&#91;!s&#93;&#91;to&#93; ) score -= myVal; else // not sufficiently protected
    if&#40;myVal > lva&#91;!s&#93;&#91;to&#93;) score += lva&#91;!s&#93;&#91;to&#93; - myVal; // or protected, but more valuable
  &#125;
  if&#40;&#40;piece & 7&#41; != 3 && attacks&#91;!s&#93;&#91;from&#93;) &#123; // from-square was attacked &#40;and not King&#41;
    if&#40;attacks&#91;s&#93;&#91;from&#93; < attacks&#91;!s&#93;&#91;from&#93; ) score += myVal; else // not sufficiently protected
    if&#40;myVal > lva&#91;!s&#93;&#91;from&#93;) score -= lva&#91;!s&#93;&#91;from&#93; - myVal; // or protected, but more valuable
  &#125;
  if&#40;&#40;piece & 7&#41; == 1 && to < 48&#41; score += 50;
  if&#40;&#40;piece & 7&#41; == 2 && to > 79&#41; score += 50;

  if&#40;score > bestScore&#41; bestScore = score, bestFrom = from, bestTo = to; // remember best move
  if&#40;post&#41; printf&#40;"2 %d 0 1 %c%d%c%d\n", score, &#40;from&7&#41; + 'a', 8 - &#40;from >> 4&#41;, &#40;to&7&#41; + 'a', 8 - &#40;to >> 4&#41;);
&#125;

int
Setup &#40;char *fen&#41;
&#123;
  char c;
  int i;
  for&#40;i=0; i<128; i++) board&#91;i&#93; = 0;
  i = 0;
  while&#40;&#40;c = *fen++)) &#123;
    if&#40;c == 'p') board&#91;i++&#93; = BLACK + 2; else
    if&#40;c >= '0' && c <= '9') i += c - '0'; else
    if&#40;c >= 'a' && c <= 'z') board&#91;i++&#93; = BLACK + pieces&#91;c - 'a'&#93; - '0'; else
    if&#40;c >= 'A' && c <= 'Z') board&#91;i++&#93; = WHITE + pieces&#91;c - 'A'&#93; - '0'; else
    if&#40;c == '/') i = &#40;i | 15&#41; + 1; else break;
  &#125;
  for&#40;i=0; i<128; i = i + 9 & ~8&#41; printf&#40;i&7 ? " %2d" &#58; "\n# %2d", board&#91;i&#93;); printf&#40;"\n");
  return (*fen == 'w' ? WHITE &#58; BLACK&#41;;
&#125;

int
main ()
&#123;
  int stm = WHITE, engineSide = 0;
  char line&#91;256&#93;, command&#91;20&#93;;
  srand&#40;GetTickCount&#40;));
  while&#40;1&#41; &#123;
    int i, c;
    if&#40;stm == engineSide&#41; &#123;
	char *promo = "";
	for&#40;i=0; i<128; i++) lva&#91;0&#93;&#91;i&#93; = lva&#91;1&#93;&#91;i&#93; = 30000, attacks&#91;0&#93;&#91;i&#93; = attacks&#91;1&#93;&#91;i&#93; = 0;
	MoveGen&#40;board, COLOR, &Count, NULL&#41;;
	bestScore = -30000;
	MoveGen&#40;board, stm, &Score, NULL&#41;;
	board&#91;bestTo&#93; = board&#91;bestFrom&#93;; board&#91;bestFrom&#93; = 0; stm ^= COLOR;
	if&#40;&#40;board&#91;bestTo&#93; & 7&#41; < 3 && &#40;stm == BLACK ? bestTo < 16 &#58; bestTo > 111&#41;) board&#91;bestTo&#93; |= 7, promo = "q"; // always promote to Q
	lastMover = bestTo;
	lastChecker = InCheck&#40;stm&#41; ? bestTo &#58; -1 ;
	printf&#40;"move %c%d%c%d%s\n", &#40;bestFrom&7&#41; + 'a', 8 - &#40;bestFrom >> 4&#41;, &#40;bestTo&7&#41; + 'a', 8 - &#40;bestTo >> 4&#41;, promo&#41;;
    &#125;
    fflush&#40;stdout&#41;; i = 0;
    while&#40;&#40;line&#91;i++&#93; = c = getchar&#40;)) != '\n') if&#40;c == EOF&#41; printf&#40;"# EOF\n"), exit&#40;1&#41;; line&#91;i&#93; = '\0';
    if&#40;*line == '\n') continue;
    sscanf&#40;line, "%s", command&#41;;
    printf&#40;"# command&#58; %s\n", command&#41;;
    if&#40;!strcmp&#40;command, "usermove")) &#123;
	int from, to; char c, d, promo, ep;
	sscanf&#40;line, "usermove %c%d%c%d%c", &c, &from, &d, &to, &promo&#41;;
	from = &#40;8 - from&#41;*16 + c - 'a'; to = &#40;8 - to&#41;*16 + d - 'a';
	if&#40;&#40;board&#91;from&#93; & 7&#41; == 3 && to - from == 2&#41; board&#91;from + 1&#93; = board&#91;to + 1&#93;, board&#91;to + 1&#93; = 0; // K-side castling
	if&#40;&#40;board&#91;from&#93; & 7&#41; == 3 && from - to == 2&#41; board&#91;from - 1&#93; = board&#91;to - 2&#93;, board&#91;to - 2&#93; = 0; // Q-side castling
	ep = (&#40;board&#91;from&#93; & 7&#41; < 3 && !board&#91;to&#93;); // recognize e.p. capture
	board&#91;to&#93; = board&#91;from&#93;; if&#40;ep&#41; board&#91;from & 0x70 | to & 7&#93; = 0; board&#91;from&#93; = 0;
	if&#40;promo == 'q') board&#91;to&#93; = board&#91;to&#93; | 7; // promote
	stm ^= COLOR;	
    &#125;
    else if&#40;!strcmp&#40;command, "protover")) printf&#40;"feature myname=\"N.E.G. 1.1\" setboard=1 usermove=1 analyze=0 colors=0 sigint=0 sigterm=0 done=1\n");
    else if&#40;!strcmp&#40;command, "new"))      stm = Setup&#40;"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"), randomize = 0, engineSide = BLACK;
    else if&#40;!strcmp&#40;command, "go"))       engineSide = stm;
    else if&#40;!strcmp&#40;command, "result"))   engineSide = 0;
    else if&#40;!strcmp&#40;command, "force"))    engineSide = 0;
    else if&#40;!strcmp&#40;command, "setboard")) stm = Setup&#40;line+9&#41;;
    else if&#40;!strcmp&#40;command, "random"))   randomize = !randomize;
    else if&#40;!strcmp&#40;command, "post"))     post = 1;
    else if&#40;!strcmp&#40;command, "nopost"))   post = 0;
    else if&#40;!strcmp&#40;command, "quit"))     break;
  &#125;
  return 0;
&#125;