-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame2048.cpp
229 lines (198 loc) · 5.81 KB
/
game2048.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
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
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include"curses.h"
using namespace std;
#define N 4 //NxN个格子
#define WIDTH 5 //每个格子宽度
#define TARGET 2048 //胜利条件
#define S_FAIL 0
#define S_WIN 1
#define S_NORMAL 2
#define S_QUIT 3
class game2048{
public:
game2048():status(S_NORMAL),score(0),maxscore(0){
setTestData(); //测试函数
}
int getstatus(){ return status; }
void processInput(){
char ch = getch();
if(ch>='a' && ch<='z') ch -= 32;
if(status==S_NORMAL){
bool updated = false;
if(ch=='A') updated = moveleft();
else if(ch=='W'){
rotate();
updated = moveleft();
rotate();
rotate();
rotate();
}else if(ch=='D'){
rotate();
rotate();
updated = moveleft();
rotate();
rotate();
}else if(ch=='S'){
rotate();
rotate();
rotate();
updated = moveleft();
rotate();
}
if(updated){
randnew();
if(isOver()) status = S_FAIL;
}
}
if(ch=='Q') status = S_QUIT;
else if(ch=='R') restart();
}
void draw(){ //绘制函数
clear();
const int offset = 12;
for(int i=0;i<=N;i++){
for(int j=0;j<=N;j++){
drawItem(i*2, 1+j*WIDTH+offset, '+');
if(i!=N) drawItem(i*2+1, 1+j*WIDTH+offset, '|');
for(int k=1;j!=N && k<WIDTH;k++) drawItem(i*2, 1+j*WIDTH+k+offset, '-');
if(i!=N && j!=N) drawNum(i*2+1, (j+1)*WIDTH+offset, data[i][j]);
}
}
// 显示提示
mvprintw(2*N+2, (5*(N-4)-1)/2,"W(UP),S(DOWN),A(LEFT),D(RIGHT),R(RESTART),Q(QUIT)");
// 显示分数
mvprintw(0, 0, "score:");
drawNum(0, 10, score);
mvprintw(1, 0, "best:");
drawNum(1, 10, maxscore);
if(status == S_WIN) mvprintw(N, 5*N/2-1, " YOU WIN,PRESS R TO CONTINUE ");
else if(status == S_FAIL) mvprintw(N, 5*N/2-1, " YOU LOSE,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:
bool isOver(){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if((j+1<N) && (data[i][j]*data[i][j+1]==0 || data[i][j]==data[i][j+1])) return false;
if((i+1<N) && (data[i][j]*data[i+1][j]==0 || data[i][j]==data[i+1][j])) return false;
}
}
return true;
}
bool moveleft(){
int tmp[N][N];
for(int i=0;i<N;i++){
int curWritePos = 0;
int lastVal = 0;
for(int j=0;j<N;j++){
tmp[i][j] = data[i][j]; //记录当前游戏状况
if(!data[i][j]) continue;
if(!lastVal) lastVal = data[i][j];
else{
if(lastVal == data[i][j]){
data[i][curWritePos] = lastVal*2;
score += lastVal*2; //记录当前分数
if(score>maxscore) maxscore = score; //记录最高分
lastVal = 0;
if(data[i][curWritePos]==TARGET) status = S_WIN;
}else{
data[i][curWritePos] = lastVal;
lastVal = data[i][j];
}
curWritePos++;
}
data[i][j] = 0;
}
if(lastVal) data[i][curWritePos] = lastVal;
}
// 检查游戏状况是否改变
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(data[i][j]!=tmp[i][j]) return true;
}
}
return false;
}
void rotate(){ //矩阵旋转函数
int tmp[N][N] = {0};
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
tmp[i][j] = data[j][N-1-i];
}
}
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
data[i][j] = tmp[i][j];
}
}
}
void restart(){ //重开
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
data[i][j] = 0;
}
}
randnew();
randnew();
score = 0;
status = S_NORMAL;
}
bool randnew(){
vector<int> emptyPos;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(!data[i][j]) emptyPos.push_back(i*N+j);
}
}
if(!emptyPos.size()) return false;
int val = emptyPos[rand()%emptyPos.size()];
data[val/N][val%N] = rand()%10 == 1 ? 4 : 2;
return true;
}
void drawItem(int row, int col, char c){
move(row, col);
addch(c);
}
void drawNum(int row, int col, int num){
while(num>0){
drawItem(row, col, num%10 + '0');
num /= 10;
col--;
}
}
private:
int data[N][N];
int status;
int score;
int maxscore;
};
void initialize(){
initscr(); // 按键不需要enter进行交互
cbreak(); // 不显示按键
noecho(); // 不显示光标
curs_set(0); // 随机数
srand(time(NULL));
}
void shutdown(){
endwin();
}
int main(){
initialize(); // 初始化
game2048 game;
do{
game.draw();
game.processInput();
}while(game.getstatus() != S_QUIT);
shutdown();
return 0;
}