-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhappyclean.cpp
More file actions
167 lines (155 loc) · 3.16 KB
/
happyclean.cpp
File metadata and controls
167 lines (155 loc) · 3.16 KB
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
#include "happyclean.h"
Grid::Grid(int num, Ctrl *parent): Ctrl(parent) {
srand((unsigned)time(NULL));
for(int i=0; i<num; i++) {
for(int j=0; j<num; j++) {
if(i==0 || j==0) {
this->data[i][j]=rand()%2+1;
continue;
}
int num=rand() % 130;
if(num<70) {
this->data[i][j]=num/10+1;
} else if(num<90) {
this->data[i][j]=this->data[i-1][j];
} else if(num<110) {
this->data[i][j]=this->data[i][j-1];
} else {
this->data[i][j]=rand()%2?this->data[i-1][j]:this->data[i][j-1];
}
}
}
this->num=num;
this->x=this->y=0;
}
void Grid::toShow(int focus) {
for(int i=0; i<this->num; i++) {
for(int j=0; j<this->num; j++) {
int show=(this->data[i][j]<<4);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), show?show|BACKGROUND_INTENSITY:show);
this->setCursor(this->pos.X+j*2, this->pos.Y+i);
char c=' ';
if(i==this->y && j==this->x) {
c=this->data[i][j]+'0';
}
printf("%c ", c);
}
}
}
int Grid::Up() {
if(this->y<=0 || this->data[this->y-1][this->x]==0) {
return 0;
}
this->y--;
return 1;
}
int Grid::Down() {
if(this->y+1>=this->num || this->data[this->y+1][this->x]==0) {
return 0;
}
this->y++;
return 1;
}
int Grid::Left() {
if(this->x<=0 || this->data[this->y][this->x-1]==0) {
return 0;
}
this->x--;
return 1;
}
int Grid::Right() {
if(this->x+1>=this->num || this->data[this->y][this->x+1]==0) {
return 0;
}
this->x++;
return 1;
}
int Grid::Enter() {
int x=this->x;
int y=this->y;
if(x>0 && this->data[y][x-1] == this->data[y][x]) {
this->erase();
this->fix();
return 1;
}
if(x<this->num-1 && this->data[y][x+1] == this->data[y][x]) {
this->erase();
this->fix();
return 1;
}
if(y>0 && this->data[y-1][x] == this->data[y][x]) {
this->erase();
this->fix();
return 1;
}
if(y<this->num-1 && this->data[y+1][x] == this->data[y][x]) {
this->erase();
this->fix();
return 1;
}
return 0;
}
void Grid::fix() {
for(int col=0; col<this->num; col++) {
for(int i=1; i<this->num; i++) {
if(this->data[i][col]==0) {
int j=i;
while(j>0 && this->data[j-1][col] != 0) {
short temp=this->data[j][col];
this->data[j][col]=this->data[j-1][col];
this->data[j-1][col]=temp;
j--;
}
}
}
}
int beCovered=-1;
for(col=0; col<this->num; col++) {
if(this->data[this->num-1][col]!=0 && beCovered!=-1) {
for(int j=0; j<this->num; j++) {
this->data[j][beCovered]=this->data[j][col];
}
beCovered++;
for(j=0; j<this->num; j++) {
this->data[j][col]=0;
}
}
if(this->data[this->num-1][col]==0) {
if(beCovered==-1) {
beCovered=col;
}
}
}
while(this->data[this->y][this->x]==0) {
this->y++;
if(this->y == this->num) {
this->x--;
this->y=0;
}
}
}
void Grid::erase(int color, int x, int y) {
if(color==-1) {
color=this->data[this->y][this->x];
}
if(x==-1 || y==-1) {
x=this->x;
y=this->y;
}
if(this->data[y][x]!=color) {
return;
}
this->data[y][x]=0;
if(x>0) {
this->erase(color, x-1, y);
}
if(y>0) {
this->erase(color, x, y-1);
}
if(x<this->num-1) {
this->erase(color, x+1, y);
}
if(y<this->num-1) {
this->erase(color, x, y+1);
}
}