Tuned null move and history prun... ops, late move reduction :)
[rattatechess.git] / io.cpp
blobc68e6f66bb84c3af270729d15d0c17a73a29b965
1 /***************************************************************************
2 main.cpp - description
3 -------------------
4 begin : Dom Oct 9 2005
5 copyright : (C) 2005 by Maurizio Monge
6 email : monge@linuz.sns.it
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include <stdarg.h>
19 #include <string.h>
20 #include "engine.h"
21 #include "commands.h"
23 void Engine::output(const char* fmt, ...)
25 char buf[4096];
26 int len;
27 va_list va;
28 va_start( va, fmt );
29 len = MIN(vsnprintf( buf, 4096, fmt, va), 4096);
30 va_end( va );
32 fwrite(buf, 1, len, stdout);
33 if(log)
35 fwrite(buf, 1, len, log);
36 fflush(log);
40 void Engine::process_input()
42 char str[4096];
43 int argc = 0;
44 char *argv[256];
45 int retv = 0;
46 Move mv;
48 if(!fgets(str,4096,stdin))
49 exit(0);
51 if(log)
53 fwrite("--> ", 1, 4, log);
54 fwrite(str, 1, strlen(str), log);
55 fflush(log);
58 char *ptr = str;
59 ptr += strspn(ptr, " \t\r\n");
61 for(int i=0;i<256;i++)
63 if(!*ptr)
64 break;
65 char *t = ptr;
66 char *tmp = (ptr += strcspn(ptr, " \t\r\n"));
67 ptr += strspn(ptr, ". \t\r\n");
68 *tmp = 0;
70 printf("got \"%s\"\n", t);
71 argv[argc++] = t;
74 if(!argc || strlen(argv[0])==0)
75 return;
77 /* is this a move? */
78 retv = parse_move(&mv,str);
80 /* not a move */
81 if(retv == 0)
83 CmdSet::iterator it = commands.lower_bound(argv[0]);
84 if( it != commands.end() &&
85 it->first.compare(0, strlen(argv[0]), argv[0]) == 0 )
87 Cmd& c = it->second;
89 if(it->first != argv[0])
90 output("%s\n", it->first.c_str());
92 if(argc-1 < c.min())
93 output("Error, not enough arguments (%d) for command '%s' (try 'help')\n", argc-1, it->first.c_str() );
94 else if(argc-1 > c.max())
95 output("Error, too many arguments (%d) for command '%s' (try 'help')\n", argc-1, it->first.c_str() );
96 else
97 c.run(argc-1, argv+1);
99 else
100 output("Illegal command: '%s' (try 'help')\n", argv[0]);
102 /* a legal valid move */
103 else if(retv == 1)
105 if(!io_xboard)
106 print_moves(&mv,1);
108 move(mv);
110 if(!io_xboard)
111 print_board();
113 /* a move that is illegal or ambiguous */
114 else if(retv == 2)