forked from bupticybee/TexasSolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategyexplorer.cpp
274 lines (249 loc) · 11.4 KB
/
strategyexplorer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "strategyexplorer.h"
#include "ui_strategyexplorer.h"
#include "qstandarditemmodel.h"
#include <QBrush>
#include <QApplication>
#include <QComboBox>
#include <QColor>
#include "include/Card.h"
StrategyExplorer::StrategyExplorer(QWidget *parent,QSolverJob * qSolverJob) :
QDialog(parent),
ui(new Ui::StrategyExplorer)
{
this->qSolverJob = qSolverJob;
this->detailWindowSetting = DetailWindowSetting();
ui->setupUi(this);
/*
QStandardItemModel* model = new QStandardItemModel();
for (int row = 0; row < 4; ++row) {
QStandardItem *item = new QStandardItem(QString("%1").arg(row) );
model->appendRow( item );
}
this->ui->gameTreeView->setModel(model);
*/
// Initial Game Tree preview panel
this->ui->gameTreeView->setTreeData(qSolverJob);
connect(
this->ui->gameTreeView,
SIGNAL(expanded(const QModelIndex&)),
this,
SLOT(item_expanded(const QModelIndex&))
);
connect(
this->ui->gameTreeView,
SIGNAL(clicked(const QModelIndex&)),
this,
SLOT(item_clicked(const QModelIndex&))
);
// Initize strategy(rough) table
this->tableStrategyModel = new TableStrategyModel(this->qSolverJob,this);
this->ui->strategyTableView->setModel(this->tableStrategyModel);
this->delegate_strategy = new StrategyItemDelegate(this->qSolverJob,&(this->detailWindowSetting),this);
this->ui->strategyTableView->setItemDelegate(this->delegate_strategy);
Deck* deck = this->qSolverJob->get_solver()->get_deck();
int index = 0;
QString board_qstring = QString::fromStdString(this->qSolverJob->clt->board);
for(Card one_card: deck->getCards()){
if(board_qstring.contains(QString::fromStdString(one_card.getCard())))continue;
QString card_str_formatted = QString::fromStdString(toFormattedString(one_card));
this->ui->turnCardBox->addItem(card_str_formatted);
this->ui->riverCardBox->addItem(card_str_formatted);
if(card_str_formatted.contains(QString::fromLocal8Bit("♦️")) ||
card_str_formatted.contains(QString::fromLocal8Bit("♥️️"))){
this->ui->turnCardBox->setItemData(0, QBrush(Qt::red),Qt::ForegroundRole);
this->ui->riverCardBox->setItemData(0, QBrush(Qt::red),Qt::ForegroundRole);
}else{
this->ui->turnCardBox->setItemData(0, QBrush(Qt::black),Qt::ForegroundRole);
this->ui->riverCardBox->setItemData(0, QBrush(Qt::black),Qt::ForegroundRole);
}
this->cards.push_back(one_card);
index += 1;
}
if(this->qSolverJob->get_solver()->getGameTree()->getRoot()->getRound() == GameTreeNode::GameRound::FLOP){
this->tableStrategyModel->setTrunCard(this->cards[0]);
this->tableStrategyModel->setRiverCard(this->cards[1]);
this->ui->riverCardBox->setCurrentIndex(1);
}
else if(this->qSolverJob->get_solver()->getGameTree()->getRoot()->getRound() == GameTreeNode::GameRound::TURN){
this->tableStrategyModel->setRiverCard(this->cards[0]);
this->ui->turnCardBox->clear();
}
else if(this->qSolverJob->get_solver()->getGameTree()->getRoot()->getRound() == GameTreeNode::GameRound::RIVER){
this->ui->turnCardBox->clear();
this->ui->riverCardBox->clear();
}
// Initize timer for strategy auto update
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update_second()));
timer->start(1000);
// On mouse event of strategy table
connect(this->ui->strategyTableView,SIGNAL(itemMouseChange(int,int)),this,SLOT(onMouseMoveEvent(int,int)));
// Initize Detail Viewer window
this->detailViewerModel = new DetailViewerModel(this->tableStrategyModel,this);
this->ui->detailView->setModel(this->detailViewerModel);
this->detailItemItemDelegate = new DetailItemDelegate(&(this->detailWindowSetting),this);
this->ui->detailView->setItemDelegate(this->detailItemItemDelegate);
// Initize Rough Strategy Viewer
this->roughStrategyViewerModel = new RoughStrategyViewerModel(this->tableStrategyModel,this);
this->ui->roughStrategyView->setModel(this->roughStrategyViewerModel);
this->roughStrategyItemDelegate = new RoughStrategyItemDelegate(&(this->detailWindowSetting),this);
this->ui->roughStrategyView->setItemDelegate(this->roughStrategyItemDelegate);
}
StrategyExplorer::~StrategyExplorer()
{
delete ui;
delete this->delegate_strategy;
delete this->tableStrategyModel;
delete this->detailViewerModel;
delete this->roughStrategyViewerModel;
delete this->timer;
}
void StrategyExplorer::item_expanded(const QModelIndex& index){
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
int num_child = item->childCount();
for (int i = 0;i < num_child;i ++){
TreeItem* one_child = item->child(i);
if(one_child->childCount() != 0)continue;
this->ui->gameTreeView->tree_model->reGenerateTreeItem(one_child->m_treedata.lock()->getRound(),one_child);
}
}
void StrategyExplorer::process_board(TreeItem* treeitem){
vector<string> board_str_arr = string_split(this->qSolverJob->clt->board,',');
vector<Card> cards;
for(string one_board_str:board_str_arr){
cards.push_back(Card(one_board_str));
}
if(treeitem != NULL){
if(treeitem->m_treedata.lock()->getRound() == GameTreeNode::GameRound::TURN && !this->tableStrategyModel->getTrunCard().empty()){
cards.push_back(Card(this->tableStrategyModel->getTrunCard()));
}
else if(treeitem->m_treedata.lock()->getRound() == GameTreeNode::GameRound::RIVER){
if(!this->tableStrategyModel->getTrunCard().empty())
cards.push_back(Card(this->tableStrategyModel->getTrunCard()));
if(!this->tableStrategyModel->getRiverCard().empty())
cards.push_back(Card(this->tableStrategyModel->getRiverCard()));
}
}
this->ui->boardLabel->setText(QString("<b>%1: </b>").arg(tr("board")) + boardCards2html(cards));
}
void StrategyExplorer::process_treeclick(TreeItem* treeitem){
shared_ptr<GameTreeNode> treenode = treeitem->m_treedata.lock();
if(treenode->getType() == GameTreeNode::GameTreeNodeType::ACTION){
shared_ptr<ActionNode> actionnode = static_pointer_cast<ActionNode>(treenode);
QString action_str = QString("<b>%1 %2</b>").arg(actionnode->getPlayer() == 0?tr("IP"):tr("OOP"),tr(" decision node"));
this->ui->nodeDisplayLabel->setText(action_str);
}
else if(treenode->getType() == GameTreeNode::GameTreeNodeType::CHANCE){
QString chance_str = tr("<b>Chance node</b>");
this->ui->nodeDisplayLabel->setText(chance_str);
}
else if(treenode->getType() == GameTreeNode::GameTreeNodeType::TERMINAL){
QString terminal_str = tr("<b>Terminal node</b>");
this->ui->nodeDisplayLabel->setText(terminal_str);
}
else if(treenode->getType() == GameTreeNode::GameTreeNodeType::SHOWDOWN){
QString showdown_str = tr("<b>Showdown node</b>");
this->ui->nodeDisplayLabel->setText(showdown_str);
}
}
void StrategyExplorer::item_clicked(const QModelIndex& index){
try{
TreeItem * treeNode = static_cast<TreeItem*>(index.internalPointer());
this->process_treeclick(treeNode);
this->process_board(treeNode);
this->tableStrategyModel->setGameTreeNode(treeNode);
this->tableStrategyModel->updateStrategyData();
this->ui->strategyTableView->viewport()->update();
this->roughStrategyViewerModel->onchanged();
this->ui->roughStrategyView->triger_resize();
this->ui->roughStrategyView->viewport()->update();
}
catch (const runtime_error& error)
{
qDebug().noquote() << tr("Encountering error:");//.toStdString() << endl;
qDebug().noquote() << error.what() << "\n";
}
}
void StrategyExplorer::selection_changed(const QItemSelection &selected,
const QItemSelection &deselected){
}
void StrategyExplorer::on_turnCardBox_currentIndexChanged(int index)
{
if(this->cards.size() > 0 && index < this->cards.size()){
this->tableStrategyModel->setTrunCard(this->cards[index]);
this->tableStrategyModel->updateStrategyData();
// TODO this somehow cause bugs, crashes, why?
//this->roughStrategyViewerModel->onchanged();
//this->ui->roughStrategyView->viewport()->update();
this->process_board(this->tableStrategyModel->treeItem);
}
this->ui->strategyTableView->viewport()->update();
this->ui->detailView->viewport()->update();
}
void StrategyExplorer::on_riverCardBox_currentIndexChanged(int index)
{
if(this->cards.size() > 0 && index < this->cards.size()){
this->tableStrategyModel->setRiverCard(this->cards[index]);
this->tableStrategyModel->updateStrategyData();
//this->roughStrategyViewerModel->onchanged();
//this->ui->roughStrategyView->viewport()->update();
this->process_board(this->tableStrategyModel->treeItem);
}
this->ui->strategyTableView->viewport()->update();
this->ui->detailView->viewport()->update();
}
void StrategyExplorer::update_second(){
if(this->cards.size() > 0){
this->tableStrategyModel->updateStrategyData();
this->roughStrategyViewerModel->onchanged();
this->ui->roughStrategyView->viewport()->update();
this->process_board(this->tableStrategyModel->treeItem);
}
this->ui->strategyTableView->viewport()->update();
this->ui->detailView->viewport()->update();
}
void StrategyExplorer::onMouseMoveEvent(int i,int j){
this->detailWindowSetting.grid_i = i;
this->detailWindowSetting.grid_j = j;
this->ui->detailView->viewport()->update();
this->ui->strategyTableView->viewport()->update();
}
void StrategyExplorer::on_strategyModeButtom_clicked()
{
this->detailWindowSetting.mode = DetailWindowSetting::DetailWindowMode::STRATEGY;
this->ui->strategyTableView->viewport()->update();
this->ui->detailView->viewport()->update();
this->roughStrategyViewerModel->onchanged();
this->ui->roughStrategyView->viewport()->update();
}
void StrategyExplorer::on_ipRangeButtom_clicked()
{
this->detailWindowSetting.mode = DetailWindowSetting::DetailWindowMode::RANGE_IP;
this->ui->strategyTableView->viewport()->update();
this->ui->detailView->viewport()->update();
this->roughStrategyViewerModel->onchanged();
this->ui->roughStrategyView->viewport()->update();
}
void StrategyExplorer::on_oopRangeButtom_clicked()
{
this->detailWindowSetting.mode = DetailWindowSetting::DetailWindowMode::RANGE_OOP;
this->ui->strategyTableView->viewport()->update();
this->ui->detailView->viewport()->update();
this->roughStrategyViewerModel->onchanged();
this->ui->roughStrategyView->viewport()->update();
}
void StrategyExplorer::on_evModeButtom_clicked()
{
this->detailWindowSetting.mode = DetailWindowSetting::DetailWindowMode::EV;
this->ui->strategyTableView->viewport()->update();
this->ui->detailView->viewport()->update();
this->ui->roughStrategyView->viewport()->update();
}
void StrategyExplorer::on_evOnlyModeButtom_clicked()
{
this->detailWindowSetting.mode = DetailWindowSetting::DetailWindowMode::EV_ONLY;
this->ui->strategyTableView->viewport()->update();
this->ui->detailView->viewport()->update();
this->roughStrategyViewerModel->onchanged();
this->ui->roughStrategyView->viewport()->update();
}