c语言 字符版 简易2048

花了两个多小时,用最蠢的方法写的……最简陋版……

还不确定这么写逻辑对不对……

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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <conio.h>
using namespace std;
 
int map[5][5];
 
int score;
 
int move(int& a, int& b, int& c, int& d) //move nonzero digit to front
{
    int vis_num = 0;    //the number of visited digit, most is 4
    int zero_num = 0;   //the number of zero digit
    while (1) {
        if (a != 0) {
            if (++vis_num >= 4) return zero_num;
            break;
        }
        ++zero_num;
        a = b;
        b = c;
        c = d;
        d = 0;
        if (++vis_num >= 4) return zero_num;
    }
    while (1) {
        if (b != 0) {
            if (++vis_num >= 4) return zero_num;
            break;
        }
        ++zero_num;
        b = c;
        c = d;
        d = 0;
        if (++vis_num >= 4) return zero_num;
    }
    while (1) {
        if (c != 0) {
            ++vis_num;
            if (vis_num >= 4) return zero_num;
            break;
        }
        ++zero_num;
        c = d;
        d = 0;
        if (++vis_num >= 4) return zero_num;
    }
    if (d == 0)
        ++zero_num;
    return zero_num;
}
 
void change(int& a, int& b, int& c, int& d)
{
    int zero_num = move(a, b, c, d);
    if (zero_num == 4 || zero_num == 3) return ;
    if (zero_num == 2) {
        if (a == b) {
            score += a * 10;
            a <<= 1;
            b = 0;
        }
        return ;
    }
    if (zero_num == 1) {
        if (a == b) {
            score += a * 10;
            a <<= 1;
            b = c;
            c = 0;
        } else if (c == b) {
            score += c * 10;
            b <<= 1;
            c = 0;
        }
        return ;
    }
    if (a == b) {
        if (c == d) {
            score += c * 10;
            score += a * 10;
            a <<= 1;
            b = c << 1;
            c = d = 0;
        } else {
            score += a * 10;
            a <<= 1;
            b = c;
            c = d;
            d = 0;
        }
    } else if (b == c){
        score += b * 10;
        b <<= 1;
        c = d;
        d = 0;
    } else if (c == d) {
        score += c * 10;
        c <<= 1;
        d = 0;
    }
}
 
int rand_2_or_4()
{
    if (rand() & 1) return 2;
    return 4;
}
 
void rand_pos()
{
    int x, y;
    do {
        x = rand() % 4;
        y = rand() % 4;
    } while (map[x][y]);
    map[x][y] = rand_2_or_4();
}
 
void print()
{
    int i, j;
    for (i = 0; i < 4; ++i) {
        for (j = 0; j < 4; ++j) {
            if (map[i][j] == 0) printf("  □ ");
            else printf("%4d ", map[i][j]);
        }
        printf("\n");
    }
    printf("score:%d\n", score);
}
 
bool is_full()
{
    int i, j;
    for (i = 0; i < 4; ++i)
        for (j = 0; j < 4; ++j)
            if (map[i][j] == 0) return false;
    return true;
}
 
bool is_over_row(int a, int b, int c, int d)
{
    if (a != b && b != c && c != d) return true;
    return false;
}
 
bool is_over()
{
    if (!is_full()) return false;
    int i;
    for (i = 0; i < 4; ++i) {
        if (!is_over_row(map[i][0], map[i][1], map[i][2], map[i][3]))
            return false;
    }
    for (i = 0; i < 4; ++i) {
        if (!is_over_row(map[0][i], map[1][i], map[2][i], map[3][i]))
            return false;
    }
    return true;
}
 
int main()
{
//    int a,b,c,d;
//    while(scanf("%d%d%d%d", &a, &b, &c, &d) != EOF){
//        change(a,b,c,d);
//        printf("%d %d %d %d\n", a, b, c, d);
//    }
    srand(unsigned(time(NULL)));
    rand_pos();
    rand_pos();
    print();
    while (!is_over()) {
        char dir = getch();
        int i;
        if (dir == 'w') {
            for (i = 0; i < 4; ++i) {
                change(map[0][i], map[1][i], map[2][i], map[3][i]);
            }
        } else if (dir == 's') {
            for (i = 0; i < 4; ++i)
                change(map[3][i], map[2][i], map[1][i], map[0][i]);
        } else if (dir == 'a') {
            for (i = 0; i < 4; ++i)
                change(map[i][0], map[i][1], map[i][2], map[i][3]);
        } else if (dir == 'd') {
            for (i = 0; i < 4; ++i)
                change(map[i][3], map[i][2], map[i][1], map[i][0]);
        } else continue;
        if (!is_full()) rand_pos();
        system("cls");
        print();
    }
    printf("Lose!");
    return 0;
}

  

后来发现我的写法有问题,因为如果不移动的话,就不会产生新的数字,而我的逻辑是只要有空位就有新的数字。之前没有认真观察过,懒得改了(其实是没想到什么好的方法……

posted @   我不吃饼干呀  阅读(309)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示