-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2048.cpp
More file actions
242 lines (235 loc) · 4.61 KB
/
Copy path2048.cpp
File metadata and controls
242 lines (235 loc) · 4.61 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <string>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <stack>
#include <queue>
#include <ctime>
#include <iostream>
#include <curses.h>
using namespace std;
#define N 4 // 格子数
#define SI 5 //每个格子的字符长度
// 游戏状态
#define S_FAIL 0
#define S_WIN 1
#define S_run 2
#define S_QUIT 3
class game2048 {
public:
const int offset = 12;
game2048(): status(S_run) {
restart();
}
int getstatus() {
return status;
}
void processINPUT() {
char c = getch();
if (c >= 'a' && c <= 'z')
c -= 32;
if (status == S_run) {
if (c == 'W') up();
else if (c == 'S') down();
else if (c == 'A') left();
else if (c == 'D') right();
nowstatus();
}
if (c == 'Q') status = S_QUIT;
else if (c == 'R') restart();
}
void man() {
clear(); //清空
for (int i = 0; i <= N; ++i) {
for (int j = 0; j <= N; ++j) {
draw(2*i, offset+j*SI, '+');
if (i != N) {
draw(2*i+1, offset+j*SI, '|');
if (j != N)
drawnum(2*i+1, offset+(j+1)*SI-1, data[i][j]);
}
if (j != N) {
for (int k = 1; k <= N; ++k)
draw(2*i, offset+j*SI+k, '-');
}
}
}
mvprintw(2*N+2, -1/2, "W(UP),S(DOWN),A(LEFT),D(RIGHT),R(RESTART),Q(QUIT)");
mvprintw(2*N+3, SI, "HCLG");
if (status == S_FAIL)
mvprintw(3*N, SI, " YOU LOSE,PRESS R TO CONTINUE ");
if (status == S_WIN)
mvprintw(3*N, SI, " YOU WIN,PRESS R TO CONTINUE ");
}
void setTestData() {
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
data[i][j] = 16>>i>>j;//调试用的测试数据
}
private:
void nowstatus() {
bool f = true;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (data[i][j] == 0 || (j != N-1 && data[i][j] == data[i][j+1]) || (i != N-1 && data[i][j] == data[i+1][j])) f = false;
if (data[i][j] == 2048) {
status = S_WIN;
return;
}
}
}
if (f) status = S_FAIL;
return;
}
void left() {
queue<int>q;
for (int i = 0; i < N; ++i) {
for (auto &j : data[i]) {
if (j) {
q.push(j);
j = 0;
}
}
int j = 0;
while (!q.empty()) {
data[i][j] = q.front();
q.pop();
if (!q.empty() && q.front() == data[i][j]) {
q.pop();
data[i][j]*=2;
}
++j;
}
}
randNew();randNew();
}
void right() {
stack<int>q;
for (int i = 0; i < N; ++i) {
for (auto &j : data[i]) {
if (j) {
q.push(j);
j = 0;
}
}
int j = N-1;
while (!q.empty()) {
data[i][j] = q.top();
q.pop();
if (!q.empty() && q.top() == data[i][j]) {
q.pop();
data[i][j]*=2;
}
--j;
}
}
randNew();randNew();
}
void up() {
queue<int>q;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (data[j][i]) {
q.push(data[j][i]);
data[j][i] = 0;
}
}
int j = 0;
while (!q.empty()) {
data[j][i] = q.front();
q.pop();
if (!q.empty() && q.front() == data[j][i]) {
q.pop();
data[j][i]*=2;
}
++j;
}
}
randNew();randNew();
}
void down() {
stack<int>q;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (data[j][i]) {
q.push(data[j][i]);
data[j][i] = 0;
}
}
int j = N-1;
while (!q.empty()) {
data[j][i] = q.top();
q.pop();
if (!q.empty() && q.top() == data[j][i]) {
q.pop();
data[j][i]*=2;
}
--j;
}
}
randNew();randNew();
}
void draw(int row, int col, char c) {
move(row, col);
addch(c);
}
void drawnum(int row, int col, int num) {
while (num > 0) {
draw(row, col, num%10+'0');
num/=10;
--col;
}
}
void restart() {
for (auto &i : data)
for (auto &j : i)
j = 0;
randNew();
randNew();
status = S_run;
}
bool randNew() {
vector<int> emp;
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
if (data[i][j] == 0)
emp.push_back(i*N+j);
if (emp.size() == 0)
return false;
int va = emp[rand()%emp.size()];
//10%的概率产生4
data[va/N][va%N] = rand()%10 == 1 ? 4 : 2;
return true;
}
int data[N][N];
int status;
};
void initialize() {
//ncurses初始化
initscr();
//按键不需要输入回车直接交互
cbreak();
//按键不显示
noecho();
//隐藏光标
curs_set(0);
//随机数
srand(time(NULL));
}
void shutdown() {
endwin();//ncurses清理
}
int main() {
initialize();
game2048 game;
do {
game.man();
//move(5,5); //移动光标位置
// addch(ch); //当前位置画字符ch
// mvprintw(2, 2, "HCL");//显示在2,2的位置
game.processINPUT();
}while (game.getstatus() != S_QUIT);
shutdown();
return 0;
}