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

📄 lian.c

📁 连连看的C语言实现
💻 C
📖 第 1 页 / 共 3 页
字号:
		   we must wait the key to release then deal the next press */
		{
			ClearSelected();
			PressState = 1;
		} /* of first times key pressed */
		/* else do nothing */
	} /* of right press */
	else PressState = 0;
}

void DealKey(void)
/*  Esc -- exit
	SPACE -- set selected
	e -- change matrix manunal
	s -- save current matrix to file
	l -- load matrix from file
*/
{
	char ch;
	int xyok;
	char changeto;
	int savecurx,savecury;

	ClearMsgLine();
	KeyScan=0;
	ch = KeyPress();
	if(AutoGoMode) if(!KeyScan) KeyScan=F5;
	
	switch(ch)
	{
		case ESC:	sprintf(dispstr,"Press ESC to exit, other continue play");
					outtextxy(DRAW_START_X,DRAW_MSG_Y,dispstr);
					if(getch()==ESC)
					{
						FinishFlag = FINISH_ESC;
						selected1x=selected1y=selected2x=selected2y=0;
					}
					ClearMsgLine();
					break;
		case ENTER:	
		case SPACE:	if(matrix[curx][cury]==0) break;
					if(selected1x==0)
					{
						selected1x = curx;
						selected1y = cury;
						unitx = selected1x;
						unity = selected1y;
						DrawUnitS(GetCharNum());
					}
					else
					{
						selected2x = curx;
						selected2y = cury;
						unitx = selected2x;
						unity = selected2y;
						DrawUnitS(GetCharNum());
						SelectedBy = BY_KEY;
					}
					break;
		case 's':	sprintf(dispstr,"Save matrix to file");
					outtextxy(DRAW_START_X,DRAW_MSG_Y,dispstr);
					getch();
					ClearMsgLine();
					MatrixSave();
					break;
		case 'l':	sprintf(dispstr,"Load matrix from file");
					outtextxy(DRAW_START_X,DRAW_MSG_Y,dispstr);
					getch();
					ClearMsgLine();
					MatrixLoad();
					MatrixDisp();
					selected1x=selected1y=selected2x=selected2y=0;
					break;
		case 'a':	if(AutoGoMode)
					{
						AutoGoMode = 0;
						sprintf(dispstr,"Control by man now");
						outtextxy(DRAW_START_X,DRAW_MSG_Y,dispstr);
						if(SelectedBy==BY_MOUSE) sleep(1);
						else getch();
						ClearMsgLine();
					}
					else
					{
						AutoGoMode = 1;
						sprintf(dispstr,"Auto Go Now");
						outtextxy(DRAW_START_X,DRAW_MSG_Y,dispstr);
						sleep(2);
						ClearMsgLine();
					}
					break;
		default:	break;
	} /* of case ch */

	if(KeyScan)
	{
		savecurx = curx;
		savecury = cury;
		switch(KeyScan)
		{
			case ARROW_UP:	cury--;
							if(cury<=0) cury=1;
							break;
			case ARROW_DOWN:cury++;
							if(cury>MAX_Y) cury=MAX_Y;
							break;
			case ARROW_LEFT:curx--;
							if(curx<=0) curx=1;
							break;
			case ARROW_RIGHT:curx++;
							if(curx>MAX_X) curx=MAX_X;
							break;
			case F6:		sprintf(dispstr,"Re arrange matrix");
							outtextxy(DRAW_START_X,DRAW_MSG_Y,dispstr);
							if(SelectedBy==BY_MOUSE) sleep(1);
							else getch();
							ClearMsgLine();
							ReArrangeMatrix();
							MatrixDisp();
							selected1x=selected1y=selected2x=selected2y=0;
							break;
			case F9:		MatrixDisp();
							break;
			case F4:		FindLinkPair();
							DispAllLink();
							ClearMsgLine();
							sprintf(dispstr,"Display all can link: %d",BankCounter);
							outtextxy(DRAW_START_X,DRAW_MSG_Y,dispstr);
							getch();
							UnDispAllLink();
							selected1x=selected1y=selected2x=selected2y=0;
							break;
			case F5:		/* if(AutoGoMode) if(SUPPER_FAST) sleep(1); */
							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());
								if(AutoGoMode)
								{
									if(SUPPER_FAST) sleep(2);
									ch = 0;
								}
								else if(SelectedBy==BY_MOUSE)
								{
									sleep(1);
									ch = ESC;
								}
								else ch = getch();
								switch(ch)
								{
									case ESC:
									case SPACE:	ClearSelected();
												break;
									default:	break; /* let main loop do it */
								}  /* of case ch in F5 */
								ClearMsgLine();
							} /* of find a pair */
							/* else impossible */
							break;
			defualt:		break;
		} /* of case keyscan */
		KeyScan = 0;
		if(savecurx!=curx||savecury!=cury)
		{
			unitx = savecurx;
			unity = savecury;
			if(unitx==selected1x&&unity==selected1y) DrawUnitS(GetCharNum());
			else DrawUnit(GetCharNum());
			unitx = curx;
			unity = cury;
			DrawUnitC(GetCharNum());
		}
	} /* of keyscan */
}

void ClearSelected(void)
{
	if(selected1x&&selected1y)
	{
		unitx = selected1x;
		unity = selected1y;
		DrawUnit(GetCharNum());
	}
	if(selected2x&&selected2y)
	{
		unitx = selected2x;
		unity = selected2y;
		DrawUnit(GetCharNum());
	}
	selected1x=selected1y=selected2x=selected2y=0;
}

int CanLink(void)
/* return 0: can not link
		  not 0: can link, return Link.Type
   the different of this Check.Link() and Can.Link() is Check.Link() do not display on screen 
*/
{	unsigned char verhi;
	union REGS regs0;
	regs0.h.ah=0x30;
	int86(0x21,&regs0,&regs0);
	verhi = regs0.h.al;
	/* verlow = regs0.h.ah; */
	/* function dalay() in Win98 is ok, but in WinXP is not work, so must check the ver of DOS */
	
	if(selected2x==selected1x&&selected2y==selected1y) return(0);
	if(!selected2x||!selected1x||!selected2y||!selected1y) return(0);
	if(matrix[selected1x][selected1y]!=matrix[selected2x][selected2y])
	{
		sprintf(dispstr,"1 and 2 not match");
		outtextxy(DRAW_START_X,DRAW_MSG_Y,dispstr);
		if(SelectedBy==BY_MOUSE)
			if(verhi==0x05) delay(30000); /* in WinXP, I don't know why the unit here is not ms */
			else delay(500); /* in Win98 delay 500ms */
		else getch();
		ClearMsgLine();
		return(0);
	}
	SwapSelected();
	LinkType = LINK_NONE;
	if(selected1x!=selected2x&&selected1y!=selected2y)
	{
		if(selected1y<selected2y)
		/* 1 is on the left-up of 2 */
		{
			IsLink22p31();
			IsLink22p32();
			IsLink21p22(); /* same of */
			IsLink21p21(); /* same of */
			IsLink21p12(); /* same of */
			IsLink21p11(); /* same of */
			IsLink1p31();
			IsLink1p32();
		} /* of left-up */
		else
		/* 1 is on the left-down of 2 */
		{
			IsLink22p41();
			IsLink22p42();
			IsLink21p22(); /* same of */
			IsLink21p21(); /* same of */
			IsLink21p41();
			IsLink21p42();
			IsLink1p41();
			IsLink1p42();

		} /* of left-down */
	} /* of x and y != */
	else if(selected1x==selected2x)
	{
		IsLink21p11();
		IsLink21p12();
		IsLink0p1();

	} /* of 1x = 2x */
	else /* selected1y = selected2y */
	{
		IsLink21p21();
		IsLink21p22();
		IsLink0p2();
	} /* of 1y = 2y */

	if(LinkType==LINK_NONE)
	{
		sprintf(dispstr,"Can not link");
		outtextxy(DRAW_START_X,DRAW_MSG_Y,dispstr);
		if(SelectedBy==BY_MOUSE)
			if(verhi==0x05) delay(30000); /* in WinXP, I don't know why the unit here is not ms */
			else delay(500); /* in Win98 delay 500ms */
		else getch();
		ClearMsgLine();
		return(0);
	}
	else
	{
		sprintf(dispstr,"Can link, type%d",LinkType);
		outtextxy(DRAW_START_X,DRAW_MSG_Y,dispstr);
		if(!AutoGoMode)
			if(SelectedBy==BY_MOUSE)
				if(verhi==0x05) delay(30000); /* in WinXP, I don't know why the unit here is not ms */
				else delay(500); /* in Win98 delay 500ms */
			else getch();
		/* else sleep(2); */
		ClearMsgLine();
		return(LinkType);
	}
}

void SwapSelected(void)
/* swap 1 and 2 if need, make sure 2 is always on the righ side 1, if 1x=2x, then 2 on the down of 1 */
{
	int needtoswap;
	int temp;

	if(selected1x < selected2x) needtoswap = 0;
	else if(selected1x > selected2x) needtoswap = 1;
	else /* 1x=2x */
	{
		if(selected1y < selected2y) needtoswap = 0;
		else needtoswap = 1;
	}
	if(needtoswap)
	{
		temp = selected1x;
		selected1x = selected2x;
		selected2x = temp;
		temp = selected1y;
		selected1y = selected2y;
		selected2y = temp;
	}
}

void LinkDisp(void)
{
	matrix[selected1x][selected1y] = 0;
	matrix[selected2x][selected2y] = 0;
	unitx = selected1x;
	unity = selected1y;
	DrawUnit(EMPTY_CHAR);
	unitx = selected2x;
	unity = selected2y;
	DrawUnit(EMPTY_CHAR);
}


void ReArrangeMatrix(void)
{
	char CharList[MAX_CHAR];
	int CharCounter[MAX_CHAR];
	int MaxChar;
	int i,j,xxx,yyy;
	char ch;
	int InList;


	/* record the char and each char_counter in matrix */
	MaxChar = 0;
	for(i=0;i<MAX_CHAR;i++)
	{
		CharList[i] = 0;
		CharCounter[i]=0;
	}
	
	for(xxx=1;xxx<=MAX_X;xxx++)
		for(yyy=1;yyy<=MAX_Y;yyy++)
		{
			ch = matrix[xxx][yyy];
			if(ch==0) continue;
			if(MaxChar==0)
			{
				CharList[0] = ch;
				CharCounter[0]++;
				MaxChar++;
			} 
			else
			{
				InList = 0;
				for(i=0;i<MaxChar;i++)
					if(ch==CharList[i])
					{
						InList = 1;
						break;
					}
				/* of check char list */
				if(InList)
				{
					CharCounter[i]++;
				} /* of char is in list */
				else
				{
					CharList[MaxChar] = ch;
					CharCounter[MaxChar]++;
					MaxChar++;
				} /* of char not in list */
			} /* of Max.Char != 0 */
			matrix[xxx][yyy] = MAX_CHAR+1;
			/* mark all the no_empty unit with MAX_CHAR+1 */
		} /* of unit */
		
	/* now re.arrange the matrix */
	for(i=0;i<MaxChar;i++)
	{
		ch = CharList[i];
		for(j=0;j<CharCounter[i];j++)
		{
			/* find a no_empty unit random */
			do
			{
				xxx=random(MAX_X)+1;
				yyy=random(MAX_Y)+1;
			}
			while(matrix[xxx][yyy]!=MAX_CHAR+1);
			matrix[xxx][yyy] = ch;
		} /* of for j char.counter */
	} /* of for i max.char */
}

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

void FindLinkPair(void)
/* if find one pair of 1 and 2 can link, give their value
   if can not find, 1 and 2 is 0
*/
{	int startx,endx,stepx;
	int starty,endy,stepy;
	int ibank;
	
	if(random(100)%2)
	{
		startx=1;endx=MAX_X;stepx=1;	
	}
	else
	{
		startx=MAX_X;endx=1;stepx=-1;
	}
	if(random(100)%2)
	{
		starty=1;endy=MAX_Y;stepy=1;	
	}
	else
	{
		starty=MAX_Y;endy=1;stepy=-1;
	}
	
	BankCounter = 0;
	ClearSelectBank();
	for(selected1x=startx;selected1x!=endx+stepx;selected1x+=stepx)
	{
		for(selected1y=starty;selected1y!=endy+stepy;selected1y+=stepy)
		{
			if(!matrix[selected1x][selected1y]) continue;
			/* this unit is empty, check next unit */

			for(selected2x=1;selected2x<=MAX_X;selected2x++)
			{
				for(selected2y=1;selected2y<=MAX_Y;selected2y++)
				{
					if(!matrix[selected2x][selected2y]) continue;
					/* this unit is empty, check next unit */

					if(CheckLink())
					{
						if(!SameSelected())
						{
							if(BankCounter<MAX_BANK)
							{
								SelectBank[BankCounter].x1 = selected1x;
								SelectBank[BankCounter].y1 = selected1y;
								SelectBank[BankCounter].x2 = selected2x;
								SelectBank[BankCounter].y2 = selected2y;
								SelectBank[BankCounter].type = LinkType;
								SelectBank[BankCounter].xy2 = type2xy;
								BankCounter++;
							}
							/* else not store into bank */
						} /* of not same selected */
						/* else not store into bank */
					}
				} /* of for 2y */
			} /* of for 2x */
		} /* of for 1y */
	} /* of for 1x */
	if(BankCounter==0)
	{
		selected1x=selected1y=selected2x=selected2y=0;
		LinkType = LINK_NONE;
		/* can not find can link, the matrix is die */
	}
	else
	{
		ibank = ChooseFromBank();
		selected1x = SelectBank[ibank].x1;
		selected1y = SelectBank[ibank].y1;
		selected2x = SelectBank[ibank].x2;
		selected2y = SelectBank[ibank].y2;
		LinkType = SelectBank[ibank].type;
		type2xy = SelectBank[ibank].xy2;
		SwapSelected();
		/* gotoxy(1,SCREEN_Y-1);
		printf("MAX Link = %d, use %d",BankCounter,ibank);
		getch();
		gotoxy(1,SCREEN_Y-1);
		printf("                                       "); */
	}
}

void DispAllLink(void)
{
	int ibank;
	int x1,y1,x2,y2;
	int ccolor;
	
	for(ibank=0;ibank<BankCounter;ibank++)
	{
		x1 = SelectBank[ibank].x1;
		y1 = SelectBank[ibank].y1;
		x2 = SelectBank[ibank].x2;
		y2 = SelectBank[ibank].y2;
		unitx = x1;
		unity = y1;
		DrawUnitS(GetCharNum());
		unitx = x2;
		unity = y2;
		DrawUnitS(GetCharNum());
	}
}

void UnDispAllLink(void)
{
	int ibank;
	int x1,y1,x2,y2;
	int ccolor;
	
	for(ibank=0;ibank<BankCounter;ibank++)
	{
		x1 = SelectBank[ibank].x1;
		y1 = SelectBank[ibank].y1;
		x2 = SelectBank[ibank].x2;
		y2 = SelectBank[ibank].y2;
		unitx = x1;
		unity = y1;
		DrawUnit(GetCharNum());
		unitx = x2;
		unity = y2;
		DrawUnit(GetCharNum());
	}
}

int CheckLink(void)
/* return 0: can not link
		  not 0: can link, return Link.Type
   the different of this Check.Link() and Can.Link() is Check.Link() do not display on screen 
*/
{
	int save1x,save1y,save2x,save2y;
	
	if(selected2x==selected1x&&selected2y==selected1y) return(0);
	if(matrix[selected1x][selected1y]!=matrix[selected2x][selected2y]) return(0);
	save1x = selected1x;
	save1y = selected1y;
	save2x = selected2x;
	save2y = selected2y;
	SwapSelected();
	LinkType = LINK_NONE;
	if(selected1x!=selected2x&&selected1y!=selected2y)
	{
		if(selected1y<selected2y)
		/* 1 is on the left-up of 2 */
		{
			IsLink22p31();
			IsLink22p32();
			IsLink21p22(); /* same of */
			IsLink21p21(); /* same of */
			IsLink21p12(); /* same of */
			IsLink21p11(); /* same of */
			IsLink1p31();
			IsLink1p32();
		} /* of left-up */
		else
		/* 1 is on the left-down of 2 */
		{
			IsLink22p41();
			IsLink22p42();
			IsLink21p22(); /* same of */
			IsLink21p21(); /* same of */

⌨️ 快捷键说明

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