消消乐

#include <stdio.h>

typedef enum{UP, DOWN, LEFT, RIGHT, INIT} DIRECT;
typedef enum{FALSE, TRUE} BOOL;

char bubble[10][10] = {0};
int count = 0;
int score = 0;

void initialization()
{
	//初始化
	srand(time(NULL));
	
	int i,j;
	
	for(i = 0; i < 10; i++)
	{
		for(j = 0; j < 10; j++)
		{
			if(i == 0)
			{
				bubble[i][j] = j+'0';			//第一行表示列号
			}
			else if(j == 0)
			{
				bubble[i][j] = i+'0';			//第一列表示行号
			}
			else
			{
				bubble[i][j] = rand()%5 + 'A';	//将'A'-'E'的随机数赋值给二维数组bubble
			}
		}
	}
	
}

void print_bubble()
{
	//打印所有泡泡
	int i,j;
	
	for(i = 0; i < 10; i++)
	{
		for(j = 0; j < 10; j++)
		{
			if(1 == j)
			{
				printf(" ");
			}
			printf("%c",bubble[i][j]);
		}
		printf("\n");
	}
}

void translate_o(int x, int y)
{
	char same = bubble[x][y];
	bubble[x][y] = 'O';
	
	if((x>1) && (bubble[x-1][y] == same))
		translate_o(x-1, y);
	
	if((x<=8) && (bubble[x+1][y] == same))
		translate_o(x+1, y);
	
	if((y>1) && (bubble[x][y-1] == same))
		translate_o(x, y-1);
	
	if((y<=8) && (bubble[x][y+1] == same))
		translate_o(x, y+1);
}


void input_xy(int *x, int *y)
{
	BOOL tmp = FALSE;
	
	while(tmp == FALSE)
	{
		printf("请输入要消除的坐标: ");
		scanf("%d,%d", x, y);
		
		if(bubble[*x][*y]>='A' && bubble[*x][*y]<='E' && *x>0 && *x<10 && *y>0 && *y<10)
		{
			tmp = TRUE;
		}
		else
		{
			printf("坐标有误,请重新输入\n");
		}
	}
}

int getscore(int count)
{
	if(count == 1)
		return 2;
	
	return (getscore(count-1)+2*(count-2));
}

BOOL delete_o()
{
	int i,j,k;
	int gap = 0;
	for(j = 1; j < 10; j++)
	{
		for(i = 1; i < 10 ;i++)
		{
			if(bubble[i][j] == 'O')
			{
				k = i-1;
				while(k > 0)
				{
					bubble[k+1][j] = bubble[k][j];
					k--;
				}
				
				bubble[1][j] = ' ';
				gap++;
			}
		}
	}
	
	if(gap < 2)
	{
		return FALSE;
	}
	else
	{
		count += gap;
		score = getscore(count);
		return TRUE;
	}
}


int main()
{
	int x,y;
	BOOL alive = TRUE;
	
	initialization();
	print_bubble();
		
	while(1)
	{
		input_xy(&x,&y);
		
		translate_o(x, y);	
		
		alive = delete_o();
		print_bubble();
		if(alive == TRUE)
		{
			printf("当前分数=%d\n", score);
		}
		else
		{
			printf("游戏结束,最终得分=%d\n\n", score);
			break;
		}
	}
	
	return 0;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值