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

📄 renju.c

📁 嵌入式linux下面五子棋游戏代码
💻 C
📖 第 1 页 / 共 3 页
字号:
	}
	else
	{
		// the secondly step
		put_status = Renju_MachinePutChess((unsigned short int *)&x, (unsigned short int *)&y, who);
		
		if(chess_object->level == 4)
		{
			temp_xy[many].x = temp_xy[many].y = 0;
			for(xx=0;xx<CHESS_LEN_X;xx++) for(yy=0;yy<CHESS_LEN_Y;yy++)
				if( (put_status&0xf0) == 0 && chess_object->defence_value[yy][xx] >= THRESHOLD )
				{
					temp_xy[many].x = xx; temp_xy[many].y = yy;
					many++;
					if(many > MANY-1) break;
				}
				
				if( many >= 1)
				{
					chess_object->map[y][x] = who;         // assume machine goes this point.
					
					many = Renju_rndom(many);
					temp_xx = temp_xy[many].x;
					temp_yy = temp_xy[many].y;
					while(1)
					{
						who = (who==MAN)? MACHINE : MAN;    // change player
						put_status = Renju_MachinePutChess((unsigned short int *)&temp_x, (unsigned short int *)&temp_y, who);
						chess_object->map[temp_y][temp_x] = who;         // man or machin
						save_xy[step_deep].x = temp_x;
						save_xy[step_deep].y = temp_y;
						
						if(put_status == 0x81)
						{
							if(who == MAN) // if man will win, then...
							{
								chess_object->map[y][x] = 0;
								x = temp_xx;
								y = temp_yy;
							}
							for(step=0; step<=step_deep; step++)
								chess_object->map[save_xy[step].y][save_xy[step].x] = 0;
							break;
						}
						
						step_deep++;
						if(step_deep > DEEP || put_status == 0)
						{
							for(step=0; step<step_deep; step++)
								chess_object->map[save_xy[step].y][save_xy[step].x] = 0;
							step_deep = 0;
							break;
						}
					}
				}// many >= 1
		}// chess_object->level == 4
		//	  AddToRepentQueue(chess_object, x, y);
	}
	
	if(put_status == 0)  // all_no_win
	{
		chess_object->map[y][x] = MACHINE;
		chess_object->put_chess_counter = (CHESS_LEN_X)*(CHESS_LEN_Y)/2+1 ;
	}
	else
	{
		chess_object->map[y][x] = MACHINE;
		chess_object->x=(char)x;chess_object->y=(char)y;
	}
	
	chess_object->who_go = MAN;
	chess_object->put_chess_counter++;
	
	*mX = x;
	*mY = y;
	
	return;
}


/*
* Description:calculate coordinate of the machine go and put it into para (put_x,put_y)
*
* in para    :
* 			    CHESS_OBJECT *chess_object
* out para   :
*              unsigned short int *put_x:x coordinate
*              unsigned short int *put_y:y coordinate
*                               char who:who go
* return     :
*              [1] who win    = 0x81 (attack)
*              [2] all_no_win = 0x00
*              [3] normal     = 0x01
*              [4] important  = 0x80 ~ 0x20  or 0x41 ~ 0x21
*/

static unsigned char Renju_MachinePutChess(unsigned short int *put_x, unsigned short int *put_y, char who)
{
	CALOG_ENT();	//added by calog

	unsigned short int x, y, max;
	char enemy = (who==MAN)? MACHINE : MAN;
	
	for(x=0;x<CHESS_LEN_X;x++) for(y=0;y<CHESS_LEN_Y;y++)
	{
		chess_object->attack_value[y][x] = chess_object->defence_value[y][x] = 0;
		if(chess_object->map[y][x]==0)
		{
			chess_object->attack_value[y][x] = Renju_CalValue(Renju_Connect(x, y, who));
			chess_object->defence_value[y][x] = Renju_CalValue(Renju_Connect(x, y, enemy));
			if(chess_object->attack_value[y][x]&0x8000)
			{ *put_x = x; *put_y = y; return 0x81; }
		}
	}
	
	// [1] first, conser defence.
	*put_x = *put_y = 0;
	for(x=0;x<CHESS_LEN_X;x++) for(y=0;y<CHESS_LEN_Y;y++)
		if(chess_object->defence_value[y][x]&0x8000)
			if(chess_object->defence_value[y][x]>chess_object->defence_value[*put_y][*put_x])
			{ *put_x = x; *put_y = y; }
			if(chess_object->defence_value[*put_y][*put_x]&0x8000) return 0x80;
			
			// [2] second, consider attack.
			*put_x = *put_y = 0;
			for(x=0;x<CHESS_LEN_X;x++) for(y=0;y<CHESS_LEN_Y;y++)
				if(chess_object->attack_value[y][x]&0x4000)
					if(chess_object->attack_value[y][x]>chess_object->attack_value[*put_y][*put_x])
					{ *put_x = x; *put_y = y; }
					if(chess_object->attack_value[*put_y][*put_x]&0x4000) return 0x41;
					
					// [3] 3th, consider defence.
					*put_x = *put_y = 0;
					for(x=0;x<CHESS_LEN_X;x++) for(y=0;y<CHESS_LEN_Y;y++)
						if(chess_object->defence_value[y][x]&0x4000)
							if(chess_object->defence_value[y][x]>chess_object->defence_value[*put_y][*put_x])
							{ *put_x = x; *put_y = y; }
							if(chess_object->defence_value[*put_y][*put_x]&0x4000) return 0x40;
							
							// [4] 4th, consider attack.
							*put_x = *put_y = 0;
							for(x=0;x<CHESS_LEN_X;x++) for(y=0;y<CHESS_LEN_Y;y++)
								if(chess_object->attack_value[y][x]&0x2000)
									if(chess_object->attack_value[y][x]>chess_object->attack_value[*put_y][*put_x])
									{
										*put_x = x; *put_y = y;
										if(chess_object->level<3) chess_object->attack_value[y][x]=5;
									}
									if(chess_object->attack_value[*put_y][*put_x]&0x2000) return 0x21;
									
									// [5] 5th, consider defence.
									*put_x = *put_y = 0;
									for(x=0;x<CHESS_LEN_X;x++) for(y=0;y<CHESS_LEN_Y;y++)
										if(chess_object->defence_value[y][x]&0x2000)
											if(chess_object->defence_value[y][x]>chess_object->defence_value[*put_y][*put_x])
											{
												*put_x = x; *put_y = y;
												if(chess_object->level==1) chess_object->defence_value[y][x]=5;
											}
											if(chess_object->defence_value[*put_y][*put_x]&0x2000) return 0x20;
											
											// [6] 6th, normal
											*put_x = *put_y = 0;
											for(x=0;x<CHESS_LEN_X;x++) for(y=0;y<CHESS_LEN_Y;y++)
												if(chess_object->attack_value[y][x]>chess_object->attack_value[*put_y][*put_x])
												{ *put_x = x; *put_y = y; }
												else
													if(chess_object->level>=3&&chess_object->attack_value[y][x]==chess_object->attack_value[*put_y][*put_x])
														if(chess_object->defence_value[y][x]>chess_object->defence_value[*put_y][*put_x])
														{ *put_x = x; *put_y = y; }
														
														max = 0;
														if(chess_object->level>=3)
															for(x=*put_x;x<CHESS_LEN_X;x++) for(y=0;y<CHESS_LEN_Y;y++)
															{	if(chess_object->attack_value[y][x]==chess_object->attack_value[*put_y][*put_x]
															&&	chess_object->defence_value[y][x]==chess_object->defence_value[*put_y][*put_x])
															max++;
															}
															else
																for(x=*put_x;x<CHESS_LEN_X;x++) for(y=0;y<CHESS_LEN_Y;y++)
																	if(chess_object->attack_value[y][x]==chess_object->attack_value[*put_y][*put_x])
																		max++;
																	
																	if(max!=1)
																	{
																		max = Renju_rndom(max) + 1;
																		for(x=*put_x;x<CHESS_LEN_X;x++) for(y=0;y<CHESS_LEN_Y;y++)
																		{
																			if(chess_object->level>=3 && chess_object->attack_value[y][x]==chess_object->attack_value[*put_y][*put_x]
																				&& chess_object->defence_value[y][x]==chess_object->defence_value[*put_y][*put_x])
																				max--;
																			else
																				if(chess_object->level<3 && chess_object->attack_value[y][x]==chess_object->attack_value[*put_y][*put_x])
																					max--;
																				if(max==0)
																				{ *put_x = x; *put_y = y; goto done; }
																		}
																	}
done:
																	if(chess_object->attack_value[*put_y][*put_x]+chess_object->defence_value[*put_y][*put_x] == 0 && chess_object->map[*put_y][*put_x] != 0)
																		return 0;
																	else
																		return 1;
}  //end machineputchess


void Renju_CalculateLineChessman(void)
{
	CALOG_ENT();	//added by calog

	char who,n,m,j,lChess;
	char wX, wY;
	
	if(chess_object->dir == 0) return;
	
	wX = chess_object->wx;
	wY = chess_object->wy;
	who=chess_object->map[wY][wX];
	
	lChess = 0;
	
	switch(chess_object->dir)
	{
	case 1:	// left and right	
		
		for(n=wX-1,j=0;n>0,j<4;n--,j++)
			if(chess_object->map[wY][n]==who)
			{
				lineChessman[lChess].x = n;
				lineChessman[lChess].y = wY;
				lChess++;
			}
			else break;
			for(n=wX+1,j=j;n<CHESS_LEN_X;n++,j++)
				if(chess_object->map[wY][n]==who)
				{
					lineChessman[lChess].x = n;
					lineChessman[lChess].y = wY;
					lChess++;
				}
				else break;
				break;
	case 2:		// up and down
		for(n=wY-1,j=0;n>0,j<4;n--,j++)
			if(chess_object->map[n][wX]==who)
			{
				lineChessman[lChess].x = wX;
				lineChessman[lChess].y = n;
				lChess++;
			}
			else break;
			for(n=wY+1,j=j;n<CHESS_LEN_Y,j<4;n++,j++)
				if(chess_object->map[n][wX]==who)
				{
					lineChessman[lChess].x = wX;
					lineChessman[lChess].y = n;
					lChess++;
				}
				else break;
				break;
	case 3:		// left_up and right_down
		for(n=wY-1,m=wX-1,j=0;n>0,m>0,j<4;n--,m--,j++)
			if(chess_object->map[n][m]==who)
			{
				lineChessman[lChess].x = m;
				lineChessman[lChess].y = n;
				lChess++;
			}
			else break;
			for(n=wY+1,m=wX+1,j=j;n<CHESS_LEN_Y,m<CHESS_LEN_X,j<4;n++,m++,j++)
				if(chess_object->map[n][m]==who)
				{
					lineChessman[lChess].x = m;
					lineChessman[lChess].y = n;
					lChess++;
				}
				else break;
				break;
	case 4:		// left_down and right_up
		for(n=wY+1,m=wX-1,j=0;n<CHESS_LEN_Y,m>0,j<4;n++,m--,j++)
			if(chess_object->map[n][m]==who)
			{
				lineChessman[lChess].x = m;
				lineChessman[lChess].y = n;
				lChess++;
			}
			else break;
			for(n=wY-1,m=wX+1,j=j;n>0,m<CHESS_LEN_X,j<4;n--,m++,j++)
				if(chess_object->map[n][m]==who)
				{
					lineChessman[lChess].x = m;
					lineChessman[lChess].y = n;
					lChess++;
				}
				else break;
				break;
	}
	if(chess_object->map[wY][wX] != INIT_WIN)
	{
		lineChessman[lChess].x = wX;
		lineChessman[lChess].y = wY;
		lChess++;
	}

	return;	//added by calog
}



unsigned short int Renju_CalValue(unsigned long connect_result)
{
	CALOG_ENT();	//added by calog

	static const mode_value[]=
	{/* 0   1   2   3   4   5   6   7   8   9   a   b   c   d   */
		19, 15,  9,  9,  9, 15,  7,  4,  4,  7,  3,  2,  3, 15    // level value of o+o+o+o
	};
	unsigned char mode, multi;
	unsigned short int ret_value;
	
	ret_value = multi = 0;
	if(connect_result&0x80000000l)
	{
		mode =(unsigned char) (connect_result>>12)&0xf;
		ret_value += mode_value[mode];
		switch((connect_result>>22)&3)
		{
		case 3: ret_value |= 0x8000; multi++; break;
		case 2: ret_value |= 0x4000; multi++; break;
		case 1: if( chess_object->level != 1 && chess_object->particular == 1 )
				{ //handle particular case:o+o+o+o
					ret_value |= 0x2000;
					chess_object->particular = 0;
				}
			multi ++;
			break;
		default:
			if(mode<=5) multi++;
		}
	}
	
	if(connect_result&0x40000000l)
	{
		mode = (unsigned char)(connect_result>>8)&0xf;
		ret_value += mode_value[mode];
		switch((connect_result>>20)&3)
		{
		case 3: ret_value |= 0x8000; multi++; break;
		case 2: ret_value |= 0x4000; multi++; break;
		case 1: if( chess_object->level != 1 && chess_object->particular == 1 )
				{ //handle particular case:o+o+o+o
					ret_value |= 0x2000;            //
					chess_object->particular = 0;
				}
			multi ++;
			break;
		default:
			if(mode<=5) multi++;
		}
	}
	
	if(connect_result&0x20000000l)

⌨️ 快捷键说明

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