2048 C语言版


#include<stdio.h>
#include<conio.h>
#include<stdlib.h> 
#include<time.h> 

int x[4][4]={0},i,j,k,score=0;

int empty()
{
	int n=0;
	for(i=0;i<16;i++)
	{
		if(x[i/4][i%4]==0)n++;
	}
	return n;
}

int check()
{
	int a,b,f=0;
	for(a=0;a<4;a++)
	{
		for(b=0;b<4;b++)
		{
			if(x[a][b]>=2048)return 1;
			if(b<3)
			{
				if(x[a][b]==x[a][b+1]||x[b][a]==x[b+1][a])f=1;
			}
		}
	}
	if(f==1)return 0;else return -1;
}

void add(int p)
{
	int rn=0,randp,randn;
	srand((unsigned)time(NULL));
	randp=rand()%empty();
	
	if(p==1)
	{
		randn=2;
	}else
	{
		if(rand()%2==1)randn=4;else randn=2;
	}
	
	for(i=0;i<16;i++)
	{
		if(x[i/4][i%4]==0)
		{
			if(rn==randp){x[i/4][i%4]=randn;break;}
			rn++;
		}
	}
	
}

void out()
{

	system("cls");
	for(i=0;i<10;i++)
	{
		if(i==0)printf("┏━━┳━━┳━━┳━━┓\n┃    ┃    ┃    ┃    ┃\n┃");
		else if(i==8)printf("┗━━┻━━┻━━┻━━┛\n");
		else if(i==9)printf(" SCORE:%d\n",score);
		else if(i%2==0)printf("┣━━╋━━╋━━╋━━┫\n┃    ┃    ┃    ┃    ┃\n┃");
		else
		{
			for(j=0;j<4;j++)
			{
				if(x[i/2][j]!=0)printf("%4d",x[i/2][j]); else printf("    ");
				printf("┃");
			}
			printf("\n");
		}
		
	}
}

void move(int d)
{
	int f0=0,fb=0,fm=0,dx,dy,nx=0,ny=0;
	//if(check()!=-1)
		for(j=0;j<4;j++)
		{
			for(k=0;k<3;k++)
			{if(fb==1){fb=0;break;}
				for(i=0;i<3;i++)
				{
					switch(d)
					{
						case 1:dx=i;dy=j;nx=1;ny=0;break;
						case 2:dx=j;dy=3-i;nx=0;ny=-1;break;
						case 3:dx=3-i;dy=j;nx=-1;ny=0;break;
						case 4:dx=j;dy=i;nx=0;ny=1;break;
					}
		
					if(x[dx][dy]==0)
					{
						if(x[dx+nx][dy+ny]!=0)f0=1;
						x[dx][dy]=x[dx+nx][dy+ny];
						x[dx+nx][dy+ny]=0;
						
					}else
					if(x[dx][dy]!=0&&x[dx][dy]==x[dx+nx][dy+ny])
					{
						x[dx][dy]=x[dx][dy]+x[dx+nx][dy+ny];
						x[dx+nx][dy+ny]=0;
						score+=x[dx][dy];
						fb=1;fm=1;
					}
				}
			}
		}
	
	if(empty()>0&&(f0==1||fm==1))add(0);
	out();
	if(check()==1)printf("\n YOU WIN!");
	else if(check()==-1)printf("\n YOU LOSE!");
}

int main()
{
	
	unsigned char key;
	add(1);add(1);out();
	
	while((key=getch())!=0x1b)
	{
		if(key == 0||key==0xE0)
 		{
 			key = getch();
 			switch(key)
 			{
 				case 72:move(1);break;
 				case 75:move(4);break;
 				case 77:move(2);break;
 				case 80:move(3);break;
 			}
		}
	}

	return 0;
	//C_2048 By:Blue [Email:zzx094@gmail.com]
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
2048 游戏可以使用 C 语言进行开发。以下是一个简单的代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 4 #define TARGET 2048 int board[SIZE][SIZE]; void init_board() { srand(time(NULL)); int x1 = rand() % SIZE; int y1 = rand() % SIZE; int x2 = rand() % SIZE; int y2 = rand() % SIZE; while (x1 == x2 && y1 == y2) { x2 = rand() % SIZE; y2 = rand() % SIZE; } board[x1][y1] = 2; board[x2][y2] = 2; } void print_board() { printf("\n"); for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { printf("%d\t", board[i][j]); } printf("\n"); } } int can_move(int x, int y, int dx, int dy) { if (x + dx < 0 || x + dx >= SIZE || y + dy < 0 || y + dy >= SIZE) { return 0; } if (board[x + dx][y + dy] == 0 || board[x + dx][y + dy] == board[x][y]) { return 1; } return 0; } void move(int x, int y, int dx, int dy) { if (can_move(x, y, dx, dy)) { if (board[x + dx][y + dy] == 0) { board[x + dx][y + dy] = board[x][y]; board[x][y] = 0; } else if (board[x + dx][y + dy] == board[x][y]) { board[x + dx][y + dy] *= 2; board[x][y] = 0; } } } void move_left() { for (int i = 0; i < SIZE; i++) { for (int j = 1; j < SIZE; j++) { move(i, j, 0, -1); } } } void move_right() { for (int i = 0; i < SIZE; i++) { for (int j = SIZE - 2; j >= 0; j--) { move(i, j, 0, 1); } } } void move_up() { for (int i = 1; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { move(i, j, -1, 0); } } } void move_down() { for (int i = SIZE - 2; i >= 0; i--) { for (int j = 0; j < SIZE; j++) { move(i, j, 1, 0); } } } int game_over() { int count = 0; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (board[i][j] == 0) { return 0; } if (board[i][j] == TARGET) { return 1; } if (i > 0 && board[i][j] == board[i - 1][j]) { count++; } if (j > 0 && board[i][j] == board[i][j - 1]) { count++; } } } if (count == 0) { return 1; } return 0; } int main() { init_board(); print_board(); while (!game_over()) { char c = getchar(); switch (c) { case 'a': move_left(); break; case 'd': move_right(); break; case 'w': move_up(); break; case 's': move_down(); break; default: break; } print_board(); } printf("Game over!\n"); return 0; } ``` 该代码使用二维数组 `board` 来存储游戏棋盘,其中 0 表示空格,2 表示初始的两个方块。游戏的核心逻辑在 `move` 函数中,用于判断是否可以移动并更新棋盘状态。在 `main` 函数中,根据用户输入来调用相应的移动函数,并在每次移动后打印当前的棋盘状态。游戏结束的判断逻辑在 `game_over` 函数中,当棋盘上没有空格并且没有相邻的方块可以合并时,游戏结束。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值