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

📄 renju.c

📁 嵌入式linux下面五子棋游戏代码
💻 C
📖 第 1 页 / 共 3 页
字号:
//---------- DO NOT EDIT or MOVE THIS LINE: added by calog at Mon Aug 09 15:57:09 2004
	//added by calog
//uncoment the following line to close call log	//added by calog
//define NO_CALOG	//added by calog
#include "calog.h"	//added by calog
/*
 * Copyright (c) 2003,2004 Jing Cheng Tong Wireless Technology (ShenZhen) Ltd.
 * All rights reserved.
 * 
 * Filename: Renju.c
 * Description: <FIXME>
 * 
 * Author: Liu Zhi <Zhi.LIU@JCTMobile.COM>
 * Department: R&D Center, JCTCD
 * Creation: <UNKNOWN>
 *
 * History:  2004/05/26,YZB  add copyright and file header.
 *           2004/05/26,XXX  
 */

/* $Id: Renju.c,v 1.6 2004/08/09 08:27:01 hulei Exp $ */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Renju.h"



/*
*
*            Data struct and variable define area
*
*
*/


// 评分
typedef struct
{
	char step,min,sec,flag;
	char name[12];
}	_GRADE;

/*
typedef struct _CHESS_POINT{
short int x;						// 棋子x坐标
short int y;						// 棋子y坐标
} CHESS_POINT;
*/
CHESS_POINT lineChessman[5];

CHESS_OBJECT  *chess_object;
_GRADE grade[4];

char           xpos,ypos;		    	  			// 棋盘坐标
unsigned char  global_who_go = MAN;		  		// men first go
unsigned char  global_game_level = INIT_LEVEL;   // set game level to 1
unsigned char  global_game_step  = 0;   // set game level to 1



static unsigned char Renju_ConnectSub(unsigned char left, unsigned char right, unsigned short int  board_pattern);
unsigned short int Renju_CalValue(unsigned long connect_result);
static unsigned char Renju_MachinePutChess(unsigned short int *put_x, unsigned short int *put_y, char who);
void Renju_MachineGo(short int *mX, short int *mY);
short int Renju_ManGo( char manX, char manY);
short int Renju_JudgeWin(int currX, int currY);
short int Renju_InitChess(void);
static unsigned long Renju_Connect(unsigned short int  x, unsigned short int  y, unsigned char who);
int Renju_rndom(int max);
void Renju_CalculateLineChessman(void);
short int Renju_CanGo(int manX, int manY);


/*
* description :Initialization
*               initialization chess_object
initialization repent queue 
* in para     :CHESS_OBJECT *chess_object
* out para    :no
* return      :<0 fail,>=0 success

*/

short int Renju_InitChess(void)
{
	CALOG_ENT();	//added by calog

	short int i, j;
	short int ret = 0;
	
	chess_object = (CHESS_OBJECT*)malloc((size_t)sizeof(CHESS_OBJECT));
	if (chess_object == NULL) ret = -1;
	
	// initialization man[][],attack_value[][]and defence_value[][] array
	for(i = 0; i < CHESS_LEN_Y; i++)
		for(j = 0; j < CHESS_LEN_X; j++)
		{
			chess_object->map[i][j] = 0;
			chess_object->attack_value[i][j] = 0;
			chess_object->defence_value[i][j] = 0;
		}
		
		for(i = 0; i < 5; i++) 
		{
			lineChessman[i].x = 0;
			lineChessman[i].y = 0;
		}
		
		global_game_step  = 0;
		chess_object->particular = 0;
		chess_object->level = global_game_level;
		chess_object->begin_flag = global_who_go;
		chess_object->who_go = global_who_go;
		chess_object->who_win = INIT_WIN;
		chess_object->put_chess_counter = 0;
		chess_object->old_who_go = MAN;
		chess_object->msgflag = 0;
		chess_object->step = global_game_step;
		chess_object->mmin = 0;
		chess_object->sec = -1;
		chess_object->oldsec = -1;
		chess_object->dir = 0;
		
		return ret;
}



/*
* description :Initialization
*               initialization chess_object
initialization repent queue 
* in para     :CHESS_OBJECT *chess_object
* out para    :no
* return      :<0 fail,>=0 success

*/

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

	
	free(chess_object);
	
	return;
	
}




/*
* Description:Judge result。whether who go a step,must call this function to judge.
* in para    :CHESS_OBJECT *chess_object
* out pata   :
*              char currX : current point x coordinate 
*              char currY : current point y coordinate
* return     :
*           0:not finish
*           1:man win
*           2:machine win
*           3:tie
*           result save in chess_object->who_win too.
*/
short int Renju_JudgeWin(int currX, int currY)
{
//	printf(" come in Renju_JudgeWin function\n");
	CALOG_ENT();	//added by calog

	char row, col ,i ,j, n;
	char who;
	
	// 判断落子是否已满,如满则为和棋
	if( chess_object->put_chess_counter > (CHESS_LEN_X)*(CHESS_LEN_Y)/2 )
	{ chess_object->who_win = ALL_NO_WIN; return ALL_NO_WIN;}
	
	for(row = 0; row < CHESS_LEN_Y; row++)
		for(col = 0; col < CHESS_LEN_X; col++)
		{
			if((who = chess_object->map[row][col]) != 0)
			{
				// left and right inspect
				for(n=0, j=-1; col+j>=0; j--)
					if(chess_object->map[row][col+j]==who)	n++; else break;
					for(j=1; col+j<CHESS_LEN_X; j++)
						if(chess_object->map[row][col+j]==who) n++; else break;
						if(n >= 4)
						{ 
							chess_object->who_win = who;
							chess_object->wx = currX;
							chess_object->wy = currY; 
							chess_object->dir=1;
							Renju_CalculateLineChessman();
							return who;
						}
						
						// up and down inspect
						for(n=0, i=-1; row+i>=0; i--)
							if(chess_object->map[row+i][col]==who) n++; else break;
							for(i=1; row+i<CHESS_LEN_Y; i++)
								if(chess_object->map[row+i][col]==who) n++; else break;
								if(n >= 4)
								{ 
									chess_object->who_win = who;
									chess_object->wx = currX;
									chess_object->wy = currY; 
									chess_object->dir = 2;
									Renju_CalculateLineChessman();
									return who;
								}
								
								// left_up and right_down inspect
								for(n=0, i=-1, j=-1; row+i>=0 && col+j>=0; i--, j--)
									if(chess_object->map[row+i][col+j]==who) n++; else break;
									for(i=1, j=1; row+i<CHESS_LEN_Y && col+j<CHESS_LEN_X; i++, j++)
										if(chess_object->map[row+i][col+j]==who) n++; else break;
										if(n >= 4)
										{ 
											chess_object->who_win = who;
											chess_object->wx = currX;
											chess_object->wy = currY; 
											chess_object->dir = 3;
											Renju_CalculateLineChessman();
											return who;
										}
										
										// left_down and right_up inspect
										for(n=0, i=1, j=-1; row+i<CHESS_LEN_Y && col+j>=0; i++, j--)
											if(chess_object->map[row+i][col+j]==who) n++; else break;
											for(i=-1, j=1; row+i>=0 && col+j<CHESS_LEN_X; i--, j++)
												if(chess_object->map[row+i][col+j]==who) n++; else break;
												if(n >= 4)
												{ 
													chess_object->who_win = who;
													chess_object->wx = currX;
													chess_object->wy = currY; 
													chess_object->dir = 4;
													Renju_CalculateLineChessman();
													return who;
												}
			}
		}
		return 0;
}


/*
* Description:calculate coordinate of the machine go,and put it to para (mX,mY),
*              and save chess_object->x,chess_object->y too.
*
* in para    :
* 			    CHESS_OBJECT *chess_object
* out para   :
*              short int mX:x coordinate
*              short int mY:y coordinate
* return     :
*              < 0  fail
*              >= 0 success
*/
short int Renju_ManGo( char manX, char manY)
{
	CALOG_ENT();	//added by calog

	short int ret = -1;
	
	if(chess_object->map[manY][manX] == 0) // the point is not MAN or MACHINE
	{
		chess_object->map[manY][manX] = MAN;
		chess_object->who_go = MACHINE;
		chess_object->step++;
		global_game_step++;
		ret = 0;
	}
	
	return ret;	
}

short int Renju_CanGo(int manX, int manY)
{
	CALOG_ENT();	//added by calog

	return chess_object->map[manY][manX];
}



/*
* Description:calculate coordinate of the machine go,and put it to para (mX,mY),
*              and save chess_object->x,chess_object->y too.
*
* in para    :
* 			    CHESS_OBJECT *chess_object
* out para   :
*              short int mX:x coordinate
*              short int mY:y coordinate
* return     :no
*/
void Renju_MachineGo(short int *mX, short int *mY)
{
	CALOG_ENT();	//added by calog

	short int  x, y, xx, yy, temp_x, temp_xx, temp_y, temp_yy;
	unsigned char put_status = 1;
	unsigned char who = MACHINE;
	short int  step, step_deep = 0;
	CHESS_POINT save_xy[DEEP+1];
	short int  many = 0;
	CHESS_POINT temp_xy[MANY];
	
	if(chess_object->who_go!=MACHINE) return;
	
	if( chess_object->begin_flag == MAN )
	{
		// the machine's first step if man first go in this game
loop_again:
	switch(Renju_rndom(8))
	{
	case 0:
		x = (CHESS_LEN_X-1)/2 - 1; y = (CHESS_LEN_Y-1)/2;
		if(chess_object->map[y][x] != 0) goto loop_again;
		break;
	case 1:
		x = (CHESS_LEN_X-1)/2 - 1; y = (CHESS_LEN_Y-1)/2 - 1;
		if(chess_object->map[y][x] != 0) goto loop_again;
		break;
	case 2:
		x = (CHESS_LEN_X-1)/2    ; y = (CHESS_LEN_Y-1)/2 - 1;
		if(chess_object->map[y][x] != 0) goto loop_again;
		break;
	case 3:
		x = (CHESS_LEN_X-1)/2 + 1; y = (CHESS_LEN_Y-1)/2 - 1;
		if(chess_object->map[y][x] != 0) goto loop_again;
		break;
	case 4:
		x = (CHESS_LEN_X-1)/2 + 1; y = (CHESS_LEN_Y-1)/2;
		if(chess_object->map[y][x] != 0) goto loop_again;
		break;
	case 5:
		x = (CHESS_LEN_X-1)/2 + 1; y = (CHESS_LEN_Y-1)/2 + 1;
		if(chess_object->map[y][x] != 0) goto loop_again;
		break;
	case 6:
		x = (CHESS_LEN_X-1)/2    ; y = (CHESS_LEN_Y-1)/2 + 1;
		if(chess_object->map[y][x] != 0) goto loop_again;
		break;
	case 7:
		x = (CHESS_LEN_X-1)/2 - 1; y = (CHESS_LEN_Y-1)/2 + 1;
		if(chess_object->map[y][x] != 0) goto loop_again;
		break;
	}
	
	chess_object->begin_flag = 0;
	}
	else if( chess_object->begin_flag == MACHINE) // if machin first_go then...
	{
		// the machine's first step if machine first go in this game
		x = (CHESS_LEN_X-1)/2;
		y = (CHESS_LEN_Y-1)/2;
		chess_object->begin_flag = 0;

⌨️ 快捷键说明

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