-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcardfield.cpp
116 lines (110 loc) · 2.89 KB
/
cardfield.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
#include "cardfield.h"
#include "field.h"
#include "getrandommatrix.h"
#include <QGridLayout>
#include <QDebug>
#include <QDir>
#include <iostream>
#include <string>
#include <sstream>
QString to_string(int number)
{
std::stringstream str;
str << number;
return QString::fromStdString(str.str());
}
CardField::CardField(QWidget* parent, int w, int h, Field* _field) : QWidget(parent), field(_field)
{
fieldStats = w * h;
width = w;
height = h;
data.resize(h);
auto matrix = getRandomMatrix(w, h);
mainLayout = new QGridLayout(this);
for (int i = 0; i < h; ++i)
{
for (int j = 0; j < w; ++j)
{
data[i].push_back(
new Card
(
nullptr,
matrix[i][j],
QDir::currentPath() + "/../Summer-Project-Game/ImagesForCards/cover.jpg",
QDir::currentPath() + "/../Summer-Project-Game/ImagesForCards/" + to_string(matrix[i][j]) + ".jpg",
100,
150,
this
)
);
mainLayout->addWidget(data[i][j],3 * j, 4 * i, 2, 3, Qt::AlignCenter);
}
}
}
CardField::~CardField(){
delete mainLayout;
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
delete data[i][j];
}
}
}
void CardField::check(){
int count = 0;
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
if(data[i][j]->getState()){
count++;
}
}
}
if(count == 2){
startTimer(500);
}
}
void CardField::timerEvent(QTimerEvent *event)
{
int id_first = -1;
int id_second = -1;
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
if(data[i][j]->getState()){
if(id_first == -1)
{
id_first = data[i][j]->getId();
}
else
{
id_second = data[i][j]->getId();
}
}
}
}
if(id_first == id_second)
{
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
if(data[i][j]->getState()){
data[i][j]->pushed();
data[i][j]->hide();
}
}
}
fieldStats -= 2;
}
else
{
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
if(data[i][j]->getState()){
data[i][j]->pushed();
}
}
}
}
if (fieldStats == 0)
{
field->setWin();
}
killTimer(event->timerId());
}