-
Notifications
You must be signed in to change notification settings - Fork 4
/
MainWindow.cpp
154 lines (128 loc) · 3.7 KB
/
MainWindow.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
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->m_gamePanel->create(QRect(19, 19, 412, 412), 15, 15);
//connect(ui->m_hostButton, SIGNAL(clicked()), this, SLOT(onHost()));
connect(ui->m_regretButton, SIGNAL(clicked()), this, SLOT(onRegret()));
connect(ui->m_connectButton, SIGNAL(clicked()), this, SLOT(onConnectClicked()));
m_network = new Network(this);
//connect(m_network, SIGNAL(receiveHost(QHostAddress)), this, SLOT(onReceiveHost(QHostAddress)));
connect(m_network, SIGNAL(otherConnected()), this, SLOT(onOtherConnected()));
connect(m_network, SIGNAL(joinGame(Qizi, bool)), this, SLOT(onJoinGame(Qizi, bool)));
connect(ui->m_gamePanel, SIGNAL(dropMine(Step)), this, SLOT(onDropMine(Step)));
connect(ui->m_gamePanel, SIGNAL(win(Qizi)), this, SLOT(onWin(Qizi)));
connect(m_network, SIGNAL(otherDrops(Step)), this, SLOT(onQiziDropped(Step)));
connect(m_network, SIGNAL(otherAsksRegret()), this, SLOT(onOtherAsksRegret()));
connect(m_network, SIGNAL(otherRespondsRegret(bool)), this, SLOT(onOtherRespondsRegret(bool)));
QList<QHostAddress> localAddresses = m_network->localAddresses();
foreach(const QHostAddress& addr, localAddresses)
{
ui->m_localAddrCombo->addItem(addr.toString());
}
reset();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::reset()
{
ui->m_gamePanel->reset();
m_network->close();
m_regretAsked = false;
showInfo(tr("Game ready."));
}
//void MainWindow::onHost()
//{
// m_network->broadcastToHost();
// showInfo(tr("Host message has been sent."));
//}
void MainWindow::onRegret()
{
// already regret
if (m_regretAsked)
return;
m_network->askRegret();
m_regretAsked = true;
showInfo(tr("Ask to regret..."));
}
void MainWindow::onConnectClicked()
{
m_network->join(ui->m_serverAddr->text());
}
//void MainWindow::onReceiveHost(QHostAddress addr)
//{
// QString text = tr("Received host from %1, join it?").arg(addr.toString());
// if (QMessageBox::question(this, tr("Query"), text, QMessageBox::Yes|QMessageBox::No) == QMessageBox::Yes)
// m_network->join(addr);
//}
void MainWindow::onOtherConnected()
{
ui->m_gamePanel->start(BLACK_QIZI, true);
m_network->notifyStart(WHITE_QIZI, false);
showInfo(tr("A player has joined. You go first."));
}
void MainWindow::onJoinGame(Qizi qizi, bool firstDrop)
{
ui->m_gamePanel->start(qizi, firstDrop);
showInfo(tr("Joined, wait for the other player."));
}
void MainWindow::onDropMine(Step step)
{
m_network->notifyDrop(step);
onQiziDropped(step);
}
void MainWindow::onWin(Qizi qizi)
{
if (qizi == ui->m_gamePanel->myQizi())
{
QMessageBox::information(this, tr("Win"), tr("You win the game!"));
}
else
{
QMessageBox::information(this, tr("Lose"), tr("You lose the game!"));
}
reset();
}
void MainWindow::onQiziDropped(Step step)
{
if (step.qizi == ui->m_gamePanel->myQizi())
showInfo(tr("Wait for the other player."));
else
showInfo(tr("It's my turn to play."));
ui->m_gamePanel->dropQizi(step);
}
void MainWindow::onOtherAsksRegret()
{
if (QMessageBox::question(this, tr("Regret"), tr("The other play asks to regret, agree?"),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
{
m_network->respondRegret(true);
ui->m_gamePanel->regret(ui->m_gamePanel->otherQizi());
}
else
{
m_network->respondRegret(false);
}
}
void MainWindow::onOtherRespondsRegret(bool agree)
{
if (agree)
{
ui->m_gamePanel->regret(ui->m_gamePanel->myQizi());
}
else
{
QMessageBox::information(this, tr("Regret"), tr("The other player disagrees to regret."));
}
m_regretAsked = false;
}
void MainWindow::showInfo(const QString& text)
{
ui->m_info->setText(text);
}