#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;
}
消消乐
最新推荐文章于 2022-06-01 06:43:20 发布