2048版本1
若本文章为原创文章,转载本文时需加上本文原地址
C语言实现2048小游戏(图形界面)——版本1
广东好冷,动都不想动,只能玩玩小游戏这个样子
版本一已经能玩了,只是还有一些小BUG,容我继续试错再发叭
试玩视频==>地址(B站)
实现思路
按部就班,不要急
-
图形界面,需要easyX库
下载地址在这里
先构思好自己要做的界面
我要做的长成这样
可以看出,尺寸基准——原点在右上角,那是因为easyx里面的坐标轴的y轴是朝下的
设定图形界面总长650,总高800,但是图形界面是黑色的,所以我设计了背景色之类的颜色作为一个结构体
struct COLOR { const int background = RGB(242, 248, 241); //茶白色>>背景 const int partition = RGB(22, 23, 36);//漆黑>>隔条 const int box_co[10] = { RGB(254,241,67),RGB(217,182,18),RGB(202,105,36),RGB(178,93,38),RGB(174,112,1),RGB(155,68,0),RGB(124,75,0),RGB(111,81,31),RGB(138,108,58),RGB(120,146,98)}; const int final_co = RGB(195,39,43);//赤色>>最后方块的颜色 const int botton1_co = RGB(73, 65, 102);//黛色>>“重新开始”按钮 const int score_co = RGB(201, 221, 35);//柳黄色>>成绩分数 const int botton2_co = RGB(0, 225, 159);//青色>>最高分数 }color;
颜色从一篇博文上找的==>地址
初始化图形界面我查了好久,网上找到的又没有注释,建议大家去官网上找说明文档,那个才是最清楚的
代码如下:void menu() { initgraph(650,800); setbkcolor(color.background); cleardevice(); setfillcolor(color.partition); solidrectangle(75, 280, 575, 780); int i, j; setfillcolor(color.background); for (i = 0; i < 4; ++i) for (j = 0; j < 4; ++j) solidrectangle(95 + 120 * i, 300 + 120 * j, 195 + 120 * i, 400 + 120 * j); settextcolor(color.score_co); settextstyle(80,0,"score_type",0,0,500,0,0,0); outtextxy(20, 25,"score:"); setfillcolor(color.botton1_co); solidroundrect(75, 135, 235, 215, 20, 20); setfillcolor(color.botton2_co); solidroundrect(415, 135, 575, 215, 20, 20); }
最后就长这样
-
逻辑部分就相对难了呀,我做的是检测按下方向键后方块移动,然后合并,这个不知道怎么解说,所以还是直接上代码叭😄
void play() { time_rand(normal_num); time_rand(normal_num); update(); int is_press = 2; char press_str1; char press_str2; is_batter = 0; while (1) { press_str1 = 0; press_str2 = 0; if (press_str1 = _getch()) { press_str2 = _getch(); switch (press_str2) { case 72: move.p_up(); is_press = 1;; break;//↑ case 80: move.p_down(); is_press = 1;; break;//↓ case 75: move.p_left(); is_press = 1;; break;//← case 77: move.p_right(); is_press = 1;; break;//→ default: MessageBox(NULL, TEXT("请用方向键操作!"), TEXT("提示"), MB_OK); is_press = 0; break; } } if (is_press == 1) { if (is_batter == 0) { time_rand(normal_num); } if (is_batter != 0 && is_batter % 3 == 0) { is_batter = 0; time_rand(batter_num); } } update(); if (judge() == 0 && is_win == 0) break; } }
移动和合并的代码(示范):
void up() { int i, j, k; for (j = 0; j < 4; ++j) //移动 { for (i = 0; i < 4; ++i) { if (panel[i][j] == 0) for (k = i+1; k < 4; ++k) { if (panel[k][j] != 0) { panel[i][j] = panel[k][j]; panel[k][j] = 0; break; } } } for (i = 0; i + 1 < 4; ++i) //合并 { if (panel[i][j] == panel[i + 1][j]&&panel[i][j]!=0) { score += panel[i][j]; panel[i][j] *= 2; panel[i + 1][j] = 0; is_batter += 1; } else is_batter = 0; } } }
-
随机数生成
我之前都是采用的这种逻辑:随机生成范围内的数字>检查数字所在位置是否可放置>放置
我想了挺久,才完善回来,现在的逻辑是:选出范围内可放置的数字,放在一个数组,再在里面随机抽取,再放置
根据实际体验,将程序计算减少了很大的计算量
旧逻辑:while (is_set != 1) { time_t t; srand((unsigned)time(&t)); randnum = rand() % 17 + 1; //1-16随机数 switch (randnum) { case 1:if (panel[0][0] == 0); else continue; break; case 2:if (panel[0][1] == 0); else continue; break; case 3:if (panel[0][2] == 0); else continue; break; case 4:if (panel[0][3] == 0); else continue; break; case 5:if (panel[1][0] == 0); else continue; break; case 6:if (panel[1][1] == 0); else continue; break; case 7:if (panel[1][2] == 0); else continue; break; case 8:if (panel[1][3] == 0); else continue; break; case 9:if (panel[2][0] == 0); else continue; break; case 10:if (panel[2][1] == 0); else continue; break; case 11:if (panel[2][2] == 0); else continue; break; case 12:if (panel[2][3] == 0); else continue; break; case 13:if (panel[3][0] == 0); else continue; break; case 14:if (panel[3][1] == 0); else continue; break; case 15:if (panel[3][2] == 0); else continue; break; case 16:if (panel[3][3] == 0); else continue; break; } switch (randnum) { case 1:panel[0][0] = put_num; break; case 2:panel[0][1] = put_num; break; case 3:panel[0][2] = put_num; break; case 4:panel[0][3] = put_num; break; case 5:panel[1][0] = put_num; break; case 6:panel[1][1] = put_num; break; case 7:panel[1][2] = put_num; break; case 8:panel[1][3] = put_num; break; case 9:panel[2][0] = put_num; break; case 10:panel[2][1] = put_num; break; case 11:panel[2][2] = put_num; break; case 12:panel[2][3] = put_num; break; case 13:panel[3][0] = put_num; break; case 14:panel[3][1] = put_num; break; case 15:panel[3][2] = put_num; break; case 16:panel[3][3] = put_num; break; } } box_num += 1; update();
看程序就明显,panel数组里面0值多的时候还没什么,少的时候是真的很烦的,假设我现在只有panel[0][0]和panel[1][1]是零值,那他算到这两个位置概率就是1/8,已经是很小的概率了,更何况这只是个4*4的小数组,如果大一点,那他概率就更加小了。这也是我要想新逻辑的原因。
新逻辑:
int i, j, k, n, temp; k = 0, n = 0; for (i = 0; i < 4; ++i) { for (j = 0; j < 4; ++j) { if (panel[i][j] == 0) { randgroup[k] = 4 * i + j; ++k; } } } for (n = 15; n >= 0; --n) { if (randgroup[n] != 0) break; } time_t t; srand((unsigned)time(&t)); randnum = rand() % (n + 1); //0-n随机数 temp = randgroup[randnum]; switch (temp) { case 0:panel[0][0] = put_num; break; case 1:panel[0][1] = put_num; break; case 2:panel[0][2] = put_num; break; case 3:panel[0][3] = put_num; break; case 4:panel[1][0] = put_num; break; case 5:panel[1][1] = put_num; break; case 6:panel[1][2] = put_num; break; case 7:panel[1][3] = put_num; break; case 8:panel[2][0] = put_num; break; case 9:panel[2][1] = put_num; break; case 10:panel[2][2] = put_num; break; case 11:panel[2][3] = put_num; break; case 12:panel[3][0] = put_num; break; case 13:panel[3][1] = put_num; break; case 14:panel[3][2] = put_num; break; case 15:panel[3][3] = put_num; break; } box_num += 1; update();
按上面的panel[0][0]和[1][1]来说,他现在就选出这两个数进一个一维数组,然后再读取数组内有多少个数,再随机抽取,再对号入座,概率就变成1,是不是很不错呀
我自己是感觉真不错!
-
更新面板
这个就很简单了,直接覆盖(防止移动方块后方块颜色不会移动)总之就是很多color、很多矩形,然后就是更新scorevoid update() { int i = 0, j = 0; int text_xp, text_yp; int box_xp, box_yp; setfillcolor(color.partition); solidrectangle(75, 280, 575, 780); setfillcolor(color.background); for (i = 0; i < 4; ++i) for (j = 0; j < 4; ++j) solidrectangle(95 + 120 * i, 300 + 120 * j, 195 + 120 * i, 400 + 120 * j); char Scores[16] = { 0 }; _itoa_s (score, Scores, 10); settextcolor(color.score_co); settextstyle(80, 0, "score_type", 0, 0, 500, 0, 0, 0); outtextxy(360, 25, Scores); for (i = 0; i < 4; ++i) { box_yp = 300 + 120 * i; text_yp = 325 + (120 * i); for (j = 0; j < 4; ++j) { box_xp = 95 + 120 * j; text_xp = 130 + (120 * j); switch (panel[i][j]) { case 2:setfillcolor(color.box_co[0]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100); settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp, text_yp, "2"); break; case 4:setfillcolor(color.box_co[1]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100); settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp, text_yp, "4"); break; case 8:setfillcolor(color.box_co[2]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100); settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp, text_yp, "8"); break; case 16:setfillcolor(color.box_co[3]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100); settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp-8, text_yp, "16"); break; case 32:setfillcolor(color.box_co[4]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100); settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp-8, text_yp, "32"); break; case 64:setfillcolor(color.box_co[5]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100); settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp-8, text_yp, "64"); break; case 128:setfillcolor(color.box_co[6]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100); settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp-20, text_yp, "128"); break; case 256:setfillcolor(color.box_co[7]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100); settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp-20, text_yp, "256"); break; case 512:setfillcolor(color.box_co[8]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100); settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp-20, text_yp, "512"); break; case 1024:setfillcolor(color.box_co[9]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100); settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp-40, text_yp, "1024"); break; case 2048:setfillcolor(color.final_co); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100); settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp-40, text_yp, "2048"); break; } } } }
-
判断胜负
我大概猜测BUG来源就是这里
没什么可解释的int judge() { int i, j; int can_go = 1; for (i = 0; i < 4; ++i) { for (j = 0; j < 3; ++j) { if (panel[i][j] != 0 && panel[i][j] == panel[i][j + 1]) { can_go = 1; return 1; } } } for (j = 0; j < 4; ++j) { for (i = 0; i < 3; ++i) { if (panel[i][j] != 0 && panel[i][j] == panel[i + 1][j]) { can_go = 1; return 1; } } } for (j = 0; j < 4; ++j) { for (i = 0; i < 3; ++i) { if (panel[i][j] == 0) { can_go = 1; return 1; } } } for (i = 0; i < 4; ++i) { for (j = 0; j < 4; ++j) { if (panel[i][j] == 2048) { MessageBox(NULL, TEXT("恭喜获胜!"), TEXT("提示"), MB_OK); is_win = 1; return 1; } } } can_go = 0; return 0; }
-
后续进度
我的想法是再界面那两个有颜色的区域做两个按钮,分别是重新开始和结束
再检查一下有没有什么BUG什么的 -
源码
小游戏而已,网上大把,所以就放出来,大家可以看看有没有可以改进的点?
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
#include <time.h>
#include <windows.h>
/*
project name: 2048
author name: dragonet
date: 2020/12/14(版本1)
*/
/*声明函数*/
void menu();
void play();
void update();
int judge();
void time_rand(int put_num);
void up();
void down();
void left();
void right();
/*全局参数*/
int panel[4][4] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
const int normal_num = 2;
const int batter_num = 4;
int score = 0;
int is_win = 0;
int box_num = 0;
int is_batter = 0;
struct COLOR
{
const int background = RGB(242, 248, 241); //茶白色>>背景
const int partition = RGB(22, 23, 36);//漆黑>>隔条
const int box_co[10] = { RGB(254,241,67),RGB(217,182,18),RGB(202,105,36),RGB(178,93,38),RGB(174,112,1),RGB(155,68,0),RGB(124,75,0),RGB(111,81,31),RGB(138,108,58),RGB(120,146,98)};
const int final_co = RGB(195,39,43);//赤色>>最后方块的颜色
const int botton1_co = RGB(73, 65, 102);//黛色>>“重新开始”按钮
const int score_co = RGB(201, 221, 35);//柳黄色>>成绩分数
const int botton2_co = RGB(0, 225, 159);//青色>>“结束”分数
}color;
struct MOVE
{
void (*p_up)();
void (*p_down)();
void (*p_left)();
void (*p_right)();
}move;
int main()
{
move.p_up = up;
move.p_down = down;
move.p_left = left;
move.p_right = right;
menu();
play();
MessageBox(NULL, TEXT("输了!"), TEXT("提示"), MB_OK);
while (1);
closegraph;
return 0;
}
void menu()
{
initgraph(650,800);
setbkcolor(color.background);
cleardevice();
setfillcolor(color.partition);
solidrectangle(75, 280, 575, 780);
int i, j;
setfillcolor(color.background);
for (i = 0; i < 4; ++i)
for (j = 0; j < 4; ++j)
solidrectangle(95 + 120 * i, 300 + 120 * j, 195 + 120 * i, 400 + 120 * j);
settextcolor(color.score_co);
settextstyle(80,0,"score_type",0,0,500,0,0,0);
outtextxy(20, 25,"score:");
setfillcolor(color.botton1_co);
solidroundrect(75, 135, 235, 215, 20, 20);
setfillcolor(color.botton2_co);
solidroundrect(415, 135, 575, 215, 20, 20);
}
void play()
{
time_rand(normal_num);
time_rand(normal_num);
update();
int is_press = 2;
char press_str1;
char press_str2;
is_batter = 0;
while (1)
{
press_str1 = 0;
press_str2 = 0;
if (press_str1 = _getch())
{
press_str2 = _getch();
switch (press_str2)
{
case 72: move.p_up(); is_press = 1;; break;//↑
case 80: move.p_down(); is_press = 1;; break;//↓
case 75: move.p_left(); is_press = 1;; break;//←
case 77: move.p_right(); is_press = 1;; break;//→
default: MessageBox(NULL, TEXT("请用方向键操作!"), TEXT("提示"), MB_OK); is_press = 0; break;
}
}
if (is_press == 1)
{
if (is_batter == 0)
{
time_rand(normal_num);
}
if (is_batter != 0 && is_batter % 3 == 0)
{
is_batter = 0;
time_rand(batter_num);
}
}
update();
if (judge() == 0 && is_win == 0) break;
}
}
void update()
{
int i = 0, j = 0;
int text_xp, text_yp;
int box_xp, box_yp;
setfillcolor(color.partition);
solidrectangle(75, 280, 575, 780);
setfillcolor(color.background);
for (i = 0; i < 4; ++i)
for (j = 0; j < 4; ++j)
solidrectangle(95 + 120 * i, 300 + 120 * j, 195 + 120 * i, 400 + 120 * j);
char Scores[16] = { 0 };
_itoa_s (score, Scores, 10);
settextcolor(color.score_co);
settextstyle(80, 0, "score_type", 0, 0, 500, 0, 0, 0);
outtextxy(360, 25, Scores);
for (i = 0; i < 4; ++i)
{
box_yp = 300 + 120 * i;
text_yp = 325 + (120 * i);
for (j = 0; j < 4; ++j)
{
box_xp = 95 + 120 * j;
text_xp = 130 + (120 * j);
switch (panel[i][j])
{
case 2:setfillcolor(color.box_co[0]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100);
settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp, text_yp, "2"); break;
case 4:setfillcolor(color.box_co[1]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100);
settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp, text_yp, "4"); break;
case 8:setfillcolor(color.box_co[2]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100);
settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp, text_yp, "8"); break;
case 16:setfillcolor(color.box_co[3]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100);
settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp-8, text_yp, "16"); break;
case 32:setfillcolor(color.box_co[4]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100);
settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp-8, text_yp, "32"); break;
case 64:setfillcolor(color.box_co[5]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100);
settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp-8, text_yp, "64"); break;
case 128:setfillcolor(color.box_co[6]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100);
settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp-20, text_yp, "128"); break;
case 256:setfillcolor(color.box_co[7]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100);
settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp-20, text_yp, "256"); break;
case 512:setfillcolor(color.box_co[8]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100);
settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp-20, text_yp, "512"); break;
case 1024:setfillcolor(color.box_co[9]); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100);
settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp-40, text_yp, "1024"); break;
case 2048:setfillcolor(color.final_co); solidrectangle(box_xp, box_yp, box_xp + 100, box_yp + 100);
settextcolor(BLACK); settextstyle(50, 0, "box_type", 0, 0, 300, 0, 0, 0); outtextxy(text_xp-40, text_yp, "2048"); break;
}
}
}
}
int judge()
{
int i, j;
int can_go = 1;
for (i = 0; i < 4; ++i)
{
for (j = 0; j < 3; ++j)
{
if (panel[i][j] != 0 && panel[i][j] == panel[i][j + 1])
{
can_go = 1;
return 1;
}
}
}
for (j = 0; j < 4; ++j)
{
for (i = 0; i < 3; ++i)
{
if (panel[i][j] != 0 && panel[i][j] == panel[i + 1][j])
{
can_go = 1;
return 1;
}
}
}
for (j = 0; j < 4; ++j)
{
for (i = 0; i < 3; ++i)
{
if (panel[i][j] == 0)
{
can_go = 1;
return 1;
}
}
}
for (i = 0; i < 4; ++i)
{
for (j = 0; j < 4; ++j)
{
if (panel[i][j] == 2048)
{
MessageBox(NULL, TEXT("恭喜获胜!"), TEXT("提示"), MB_OK);
is_win = 1;
return 1;
}
}
}
can_go = 0;
return 0;
}
void time_rand(int put_num)
{
int randnum, randgroup[16];
int i, j, k, n, temp;
k = 0, n = 0;
for (i = 0; i < 4; ++i)
{
for (j = 0; j < 4; ++j)
{
if (panel[i][j] == 0)
{
randgroup[k] = 4 * i + j;
++k;
}
}
}
for (n = 15; n >= 0; --n)
{
if (randgroup[n] != 0) break;
}
time_t t;
srand((unsigned)time(&t));
randnum = rand() % (n + 1); //0-n随机数
temp = randgroup[randnum];
switch (temp)
{
case 0:panel[0][0] = put_num; break;
case 1:panel[0][1] = put_num; break;
case 2:panel[0][2] = put_num; break;
case 3:panel[0][3] = put_num; break;
case 4:panel[1][0] = put_num; break;
case 5:panel[1][1] = put_num; break;
case 6:panel[1][2] = put_num; break;
case 7:panel[1][3] = put_num; break;
case 8:panel[2][0] = put_num; break;
case 9:panel[2][1] = put_num; break;
case 10:panel[2][2] = put_num; break;
case 11:panel[2][3] = put_num; break;
case 12:panel[3][0] = put_num; break;
case 13:panel[3][1] = put_num; break;
case 14:panel[3][2] = put_num; break;
case 15:panel[3][3] = put_num; break;
}
box_num += 1;
update();
/*while (is_set != 1) //旧逻辑
{
time_t t;
srand((unsigned)time(&t));
randnum = rand() % 17 + 1; //1-16随机数
switch (randnum)
{
case 1:if (panel[0][0] == 0); else continue; break;
case 2:if (panel[0][1] == 0); else continue; break;
case 3:if (panel[0][2] == 0); else continue; break;
case 4:if (panel[0][3] == 0); else continue; break;
case 5:if (panel[1][0] == 0); else continue; break;
case 6:if (panel[1][1] == 0); else continue; break;
case 7:if (panel[1][2] == 0); else continue; break;
case 8:if (panel[1][3] == 0); else continue; break;
case 9:if (panel[2][0] == 0); else continue; break;
case 10:if (panel[2][1] == 0); else continue; break;
case 11:if (panel[2][2] == 0); else continue; break;
case 12:if (panel[2][3] == 0); else continue; break;
case 13:if (panel[3][0] == 0); else continue; break;
case 14:if (panel[3][1] == 0); else continue; break;
case 15:if (panel[3][2] == 0); else continue; break;
case 16:if (panel[3][3] == 0); else continue; break;
}
switch (randnum)
{
case 1:panel[0][0] = put_num; break;
case 2:panel[0][1] = put_num; break;
case 3:panel[0][2] = put_num; break;
case 4:panel[0][3] = put_num; break;
case 5:panel[1][0] = put_num; break;
case 6:panel[1][1] = put_num; break;
case 7:panel[1][2] = put_num; break;
case 8:panel[1][3] = put_num; break;
case 9:panel[2][0] = put_num; break;
case 10:panel[2][1] = put_num; break;
case 11:panel[2][2] = put_num; break;
case 12:panel[2][3] = put_num; break;
case 13:panel[3][0] = put_num; break;
case 14:panel[3][1] = put_num; break;
case 15:panel[3][2] = put_num; break;
case 16:panel[3][3] = put_num; break;
}
}
box_num += 1;
update();*/
}
void up()
{
int i, j, k;
for (j = 0; j < 4; ++j) //移动
{
for (i = 0; i < 4; ++i)
{
if (panel[i][j] == 0)
for (k = i+1; k < 4; ++k)
{
if (panel[k][j] != 0)
{
panel[i][j] = panel[k][j];
panel[k][j] = 0;
break;
}
}
}
for (i = 0; i + 1 < 4; ++i) //合并
{
if (panel[i][j] == panel[i + 1][j]&&panel[i][j]!=0)
{
score += panel[i][j];
panel[i][j] *= 2;
panel[i + 1][j] = 0;
is_batter += 1;
}
else is_batter = 0;
}
}
}
void down()
{
int i, j, k;
for (j = 0; j < 4; ++j) //移动
{
for (i = 3; i >= 0; --i)
{
if (panel[i][j] == 0)
for (k = i-1; k >=0; --k)
{
if (panel[k][j] != 0)
{
panel[i][j] = panel[k][j];
panel[k][j] = 0;
break;
}
}
}
for (i = 3; i >= 0; --i) //合并
{
if (panel[i][j] == panel[i - 1][j] && panel[i][j] != 0)
{
score += panel[i][j];
panel[i][j] *= 2;
panel[i - 1][j] = 0;
is_batter += 1;
}
else is_batter = 0;
}
}
}
void left()
{
int i, j, k;
for (i = 0; i < 4; ++i) //移动
{
for (j = 0; j < 4; ++j)
{
if (panel[i][j] == 0)
for (k = j + 1; k < 4; ++k)
{
if (panel[i][k] != 0)
{
panel[i][j] = panel[i][k];
panel[i][k] = 0;
break;
}
}
}
for (j = 0; j + 1 < 4; ++j) //合并
{
if (panel[i][j] == panel[i][j + 1] && panel[i][j] != 0)
{
score += panel[i][j];
panel[i][j] *= 2;
panel[i][j+1] = 0;
is_batter += 1;
}
else is_batter = 0;
}
}
}
void right()
{
int i, j, k;
for (i = 0; i < 4; ++i) //移动
{
for (j = 3; j >= 0; --j)
{
if (panel[i][j] == 0)
for (k = j - 1; k >= 0; --k)
{
if (panel[i][k] != 0)
{
panel[i][j] = panel[i][k];
panel[i][k] = 0;
break;
}
}
}
for (j = 3; j >= 0; --j) //合并
{
if (panel[i][j] == panel[i][j - 1] && panel[i][j] != 0)
{
score += panel[i][j];
panel[i][j] *= 2;
panel[i][j - 1] = 0;
is_batter += 1;
}
else is_batter = 0;
}
}
}
作者:Dragonet-Z
出处:https://www.cnblogs.com/dragonet-Z/p/14689269.html
版权声明:本博客所有文章除特殊声明外,均遵循BY-NC-ND许可协议,转载请注明出处!