C语言实现2048游戏代码

时间:2022-03-28 12:06:00

本文实例为大家分享了C语言实现2048游戏具体代码,供大家参考,具体内容如下

效果图:

C语言实现2048游戏代码

使用文本界面的屏幕绘图库 ncurses.

设计思路:

  • 在满足条件情况下消除方块
  • 允许在游戏主界面(16 宫格)中任意一格输出数据

实现代码:

?
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
 
void draw(); // 用于绘制游戏界面
void play(); // 游戏运行的逻辑主体
void init(); // 初始化函数,用于完成一些必要的初始化操作
void draw_one(int y, int x); // 绘制单个数字
void cnt_value(int *new_y, int *new_x); //统计(y, x)对应的格子周围一圈的空格的个数
int game_over(); // 结束游戏
int cnt_one(int y, int x); //统计(y, x)对应的格子周围一圈的空格的个数
 
// 游戏主界面是一个 4*4 的 16 宫格,使用二维数组进行表示,用 0 表示空格
int a[4][4] = { 0 };
// 16 宫格中空格的个数
 
int empty;
int old_y, old_x;
 
int main()
{
  init();
  play();
  endwin();
 
  return 0;
}
 
void init()
{
  int x, y;
 
  initscr(); //开启curses模式
  cbreak(); //开启cbreak模式,除 DELETE 或 CTRL 等仍被视为特殊控制字元外一切输入的字元将立刻被一一读取
  noecho(); //echo() and noecho(): 此函式用来控制从键盘输入字元时是否将字元显示在终端机上
  curs_set(0); //设置光标模式
 
  empty = 15;
  srand(time(0));
  x = rand() % 4;
  y = rand() % 4;
  a[y][x] = 2;
  draw();
}
 
void draw()
{
  int n, m, x, y;
  char c[4] = {'0', '0', '0', '0'};
  clear(); //清除终端屏幕
 
  for(n = 0; n < 9; n += 2)
  {
    for(m = 0; m < 21; m++)
    {
      move(n, m);//将游标移动至 x,y 的位置
      addch('-');//在当前位置画字符'-'
      refresh();//将做清除萤幕的工作
    }
  }
 
  for(m = 0; m < 22; m += 5)
  {
    for(n = 1; n < 8; n++)
    {
      move(n, m);
      addch('|');
      refresh();
    }
  }
 
  for(y = 0; y < 4; y++)
  {
    for(x = 0; x < 4; x++)
    {
      draw_one(y, x);
    }
  }
}
 
void draw_one(int y, int x)
{
  int i, m, k, j;
  char c[5] = {0x00};
  i = a[y][x];
  m = 0;
 
  while(i > 0)
  {
    j = i % 10;
    c[m++] = j + '0';
    i = i / 10;
  }
 
  m = 0;
  k = (x + 1) * 5 - 1;
 
  while(c[m] != 0x00)
  {
    move(2 * y + 1, k);
    addch(c[m++]);
    k--;
  }
}
 
void play()
{
  int x, y, i, new_x, new_y, temp;
  int old_empty, move;
  char ch;
 
  while(1)
  {
    move = 0;
    old_empty = empty;
    ch = getch();
 
    switch(ch)
    {
      case 97: //左移 a
      case 104: // h
      case 68: // 左移方向键
        for(y = 0; y < 4; y++)
          for(x = 0; x < 4; )
          {
            if(a[y][x] == 0)
            {
              x++;
              continue;
            }
            else
            {
              for(i = x + 1; i < 4; i++)
              {
                if(a[y][i] == 0)
                {
                  continue;
                }
                else
                {
                  if(a[y][x] == a[y][i])
                  {
                    a[y][x] += a[y][i];
                    a[y][i] = 0;
                    empty++;
                    break;
                  }
                  else
                  {
                    break;
                  }
                }
              }
              x = i;
            }
          }
 
        for(y = 0; y < 4; y++)
          for(x = 0; x < 4; x++)
          {
            if(a[y][x] == 0)
            {
              continue;
            }
            else
            {
              for(i = x; (i > 0) && (a[y][i - 1] == 0); i--)
              {
                a[y][i - 1] = a[y][i];
                a[y][i] = 0;
                move = 1;
              }
            }
          }
        break;
      case 100: //右移 d 
      case 108: // l
      case 67:  //右移方向键
        for(y = 0; y < 4; y++)
          for(x = 3; x >= 0; )
          {
            if(a[y][x] == 0)
            {
              x--;
              continue;
            }
            else
            {
              for(i = x - 1; i >= 0; i--)
              {
                if(a[y][i] == 0)
                {
                  continue;
                }
                else if(a[y][x] == a[y][i])
                {
                  a[y][x] += a[y][i];
                  a[y][i] = 0;
                  empty++;
                  break;
                }
                else
                {
                  break;
                }
              }
              x = i;
            }
          }
 
        for(y = 0; y < 4; y++)
          for(x = 3; x >= 0; x--)
          {
            if(a[y][x] == 0)
            {
              continue;
            }
            else
            {
              for(i = x; (i < 3) && (a[y][i + 1] == 0); i++)
              {
                a[y][i + 1] = a[y][i];
                a[y][i] = 0;
                move = 1;
              }
            }
          }
        break;
      case 119: //上移 w
      case 107: //k
      case 65:  //上移方向键
        for(x = 0; x < 4; x++)
          for(y = 0; y < 4; )
          {
            if(a[y][x] == 0)
            {
              y++;
              continue;
            }
            else
            {
              for(i = y + 1; i < 4; i++)
              {
                if(a[i][x] == 0)
                {
                  continue;
                }
                else if(a[y][x] == a[i][x])
                {
                  a[y][x] += a[i][x];
                  a[i][x] = 0;
                  empty++;
                  break;
                }
                else
                {
                  break;
                }
              }
              y = i;
            }
          }
 
        for(x = 0; x < 4; x++)
          for(y = 0; y < 4; y++)
          {
            if(a[y][x] == 0)
            {
              continue;
            }
            else
            {
              for(i = y; (i > 0) && (a[i - 1][x] == 0); i--)
              {
                a[i - 1][x] = a[i][x];
                a[i][x] = 0;
                move = 1;
              }
            }
          }
        break;
      case 115: //下移 s 
      case 106: //j
      case 66:  //下移方向键
        for(x = 0; x < 4; x++)
          for(y = 3; y >= 0; )
          {
            if(a[y][x] == 0)
            {
              y--;
              continue;
            }
            else
            {
              for(i = y - 1; i >= 0; i--)
              {
                if(a[i][x] == 0)
                {
                  continue;
                }
                else if(a[y][x] == a[i][x])
                {
                  a[y][x] += a[i][x];
                  a[i][x] = 0;
                  empty++;
                  break;
                }
                else
                {
                  break;
                }
              }
              y = i;
            }
          }
 
        for(x = 0; x < 4; x++)
          for(y = 3; y >= 0; y--)
          {
            if(a[y][x] == 0)
            {
              continue;
            }
            else
            {
              for(i = y; (i < 3) && (a[i + 1][x] == 0); i++)
              {
                a[i + 1][x] = a[i][x];
                a[i][x] = 0;
                move = 1;
              }
            }
          }
        break;
      case 'Q':
      case 'q':
        game_over();
        break;
      default:
        continue;
        break;
 
    }
 
    if(empty <= 0)
      game_over();
 
    if((empty != old_empty) || (move == 1))
    {
      do
      {
        new_x = rand() % 4;
        new_y = rand() % 4;
      }while(a[new_y][new_x] != 0);
 
      cnt_value(&new_y, &new_x);
 
      do
      {
        temp = rand() % 4;
      }while(temp == 0 || temp == 2);
 
      a[new_y][new_x] = temp + 1;
      empty--;
    }
    draw();
  }
}
 
int cnt_one(int y, int x)
{
  int value = 1;
 
  if(y - 1 > 0)
    a[y - 1][x] ? 0 : value++;
  if(y + 1 < 4)
    a[y + 1][x] ? 0 : value++;
  if(x - 1 >= 0)
    a[y][x - 1] ? 0 : value++;
  if(x + 1 < 4)
    a[y][x + 1] ? 0 : value++;
  if(y - 1 >= 0 && x - 1 >= 0)
    a[y - 1][x - 1] ? 0 : value++;
  if(y - 1 >= 0 && x + 1 < 4)
    a[y - 1][x + 1] ? 0 : value++;
  if(y + 1 < 4 && x - 1 >= 0)
    a[y + 1][x - 1] ? 0 : value++;
  if(y + 1 < 4 && x + 1 < 4)
    a[y + 1][x + 1] ? 0 : value++;
 
  return value;
}
 
void cnt_value(int *new_y, int *new_x)
{
  int max_x, max_y, x, y, value;
  int max = 0;
 
  max = cnt_one(*new_y, *new_x);
  for(y = 0; y < 4; y++)
    for(x = 0; x < 4; x++)
    {
      if(!a[y][x])
      {
        value = cnt_one(y, x);
        if(value > max && old_y != y && old_x != x)
        {
          *new_y = y;
          *new_x = x;
          old_x = x;
          old_y = y;
          break;
        }
      }
    }
}
 
int game_over()
{
  sleep(1); //暂停1秒
  endwin(); //关闭curses并重置tty(结束curses编程时,最后调用的一个函数)
  exit(0);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_15345805/article/details/70149005