2048

来源:互联网 发布:伊辛模型c语言代码 编辑:程序博客网 时间:2024/05/20 06:41

代码注释 没有写完  请大家 自己分析分析

/***********************************************************************

项    目:2048
编译坏境:VS2015
 者:零起点
 间:2017年2月18日
 本:1.0
*************************************************************************/


#include<stdio.h>
#include<stdlib.h>
#define N 4//宏常量
//全局变量
int array[N][N] = { 0 };
int sum = 0;
int judge = 0;
//函数声明
void control();    //按键控制
void output(void);   //输出
void append(int cos);   //添加数字
void upward();    //向上移动
void downward();    //向下移动
void rightwards(); //向右移动
void towards(); //向左移动




int main(void)
{
append(0);
output();
control();
return 0;
}


void output(void)
{
system("cls");
printf("你的得分是:%d", sum);
printf("\n\n\n\t\t\t┏━━━┳━━━┳━━━┳━━━┓\n");
printf("\t\t\t┃      ┃      ┃      ┃      ┃\n");
printf("\t\t\t┃%4d  ┃ %4d ┃ %4d ┃ %4d ┃\n",array[0][0],array[0][1],array[0][2],array[0][3]);
printf("\t\t\t┃      ┃      ┃      ┃      ┃\n");
printf("\t\t\t┣━━━╋━━━╋━━━╋━━━┫\n");
printf("\t\t\t┃      ┃      ┃      ┃      ┃\n");
printf("\t\t\t┃%4d  ┃ %4d ┃ %4d ┃ %4d ┃\n", array[1][0], array[1][1], array[1][2], array[1][3]);
printf("\t\t\t┃      ┃      ┃      ┃      ┃\n");
printf("\t\t\t┣━━━╋━━━╋━━━╋━━━┫\n");
printf("\t\t\t┃      ┃      ┃      ┃      ┃\n");
printf("\t\t\t┃%4d  ┃ %4d ┃ %4d ┃ %4d ┃\n", array[2][0], array[2][1], array[2][2], array[2][3]);
printf("\t\t\t┃      ┃      ┃      ┃      ┃\n");
printf("\t\t\t┣━━━╋━━━╋━━━╋━━━┫\n");
printf("\t\t\t┃      ┃      ┃      ┃      ┃\n");
printf("\t\t\t┃%4d  ┃ %4d ┃ %4d ┃ %4d ┃\n", array[3][0], array[3][1], array[3][2], array[3][3]);;
printf("\t\t\t┃      ┃      ┃      ┃      ┃\n");
printf("\t\t\t┗━━━┻━━━┻━━━┻━━━┛\n");
return;
}


void append(int cos)
{
int pot_x, pot_y,i;
int num, sum;
srand(time(NULL));
pot_x = rand() % 4;
pot_y = rand() % 4;
sum = 0;
if (rand() % 2 == 0)
num = 2;
else
num = 4;
if (cos == 0)//0 初始化 1上 2下 3左 4右
array[pot_x][pot_y] = num;
else if (cos == 1)
{
if (array[3][pot_x] == 0)
array[3][pot_x] = num;
else
for (i = 0; i < N - 1; ++i)
{
if (array[3][i] == 0)
{
array[3][i] = num;
break;
}
else
++sum;
}
}
else if (cos == 2)
{
if (array[0][pot_x] == 0)
array[0][pot_x] = num;
else
for (i = 0; i < N; ++i)
{
if (array[0][i] == 0)
{
array[0][i] = num;
break;
}
else
++sum;
}
}
else if (cos == 3)
{
if (array[pot_y][3] == 0)
array[pot_y][3] = num;
else
for (i = 0; i < N; ++i)
{
if (array[i][3] == 0)
{
array[i][3] = num;
break;
}
else
++sum;
}
}
else if (cos == 4)
{
if (array[pot_y][0] == 0)
array[pot_y][0] = num;
else
for (i = 0; i < N; ++i)
{
if (array[i][0] == 0)
{
array[i][0] = num;
break;
}
else
++sum;
}
}
judge = sum;
sum = 0;
}


void control()
{
while (1)
{
fflush(stdin);
switch (getch())
{
case 'a':
case 'A':
towards();
append(3);
break;
case 'w':
case 'W':
upward();
append(1);
break;
case 'd':
case 'D':
rightwards();
append(4);
break;
case 's':
case 'S':
downward();
append(2);
break;
}
output();
}
}


void upward()
{
int pot_x, pot_y,t;
for (pot_x = 0; pot_x < N; ++pot_x)
{
for (pot_y = 0; pot_y < N-1; ++pot_y)
{
if (array[pot_y][pot_x] == 0)
for (t = pot_y + 1; t < N; ++t)
if (array[t][pot_x] != 0)
{
array[pot_y][pot_x] = array[t][pot_x];
array[t][pot_x] = 0;
}
}
for (pot_y = 0; pot_y < N - 1; ++pot_y)
if (array[pot_y][pot_x]== array[pot_y+1][pot_x])
{
array[pot_y][pot_x] *= 2;
sum += array[pot_y][pot_x];
for (t = pot_y + 1; t < N; ++t)
if (array[t][pot_x] != 0)
{
array[t][pot_x] = array[t+1][pot_x];
array[t+1][pot_x] = 0;
}
}
}

}


void downward()
{
int pot_x, pot_y, t;
for (pot_x = N-1; pot_x >= 0; --pot_x)
{
for (pot_y = 3; pot_y >= 0; --pot_y)
{
if (array[pot_y][pot_x] == 0)
for (t = pot_y - 1; t >= 0; --t)
if (array[t][pot_x] != 0)
{
array[pot_y][pot_x] = array[t][pot_x];
array[t][pot_x] = 0;
}
}
for (pot_y = 3; pot_y >=0; --pot_y)
if (array[pot_y][pot_x] == array[pot_y -1][pot_x])
{
array[pot_y][pot_x] *= 2;
sum += array[pot_y][pot_x];
for (t = pot_y - 1; t >=0; --t)
if (array[t][pot_x] != 0)
{
array[t][pot_x] = array[t - 1][pot_x];
array[t - 1][pot_x] = 0;
}
}
}
}


void rightwards()
{
int pot_x, pot_y, t;
for (pot_y = N - 1; pot_y >= 0; --pot_y)
{
for (pot_x = 3; pot_x > 0; --pot_x)
{
if (array[pot_y][pot_x] == 0)
for (t = pot_x - 1; t >= 0; --t)
if (array[pot_y][t] != 0)
{
array[pot_y][pot_x] = array[pot_y][t];
array[pot_y][t] = 0;
}
}
for (pot_x = 3; pot_x > 0; --pot_x)
if (array[pot_y][pot_x] == array[pot_y][pot_x-1])
{
array[pot_y][pot_x] *= 2;
sum += array[pot_y][pot_x];
for (t = pot_x - 1; t >= 0; --t)
if (array[pot_y][t] != 0)
{
array[pot_y][t] = array[pot_y][t-1];
array[pot_y][t-1] = 0;
}
}
}
}


void towards()
{
int pot_x, pot_y, t;
for (pot_y = 0; pot_y <N; ++pot_y)
{
for (pot_x = 0; pot_x <N; ++pot_x)
{
if (array[pot_y][pot_x] == 0)
for (t = pot_x + 1; t< N; ++t)
if (array[pot_y][t] != 0)
{
array[pot_y][pot_x] = array[pot_y][t];
array[pot_y][t] = 0;
}
}
for (pot_x = 0; pot_x <N; ++pot_x)
if (array[pot_y][pot_x] == array[pot_y][pot_x+ 1])
{
array[pot_y][pot_x] *= 2;
sum += array[pot_y][pot_x];
for (t = pot_x +1 ; t < N-1; ++t)
if (array[pot_y][t] != 0)
{
array[pot_y][t] = array[pot_y][t + 1];
array[pot_y][t + 1] = 0;
}
}
}
}
1 0
原创粉丝点击