-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2048.cpp
More file actions
147 lines (135 loc) · 3.54 KB
/
2048.cpp
File metadata and controls
147 lines (135 loc) · 3.54 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
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <bits/stdc++.h>
#define GAME_SIZE 4
static void left(int *data)
{
int i, j, f;
int row = GAME_SIZE;
while (row--) {
for (j=0,f=0,i=0; i<GAME_SIZE; i++) {
if (data[i]) {
if (!f && j>0 && data[j-1] == data[i]) {
data[j-1]+= data[i]; f = 1;
} else {
data[j++] = data[i]; f = 0;
}
}
}
while (j < GAME_SIZE) data[j++] = 0;
data += GAME_SIZE;
}
}
static void right(int *data)
{
int i, j, f;
int row = GAME_SIZE;
while (row--) {
for (j=GAME_SIZE-1,f=0,i=GAME_SIZE-1; i>=0; i--) {
if (data[i]) {
if (!f && j<GAME_SIZE-1 && data[j+1] == data[i]) {
data[j+1]+= data[i]; f = 1;
} else {
data[j--] = data[i]; f = 0;
}
}
}
while (j >= 0) data[j--] = 0;
data += GAME_SIZE;
}
}
static void up(int *data)
{
int i, j, f;
int col = GAME_SIZE;
while (col--) {
for (j=0,f=0,i=0; i<GAME_SIZE; i++) {
if (data[i*GAME_SIZE]) {
if (!f && j>0 && data[(j-1)*GAME_SIZE] == data[i*GAME_SIZE]) {
data[(j-1)*GAME_SIZE]+= data[i*GAME_SIZE]; f = 1;
} else {
data[(j++)*GAME_SIZE] = data[i*GAME_SIZE]; f = 0;
}
}
}
while (j < GAME_SIZE) data[(j++)*GAME_SIZE] = 0;
data++;
}
}
static void down(int *data)
{
int i, j, f;
int col = GAME_SIZE;
while (col--) {
for (j=GAME_SIZE-1,f=0,i=GAME_SIZE-1; i>=0; i--) {
if (data[i*GAME_SIZE]) {
if (!f && j<GAME_SIZE-1 && data[(j+1)*GAME_SIZE] == data[i*GAME_SIZE]) {
data[(j+1)*GAME_SIZE]+= data[i*GAME_SIZE]; f = 1;
} else {
data[(j--)*GAME_SIZE] = data[i*GAME_SIZE]; f = 0;
}
}
}
while (j >= 0) data[(j--)*GAME_SIZE] = 0;
data++;
}
}
static int next(int *data)
{
int empidx[GAME_SIZE*GAME_SIZE];
int empnum = 0;
int max = 0;
int i, j;
for (j=0,i=0; i<GAME_SIZE*GAME_SIZE; i++) {
if (data[i] == 0) {
empidx[j++] = i;
empnum++;
}
max = max > data[i] ? max : data[i];
}
if (empnum) {
data[empidx[rand()%empnum]] = 1;
return max == 2048 ? 1 : 0;
}
else return -1;
}
static void output(int *data)
{
int i, sum = 0;
printf("+--------------------+\n\n");
for (i=1; i<=GAME_SIZE*GAME_SIZE; i++) {
if (0 || data[i-1]) {
printf("%4d %s", data[i-1], i%GAME_SIZE ? "" : "\n\n");
} else {
printf("%s %s" , " ." , i%GAME_SIZE ? "" : "\n\n");
}
sum += data[i-1];
}
printf("+--------------------+\n\n");
printf("·ÖÊý£º%d\n\n", sum);
}
int main(void)
{
int data[GAME_SIZE * GAME_SIZE] = {0};
int ret = 0;
printf("ÓÃw,a,s,dÀ´²Ù×÷\n");
next (data);
next (data);
output(data);
while (1) {
int c = getch();
switch (c) {
case 'a': left (data); break;
case 's': down (data); break;
case 'd': right(data); break;
case 'w': up (data); break;
case 'q': return 0;
default : continue;
}
ret = next(data);
output(data);
if (ret == -1) printf("game over !\n");
if (ret == 1) printf("you win !\n");
}
}