-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameWindow.cpp
146 lines (121 loc) · 4.23 KB
/
GameWindow.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
//Copyright 2011 Spencer Clark
/*
---------------------------------------------------------------------
This file is part of DeathReversi.
DeathReversi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DeathReversi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DeathReversi. If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------
*/
#include "GameWindow.h"
#include "ui_GameWindow.h"
#include "AIOptionsWidget.h"
#include "ReversiGame.h"
#include "AIReversiGame.h"
#include "AIvsAIReversiGame.h"
#include <QtDebug>
GameWindow::GameWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::GameWindow)
{
ui->setupUi(this);
options.blackSearchDepth = 6;
options.whiteSearchDepth = 6;
}
GameWindow::~GameWindow()
{
delete ui;
}
//private slot
void GameWindow::handleCountChange(quint16 white, quint16 black)
{
this->ui->statusBar->showMessage("White:" + QString::number(white) + " Black:" + QString::number(black));
}
//private slot
void GameWindow::handleGameOver(CELL_STATE)
{
quint16 whiteScore = this->game->getBoard()->getWhiteCount();
quint16 blackScore = this->game->getBoard()->getBlackCount();
if (whiteScore > blackScore)
this->ui->statusBar->showMessage("White wins " + QString::number(whiteScore) + " - " + QString::number(blackScore) + "!");
else if (blackScore > whiteScore)
this->ui->statusBar->showMessage("Black wins " + QString::number(blackScore) + " - " + QString::number(whiteScore) + "!");
else
this->ui->statusBar->showMessage("Tie! " + QString::number(blackScore) + " - " + QString::number(whiteScore) + "!");
}
//private slot
void GameWindow::on_actionVs_human_triggered()
{
this->setGame(QSharedPointer<ReversiGame>(new ReversiGame()));
}
//private slot
void GameWindow::on_actionAI_vs_AI_triggered()
{
this->setGame(QSharedPointer<ReversiGame>(new AIvsAIReversiGame()));
}
//private slot
void GameWindow::on_actionPlay_as_White_triggered()
{
this->setGame(QSharedPointer<ReversiGame>(new AIReversiGame(WHITE_CELL)));
}
//private slot
void GameWindow::on_actionPlay_as_Black_triggered()
{
this->setGame(QSharedPointer<ReversiGame>(new AIReversiGame(BLACK_CELL)));
}
//private slot
void GameWindow::on_actionAI_Settings_triggered()
{
AIOptionsWidget * widget = new AIOptionsWidget();
widget->setAttribute(Qt::WA_DeleteOnClose);
widget->setBlackSearchDepth(options.blackSearchDepth);
widget->setWhiteSearchDepth(options.whiteSearchDepth);
connect(widget,
SIGNAL(AIOptionsChanged(AIOptions)),
this,
SLOT(handleAIOptionChange(AIOptions)));
widget->show();
}
//private slot
void GameWindow::handleAIOptionChange(AIOptions options)
{
this->options = options;
if (game.isNull())
return;
game->setBlackAIDepth(options.blackSearchDepth);
game->setWhiteAIDepth(options.whiteSearchDepth);
}
//private
void GameWindow::setGame(QSharedPointer<ReversiGame> game)
{
if (game.isNull())
return;
//Force us to update the game's AI options
this->handleAIOptionChange(this->options);
this->ui->widget->setBoard(game->getBoard());
ReversiGame * raw = game.data();
connect(this->ui->widget,
SIGNAL(cellClicked(BoardPos)),
raw,
SLOT(handleCellClicked(BoardPos)));
connect(raw,
SIGNAL(gameOver(CELL_STATE)),
this,
SLOT(handleGameOver(CELL_STATE)));
this->game = game;
connect(raw,
SIGNAL(scoreChanged(quint16,quint16)),
this,
SLOT(handleCountChange(quint16,quint16)));
}
void GameWindow::on_actionExit_triggered()
{
this->close();
}