-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
137 lines (133 loc) · 3.97 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow), game_qt(nullptr) {
ui->setupUi(this);
db = new DrawBoard();
random_layout_btn = new QPushButton("随机布局");
custom_layout_btn = new QPushButton("人工布局");
ai_btn = new QPushButton("AI");
debug_check_box = new QCheckBox("Debug");
connect(random_layout_btn, SIGNAL(clicked(bool)), this,
SLOT(random_layout_btn_clicked(bool)));
connect(custom_layout_btn, SIGNAL(clicked(bool)), this,
SLOT(custom_layout_btn_clicked(bool)));
connect(ai_btn, SIGNAL(clicked(bool)), this, SLOT(ai_btn_clicked(bool)));
connect(debug_check_box, SIGNAL(stateChanged(int)), this,
SLOT(debug_check_box_state_changed(int)));
layout = new QVBoxLayout();
layout->addWidget(db);
layout->addWidget(random_layout_btn);
layout->addWidget(custom_layout_btn);
layout->addWidget(ai_btn);
layout->addWidget(debug_check_box);
ui->centralWidget->setLayout(layout);
}
void MainWindow::game_over() {
int score = 0;
int grab_gold = 0;
int dead = 0;
Player *player = game_qt->get_player();
if (player->is_grab_gold())
grab_gold = 1000;
score += grab_gold;
score -= player->get_action_count();
score -= DEFAULT_ARROW_COUNT - player->get_arrow_count();
if (player->is_dead)
dead -= 1000;
score += dead;
QString prompt =
"Game over! Score = (Grab gold?) " + QString::number(grab_gold) +
" + (dead?) " + QString::number(dead) + " - (step) " +
QString::number(player->get_action_count()) + " - (arrow used) " +
QString::number(DEFAULT_ARROW_COUNT - player->get_arrow_count()) + " = " +
QString::number(score);
QMessageBox::information(this, "Game Over", prompt);
}
void MainWindow::debug_check_box_state_changed(int state) {
is_debug = state;
db->set_map(game_qt->extract_map_info(is_debug));
db->set_player(game_qt->get_player());
db->update();
}
void MainWindow::random_layout_btn_clicked(bool) {
if (game_qt) {
delete game_qt;
game_qt = nullptr;
}
game_qt = new GameQt();
game_qt->start();
db->set_map(game_qt->extract_map_info(is_debug));
db->set_player(game_qt->get_player());
db->update();
}
void MainWindow::custom_layout_btn_clicked(bool) {
db->set_map(nullptr);
if(game_qt) {
delete game_qt;
game_qt = nullptr;
}
game_qt = new GameQt();
game_qt->generate_map(false);
Dialog dlg(game_qt);
dlg.exec();
if(dlg.ok) {
int start_x = game_qt->get_player()->get_x();
int start_y = game_qt->get_player()->get_y();
game_qt->move(start_x, start_y, start_x, start_y);
db->set_map(game_qt->extract_map_info(is_debug));
db->set_player(game_qt->get_player());
db->update();
}
}
void MainWindow::ai_btn_clicked(bool) {
if (game_qt->is_over())
return;
int data[10];
Action action = game_qt->ask(data);
auto event = game_qt->take_action(action, data);
db->set_map(game_qt->extract_map_info(is_debug));
db->set_player(game_qt->get_player());
db->update();
if (event == Event::GameOver) {
game_over();
}
}
MainWindow::~MainWindow() {
delete ui;
if (game_qt)
delete game_qt;
}
void MainWindow::keyPressEvent(QKeyEvent *event) {
if (game_qt->is_over())
return;
int key = event->key();
Event game_event;
switch (key) {
case Qt::Key_W:
game_event = game_qt->take_action(Action::MoveForward);
break;
case Qt::Key_A:
game_event = game_qt->take_action(Action::TurnLeft);
break;
case Qt::Key_D:
game_event = game_qt->take_action(Action::TurnRight);
break;
case Qt::Key_S:
game_event = game_qt->take_action(Action::Shoot);
break;
case Qt::Key_G:
game_event = game_qt->take_action(Action::Grab);
break;
case Qt::Key_L:
game_event = game_qt->take_action(Action::Leave);
break;
default:
return;
}
db->set_map(game_qt->extract_map_info(is_debug));
db->update();
if (game_event == Event::GameOver) {
game_over();
}
}