五子棋人机交互
程序分两部分:1。核心程序 2。windows图形界面程序
此处只介绍核心程序部分。下面,我们边看边侃。
//========定义文件部分=============================
#define MAX_LENGTH 19 //棋盘格数
#define COMPUTER 1 //计算机棋子
#define PLAYER 2 //玩家棋子
int qp[MAX_LENGTH][MAX_LENGTH]; //定义棋盘
int iswin; //输赢标记,1=计算机胜,2=玩家胜,0=未分胜负;
#define RandInt(n) (rand()%n) //随机数宏
struct LIST
{
int id;
struct LIST *next;
}*PList=NULL,*CList=NULL; //定义链表,Plist为玩家棋局链表指针,用于阻击,Clist为计算机棋局链表指针,用于攻击
//=========核心程序代码部分=========================
//本函数将数据add添加到链表中,并将数据按从大到小排序
//add低位为棋盘位置,高位代表该位置在当前棋局中的优先级
static int AddList(struct LIST **List,int add)
{
struct LIST *tmp=*List;
struct LIST **temp=List;
int atemp=add>>16;
int id;
struct LIST *newlist=malloc(sizeof(*newlist));
if(!newlist)
return 1;
while(tmp)
{
id=(tmp->id)>>16;
if(id<=atemp)
break;
if(id>atemp)
{
temp=&(tmp->next);
tmp=tmp->next;
}
}
newlist->id=add;
newlist->next=tmp;
*temp=newlist;
return 0;
}
//函数获得指定链中最大优先级的值
static int GetMax(struct LIST *List)
{
if(List)
return (List->id>>16);
return 0;
}
//函数获得指定链中的链首数据
static int GetLast(struct LIST **List)
{
if(*List)
{
int ret;
struct LIST *temp;
ret=((*List)->id&0xffff);//取低字节棋盘位置数据
temp=*List;
*List=(*List)->next;
free(temp);
return ret;
}
return 0;
}
#define DestroyList(l) while(GetLast(&l)) //宏——销毁链表
//函数探测参数tmp指定的棋盘位置的优先级并加入相应的链表中