2048c语言程序,C语言实现2048小游戏

#include

#include

#include

#define TTY_PATH "/dev/tty"

#define STTY_ON "stty raw -echo -F"

#define STTY_OFF "stty -raw echo -F"

int map[4][4];

typedef struct node

{

int x;

int y;

int num;

}Node;

Node node;

void init_map()//初始化全部方格

{

int i, j;

for(i=0; i<4; i++)

for(j=0; j<4; j++)

map[i][j] = 0;

}

void new_node()//新增方格,避免重复。

{

int x = 0, y = 0 ,num = 0;

do{

int fals = 1;

int i,j;

for(i=0; i<4; i++)

for(j=0; j<4; j++)

if(map[i][j] == 0)

fals =0;

if(fals)//新方格无法被填入,游戏失败

{

system("clear");

printf("game over!!!!!!!!!!!\n");

exit(0);

}

x = rand() % 4;

y = rand() % 4;

num = rand() % 2;

if(num == 0)

num = 3;

else if(num == 1)

num = 5;

}while(map[x][y] > 0);

node.x = x;

node.y = y;

node.num = num;

map[x][y] = node.num;

}

void show()//彩色打印数字

{

int i, j;

for(i=0; i<4; i++)

{

for(j=0; j<4; j++)

{

if(map[i][j]%2 == 1)

{

map[i][j] -= 1;

printf("\33[31m%d\33[0m\t",map[i][j]);

}

else if(map[i][j] == 0)

{

printf("%d\t",map[i][j]);

}

else if(map[i][j] == 2)

{

printf("\33[32m%d\33[0m\t",map[i][j]);

}

else if(map[i][j] == 4)

{

printf("\33[33m%d\33[0m\t",map[i][j]);

}

else if(map[i][j] == 8)

{

printf("\33[34m%d\33[0m\t",map[i][j]);

}

else if(map[i][j] == 16)

{

printf("\33[35m%d\33[0m\t",map[i][j]);

}

else if(map[i][j] == 32)

{

printf("\33[36m%d\33[0m\t",map[i][j]);

}

else

{

printf("\33[44m%d\33[0m\t",map[i][j]);

}

}

printf("\n");

}

}

void left()

{

int i, j, z, tmp;

for(i=0; i<4; i++)//全体方格左移

for(j=0; j<4; j++)

if(map[i][j] == 0 )

for(z = j + 1; z<4; z++)

if(map[i][z] > 0)

{

tmp = map[i][j];

map[i][j] = map[i][z];

map[i][z] = tmp;

break;

}

for(i=0; i<4; i++)//如果方格数字相同则,相加,通过主函数多次调用,排列好

for(j=0; j<4; j++)

if(map[i][j] > 0 )

for(z = j + 1; z<4; z++)

if(map[i][z] > 0)

if(map[i][z] == map[i][j])

{

map[i][j] *= 2;

map[i][z] = 0;

}else

break;

else

break;

else

break;

}

void right()

{

int i, j, z, tmp;

for(i=0; i<4; i++)

for(j=3; j>=0; j--)

if(map[i][j] == 0 )

for(z = j-1; z>=0; z--)

if(map[i][z] > 0)

{

tmp = map[i][j];

map[i][j] = map[i][z];

map[i][z] = tmp;

break;

}

for(i=0; i<4; i++)

for(j=3; j>=0; j--)

if(map[i][j] > 0 )

for(z = j-1; z>=0; z--)

if(map[i][z] > 0)

if(map[i][z] == map[i][j])

{

map[i][j] *= 2;

map[i][z] = 0;

}else

break;

else

break;

else

break;

}

void up()

{

int i, j, z, tmp;

for(i=0; i<4; i++)

for(j=0; j<4; j++)

if(map[j][i] == 0 )

for(z = j+1; z<4; z++)

if(map[z][i] > 0)

{

tmp = map[j][i];

map[j][i] = map[z][i];

map[z][i] = tmp;

break;

}

for(i=0; i<4; i++)

for(j=0; j<4; j++)

if(map[j][i] > 0 )

for(z = j+1; z<4; z++)

if(map[z][i] > 0)

if(map[z][i] == map[j][i])

{

map[j][i] *= 2;

map[z][i] = 0;

}else

break;

else

break;

else

break;

}

void down()

{

int i, j, z, tmp;

for(i=0; i<4; i++)

for(j=3; j>=0; j--)

if(map[j][i] == 0 )

for(z = j-1; z>=0; z--)

if(map[z][i] > 0)

{

tmp = map[j][i];

map[j][i] = map[z][i];

map[z][i] = tmp;

break;

}

for(i=0; i<4; i++)

for(j=3; j>=0; j--)

if(map[j][i] > 0 )

for(z = j-1; z>=0; z--)

if(map[z][i] > 0)

if(map[z][i] == map[j][i])

{

map[j][i] *= 2;

map[z][i] = 0;

}else

break;

else

break;

else

break;

}

void move(char ch)

{

switch(ch)

{

case 'a':

left();

break;

case 'd':

right();

break;

case 'w':

up();

break;

case 's':

down();

break;

}

}

char in_direct()//非堵塞输入

{

fd_set fd;

struct timeval tv;

char ch;

FD_ZERO(&fd);

FD_SET(0, &fd);

tv.tv_sec = 0;

tv.tv_usec = 10;

if(select(1, &fd ,NULL, NULL, &tv) > 0)

{

ch = getchar();

}

return ch;

}

int main()

{

srand(time(NULL));

init_map();

new_node();

show();

char ch;

int i=0;

while(1)

{

system(STTY_ON TTY_PATH);

ch = in_direct();

system(STTY_OFF TTY_PATH);

if(ch=='a'||ch=='d'||ch=='s'||ch=='w')

{

system("clear");

for(i=0;i<3;i++)//重复多次才能排序好

move(ch);

new_node();

show();

}

if(ch=='q')//退出游戏

{

system("clear");

printf("game over!!!!!!!!\n");

break;

}

usleep(500000);

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值