-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheatbean.cpp
More file actions
110 lines (102 loc) · 2.14 KB
/
eatbean.cpp
File metadata and controls
110 lines (102 loc) · 2.14 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
#include "eatbean.h"
EatbeanBg::EatbeanBg(int h, int w, bool fillSpace, Ctrl *parent): BackGround(h, w, fillSpace, parent) {
srand((unsigned)time(NULL));
for(int i=1; i<this->height-1; i++) {
for(int j=1; j<this->width-1; j++) {
int num = rand() % 100;
this->data[i][j] = num>80?1:0;
if(this->data[i][j] == 0) {
int num = rand() % 100;
this->data[i][j] = num>80?2:0;
}
}
}
this->data[1][1] = 0;
}
void EatbeanBg::toShow(int focus) {
for(int i=0; i<this->height; i++) {
for(int j=0; j<this->width; j++) {
this->setCursor(this->pos.X+j, this->pos.Y+i);
if(this->data[i][j] == 1) {
printf("*");
} else if(this->data[i][j] == 2) {
printf(".");
} else if(this->fillSpace) {
printf(" ");
}
}
}
}
bool EatbeanBg::isWin() {
for(int i=1; i<this->height-1; i++) {
for(int j=1; j<this->width-1; j++) {
if(this->data[i][j] == 2) {
return false;
}
}
}
return true;
}
void Ball::toShow(int focus) {
printf("O");
}
int Ball::Up() {
EatbeanBg *bg = (EatbeanBg*)this->parent;
int x=this->pos.X-bg->pos.X;
int y=this->pos.Y-bg->pos.Y-1;
if(bg->data[y][x] == 1) {
return 0;
} else if(bg->data[y][x] == 2) {
bg->data[y][x] = 0;
}
this->pos.Y--;
if(bg->isWin()) {
return -1;
}
return 1;
}
int Ball::Down() {
EatbeanBg *bg = (EatbeanBg*)this->parent;
int x=this->pos.X-bg->pos.X;
int y=this->pos.Y-bg->pos.Y+1;
if(bg->data[y][x] == 1) {
return 0;
} else if(bg->data[y][x] == 2) {
bg->data[y][x] = 0;
}
this->pos.Y++;
if(bg->isWin()) {
return -1;
}
return 1;
}
int Ball::Left() {
EatbeanBg *bg = (EatbeanBg*)this->parent;
int x=this->pos.X-bg->pos.X-1;
int y=this->pos.Y-bg->pos.Y;
if(bg->data[y][x] == 1) {
return 0;
} else if(bg->data[y][x] == 2) {
bg->data[y][x] = 0;
}
this->pos.X--;
if(bg->isWin()) {
return -1;
}
return 1;
}
int Ball::Right() {
EatbeanBg *bg = (EatbeanBg*)this->parent;
int x=this->pos.X-bg->pos.X+1;
int y=this->pos.Y-bg->pos.Y;
if(bg->data[y][x] == 1) {
return 0;
} else if(bg->data[y][x] == 2) {
bg->data[y][x] = 0;
}
this->pos.X++;
if(bg->isWin()) {
return -1;
}
return 1;
}