Changed eval: bishop pair, dynamic rook and knight. Small cleanup.
[rattatechess.git] / search_gui.cpp
blob92d76f5d56068182ad6fe31772e45e1382632094
1 /***************************************************************************
2 search_gui.cpp - Gui to view the search tree
3 -------------------
4 begin : Sat Oct 06 2007
5 copyright : (C) 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 ***************************************************************************/
18 #include "search_gui.h"
19 #include <stdarg.h>
20 #include <QWidget>
21 #include <QApplication>
22 #include <QTreeWidget>
23 #include <QHeaderView>
24 #include <QSocketNotifier>
25 #include <QTreeWidgetItem>
27 QString qPrintf(const char* fmt, ...) {
28 va_list ap;
30 va_start(ap, fmt);
31 int l = vsnprintf(NULL, 0, fmt, ap)+1;
32 va_end(ap);
34 char *str = (char*)alloca(l);
35 //char *str = (char*)malloc(l);
36 va_start(ap, fmt);
37 vsnprintf(str, l, fmt, ap);
38 va_end(ap);
40 QString retv(str);
42 //free(str);
43 return retv;
46 SearchGui::SearchGui(int& argc, char** argv)
48 printf("Hello!\n");
49 app = new QApplication(argc, argv);
50 tree_widget = new QTreeWidget();
51 tree_widget->header()->hide();
52 tree_widget->resize(640, 480);
53 tree_widget->show();
55 max_ply = 4;
57 new QSocketNotifier( fileno(stdin), QSocketNotifier::Read, this);
58 new QSocketNotifier( fileno(stdin), QSocketNotifier::Exception, this);
61 SearchGui::~SearchGui()
63 delete tree_widget;
64 delete app;
67 void SearchGui::wait_input()
69 while(1)
71 if(input_available())
72 return;
73 app->processEvents(QEventLoop::WaitForMoreEvents);
77 void SearchGui::process_events()
79 while(app->hasPendingEvents())
80 app->processEvents();
83 void SearchGui::apply_flags(QTreeWidgetItem* w, int flags)
85 QFont font = w->font(0);
86 if(flags & Italic)
87 font.setItalic(true);
88 if(flags & Bold)
89 font.setBold(true);
90 if(flags & NoItalic)
91 font.setItalic(false);
92 if(flags & NoBold)
93 font.setBold(false);
94 w->setFont(0, font);
95 if(flags & Red)
96 w->setForeground(0, Qt::red);
97 if(flags & Gray)
98 w->setForeground(0, Qt::gray);
99 if(flags & Green)
100 w->setForeground(0, Qt::green);
101 if(flags & Blue)
102 w->setForeground(0, Qt::blue);
103 if(flags & Magenta)
104 w->setForeground(0, Qt::magenta);
107 void SearchGui::init_root()
109 tree_widget->clear();
112 void SearchGui::new_root_level(int depth)
114 QTreeWidgetItem *item = new QTreeWidgetItem(QStringList()<<qPrintf("ROOT(%d)", depth));
115 tree_widget->addTopLevelItem(item);
116 item->setExpanded(true);
119 void SearchGui::notify_value(int ply, int value, int nodecount, int newflags)
121 if(ply>max_ply)
122 return;
124 QTreeWidgetItem *item = tree_widget->topLevelItem(tree_widget->topLevelItemCount()-1);
125 for(int i=0;i<=ply;i++)
126 item = (item && item->childCount()) ? item->child(item->childCount()-1) : NULL;
127 if(!item)
128 return;
129 item->setText(0, qPrintf( qPrintable(item->text(0)), value, nodecount ) );
130 apply_flags(item, newflags);
133 void SearchGui::notify(Move m, int ply, int depth, int heuval, int alpha, int beta, int flags)
135 if(ply>max_ply)
136 return;
138 char str[64];
139 if(m == Move::None())
140 strcpy(str, "[NULL]");
141 else
143 Engine::eng()->board.undo_move(m);
144 Engine::eng()->move_to_alg(str, &m);
145 Engine::eng()->board.do_move(m);
146 if(heuval != -INF)
147 sprintf(str+strlen(str), "(%d)", heuval);
149 QTreeWidgetItem *parent = tree_widget->topLevelItem(tree_widget->topLevelItemCount()-1);
150 for(int i=0;i<ply;i++)
151 parent = (parent && parent->childCount()) ? parent->child(parent->childCount()-1) : NULL;
152 if(!parent)
153 return;
154 QTreeWidgetItem *item = new QTreeWidgetItem(parent, QStringList()<<
155 qPrintf("%s -> %%d @%d [%d .. %d] nc=%%d", str, depth, alpha, beta) );
156 if(parent->isExpanded())
157 tree_widget->scrollToItem(item);
158 apply_flags(item, flags);
161 void SearchGui::notify_eval(int ply, int value, int alpha, int beta, int flags)
163 if(ply>max_ply)
164 return;
166 QTreeWidgetItem *parent = tree_widget->topLevelItem(tree_widget->topLevelItemCount()-1);
167 for(int i=0;i<ply;i++)
168 parent = (parent && parent->childCount()) ? parent->child(parent->childCount()-1) : NULL;
169 if(!parent)
170 return;
171 QTreeWidgetItem *item = new QTreeWidgetItem(parent, QStringList()<<
172 qPrintf("[EVAL] -> %d [%d .. %d]", value, alpha, beta) );
173 if(parent->isExpanded())
174 tree_widget->scrollToItem(item);
175 apply_flags(item, flags);
178 void SearchGui::notify_hash(int ply, int lower, int upper, int depth, int alpha, int beta, Move best, bool write, int flags)
180 if(ply>max_ply)
181 return;
183 char buf[32];
184 if(best == Move::None())
185 strcpy(buf, "none");
186 else
187 Engine::eng()->move_to_coord(buf, &best);
188 QTreeWidgetItem *parent = tree_widget->topLevelItem(tree_widget->topLevelItemCount()-1);
189 for(int i=0;i<ply;i++)
190 parent = (parent && parent->childCount()) ? parent->child(parent->childCount()-1) : NULL;
191 if(!parent)
192 return;
193 QTreeWidgetItem *item = new QTreeWidgetItem(parent, QStringList()<<
194 qPrintf("[%s] -> (%d .. %d) <%s> [%d .. %d]", write?"HTWR":"HTLK", lower, upper, buf, alpha, beta) );
195 if(parent->isExpanded())
196 tree_widget->scrollToItem(item);
197 apply_flags(item, flags);
200 #include "search_gui.moc"