Fixes to pluto heuristic, small cleanups.
[rattatechess.git] / main.cpp
blob2d730f21bcea49f13940244824e13dd6c3301e3a
1 /***************************************************************************
2 main.cpp - description
3 -------------------
4 begin : Fri Jan 11 2002
5 copyright : (C) 2002-2007 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 ***************************************************************************/
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "board.h"
24 #include "engine.h"
26 //--------------------------------------------------------------------------------------------
29 int main(int argc, char *argv[])
31 #if defined(_WIN32) || defined(_WIN64)
32 putenv("HOME=C:");
33 #endif
35 init_stdin();
36 //setvbuf(stdin, NULL, _IOLBF, 0);
37 setbuf(stdin, NULL);
38 setvbuf(stdout, NULL, _IOLBF, 0);
40 bool message = false;
41 Engine *eng = new Engine();
43 /*#define PRINT_SIZE(c) printf("sizeof(" #c ") = %ld\n", sizeof(c))
44 PRINT_SIZE(Move);
45 PRINT_SIZE(HashKey);
46 PRINT_SIZE(HashEntry);
47 PRINT_SIZE(LinePawns);
48 PRINT_SIZE(MatTrack);*/
50 if(argc >=2 && !strcmp(argv[1], "-a"))
51 eng->autotune();
53 if(argc >=4 && !strcmp(argv[1], "-b"))
55 int tot_nodes = 0;
56 eng->set_depth(atoi(argv[2]));
57 for(int i=3;i<argc;i++)
59 FILE* f = fopen(argv[i],"r");
60 if(f)
62 eng->read_board(f);
63 fclose(f);
65 eng->start();
66 printf("\nanalyzing %s\n", argv[i]);
67 eng->find_best_move();
68 tot_nodes += eng->processed_nodes;
71 printf("\ntot processed nodes is %d\n", tot_nodes);
72 return 0;
75 if(argc >=3 && !strcmp(argv[1], "-e"))
77 for(int i=2;i<argc;i++)
79 FILE* f = fopen(argv[i],"r");
80 if(f)
82 eng->read_board(f);
83 fclose(f);
84 eng->print_board();
87 return 0;
90 #if 0
91 eng->print_board();
92 eng->set_depth(9);
93 eng->read_board("r2rb1k1/pp1q1p1p/2n1p1p1/2bp4/5P2/PP1BPR1Q/1BPN2PP/R5K1 w - - 0 1");
94 eng->start();
95 eng->find_best_move();
96 #endif
98 if(!eng->io_xboard)
99 eng->print_board();
101 int prev_move_index = 999;
102 int prev_move_val = 0;
104 while(1)
106 if(eng->eng_status==ANALYZING)
108 eng->analyze();
110 else if(eng->computer_color() != eng->color_to_move()
111 || eng->get_status() != PLAYING) //wait for command or move...
113 if(eng->ponder && eng->eng_status==PLAYING &&
114 eng->status == PLAYING &&
115 eng->st_computer_color == eng->board.other_color)
116 eng->do_ponder();
117 else
118 eng->process_input();
120 else
122 if(prev_move_index > eng->mv_done_num)
123 prev_move_val = +INF;
124 prev_move_index = eng->mv_done_num;
126 Move mv = eng->find_best_move();
127 int t = current_time(); ////
128 if(t < eng->max_think_time-5) ////
130 printf("# will simulate a human think %d centiseconds\n", 50*(eng->max_think_time-t));
131 //usleep(5000*(eng->max_think_time-t)); ////
134 //// if(mv.val > prev_move_val+200 && mv.capture && PIECE_OF(mv.capture) != PAWN)
135 //// eng->output("tellicsnoalias say Chomp!\n");
137 prev_move_val = mv.val;
139 if(!eng->io_xboard)
140 eng->print_moves(&mv,1);
142 if(eng->io_xboard && !eng->io_san)
143 eng->output("move %s\n",eng->board.move_to_coord(MoveStr().data(), mv));
144 else
145 eng->output("move %s\n",eng->board.move_to_alg(MoveStr().data(), mv));
146 eng->move(mv);
148 if(!eng->io_xboard)
149 eng->print_board();
150 message=true;
153 if(eng->get_status()!=PLAYING && message)
155 message=false;
156 eng->output("%s {%s}\n", eng->status==_01?"0-1":
157 eng->status==_10?"1-0":"1/2-1/2",
158 eng->status_string );