⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lian.c

📁 连连看的C语言实现
💻 C
📖 第 1 页 / 共 3 页
字号:
#include "lian.h"
#include "lianP.c"

void main(void)
{
	clrscr();
	GetGuan();
	do
	{
		MatrixInitial();
		InitialMouse();
		MatrixDisp();
		gettime(&starttime);
		do
		{
			DealMouse();
			DealKey();
			if(selected1x!=0&&selected1y!=0&&selected2x!=0&&selected2y!=0)
			/* get the 2 selected */
			{
				if(CanLink()) LinkDisp();
				else ClearSelected();
				selected1x=selected1y=selected2x=selected2y=0;
				MatrixMove();
				if(IsMatrixDie())
				{
					ClearMsgLine();
					sprintf(dispstr,"Matrix is die!");
					outtextxy(DRAW_START_X,DRAW_MSG_Y,dispstr);
					if(AutoGoMode) sleep(2);
					else if(SelectedBy==BY_MOUSE) sleep(2);
					else while(getch()!=ENTER);
					ReArrangeMatrix();
					MatrixDisp();
					sprintf(dispstr,"matrix is re-arrange now");
					outtextxy(DRAW_START_X,DRAW_MSG_Y,dispstr);
					if(AutoGoMode) sleep(2);
					else if(SelectedBy==BY_MOUSE) sleep(1);
					else getch();
					ClearMsgLine();
				}
				if(IsMatrixEmpty()) FinishFlag = FINISH_DONE;
				if(AutoGoMode) sleep(2);
			} /* of deal selected */
		} /* of main do loop */
		while(!FinishFlag);
		
		switch(FinishFlag)
		{
			case FINISH_ESC:	sprintf(dispstr,"User break...");
								break;
			case FINISH_DIE:	sprintf(dispstr,"Matrix is die!");
								break;
			case FINISH_DONE:	sprintf(dispstr,"Matrix is empty.");
								break;
			default:			sprintf(dispstr,"Any key to exit...");
		}
		gettime(&endtime);
		DispPlayTime();
		ClearMsgLine();
		outtextxy(DRAW_START_X,DRAW_MSG_Y,dispstr);
		if(FinishFlag!=FINISH_ESC)
		{
			if(AutoGoMode) sleep(2);
			else if(SelectedBy==BY_MOUSE)
			{
				while(!LeftPress()&&!RightPress());
				while(LeftPress()||RightPress());
			}
			else getch();
			GetNextGuan();
		}
	}
	while(FinishFlag!=FINISH_ESC);
	closegraph();
} /* of main */


/* ================================================= */
/*     Main function                                 */
/* ================================================= */
void DispPlayTime(void)
{
	char tstr[20],tstr2[20];
	int ph,pm,ps;
	
	ph = endtime.ti_hour - starttime.ti_hour;
	pm = endtime.ti_min - starttime.ti_min;
	ps = endtime.ti_sec - starttime.ti_sec;
	
	if(ph<0) ph+=24;
	if(pm<0) { ph--; pm+=60; }
	if(ps<0) { pm--; ps+=60; }
	
	sprintf(tstr," Time use: ");
	if(ph) { sprintf(tstr2,"%d hour- ",ph); strcat(tstr,tstr2); }
	if(pm) { sprintf(tstr2,"%d min- ",pm); strcat(tstr,tstr2); }
	sprintf(tstr2,"%d sec",ps); strcat(tstr,tstr2);
	strcat(dispstr,tstr);
}

void GetGuan(void)
{
	char guanstr[20];
	
	printf("0: not move\n");
    printf("1: move down\n");
    printf("2: move left\n");
    printf("3: move outside up and down\n");
    printf("4: move outside left and right\n");
    printf("5: move inside up and down\n");
    printf("6: move inside left and right\n");
    printf("7: move up-left and down-right\n");
    printf("8: move left-down and right-up\n");
    printf("9: move outside\n");
    printf("10: move inside\n");
    printf("--------------------------------\n\n");
    printf("Input guan:");
	gets(guanstr);
	if(guanstr[0]==0||guanstr[0]<'0'||guanstr[0]>'9') guan = 0;
	else
	{
		guan=atoi(guanstr);
		if(guan>MAX_GUAN) guan = MAX_GUAN;
		if(guan<0) guan = 0;
	}
}

void GetNextGuan(void)
{
	guan++;
	if(guan>MAX_GUAN) guan = 0;

	ClearMsgLine();
	switch(guan)
	{
		case 0:	sprintf(dispstr,"Now 0 guan: not move"); break;
	    case 1:	sprintf(dispstr,"Now 1 guan: move down"); break;
	    case 2:	sprintf(dispstr,"Now 2 guan: move left"); break;
	    case 3:	sprintf(dispstr,"Now 3 guan: move outside up and down"); break;
	    case 4:	sprintf(dispstr,"Now 4 guan: move outside left and right"); break;
	    case 5:	sprintf(dispstr,"Now 5 guan: move inside up and down"); break;
	    case 6:	sprintf(dispstr,"Now 6 guan: move inside left and right"); break;
	    case 7:	sprintf(dispstr,"Now 7 guan: move up-left and down-right"); break;
	    case 8:	sprintf(dispstr,"Now 8 guan: move left-down and right-up"); break;
	    case 9:	sprintf(dispstr,"Now 9 guan: move outside"); break;
	    case 10:sprintf(dispstr,"Now 10 guan: move inside"); break;
	    default:sprintf(dispstr,"I don't know what guan it is :)"); break;
	}
	outtextxy(DRAW_START_X,DRAW_MSG_Y,dispstr);
	
	if(AutoGoMode) sleep(3);
	else if(SelectedBy==BY_MOUSE)
	{
		while(!LeftPress()&&!RightPress());
		while(LeftPress()||RightPress());
	}
	else getch();
	ClearMsgLine();
}	
		
void MatrixInitial(void)
{
	randomize();
	OpenBmpFile();
	GetFileInfo();
	ClearMatrix();
	for(unity=1;unity<=MAX_UNIT_Y;unity++)
	{
		for(unitx=1;unitx<=MAX_UNIT_X;unitx++)
		{
			GetUnit();
			FillMatrix();
		} /* of ix */
	} /* of iy */
	
	/* MatrixConvert(); /* convert from number to char */
	/* only get unit grap from BMP file, matrix arrange is not get from BMP file
	   creat a radom matrix */
	CreatMatrix();
	fclose(fp);
	InitialGraph();
	
	/* -------------------------- */
	/* following is other initial */
	selected1x=selected1y=selected2x=selected2y=0;
	FinishFlag = 0;
	KeyScan = 0;
	LinkType = LINK_NONE;
	type2xy = 0;
	curx=cury=1;
	SelectedBy = BY_MOUSE;
}

void CreatMatrix(void)
/* creat a radom matrix, and sure the matrix is pair by pair */
{
	int xxx,yyy;
	int randnum;
	int i;

	for(yyy=1;yyy<=MAX_Y;yyy++)
         for(xxx=1;xxx<=MAX_X;xxx++)
			matrix[xxx][yyy] = 0;
	
	if(MAX_X*MAX_Y%2)
	{
		gotoxy(1,SCREEN_Y-1);
		printf("Matrix unit must be even!");
		getch();
		gotoxy(1,SCREEN_Y-1);
		printf("                                       ");
		exit(4);
	}

	for(i=1;i<=(MAX_X*MAX_Y)/2;i++)
	/* random generate one char, then find one empty place to fill it,
	   then find another empty place to fill the same char */
	{
		randnum = random(MAX_CHAR); /* from 0 to MAX CHAR - 1 */
		if(randnum>=0&&randnum<=9) randnum = randnum+'0';
		else if(randnum<=35) randnum = randnum-10+'a';
		else if(randnum<=61) randnum = randnum-36+'A';
		else if(randnum<=76) randnum = randnum-62+33;
		else randnum = FILL_CHAR;
		
		do
		{
			xxx=random(MAX_X)+1;
			yyy=random(MAX_Y)+1;
		}
		while(matrix[xxx][yyy]!=0); /* !=0 means not empty, find another x,y */ 
		matrix[xxx][yyy]=randnum;
		do
		{
			xxx=random(MAX_X)+1;
			yyy=random(MAX_Y)+1;
		}
		while(matrix[xxx][yyy]!=0); /* !=0 means not empty, find another x,y */ 
		matrix[xxx][yyy]=randnum;
	}
}

void ClearMatrix(void)
{
	int x,y;
	
	for(y=0;y<=MAX_UNIT_Y;y++)
		for(x=0;x<=MAX_UNIT_X;x++)
			matrix[x][y] = 0xcc;
	for(y=0;y<MAX_CHAR;y++)
		for(x=0;x<OneUnitSize;x++)
			CharList[y][x] = 0;
	for(y=0;y<MAX_CHAR;y++)
		CharCounter[y] = 0;
	MaxChar = 0;
}

void FillMatrix(void)
{
	int CharNumber;

	if(UnitEmpty())
	{
		matrix[unitx][unity] = EMPTY_CHAR; /* 0xff means empty */
		return;
	}

	if(MaxChar==0)
	{
		CopyChar(CharList[0],OneUnit[0]);
		CharCounter[0]++;
		MaxChar++;
		matrix[unitx][unity] = 0;
	} /* of Max.Char = 0 */
	else
	{
		CharNumber=UnitInList();
		if(CharNumber==EMPTY_CHAR) /* 0xff means not in list */
		{
			if(MaxChar<MAX_CHAR)
			{
				CopyChar(CharList[MaxChar],OneUnit[0]);
				CharCounter[MaxChar]++;
				matrix[unitx][unity] = MaxChar;
				MaxChar++;
			}
		} /* of  not in list */
		else
		/* in list */
		{
			CharCounter[CharNumber]++;
			matrix[unitx][unity] = CharNumber;
		} /* of in list */
	} /* of Max.Char != 0 */
}

int UnitEmpty(void)
/* check One.Unit[], if is all black, means this char have been deleted, it is empty.
   return 1 when empty, else return 0 */
{
	int ix,iy;
	int BlackCounter;

	BlackCounter=0;
	for(iy=0;iy<UNIT_HIGH;iy++)
		for(ix=0;ix<UNIT_WIDE;ix++)
			if(OneUnit[iy][ix]==0) BlackCounter++;
	if(BlackCounter>=OneUnitSize*SAME_PERCENT) return(1);
	else return(0);
}

int UnitInList(void)
/* check if One.Unit[] is in Char.List
   if yes, return the position on List of this char
   if no, return EMPTY_CHAR--0xff */
{
	int InList;
	int i;
	
	InList = 0;
	for(i=0;i<MaxChar;i++)
		if(CompareChar(OneUnit[0],CharList[i]))
		{
			InList = 1;
			break;
		}
	if(InList) return(i);
	else return(EMPTY_CHAR);
}

void DrawUnit(int CharNum)
/* input: unitx, unity
          Char.Num -- the location of char in CharList */
{
	int ux,uy;
	int dispx,dispy;
	int dot;
	
	dispx = DRAW_START_X + (unitx-1)*(UNIT_WIDE+2);
	dispy = DRAW_START_Y + unity*(UNIT_HIGH+2);
	for(uy=0;uy<UNIT_HIGH;uy++)
	{
		for(ux=0;ux<UNIT_WIDE;ux++)
		{
			if(CharNum==EMPTY_CHAR) dot = 0; /* 0xff means empty, draw black */
			else if(CharNum>=MaxChar) dot = 9; /* not in char list, draw a blue block */
			else dot = ColorConvert(CharList[CharNum][uy*UNIT_WIDE+ux]);
			if(dispx+ux<MouseX||dispx+ux>=MouseX+16||dispy-uy<MouseY||dispy-uy>=MouseY+16)
				putpixel(dispx+ux,dispy-uy,dot);
			else
			{
				pixel_save[dispy-uy-MouseY][dispx+ux-MouseX]=dot;
				if(mouse_draw[dispy-uy-MouseY][dispx+ux-MouseX]==3||
				   mouse_draw[dispy-uy-MouseY][dispx+ux-MouseX]==4)
				putpixel(dispx+ux,dispy-uy,dot);
			}
		} /* of ux */
	} /* of uy */
}

void DrawUnitS(int CharNum)
/* input: unitx, unity
          Char.Num -- the location of char in Char.List */
{
	int ux,uy;
	int dispx,dispy;
	int dot;

	dispx = DRAW_START_X + (unitx-1)*(UNIT_WIDE+2);
	dispy = DRAW_START_Y + unity*(UNIT_HIGH+2);
	for(uy=0;uy<UNIT_HIGH;uy++)
	{
		for(ux=0;ux<UNIT_WIDE;ux++)
		{
			if(CharNum==EMPTY_CHAR) dot = 0; /* 0xff means empty, draw black */
			else if(CharNum>=MaxChar) dot = 9; /* not in char list, draw a blue block */
			else dot = ColorConvert(CharList[CharNum][uy*UNIT_WIDE+ux]);
			if(dot==15) dot= 12;
			if(dispx+ux<MouseX||dispx+ux>=MouseX+16||dispy-uy<MouseY||dispy-uy>=MouseY+16)
				putpixel(dispx+ux,dispy-uy,dot);
			else
			{
				pixel_save[dispy-uy-MouseY][dispx+ux-MouseX]=dot;
				if(mouse_draw[dispy-uy-MouseY][dispx+ux-MouseX]==3||
				   mouse_draw[dispy-uy-MouseY][dispx+ux-MouseX]==4)
				putpixel(dispx+ux,dispy-uy,dot);
			}
		} /* of ux */
	} /* of uy */
}

void DrawUnitC(int CharNum)
/* input: unitx, unity
          Char.Num -- the location of char in Char.List */
{
	int ux,uy;
	int dispx,dispy;
	int dot;

	dispx = DRAW_START_X + (unitx-1)*(UNIT_WIDE+2);
	dispy = DRAW_START_Y + unity*(UNIT_HIGH+2);
	for(uy=0;uy<UNIT_HIGH;uy++)
	{
		for(ux=0;ux<UNIT_WIDE;ux++)
		{
			if(CharNum==EMPTY_CHAR) dot = 14; /* 0xff means empty, draw light-yellow */
			else if(CharNum>=MaxChar) dot = 9; /* not in char list, draw a blue block */
			else dot = 0x01^ColorConvert(CharList[CharNum][uy*UNIT_WIDE+ux]);
			if(dispx+ux<MouseX||dispx+ux>=MouseX+16||dispy-uy<MouseY||dispy-uy>=MouseY+16)
				putpixel(dispx+ux,dispy-uy,dot);
			else
			{
				pixel_save[dispy-uy-MouseY][dispx+ux-MouseX]=dot;
				if(mouse_draw[dispy-uy-MouseY][dispx+ux-MouseX]==3||
				   mouse_draw[dispy-uy-MouseY][dispx+ux-MouseX]==4)
				putpixel(dispx+ux,dispy-uy,dot);
			}
		} /* of ux */
	} /* of uy */
}


/* ------------------- disp ---------------- */

void MatrixDisp(void)
{
	cleardevice();
	for(unity=1;unity<=MAX_Y;unity++)
	{
		for(unitx=1;unitx<=MAX_X;unitx++)
		{
			DrawUnit(GetCharNum());	
		}
	}
}

/* ---------------------------------------- */

void DealMouse(void)
{
	int x,y;
	int i,j,color;
	
	/* Deal mouse move */
	x=MouseX;
	y=MouseY;
	MouseGetXY();
	if(MouseX!=x||MouseY!=y)
	{
		for(i=0;i<16;i++)/*原位置异或消去*/
			for(j=0;j<16;j++)
			{
				if(mouse_draw[i][j]==3||mouse_draw[i][j]==4) continue;
				/* color=getpixel(x+j,y+i);
				putpixel(x+j,y+i,color^color); */
				/* this two lines in the original source code is no use. I don't know why */
				putpixel(x+j,y+i,pixel_save[i][j]);
			}
		MouseOn(MouseX,MouseY);/*新位置显示*/
	} /* of mouse move */
	
	/* Deal mouse press */
	if(LeftPress())
	{
		if(!PressState)
		/* deal the mouse press only when first times we find the key is pressed
		   next time find key press, just mean the key is holding(not release yet)
		   we must wait the key to release then deal the next press */
		{
			if(MouseX<=5&&MouseY<=5)
			/* here is a special area, just like key:F5 */
			{
				if(selected1x!=0||selected1y!=0)
				{
					unitx = selected1x;
					unity = selected1y;
					DrawUnit(GetCharNum());
				}
				FindLinkPair();
				if(LinkType)
				{
					sprintf(dispstr,"Find(%d,%d)-(%d,%d) Type:%d",
					        selected1x,selected1y,selected2x,selected2y,LinkType);
					outtextxy(DRAW_START_X,DRAW_MSG_Y,dispstr);
					unitx = selected1x;
					unity = selected1y;
					DrawUnitS(GetCharNum());
					unitx = selected2x;
					unity = selected2y;
					DrawUnitS(GetCharNum());
					while(LeftPress()||RightPress());
					ClearSelected();
					ClearMsgLine();
				} /* of find a pair */
				/* else impossible */
			} /* of auto find link pair */
			else if(MouseX>=630&&MouseY<=5)
			/* here is a special area, just like key:F5 */
			{
				if(selected1x!=0||selected1y!=0)
				{
					unitx = selected1x;
					unity = selected1y;
					DrawUnit(GetCharNum());
				}
				FindLinkPair();
				if(LinkType)
				{
					unitx = selected1x;
					unity = selected1y;
					DrawUnitS(GetCharNum());
					unitx = selected2x;
					unity = selected2y;
					DrawUnitS(GetCharNum());
					SelectedBy = BY_MOUSE;
				} /* of find a pair */
				/* else impossible */
			} /* of auto find link pair */
			else if(MouseX<=5&&MouseY>=DRAW_START_Y&&MouseY<=DRAW_START_Y+UNIT_HIGH)
			/* here is a special area, just like key:F4 */
			{
				FindLinkPair();
				DispAllLink();
				ClearMsgLine();
				sprintf(dispstr,"Display all can link: %d",BankCounter);
				outtextxy(DRAW_START_X,DRAW_MSG_Y,dispstr);
				/* while(!LeftPress()&&!RightPress()); */ /* because it is in press state now */
				while(LeftPress()||RightPress()); /* wait until mouse key release */
				UnDispAllLink();
				selected1x=selected1y=selected2x=selected2y=0;
			} /* of auto find link pair */
			else if(MouseX>=630&&MouseY>=DRAW_START_Y&&MouseY<=DRAW_START_Y+UNIT_HIGH)
			/* here is a special area, just like key:F6 */
			{
				sprintf(dispstr,"Re arrange matrix");
				outtextxy(DRAW_START_X,DRAW_MSG_Y,dispstr);
				sleep(1);
				ClearMsgLine();
				ReArrangeMatrix();
				MatrixDisp();
				selected1x=selected1y=selected2x=selected2y=0;
			}
			else		
			/* else select a unit */
			{
				GetMouseCur();
			
				if(matrix[MouseCurX][MouseCurY])
				/* matrix[curx][cury]==0 means it is a empty unit */
				{
					if(selected1x==0)
					{
						selected1x = MouseCurX;
						selected1y = MouseCurY;
						unitx = selected1x;
						unity = selected1y;
						DrawUnitS(GetCharNum());
					}
					else
					{
						selected2x = MouseCurX;
						selected2y = MouseCurY;
						unitx = selected2x;
						unity = selected2y;
						DrawUnitS(GetCharNum());
						SelectedBy = BY_MOUSE;
					}
				} /* of unit not empty */
			}  /* of select a unit */
			PressState = 1;
		} /* of first times key pressed */
		/* else do nothing */
	} /* of mouse left press */
	else if(RightPress())
	{
		if(!PressState)
		/* deal the mouse press only when first times we find the key is pressed
		   next time find key press, just mean the key is holding(not release yet)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -