//五子棋核心算法.h
///
#define NUM 15
#define MAXPRI 500
#define DEFENPRI 90
#define NULL 0
///优先级定义
#define PRI_0 0
#define PRI_1 1
#define PRI_2 2
#define PRI_3 3
#define PRI_4 4
#define PRI_5 5
/
#define COMCHESSMAN 1
#define OPPCHESSMAN 2
#define NOCHESSMAN 0
/
#define COMWIN 1
#define OPPWIN 2
#define NOWIN 0
//#include "stdafx.h"
//#include "StdAfx.h"
//#include <alloc.h> //使用free函数
#include <windows.h>
#include<iostream>
#include "IPHlpApi.h"
#include "Iphlpapi.h"
#include "Iphlpapi.h"
#include <string.h>
#include <stdio.h>
#include<cstring>
#include "resource.h"
#include<atlstr.h>//使用cstring类用到
#pragma comment(lib,"Iphlpapi.lib")
using namespace std;
typedef struct
{
int x; //棋盘的x坐标
int y; //棋盘的y坐标
}Point;
typedef struct po
{
Point point;
struct po *next;
}*LPoint; //某一个优先级上的所有下一步可能下法
typedef struct
{
LPoint PRI[MAXPRI];
}NextPri; //在优先级0~MAXPRI-1上的所有下一步可能下法
class Computer
{
private:
//
Point ComExistChessman[(NUM*NUM)/2+1]; //棋盘上所有电脑已有棋子坐标
NextPri ComNext; //电脑一方下一步的所有可能下法,按优先级存储(用于计算下一步棋的进攻下法)
//
Point OppExistChessman[(NUM*NUM)/2+1];//棋盘上所有对手已有棋子坐标
NextPri OppNext; //对方下一步的所有可能下法,按优先级存储(用于计算防御下法)
//
public:
int ComCount;//棋盘上所有电脑已有棋子个数
int OppCount;//棋盘上所有对手已有棋子个数
int CoordinateR[NUM][NUM]; //在图形上面显示的所有棋子坐标
int CoordinateSNum[NUM][NUM];//各个棋子的先后序号
int Result;//表示胜利的为哪一方。初始值为0,表示比赛正在进行;如果电脑胜利者值为1;对手胜利则为2;
Computer();//构造函数,初始化该类的各数据成员
bool JudgeCoordinate(int x,int y);//判断一个坐标是否已经被占用(没有占用返回true)
int Max(int a,int b,int c,int d,int &count);//函数返回4各数的最大值,并计算出有几个值并列为最大值
int RandNum(int min,int max);//生成一个min到max的随机数(不包括max和min)
bool DeleCoordinateR();//清空图形上面显示的所有棋子坐标
///computer核心算法///
bool AttackNextPri();//计算computer的最优下一步棋子,并添加到ComNext数据成员中
bool AddComNewCh();//添加确定最优的棋子为下一步,并且添加到ComExistChessman中
bool ContinuumComNum(int x,int y,int &left,int &right,
int &top,int &bottom,int &leftT,int &leftB,int &rightT,int &rightB);//计算一个点在各个方向上各有几个点相连
bool AddToComNext(int x,int y,int pri); //添加新的棋子到ComNext成员
bool DeleComNext();//清空ComNext数据成员中的所有内容
bool DeleComExistChessman();//清空ComExistChessman的所有内容
bool JudgeComCoordinate(int x,int y);//判断一个坐标是否已经被Computer的棋子所占用(没有占用返回true)
int GetComCount();//返回ComCount数据成员的值
bool JudgeComWin();//判断是computer是否已经胜利 胜利则返回true
///computer核心算法///
///Opp核心算法///
bool AddNewChessmanOpp(int x,int y);//添加新的棋子到OppExistChessman数据成员
bool AddDefend();//在continuumChessmanOpp中确定防御下一步棋
bool ContinuumOppNum(int x,int y,int &left,int &right,
int &top,int &bottom,int &leftT,int &leftB,int &rightT,int &rightB);//计算一个点在各个方向上各有几个Opp的点相连
bool JudgeOppCoordinate(int x,int y);//判断一个坐标是否已经被Opp的棋子所占用(没有占用返回true)
bool DeleOppExistChessman();// 清空OppExistChessman的所有内容
int GetOppCount();//返回OppCount数据成员的值
bool JudgeOppWin();//判断是Opp是否已经胜利 胜利则返回true
///Opp核心算法///
/*
int GetResult();
bool EnsureNext();//结合进攻下一步和防御下一步的所有情况,确定真正的下一步坐标,并且保存到RealNext数据成员中//
bool JudgeComNext(int x,int y);//判断一个坐标是否已经被加入到ComNext里面
bool AddToContinuumChessman(int x,int y);//添加一个点到ContinuumChessman数据成员中
bool DeleConCheTwo(TwoChessman two);//删除continuumChessman中连续两个棋子的一个节点
bool DeleConCheThree(ThreeChessman three); //删除continuumChessman中连续三个棋子的一个节点
///computer核心算法///
bool AttackNextPri0(); //计算出进攻下一步棋的所有可能情况,并加入到ComNext数据成员中的第0优先级
bool RecoveryNext();//计算出防御下一步棋的所有可能情况,并加入到OppNext数据成员中///
bool AttackNextPri1(); //在ComNext数据成员的第0优先级中提取更高的优先级坐标,并加入到ComNext数据成员中的更高优先级
bool AddNewChessman(); //确定并添加棋子到ComExistChessman和continuumChessman
///computer核心算法///
///Opp核心算法///
bool AddToContinuumChessmanOpp(int x,int y);//添加一个点到continuumChessmanOpp数据成员中
bool DeleConCheTwoOpp(TwoChessman two);//删除continuumChessmanOpp中连续两个棋子的一个节点
bool DeleConCheThreeOpp(ThreeChessman three); //删除continuumChessmanOpp中连续三个棋子的一个节点
bool AddDefenNewCh();//在continuumChessmanOpp中确定防御下一步棋
///Opp核心算法///
*/
};
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
PMIB_TCPTABLE MyGetTcpTable(BOOL bOrder);
//新核心.cpp
#include"五子棋核心算法.h"
/
// AddNewChessmanOpp构造函数定义——开始 //
// 添加新的棋子到OppExistChessman数据成员 //
bool Computer::AddNewChessmanOpp(int x,int y)
{
int tempX=2*NUM,tempY=2*NUM;
tempX=x;
tempY=y;
//if(JudgeCoordinate(tempX,tempY))//检查该点是否已经被占用
// {
OppExistChessman[OppCount].x=tempX;
OppExistChessman[OppCount].y=tempY;
OppCount++;
// }
return true;
}
/
// AddNewChessmanOpp构造函数定义——结束 //
// 添加新的棋子到OppExistChessman数据成员 //
//
// AttackNextPri函数定义——开始 //
// 计算computer的最优下一步棋子,并添加到ComNext数据成员中 //
/
bool Computer::AttackNextPri()
{
int left,right,top,bottom,leftT,leftB,rightT,rightB,maxC=0,count=0,pri=0;
if(ComCount==0)//如果这是第一步棋子
{
for(int i=0;i<NUM;i++)
for(int j=0;j<NUM;j++)
{
ContinuumComNum(i,j,left,right,top,bottom,leftT,leftB,rightT,rightB);//计算一个点在各个方向上各有几个点相连
if(JudgeCoordinate(i,j))
{
ContinuumOppNum(i,j,left,right,top,bottom,leftT,leftB,rightT,rightB);//计算一个点在各个方向上各有几个点相连
maxC=Max((left+right)*9,(top+bottom)*9,(leftT+rightB)*9,(leftB+rightT)*9,count);
pri=maxC+count; //计算该点的优先级
if(pri>0)//要优先级有所提高的才运行一下函数
AddToComNext(i,j,pri); //将该点加入到指定的ComNext的优先级队列中
}//if
}//for
}//if
else
{
for(int i=0;i<NUM;i++)
for(int j=0;j<NUM;j++)
{
ContinuumComNum(i,j,left,right,top,bottom,leftT,leftB,rightT,rightB);//计算一个点在各个方向上各有几个点相连
if(JudgeCoordinate(i,j))
{
ContinuumComNum(i,j,left,right,top,bottom,leftT,leftB,rightT,rightB);//计算一个点在各个方向上各有几个点相连
/*
if((left+right)==4||(top+bottom)==4||(leftT+rightB)==4||(leftB+rightT)==4) //判断是否已经有连续五个棋子
{
Result=COMWIN;
CoordinateR[i][j]=COMCHESSMAN;
CoordinateSNum[i][j]=ComCount+OppCount+1;
return true;//结束程序的函数
}
*/
maxC=Max((left+right)*9,(top+bottom)*9,(leftT+rightB)*9,(leftB+rightT)*9,count);
pri=maxC+count; //计算该点的优先级
if(pri>0)//要优先级有所提高的才运行一下函数
AddToComNext(i,j,pri); //将该点加入到指定的ComNext的优先级队列中
}
}
}
return true;
}
//
// AttackNextPri函数定义——结束 //
// 计算computer的最优下一步棋子,并添加到ComNext数据成员中 //
/
//
// AddComNewCh函数定义——开始 //
// 添加确定最优的棋子为下一步,并且添加到ComExistChessman中 //
/
bool Computer::AddComNewCh()
{
if(Result==1)return true;
NextPri temp=ComNext;
for(int i=MAXPRI-1;i>=0;i--)
{
if(temp.PRI[i]->next!=NULL)
{
ComExistChessman[ComCount]=temp.PRI[i]->next->point; //将新的点加入到ComExistChessman成员
CoordinateR[temp.PRI[i]->next->point.x][temp.PRI[i]->next->point.y]=COMCHESSMAN;//在棋盘中添加该棋子为计算机的棋子
ComCount++;
CoordinateSNum[temp.PRI[i]->next->point.x][temp.PRI[i]->next->point.y]= ComCount+OppCount;
if(Result!=0)return true;
LPoint TLPoint=temp.PRI[i]->next;
temp.PRI[i]->next=temp.PRI[i]->next->next;//在ComNext中删除刚刚加入的这个点
free(TLPoint);
DeleComNext();//清空ComNext的函数
return true;//该点加入后结束此函数
}
}
return true;
}
//
// AddComNewCh函数定义——结束 //
// 添加确定最优的棋子为下一步,并且添加到ComExistChessman中 //
/
//
// AddDefend函数定义——开始 //
// 在continuumChessmanOpp中确定防御下一步棋 //
/
bool Computer::AddDefend()
{
int left,right,top,bottom,leftT,leftB,rightT,rightB,maxC=0,count=0,pri=0;
for(int i=0;i<NUM;i++)
for(int j=0;j<NUM;j++)
{
/*
///判断Opp是否已经赢了-开始/
ContinuumOppNum(i,j,left,right,top,bottom,leftT,leftB,rightT,rightB);//计算一个点在各个方向上各有几个点相连
if((left+right)==5||(top+bottom)==5||(leftT+rightB)==5||(leftB+rightT)==5) //判断是否已经有连续五个棋子
{
Result=OPPWIN;
return true;//结束程序的函数
}
///判断Opp是否已经赢了-结束/
*/
if(JudgeCoordinate(i,j))
{
ContinuumOppNum(i,j,left,right,top,bottom,leftT,leftB,rightT,rightB);//计算一个点在各个方向上各有几个点相连
maxC=Max((left+right)*9,(top+bottom)*9,(leftT+rightB)*9,(leftB+rightT)*9,count);
pri=maxC+count; //计算该点的优先级
if(pri>0&&maxC>=26)//要优先级有所提高的才运行一下函数
AddToComNext(i,j,pri+50); //将该点加入到指定的ComNext的优先级队列中
}
}
return true;
}
//
// AddDefend函数定义——结束 //
// 在continuumChessmanOpp中确定防御下一步棋 //
/
//
// bool ContinuumComNum开始 //
// 计算一个点在各个方向上各有几个点相连 //
/
bool Computer::ContinuumComNum(int x,int y,int &left,int &right,
int &top,int &bottom,int &leftT,int &leftB,int &rightT,int &rightB)
{
int tempX,tempY;
left=0;
right=0;
top=0;
bottom=0;
leftT=0;
leftB=0;
rightT=0;
rightB=0;
tempX=x;
tempY=y;
if(x>=0&&x<NUM)
{
while(tempX>0&&!JudgeComCoordinate(--tempX,tempY))
left++;//tempX一定要放在!JudgeComCoordinate(--tempX,tempY)的前面
tempX=x;
tempY=y;
while(tempX<NUM&&!JudgeComCoordinate(++tempX,tempY))
right++;
tempX=x;
tempY=y;
while(tempY>0&&!JudgeComCoordinate(tempX,--tempY))
top++;
tempX=x;
tempY=y;
while(tempY<NUM&&!JudgeComCoordinate(tempX,++tempY))
bottom++;
tempX=x;
tempY=y;
while(tempX>0&&tempY>0&&!JudgeComCoordinate(--tempX,--tempY))
leftT++;
tempX=x;
tempY=y;
while(tempX<NUM&&tempY<NUM&&!JudgeComCoordinate(++tempX,++tempY))
rightB++;
tempX=x;
tempY=y;
while(tempX>0&&tempY<NUM&&!JudgeComCoordinate(--tempX,++tempY))
leftB++;
tempX=x;
tempY=y;
while(tempX<NUM&&tempY>0&&!JudgeComCoordinate(++tempX,--tempY))
rightT++;
}
return true;
}
// bool ContinuumComNum结束 //
// 计算一个点在各个方向上各有几个点相连 //
///
//
// ContinuumOppNum函数定义——开始 //
// 计算一个点在各个方向上各有几个Opp的点相连 //
/
bool Computer::ContinuumOppNum(int x,int y,int &left,int &right,
int &top,int &bottom,int &leftT,int &leftB,int &rightT,int &rightB)
{
int tempX,tempY;
left=0;
right=0;
top=0;
bottom=0;
leftT=0;
leftB=0;
rightT=0;
rightB=0;
tempX=x;
tempY=y;
if(x>=0&&x<NUM)
{
while(tempX>0&&!JudgeOppCoordinate(--tempX,tempY))
left++;//tempX一定要放在!JudgeOppCoordinate(--tempX,tempY)的前面
tempX=x;
tempY=y;
while(tempX<NUM&&!JudgeOppCoordinate(++tempX,tempY))
right++;
tempX=x;
tempY=y;
while(tempY>0&&!JudgeOppCoordinate(tempX,--tempY))
top++;
tempX=x;
tempY=y;
while(tempY<NUM&&!JudgeOppCoordinate(tempX,++tempY))
bottom++;
tempX=x;
tempY=y;
while(tempX>0&&tempY>0&&!JudgeOppCoordinate(--tempX,--tempY))
leftT++;
tempX=x;
tempY=y;
while(tempX<NUM&&tempY<NUM&&!JudgeOppCoordinate(++tempX,++tempY))
rightB++;
tempX=x;
tempY=y;
while(tempX>0&&tempY<NUM&&!JudgeOppCoordinate(--tempX,++tempY))
leftB++;
tempX=x;
tempY=y;
while(tempX<NUM&&tempY>0&&!JudgeOppCoordinate(++tempX,--tempY))
rightT++;
}
return true;
}
//
// ContinuumOppNum函数定义——结束 //
// 计算一个点在各个方向上各有几个Opp的点相连 //
/
// JudgeCoordinate函数定义开始 //
// 判断一个坐标是否已经被占用 //
///
bool Computer::JudgeCoordinate(int x,int y)
{
int temp=0;
for(int i=0;i<ComCount;i++)
{
if(x==ComExistChessman[i].x&&y==ComExistChessman[i].y)
temp++;
}
for(int j=0;j<OppCount;j++)
{
if(x==OppExistChessman[j].x&&y==OppExistChessman[j].y)
temp++;
}
if(temp==0)
return true;
else
return false;
}//如果该点不存在着返回真
// JudgeCoordinate函数定义结束 //
// 判断一个坐标是否已经被占用 //
///
///
// AddToComNext函数定义开始 //
// 添加新的棋子到ComNext成员 //
//
bool Computer::AddToComNext(int x,int y,int pri)
{
NextPri temp=ComNext;
while(temp.PRI[pri]->next!=NULL)
temp.PRI[pri]=temp.PRI[pri]->next;
LPoint AddPoint=(LPoint)malloc(sizeof(struct po));
AddPoint->point.x=x;
AddPoint->point.y=y;
AddPoint->next=NULL;
temp.PRI[pri]->next=AddPoint;
return true;
}
/
// AddToComNext函数定义结束 //
// 添加新的棋子到ComNext成员 //
//
// MAX开始 //
// 函数返回4各数的最大值, //
// 并计算出有几个值并列为最大值 //
//
int Computer::Max(int a,int b,int c,int d,int &count)
{
count=-1;
int temp=a;
if(temp<b)temp=b;
if(temp<c)temp=c;
if(temp<d)temp=d;
if(a==temp)count++;
if(b==temp)count++;
if(c==temp)count++;
if(d==temp)count++;
if(a==0&&b==0&&c==0&&d==0)count=0;
return temp;
}
//
// MAX结束 //
// 函数返回4各数的最大值, //
// 并计算出有几个值并列为最大值 //
//
/
// DeleComNext构造函数定义——开始 //
// 清空ComNext数据成员中的所有内容 //
bool Computer::DeleComNext()
{
NextPri TComN=ComNext;
for(int i=0;i<MAXPRI;i++)
{
while(TComN.PRI[i]->next!=NULL)
{
LPoint temp=TComN.PRI[i]->next;
TComN.PRI[i]->next=TComN.PRI[i]->next->next;
free(temp);
}
}
return true;
}
/
// DeleComNext构造函数定义——结束 //
// 清空ComNext数据成员中的所有内容 //
//
// JudgeComCoordinate函数定义——开始 //
//判断一个坐标是否已经被Computer的棋子所占用(没有占用返回true)//
/
bool Computer::JudgeComCoordinate(int x,int y)
{
int temp=0;
for(int i=0;i<ComCount;i++)
{
if(x==ComExistChessman[i].x&&y==ComExistChessman[i].y)
temp++;
}
if(temp==0)
return true;
else
return false;
}
//
// JudgeComCoordinate函数定义——结束 //
//判断一个坐标是否已经被Computer的棋子所占用(没有占用返回true)//
/
//
// JudgeOppCoordinate函数定义——开始 //
// 判断一个坐标是否已经被Opp的棋子所占用(没有占用返回true) //
/
bool Computer::JudgeOppCoordinate(int x,int y)
{
int temp=0;
for(int i=0;i<OppCount;i++)
{
if(x==OppExistChessman[i].x&&y==OppExistChessman[i].y)
temp++;
}
if(temp==0)
return true;
else
return false;
}
//
// JudgeOppCoordinate函数定义——结束 //
// 判断一个坐标是否已经被Opp的棋子所占用(没有占用返回true) //
/
/
// Computer构造函数定义开始 //
// 构造函数,初始化该类的各数据成员 //
Computer::Computer()
{
///初始化OppExistChessman成员-开始/
for(int i=0;i<(NUM*NUM)/2+1;i++)
{
OppExistChessman[i].x=2*NUM; //表示这个值一定不会是坐标
OppExistChessman[i].y=2*NUM;
}
///初始化OppExistChessman成员-结束/
///初始化ComCount成员-开始/
ComCount=0;
///初始化ComCount成员-结束/
///初始化ComNext成员-开始/
for(int i=0;i<MAXPRI;i++)
{
ComNext.PRI[i]=(LPoint)malloc(sizeof(struct po));//分配该链表的头结点
ComNext.PRI[i]->point.x=3*NUM;
ComNext.PRI[i]->point.y=3*NUM;
ComNext.PRI[i]->next=NULL;
}
///初始化ComNext成员-结束/
//**********************************************************//
///初始化ComExistChessman成员-开始/
for(int i=0;i<(NUM*NUM)/2+1;i++)
{
ComExistChessman[i].x=2*NUM;
ComExistChessman[i].y=2*NUM;
}
///初始化OppCount成员-开始/
OppCount=0;
///初始化OppCount成员-结束/
///初始化OppNext成员-开始/
for(int i=0;i<MAXPRI;i++)
{
OppNext.PRI[i]=(LPoint)malloc(sizeof(struct po));//分配该链表的头结点
OppNext.PRI[i]->point.x=3*NUM;
OppNext.PRI[i]->point.y=3*NUM;
OppNext.PRI[i]->next=NULL;
}
///初始化OppNext成员-结束/
///初始化Result成员-开始/
Result=0;
///初始化Result成员-结束/
for(int i=0;i<NUM;i++)
for(int j=0;j<NUM;j++)
{
CoordinateR[i][j]=NOCHESSMAN;
CoordinateSNum[i][j]=2*NUM;//各个棋子的先后序号
}
}
/
// Computer构造函数定义结束 //
// 构造函数,初始化该类的各数据成员 //
/
// RandNum构造函数定义-开始 //
// 生成一个min到max的随机数(不包括max和min) //
int Computer:: RandNum(int min,int max)
{
int temp;
temp=rand()%max;
if(temp<=min)temp+=min;
return temp;
}
/
// RandNum构造函数定义-结束 //
// 生成一个min到max的随机数(不包括max和min) //
/
// DeleComExistChessman构造函数定义-开始 //
// 清空ComExistChessman的所有内容 //
bool Computer::DeleComExistChessman()
{
for(int i=0;i<ComCount;i++)
{
ComExistChessman[i].x=2*NUM;
ComExistChessman[i].y=2*NUM;
}
ComCount=0;
return true;
}
/
// DeleComExistChessman构造函数定义-结束 //
// 清空ComExistChessman的所有内容 //
/
// DeleOppExistChessman构造函数定义-开始 //
// 清空OppExistChessman的所有内容 //
bool Computer::DeleOppExistChessman()
{
for(int i=0;i<OppCount;i++)
{
OppExistChessman[i].x=2*NUM;
OppExistChessman[i].y=2*NUM;
}
OppCount=0;
return true;
}
/
// DeleOppExistChessman构造函数定义-结束 //
// 清空OppExistChessman的所有内容 //
/
// DeleCoordinateR构造函数定义-开始 //
// 清空图形上面显示的所有棋子坐标 //
bool Computer::DeleCoordinateR()
{
for(int i=0;i<NUM;i++)
for(int j=0;j<NUM;j++)
CoordinateR[i][j]=NOCHESSMAN;
return true;
}
/
// DeleCoordinateR构造函数定义-结束 //
// 清空图形上面显示的所有棋子坐标 //
/
// GetComCount构造函数定义-开始 //
// 返回ComCount数据成员的值 //
int Computer::GetComCount()
{
return ComCount;
}
/
// GetComCount构造函数定义-结束 //
// 返回ComCount数据成员的值 //
/
// GetOppCount构造函数定义-开始 //
// 返回OppCount数据成员的值 //
int Computer::GetOppCount()
{
return OppCount;
}
/
// GetOppCount构造函数定义-结束 //
// 返回OppCount数据成员的值 //
/
// JudgeWin构造函数定义-开始 //
// 判断是computer是否已经胜利 胜利则返回true //
bool Computer::JudgeComWin()
{
int left,right,top,bottom,leftT,leftB,rightT,rightB;
for(int i=0;i<NUM;i++)
for(int j=0;j<NUM;j++)
{
ContinuumComNum(i,j,left,right,top,bottom,leftT,leftB,rightT,rightB);//计算一个点在各个方向上各有几个点相连
if(left==5||right==5||top==5||bottom==5||leftT==5||rightB==5||leftB==5||rightT==5) //判断是否已经有连续五个棋子
{
Result=COMWIN;
CoordinateSNum[i][j]=ComCount+OppCount;
return true;//结束程序的函数
}
}
return true;
}
/
// JudgeWin构造函数定义-结束 //
// 判断是computer是否已经胜利 胜利则返回true //
/
// JudgeOppWin构造函数定义-开始 //
// 判断是Opp是否已经胜利 胜利则返回true //
bool Computer::JudgeOppWin()
{
int left,right,top,bottom,leftT,leftB,rightT,rightB;
for(int i=0;i<NUM;i++)
for(int j=0;j<NUM;j++)
{
ContinuumOppNum(i,j,left,right,top,bottom,leftT,leftB,rightT,rightB);//计算一个点在各个方向上各有几个点相连
if(left==5||right==5||top==5||bottom==5||leftT==5||rightB==5||leftB==5||rightT==5) //判断是否已经有连续五个棋子
{
Result=OPPWIN;
CoordinateSNum[i][j]=ComCount+OppCount;
return true;//结束程序的函数
}
}
return false;
}
/
// JudgeOppWin构造函数定义-结束 //
// 判断是Opp是否已经胜利 胜利则返回true //
//核心算法.cpp
/*
#include"五子棋核心算法.h"
int Computer::GetResult()
{
return Result;
}
/
// AddDefenNewCh函数定义——开始 //
// 在continuumChessmanOpp中确定防御下一步棋 //
bool Computer::AddDefenNewCh()
{
ContinuumChessman temp;
temp.two=(TwoChessman)malloc(sizeof(struct two));
temp.three=(ThreeChessman)malloc(sizeof(struct three));
temp.four=(FourChessman)malloc(sizeof(struct four));
temp=continuumChessmanOpp;
/如果有四个点连续,且有一端有空余位置,则将该位置设为相应优先级——开始///
if(temp.CountFour!=0)
{
if(temp.four->FOUR[0].x==2*NUM&&temp.four->FOUR[0].y==2*NUM)//跳过头节点
temp.four=temp.four->next;
while(temp.four!=NULL)
{
int XChang=temp.four->FOUR[0].x-temp.four->FOUR[1].x;
int YChang=temp.four->FOUR[0].y-temp.four->FOUR[1].y;
if( JudgeCoordinate(temp.four->FOUR[0].x+XChang,temp.four->FOUR[0].y+YChang))
AddToComNext(temp.four->FOUR[0].x+XChang,temp.four->FOUR[0].y+YChang,DEFENPRI);
XChang=temp.four->FOUR[3].x-temp.four->FOUR[2].x;
YChang=temp.four->FOUR[3].y-temp.four->FOUR[2].y;
if( JudgeCoordinate(temp.four->FOUR[3].x+XChang,temp.four->FOUR[3].y+YChang))
AddToComNext(temp.four->FOUR[3].x+XChang,temp.four->FOUR[3].y+YChang,DEFENPRI);
temp.four=temp.four->next;
}
}
/如果有四个点连续,且有一端有空余位置,则将该位置设为相应优先级——结束///
/如果有三个点连续,且两端都有空余位置,则将该位置设为相应优先级——开始///
temp=continuumChessmanOpp;
if(temp.CountThree!=0)
{
if(temp.three->THREE[0].x==2*NUM&&temp.three->THREE[0].y==2*NUM)//跳过头节点
temp.three=temp.three->next;
while(temp.three!=NULL)
{
int XChangA=temp.three->THREE[0].x-temp.three->THREE[1].x;
int YChangA=temp.three->THREE[0].y-temp.three->THREE[1].y;
int XChangB=temp.three->THREE[2].x-temp.three->THREE[1].x;
int YChangB=temp.three->THREE[2].y-temp.three->THREE[1].y;
if(JudgeCoordinate(temp.three->THREE[0].x+XChangA,temp.three->THREE[0].y+YChangA)&&
JudgeCoordinate(temp.three->THREE[2].x+XChangB,temp.three->THREE[2].y+XChangB))
{
AddToComNext(temp.three->THREE[0].x+XChangA,temp.three->THREE[0].y+YChangA,DEFENPRI+1);
AddToComNext(temp.three->THREE[2].x+XChangB,temp.three->THREE[2].y+YChangB,DEFENPRI+1);
}
temp.three=temp.three->next;
}//while
}
/如果有三个点连续,且两端都有空余位置,则将该位置设为相应优先级——结束///
/如果存在一个点使得两组或多组三个连续点产生,且这多组中至少有两组的两端都有空余位置,则将该位置设为相应优先级——开始///
for(int a=0;a<NUM;a++)
for(int b=0;b<NUM;b++)
{
temp=continuumChessmanOpp;
int count=0;
if(JudgeCoordinate(a,b))
{
if(temp.two->TWO[0].x==2*NUM&&temp.two->TWO[0].y==2*NUM)//跳过头节点
temp.two=temp.two->next;
while(temp.two!=NULL)
{
int XChangA=temp.two->TWO[1].x-temp.two->TWO[0].x;
int YChangA=temp.two->TWO[1].y-temp.two->TWO[0].y;
int XChangB=temp.two->TWO[0].x-temp.two->TWO[1].x;
int YChangB=temp.two->TWO[0].y-temp.two->TWO[1].y;
if(XChangA==(temp.two->TWO[0].x-a)&& YChangA==(temp.two->TWO[0].y-b)&&
JudgeCoordinate(temp.two->TWO[0].x+XChangB,temp.two->TWO[0].y+XChangB)&&
JudgeCoordinate(temp.two->TWO[1].x+XChangA,temp.two->TWO[1].y+XChangA))
count++;
temp.two=temp.two->next;
}
}
if(count>=2)
AddToComNext(a,b,DEFENPRI+2);
}
/如果存在一个点使得两组或多组三个连续点产生,且这多组中至少有两组的两端都有空余位置,则将该位置设为相应优先级——结束///
return true;
}
/
// AddDefenNewCh函数定义——结束 //
// 在continuumChessmanOpp中确定防御下一步棋 //
/
// DeleConCheTwoOpp函数定义——开始 //
// 删除continuumChessmanOpp中连续两个棋子的一个节点 //
bool Computer::DeleConCheTwoOpp(TwoChessman two)
{
ContinuumChessman temp=continuumChessmanOpp;
if(temp.CountTwo==0)
{
std::cout<<"There is no continuumTwo data in continuumChessmanOpp/n";
return true;
}
while(temp.two!=NULL)
{
if(temp.two->next->TWO[0].x==two->TWO[0].x&&temp.two->next->TWO[0].y==two->TWO[0].y
&&temp.two->next->TWO[1].x==two->TWO[1].x&&temp.two->next->TWO[1].y==two->TWO[1].y)
{
TwoChessman TTwo=temp.two->next;
temp.two->next=temp.two->next->next;
free(TTwo);//释放temp.two->next所指的内存空间
continuumChessmanOpp.CountTwo--;
return true;
}
temp.two=temp.two->next;
}
return true;
}
/
// DeleConCheTwoOpp构造函数定义——开始 //
// 删除continuumChessmanOpp中连续两个棋子的一个节点 //
/
// DeleConCheThreeOpp构造函数定义——开始 //
// 删除continuumChessmanOpp中连续三个棋子的一个节点 //
bool Computer::DeleConCheThreeOpp(ThreeChessman three)
{
ContinuumChessman temp=continuumChessmanOpp;
if(temp.CountThree==0)
{
std::cout<<"There is no continuumThree data in continuumChessmanOpp/n";
return true;
}
while(temp.three!=NULL)
{
if(temp.three->next->THREE[0].x==three->THREE[0].x&&temp.three->next->THREE[0].y==three->THREE[0].y&&
temp.three->next->THREE[1].x==three->THREE[1].x&&temp.three->next->THREE[1].y==three->THREE[1].y&&
temp.three->next->THREE[2].x==three->THREE[2].x&&temp.three->next->THREE[2].y==three->THREE[2].y)
{
ThreeChessman TThree=temp.three->next;
temp.three->next=temp.three->next->next;
free(TThree);//释放temp.three->next所指的内存空间
continuumChessmanOpp.CountThree--;
return true;
}
temp.three=temp.three->next;
}
return true;
}
/
// DeleConCheThreeOpp构造函数定义——结束 //
// 删除continuumChessmanOpp中连续三个棋子的一个节点 //
/
// AddToContinuumChessmanOpp构造函数定义——开始 //
// 添加一个点到continuumChessmanOpp数据成员中 //
bool Computer::AddToContinuumChessmanOpp(int x,int y)
{
ContinuumChessman temp;
temp.two=(TwoChessman)malloc(sizeof(struct two));
temp.three=(ThreeChessman)malloc(sizeof(struct three));
temp.four=(FourChessman)malloc(sizeof(struct four));
temp=continuumChessmanOpp;
/*
/没有连续的点-开始
for(int i=0;i<OppCount;i++)
{
if(x-1==OppExistChessman[i].x&&y==OppExistChessman[i].y)
{
while(temp.two->next!=NULL) temp.two=temp.two->next;
TwoChessman tempTwo=(TwoChessman)malloc(sizeof(struct two));
tempTwo->TWO[0]=OppExistChessman[i];
tempTwo->TWO[1].x=x;
tempTwo->TWO[1].y=y;
tempTwo->next=NULL;
temp.two->next=tempTwo;
continuumChessmanOpp.CountTwo++;
}
}
for(int i=0;i<OppCount;i++)
{
if(x+1==OppExistChessman[i].x&&y==OppExistChessman[i].y)
{
while(temp.two->next!=NULL) temp.two=temp.two->next;
TwoChessman tempTwo=(TwoChessman)malloc(sizeof(struct two));
tempTwo->TWO[0].x=x;
tempTwo->TWO[0].y=y;
tempTwo->TWO[1]=OppExistChessman[i];
tempTwo->next=NULL;
temp.two->next=tempTwo;
continuumChessmanOpp.CountTwo++;
}
}
for(int i=0;i<OppCount;i++)
{
if(x==OppExistChessman[i].x&&y-1==OppExistChessman[i].y)
{
while(temp.two->next!=NULL) temp.two=temp.two->next;
TwoChessman tempTwo=(TwoChessman)malloc(sizeof(struct two));
tempTwo->TWO[0]=OppExistChessman[i];
tempTwo->TWO[1].x=x;
tempTwo->TWO[1].y=y;
tempTwo->next=NULL;
temp.two->next=tempTwo;
continuumChessmanOpp.CountTwo++;
}
}
for(int i=0;i<OppCount;i++)
{
if(x==OppExistChessman[i].x&&y+1==OppExistChessman[i].y)
{
while(temp.two->next!=NULL) temp.two=temp.two->next;
TwoChessman tempTwo=(TwoChessman)malloc(sizeof(struct two));
tempTwo->TWO[0].x=x;
tempTwo->TWO[0].y=y;
tempTwo->TWO[1]=OppExistChessman[i];
tempTwo->next=NULL;
temp.two->next=tempTwo;
continuumChessmanOpp.CountTwo++;
}
}
for(int i=0;i<OppCount;i++)
{
if(x-1==OppExistChessman[i].x&&y-1==OppExistChessman[i].y)
{
while(temp.two->next!=NULL) temp.two=temp.two->next;
TwoChessman tempTwo=(TwoChessman)malloc(sizeof(struct two));
tempTwo->TWO[0]=OppExistChessman[i];
tempTwo->TWO[1].x=x;
tempTwo->TWO[1].y=y;
tempTwo->next=NULL;
temp.two->next=tempTwo;
continuumChessmanOpp.CountTwo++;
}
}
for(int i=0;i<OppCount;i++)
{
if(x+1==OppExistChessman[i].x&&y+1==OppExistChessman[i].y)
{
while(temp.two->next!=NULL) temp.two=temp.two->next;
TwoChessman tempTwo=(TwoChessman)malloc(sizeof(struct two));
tempTwo->TWO[0].x=x;
tempTwo->TWO[0].y=y;
tempTwo->TWO[1]=OppExistChessman[i];
tempTwo->next=NULL;
temp.two->next=tempTwo;
continuumChessmanOpp.CountTwo++;
}
}
for(int i=0;i<OppCount;i++)
{
if(x+1==OppExistChessman[i].x&&y-1==OppExistChessman[i].y)
{
while(temp.two->next!=NULL) temp.two=temp.two->next;
TwoChessman tempTwo=(TwoChessman)malloc(sizeof(struct two));
tempTwo->TWO[0].x=x;
tempTwo->TWO[0].y=y;
tempTwo->TWO[1]=OppExistChessman[i];
tempTwo->next=NULL;
temp.two->next=tempTwo;
continuumChessmanOpp.CountTwo++;
}
}
for(int i=0;i<OppCount;i++)
{
if(x-1==OppExistChessman[i].x&&y+1==OppExistChessman[i].y)
{
while(temp.two->next!=NULL) temp.two=temp.two->next;
TwoChessman tempTwo=(TwoChessman)malloc(sizeof(struct two));
tempTwo->TWO[0].x=x;
tempTwo->TWO[0].y=y;
tempTwo->TWO[1]=OppExistChessman[i];
tempTwo->next=NULL;
temp.two->next=tempTwo;
continuumChessmanOpp.CountTwo++;
}
}
/没有连续的点-结束
///两个连续的棋子-开始
temp=continuumChessmanOpp;
TwoChessman DeleTwo[NUM*NUM];
int DeleCountTwo=0;
if(temp.two->TWO[0].x==2*NUM&&temp.two->TWO[0].y==2*NUM)//跳过头节点
temp.two=temp.two->next;
while(temp.two!=NULL)
{
if((temp.two->TWO[1].x-temp.two->TWO[0].x)==(temp.two->TWO[0].x-x)&&
(temp.two->TWO[1].y-temp.two->TWO[0].y)==(temp.two->TWO[0].y-y))//新的点加在temp.two->TWO[0]的前面
{
while(temp.three->next!=NULL)temp.three=temp.three->next;
ThreeChessman tempThree_0=(ThreeChessman)malloc(sizeof(struct three));
tempThree_0->THREE[0].x=x;
tempThree_0->THREE[0].y=y;
tempThree_0->THREE[1]=temp.two->TWO[0];
tempThree_0->THREE[2]=temp.two->TWO[1];
tempThree_0->next=NULL;
temp.three->next=tempThree_0;
continuumChessmanOpp.CountThree++;
//加入到删除列表
DeleTwo[DeleCountTwo]=temp.two;
DeleCountTwo++;
//加入到删除列表
}
if((temp.two->TWO[0].x-temp.two->TWO[1].x)==(temp.two->TWO[1].x-x)&&
(temp.two->TWO[0].y-temp.two->TWO[1].y)==(temp.two->TWO[1].y-y))//新的点加在temp.two->TWO[1]的后面
{
while(temp.three->next!=NULL)temp.three=temp.three->next;
ThreeChessman tempThree_1=(ThreeChessman)malloc(sizeof(struct three));
tempThree_1->THREE[0]=temp.two->TWO[0];
tempThree_1->THREE[1]=temp.two->TWO[1];
tempThree_1->THREE[2].x=x;
tempThree_1->THREE[2].y=y;
tempThree_1->next=NULL;
temp.three->next=tempThree_1;
continuumChessmanOpp.CountThree++;
//加入到删除列表
DeleTwo[DeleCountTwo]=temp.two;
DeleCountTwo++;
//加入到删除列表
}
temp.two=temp.two->next;
}//while
while(DeleCountTwo!=0)
{
DeleConCheTwoOpp(DeleTwo[DeleCountTwo-1]);
DeleCountTwo--;
}
///两个连续的棋子-结束
//LABLE1:
///三个连续的棋子-开始
temp=continuumChessmanOpp;
ThreeChessman DeleThree[NUM*NUM];
int DeleCountThree=0;
if(temp.three->THREE[0].x==2*NUM&&temp.three->THREE[0].y==2*NUM)
temp.three=temp.three->next;
while(temp.three!=NULL)
{
if((temp.three->THREE[2].x-temp.three->THREE[1].x)==(temp.three->THREE[0].x-x)&&
(temp.three->THREE[2].y-temp.three->THREE[1].y)==(temp.three->THREE[0].y-y))//新的点加在temp.two->TWO[0]的前面
{
while(temp.four->next!=NULL)temp.four=temp.four->next;
FourChessman tempFour_0=(FourChessman)malloc(sizeof(struct four));
tempFour_0->FOUR[0].x=x;
tempFour_0->FOUR[0].y=y;
tempFour_0->FOUR[1]=temp.three->THREE[0];
tempFour_0->FOUR[2]=temp.three->THREE[1];
tempFour_0->FOUR[3]=temp.three->THREE[2];
tempFour_0->next=NULL;
temp.four->next=tempFour_0;
continuumChessmanOpp.CountFour++;
//加入到删除列表
DeleThree[DeleCountThree]=temp.three;
DeleCountThree++;
//加入到删除列表
}
if((temp.three->THREE[0].x-temp.three->THREE[1].x)==(temp.three->THREE[2].x-x)&&
(temp.three->THREE[0].y-temp.three->THREE[1].y)==(temp.three->THREE[2].y-y))//新的点加在temp.two->TWO[2]的后面
{
while(temp.four->next!=NULL)temp.four=temp.four->next;
FourChessman tempFour_0=(FourChessman)malloc(sizeof(struct four));
tempFour_0->FOUR[0]=temp.three->THREE[0];
tempFour_0->FOUR[1]=temp.three->THREE[1];
tempFour_0->FOUR[2]=temp.three->THREE[2];
tempFour_0->FOUR[3].x=x;
tempFour_0->FOUR[3].y=y;
tempFour_0->next=NULL;
temp.four->next=tempFour_0;
continuumChessmanOpp.CountFour++;
//加入到删除列表
DeleThree[DeleCountThree]=temp.three;
DeleCountThree++;
//加入到删除列表
}
temp.three=temp.three->next;
}//while
while(DeleCountThree!=0)
{
DeleConCheThreeOpp(DeleThree[DeleCountThree-1]);
DeleCountThree--;
}
///三个连续的棋子-结束
///四个连续的棋子-开始
temp=continuumChessmanOpp;
if(temp.four->FOUR[0].x==2*NUM&&temp.four->FOUR[0].y==2*NUM)
temp.four=temp.four->next;
while(temp.four!=NULL)
{
if((temp.four->FOUR[3].x-temp.four->FOUR[2].x)==(temp.four->FOUR[0].x-x)&&
(temp.four->FOUR[3].y-temp.four->FOUR[2].y)==(temp.four->FOUR[0].y-y))//新的点加在temp.two->TWO[0]的前面
{
Result=2;
return true;
}
if((temp.four->FOUR[0].x-temp.four->FOUR[1].x)==(temp.four->FOUR[3].x-x)&&
(temp.four->FOUR[0].y-temp.four->FOUR[1].y)==(temp.four->FOUR[3].y-y))//新的点加在temp.two->TWO[2]的后面
{
Result=2;
return true;
}
temp.four=temp.four->next;
}
///四个连续的棋子-结束
另一种算法//
int left=0,right=0,top=0,bottom=0,leftT=0,leftB=0,rightT=0,rightB=0,count=0,maxC=0,ConNum=0;
ContinuumOppNum(x,y,left,right,top,bottom,leftT,leftB,rightT,rightB);
// maxC=Max(left+right,top+bottom,leftT+rightB,leftB+rightT,count); //返回各排中的最大值,计算有个并列为最大值 (当全部都相等是count为5)
/
if(left+right==4||top+bottom==4||leftT+rightB==4||leftB+rightT==4)
{
Result=2;
return true;//结束程序的函数
}
///8888888888888888888888888888888888888888888/
/
if(left+right==3)
{
while(temp.four->next!=NULL)temp.four=temp.four->next;
FourChessman tempFour_0=(FourChessman)malloc(sizeof(struct four));
int i=0;
while(left>0)
{
tempFour_0->FOUR[i].x=x-left;
tempFour_0->FOUR[i].y=y;
left--;
i++;
}
int j=3;
while(right>=0)
{
tempFour_0->FOUR[j].x=x+right;
tempFour_0->FOUR[j].y=y;
right--;
j++;
}
tempFour_0->next=NULL;
temp.four->next=tempFour_0;
continuumChessmanOpp.CountFour++;
}
/
if(top+bottom==3)
{
while(temp.four->next!=NULL)temp.four=temp.four->next;
FourChessman tempFour_0=(FourChessman)malloc(sizeof(struct four));
int i=0;
while(bottom>0)
{
tempFour_0->FOUR[i].x=x;
tempFour_0->FOUR[i].y=y+bottom;
bottom--;
i++;
}
int j=3;
while(top>=0)
{
tempFour_0->FOUR[j].x=x;
tempFour_0->FOUR[j].y=y-top;
top--;
j++;
}
tempFour_0->next=NULL;
temp.four->next=tempFour_0;
continuumChessmanOpp.CountFour++;
}
/
if(leftT+rightB==3)
{
while(temp.four->next!=NULL)temp.four=temp.four->next;
FourChessman tempFour_0=(FourChessman)malloc(sizeof(struct four));
int i=0;
while(leftT>0)
{
tempFour_0->FOUR[i].x=x-leftT;
tempFour_0->FOUR[i].y=y-leftT;
leftT--;
i++;
}
int j=3;
while(rightB>=0)
{
tempFour_0->FOUR[j].x=x+rightB;
tempFour_0->FOUR[j].y=y+rightB;
rightB--;
j++;
}
tempFour_0->next=NULL;
temp.four->next=tempFour_0;
continuumChessmanOpp.CountFour++;
}
//
if(leftB+rightT==3)
{
while(temp.four->next!=NULL)temp.four=temp.four->next;
FourChessman tempFour_0=(FourChessman)malloc(sizeof(struct four));
int i=0;
while(leftB>0)
{
tempFour_0->FOUR[i].x=x-leftB;
tempFour_0->FOUR[i].y=y+leftB;
leftB--;
i++;
}
int j=3;
while(rightT>=0)
{
tempFour_0->FOUR[j].x=x+rightT;
tempFour_0->FOUR[j].y=y-rightT;
rightT--;
j++;
}
tempFour_0->next=NULL;
temp.four->next=tempFour_0;
continuumChessmanOpp.CountFour++;
}
///
//8888888888888888888888888888888888888888///
/
if(left+right==2)
{
while(temp.three->next!=NULL)temp.three=temp.three->next;
ThreeChessman tempThree_0=(ThreeChessman)malloc(sizeof(struct three));
int i=0;
while(left>0)
{
tempThree_0->THREE[i].x=x-left;
tempThree_0->THREE[i].y=y;
left--;
i++;
}
int j=2;
while(right>=0)
{
tempThree_0->THREE[j].x=x+right;
tempThree_0->THREE[j].y=y;
right--;
j++;
}
tempThree_0->next=NULL;
temp.three->next=tempThree_0;
continuumChessmanOpp.CountThree++;
}
/
if(top+bottom==2)
{
while(temp.three->next!=NULL)temp.three=temp.three->next;
ThreeChessman tempThree_0=(ThreeChessman)malloc(sizeof(struct three));
int i=0;
while(bottom>0)
{
tempThree_0->THREE[i].x=x;
tempThree_0->THREE[i].y=y+bottom;
bottom--;
i++;
}
int j=2;
while(top>=0)
{
tempThree_0->THREE[j].x=x;
tempThree_0->THREE[j].y=y-top;
top--;
j++;
}
tempThree_0->next=NULL;
temp.three->next=tempThree_0;
continuumChessmanOpp.CountThree++;
}
/
if(leftT+rightB==2)
{
while(temp.three->next!=NULL)temp.three=temp.three->next;
ThreeChessman tempThree_0=(ThreeChessman)malloc(sizeof(struct three));
int i=0;
while(leftT>0)
{
tempThree_0->THREE[i].x=x-leftT;
tempThree_0->THREE[i].y=y-leftT;
leftT--;
i++;
}
int j=2;
while(rightB>=0)
{
tempThree_0->THREE[j].x=x+rightB;
tempThree_0->THREE[j].y=y+rightB;
rightB--;
j++;
}
tempThree_0->next=NULL;
temp.three->next=tempThree_0;
continuumChessmanOpp.CountThree++;
}
//
if(leftB+rightT==2)
{
while(temp.three->next!=NULL)temp.three=temp.three->next;
ThreeChessman tempThree_0=(ThreeChessman)malloc(sizeof(struct three));
int i=0;
while(leftB>0)
{
tempThree_0->THREE[i].x=x-leftB;
tempThree_0->THREE[i].y=y+leftB;
leftB--;
i++;
}
int j=2;
while(rightT>=0)
{
tempThree_0->THREE[j].x=x+rightT;
tempThree_0->THREE[j].y=y-rightT;
rightT--;
j++;
}
tempThree_0->next=NULL;
temp.three->next=tempThree_0;
continuumChessmanOpp.CountThree++;
}
///
//8888888888888888888888888888888888888888///
/
/
if(left+right==1)
{
while(temp.two->next!=NULL)temp.two=temp.two->next;
TwoChessman tempTwo_0=(TwoChessman)malloc(sizeof(struct two));
int i=0;
while(left>0)
{
tempTwo_0->TWO[i].x=x-left;
tempTwo_0->TWO[i].y=y;
left--;
i++;
}
int j=1;
while(right>=0)
{
tempTwo_0->TWO[j].x=x+right;
tempTwo_0->TWO[j].y=y;
right--;
j++;
}
tempTwo_0->next=NULL;
temp.two->next=tempTwo_0;
continuumChessmanOpp.CountTwo++;
}
/
if(top+bottom==1)
{
while(temp.two->next!=NULL)temp.two=temp.two->next;
TwoChessman tempTwo_0=(TwoChessman)malloc(sizeof(struct two));
int i=0;
while(bottom>0)
{
tempTwo_0->TWO[i].x=x;
tempTwo_0->TWO[i].y=y+bottom;
bottom--;
i++;
}
int j=1;
while(top>=0)
{
tempTwo_0->TWO[j].x=x;
tempTwo_0->TWO[j].y=y-top;
top--;
j++;
}
tempTwo_0->next=NULL;
temp.two->next=tempTwo_0;
continuumChessmanOpp.CountTwo++;
}
/
if(leftT+rightB==1)
{
while(temp.two->next!=NULL)temp.two=temp.two->next;
TwoChessman tempTwo_0=(TwoChessman)malloc(sizeof(struct two));
int i=0;
while(leftT>0)
{
tempTwo_0->TWO[i].x=x-leftT;
tempTwo_0->TWO[i].y=y-leftT;
leftT--;
i++;
}
int j=1;
while(rightB>=0)
{
tempTwo_0->TWO[j].x=x+rightB;
tempTwo_0->TWO[j].y=y+rightB;
rightB--;
j++;
}
tempTwo_0->next=NULL;
temp.two->next=tempTwo_0;
continuumChessmanOpp.CountTwo++;
}
//
if(leftB+rightT==1)
{
while(temp.two->next!=NULL)temp.two=temp.two->next;
TwoChessman tempTwo_0=(TwoChessman)malloc(sizeof(struct two));
int i=0;
while(leftB>0)
{
tempTwo_0->TWO[i].x=x-leftB;
tempTwo_0->TWO[i].y=y+leftB;
leftB--;
i++;
}
int j=1;
while(rightT>=0)
{
tempTwo_0->TWO[j].x=x+rightT;
tempTwo_0->TWO[j].y=y-rightT;
rightT--;
j++;
}
tempTwo_0->next=NULL;
temp.two->next=tempTwo_0;
continuumChessmanOpp.CountTwo++;
}
///
return true;
}
/
// AddToContinuumChessmanOpp构造函数定义——结束 //
// 添加一个点到continuumChessmanOpp数据成员中 //
// AttackNextPri0函数定义——开始 //
// 计算出进攻下一步棋的所有可能情况,并加入到ComNext数据成员中 //
bool Computer::AttackNextPri0()
{
ContinuumChessman TContinuumChessman=continuumChessmanCom;
if(ComCount==0)//如果这是第一步棋子
{
AddToComNext(5,6,PRI_0);
}
else//一下为这不是第一步棋的情况
{
/如果所有电脑棋子都不连续-开始//
if(TContinuumChessman.CountTwo==0&&TContinuumChessman.CountThree==0&&TContinuumChessman.CountFour==0)
{
for(int i=0;i<ComCount;i++)//ComExistChessman[0]到ComExistChessman[ComCount-1]存储的是已经有了的棋子
{
if(JudgeCoordinate(ComExistChessman[i].x-1,ComExistChessman[i].y)&&JudgeComNext(ComExistChessman[i].x-1,ComExistChessman[i].y))
{
AddToComNext(ComExistChessman[i].x-1,ComExistChessman[i].y,PRI_0);
}
if(JudgeCoordinate(ComExistChessman[i].x+1,ComExistChessman[i].y)&&JudgeComNext(ComExistChessman[i].x+1,ComExistChessman[i].y))
{
AddToComNext(ComExistChessman[i].x+1,ComExistChessman[i].y,PRI_0);
}
if(JudgeCoordinate(ComExistChessman[i].x,ComExistChessman[i].y-1)&&JudgeComNext(ComExistChessman[i].x,ComExistChessman[i].y-1))
{
AddToComNext(ComExistChessman[i].x,ComExistChessman[i].y-1,PRI_0);
}
if(JudgeCoordinate(ComExistChessman[i].x,ComExistChessman[i].y+1)&&JudgeComNext(ComExistChessman[i].x,ComExistChessman[i].y+1))
{
AddToComNext(ComExistChessman[i].x,ComExistChessman[i].y+1,PRI_0);
}
if(JudgeCoordinate(ComExistChessman[i].x-1,ComExistChessman[i].y-1)&&JudgeComNext(ComExistChessman[i].x-1,ComExistChessman[i].y-1))
{
AddToComNext(ComExistChessman[i].x-1,ComExistChessman[i].y-1,PRI_0);
}
if(JudgeCoordinate(ComExistChessman[i].x+1,ComExistChessman[i].y+1)&&JudgeComNext(ComExistChessman[i].x+1,ComExistChessman[i].y+1))
{
AddToComNext(ComExistChessman[i].x+1,ComExistChessman[i].y+1,PRI_0);
}
if(JudgeCoordinate(ComExistChessman[i].x-1,ComExistChessman[i].y+1)&&JudgeComNext(ComExistChessman[i].x-1,ComExistChessman[i].y+1))
{
AddToComNext(ComExistChessman[i].x-1,ComExistChessman[i].y+1,PRI_0);
}
if(JudgeCoordinate(ComExistChessman[i].x+1,ComExistChessman[i].y-1)&&JudgeComNext(ComExistChessman[i].x+1,ComExistChessman[i].y-1))
{
AddToComNext(ComExistChessman[i].x+1,ComExistChessman[i].y-1,PRI_0);
}
}
}
/如果所有电脑棋子都不连续-结束//
else//以下为电脑的棋子有为连续的情况
{
/计算连续两个棋子为一组的可能下一步——开始//
if(TContinuumChessman.CountTwo!=0)
{
if(TContinuumChessman.two->TWO[0].x==2*NUM&&
TContinuumChessman.two->TWO[0].y==2*NUM)//跳跃过头节点
TContinuumChessman.two=TContinuumChessman.two->next;
while(TContinuumChessman.two!=NULL)
{
for(int i=0;i<2;i++)
{
if(JudgeCoordinate(TContinuumChessman.two->TWO[i].x-1,TContinuumChessman.two->TWO[i].y)&&
JudgeComNext(TContinuumChessman.two->TWO[i].x-1,TContinuumChessman.two->TWO[i].y))
{
AddToComNext(TContinuumChessman.two->TWO[i].x-1,TContinuumChessman.two->TWO[i].y,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.two->TWO[i].x+1,TContinuumChessman.two->TWO[i].y)&&
JudgeComNext(TContinuumChessman.two->TWO[i].x+1,TContinuumChessman.two->TWO[i].y))
{
AddToComNext(TContinuumChessman.two->TWO[i].x+1,TContinuumChessman.two->TWO[i].y,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.two->TWO[i].x,TContinuumChessman.two->TWO[i].y-1)&&
JudgeComNext(TContinuumChessman.two->TWO[i].x,TContinuumChessman.two->TWO[i].y-1))
{
AddToComNext(TContinuumChessman.two->TWO[i].x,TContinuumChessman.two->TWO[i].y-1,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.two->TWO[i].x,TContinuumChessman.two->TWO[i].y+1)&&
JudgeComNext(TContinuumChessman.two->TWO[i].x,TContinuumChessman.two->TWO[i].y+1))
{
AddToComNext(TContinuumChessman.two->TWO[i].x,TContinuumChessman.two->TWO[i].y+1,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.two->TWO[i].x-1,TContinuumChessman.two->TWO[i].y-1)&&
JudgeComNext(TContinuumChessman.two->TWO[i].x-1,TContinuumChessman.two->TWO[i].y-1))
{
AddToComNext(TContinuumChessman.two->TWO[i].x-1,TContinuumChessman.two->TWO[i].y-1,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.two->TWO[i].x+1,TContinuumChessman.two->TWO[i].y+1)&&
JudgeComNext(TContinuumChessman.two->TWO[i].x+1,TContinuumChessman.two->TWO[i].y+1))
{
AddToComNext(TContinuumChessman.two->TWO[i].x+1,TContinuumChessman.two->TWO[i].y+1,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.two->TWO[i].x-1,TContinuumChessman.two->TWO[i].y+1)&&
JudgeComNext(TContinuumChessman.two->TWO[i].x-1,TContinuumChessman.two->TWO[i].y+1))
{
AddToComNext(TContinuumChessman.two->TWO[i].x-1,TContinuumChessman.two->TWO[i].y+1,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.two->TWO[i].x+1,TContinuumChessman.two->TWO[i].y-1)&&
JudgeComNext(TContinuumChessman.two->TWO[i].x+1,TContinuumChessman.two->TWO[i].y-1))
{
AddToComNext(TContinuumChessman.two->TWO[i].x+1,TContinuumChessman.two->TWO[i].y-1,PRI_0);
}
}//for
TContinuumChessman.two= TContinuumChessman.two->next;
}
}
///计算连续两个棋子为一组的可能下一步——结束//
///计算连续三个棋子为一组的可能下一步——开始//
if(TContinuumChessman.CountThree!=0)
{
if(TContinuumChessman.three->THREE[0].x==2*NUM&&
TContinuumChessman.three->THREE[0].y==2*NUM)//跳跃过头节点
TContinuumChessman.three=TContinuumChessman.three->next;
while(TContinuumChessman.three!=NULL)
{
for(int i=0;i<3;i++)
{
if(JudgeCoordinate(TContinuumChessman.three->THREE[i].x-1,TContinuumChessman.three->THREE[i].y)&&
JudgeComNext(TContinuumChessman.three->THREE[i].x-1,TContinuumChessman.three->THREE[i].y))
{
AddToComNext(TContinuumChessman.three->THREE[i].x-1,TContinuumChessman.three->THREE[i].y,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.three->THREE[i].x+1,TContinuumChessman.three->THREE[i].y)&&
JudgeComNext(TContinuumChessman.three->THREE[i].x+1,TContinuumChessman.three->THREE[i].y))
{
AddToComNext(TContinuumChessman.three->THREE[i].x+1,TContinuumChessman.three->THREE[i].y,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.three->THREE[i].x,TContinuumChessman.three->THREE[i].y-1)&&
JudgeComNext(TContinuumChessman.three->THREE[i].x,TContinuumChessman.three->THREE[i].y-1))
{
AddToComNext(TContinuumChessman.three->THREE[i].x,TContinuumChessman.three->THREE[i].y-1,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.three->THREE[i].x,TContinuumChessman.three->THREE[i].y+1)&&
JudgeComNext(TContinuumChessman.three->THREE[i].x,TContinuumChessman.three->THREE[i].y+1))
{
AddToComNext(TContinuumChessman.three->THREE[i].x,TContinuumChessman.three->THREE[i].y+1,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.three->THREE[i].x-1,TContinuumChessman.three->THREE[i].y-1)&&
JudgeComNext(TContinuumChessman.three->THREE[i].x-1,TContinuumChessman.three->THREE[i].y-1))
{
AddToComNext(TContinuumChessman.three->THREE[i].x-1,TContinuumChessman.three->THREE[i].y-1,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.three->THREE[i].x+1,TContinuumChessman.three->THREE[i].y+1)&&
JudgeComNext(TContinuumChessman.three->THREE[i].x+1,TContinuumChessman.three->THREE[i].y+1))
{
AddToComNext(TContinuumChessman.three->THREE[i].x+1,TContinuumChessman.three->THREE[i].y+1,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.three->THREE[i].x-1,TContinuumChessman.three->THREE[i].y+1)&&
JudgeComNext(TContinuumChessman.three->THREE[i].x-1,TContinuumChessman.three->THREE[i].y+1))
{
AddToComNext(TContinuumChessman.three->THREE[i].x-1,TContinuumChessman.three->THREE[i].y+1,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.three->THREE[i].x+1,TContinuumChessman.three->THREE[i].y-1)&&
JudgeComNext(TContinuumChessman.three->THREE[i].x+1,TContinuumChessman.three->THREE[i].y-1))
{
AddToComNext(TContinuumChessman.three->THREE[i].x+1,TContinuumChessman.three->THREE[i].y-1,PRI_0);
}
}//for
TContinuumChessman.three=TContinuumChessman.three->next;
}
}
计算连续三个棋子为一组的可能下一步——结束//
计算连续四个棋子为一组的可能下一步——开始//
if(TContinuumChessman.CountFour!=0)
{
if(TContinuumChessman.four->FOUR[0].x==2*NUM&&
TContinuumChessman.four->FOUR[0].y==2*NUM)//跳过头节点
TContinuumChessman.four=TContinuumChessman.four->next;
while(TContinuumChessman.four!=NULL)
{
for(int i=0;i<4;i++)
{
if(JudgeCoordinate(TContinuumChessman.four->FOUR[i].x-1,TContinuumChessman.four->FOUR[i].y)&&
JudgeComNext(TContinuumChessman.four->FOUR[i].x-1,TContinuumChessman.four->FOUR[i].y))
{
AddToComNext(TContinuumChessman.four->FOUR[i].x-1,TContinuumChessman.four->FOUR[i].y,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.four->FOUR[i].x+1,TContinuumChessman.four->FOUR[i].y)&&
JudgeComNext(TContinuumChessman.four->FOUR[i].x+1,TContinuumChessman.four->FOUR[i].y))
{
AddToComNext(TContinuumChessman.four->FOUR[i].x+1,TContinuumChessman.four->FOUR[i].y,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.four->FOUR[i].x,TContinuumChessman.four->FOUR[i].y-1)&&
JudgeComNext(TContinuumChessman.four->FOUR[i].x,TContinuumChessman.four->FOUR[i].y-1))
{
AddToComNext(TContinuumChessman.four->FOUR[i].x,TContinuumChessman.four->FOUR[i].y-1,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.four->FOUR[i].x,TContinuumChessman.four->FOUR[i].y+1)&&
JudgeComNext(TContinuumChessman.four->FOUR[i].x,TContinuumChessman.four->FOUR[i].y+1))
{
AddToComNext(TContinuumChessman.four->FOUR[i].x,TContinuumChessman.four->FOUR[i].y+1,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.four->FOUR[i].x-1,TContinuumChessman.four->FOUR[i].y-1)&&
JudgeComNext(TContinuumChessman.four->FOUR[i].x-1,TContinuumChessman.four->FOUR[i].y-1))
{
AddToComNext(TContinuumChessman.four->FOUR[i].x-1,TContinuumChessman.four->FOUR[i].y-1,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.four->FOUR[i].x+1,TContinuumChessman.four->FOUR[i].y+1)&&
JudgeComNext(TContinuumChessman.four->FOUR[i].x+1,TContinuumChessman.four->FOUR[i].y+1))
{
AddToComNext(TContinuumChessman.four->FOUR[i].x+1,TContinuumChessman.four->FOUR[i].y+1,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.four->FOUR[i].x-1,TContinuumChessman.four->FOUR[i].y+1)&&
JudgeComNext(TContinuumChessman.four->FOUR[i].x-1,TContinuumChessman.four->FOUR[i].y+1))
{
AddToComNext(TContinuumChessman.four->FOUR[i].x-1,TContinuumChessman.four->FOUR[i].y+1,PRI_0);
}
if(JudgeCoordinate(TContinuumChessman.four->FOUR[i].x+1,TContinuumChessman.four->FOUR[i].y-1)&&
JudgeComNext(TContinuumChessman.four->FOUR[i].x+1,TContinuumChessman.four->FOUR[i].y-1))
{
AddToComNext(TContinuumChessman.four->FOUR[i].x+1,TContinuumChessman.four->FOUR[i].y-1,PRI_0);
}
}//for
TContinuumChessman.four= TContinuumChessman.four->next;
}
}
//计算连续四个棋子为一组的可能下一步——结束//
}
}
return true;
}
// AttackNextPri0函数定义——结束 //
// 计算出进攻下一步棋的所有可能情况,并加入到ComNext数据成员中 //
// AttackNextPri1函数定义——开始 //
// 在ComNext数据成员的第0优先级中提取更高的优先级坐标,并加入到 //
// ComNext数据成员中的更高优先级 //
///
bool Computer::AttackNextPri1()
{
int left=0,right=0,top=0,bottom=0,leftT=0,leftB=0,rightT=0,rightB=0,count=0,maxC=0,pri=0;
NextPri temp=ComNext;
//temp.PRI[PRI_0]=temp.PRI[PRI_0]->next;
if(temp.PRI[PRI_0]->point.x==3*NUM&&temp.PRI[PRI_0]->point.y==3*NUM)
temp.PRI[PRI_0]=temp.PRI[PRI_0]->next;
while(temp.PRI[PRI_0]!=NULL)//头节点可能会有问题!!!!!
{
ContinuumComNum(temp.PRI[PRI_0]->point.x,temp.PRI[PRI_0]->point.y,
left,right,top,bottom,leftT,leftB,rightT,rightB);
maxC=Max(left*9+right*9,top*9+bottom*9,leftT*9+rightB*9,leftB*9+rightT*9,count); //返回各排中的最大值,计算有个并列为最大值 (当全部都相等是count为5)
pri=maxC+count; //计算该点的优先级
if(pri>0)//要优先级有所提高的才运行一下函数
AddToComNext(temp.PRI[PRI_0]->point.x,temp.PRI[PRI_0]->point.y,pri); //将该点加入到指定的ComNext的优先级队列中
temp.PRI[PRI_0]=temp.PRI[PRI_0]->next;
count=0;maxC=0;pri=0;
}
return true;
}
// AttackNextPri1函数定义——结束 //
// 在ComNext数据成员的第0优先级中提取更高的优先级坐标,并加入到 //
// ComNext数据成员中的更高优先级 //
///
///
// AddNewChessman函数定义——开始 //
// 确定并添加棋子到ComExistChessman和continuumChessman //
//
bool Computer::AddNewChessman()
{
if(Result==1)return true;
NextPri temp=ComNext;
for(int i=MAXPRI-1;i>=0;i--)
{
if(temp.PRI[i]->next!=NULL)
{
ComExistChessman[ComCount]=temp.PRI[i]->next->point; //将新的点加入到ComExistChessman成员
//AddToContinuumChessman(temp.PRI[i]->next->point.x,temp.PRI[i]->next->point.y);//将新的点加入到ContinuumChessman成员
CoordinateR[temp.PRI[i]->next->point.x][temp.PRI[i]->next->point.y]=COMCHESSMAN;/
ComCount++;
if(Result!=0)return true;
std::cout<<"x="<<temp.PRI[i]->next->point.x<<" y="<<temp.PRI[i]->next->point.y<<endl;
LPoint TLPoint=temp.PRI[i]->next;
temp.PRI[i]->next=temp.PRI[i]->next->next;//在ComNext中删除刚刚加入的这个点
free(TLPoint);
DeleComNext();//清空ComNext的函数
return true;//该点加入后结束此函数
}
}
return true;
}
///
// AddNewChessman函数定义——结束 //
// 确定并添加棋子到ComExistChessman和continuumChessman //
//
/
// DeleConCheTwo函数定义开始 //
// 删除continuumChessman中连续两个棋子的一个节点 //
bool Computer::DeleConCheTwo(TwoChessman two)
{
ContinuumChessman temp=continuumChessmanCom;
if(temp.CountTwo==0)
{
std::cout<<"There is no continuumTwo data in continuumChessmanCom/n";
return true;
}
while(temp.two!=NULL)
{
if(temp.two->next->TWO[0].x==two->TWO[0].x&&temp.two->next->TWO[0].y==two->TWO[0].y
&&temp.two->next->TWO[1].x==two->TWO[1].x&&temp.two->next->TWO[1].y==two->TWO[1].y)
{
TwoChessman TTwo=temp.two->next;
temp.two->next=temp.two->next->next;
free(TTwo);//释放temp.two->next所指的内存空间
continuumChessmanCom.CountTwo--;
return true;
}
temp.two=temp.two->next;
}
return true;
}
/
// DeleConCheTwo函数定义结束 //
// 删除continuumChessman中连续两个棋子的一个节点 //
/
// DeleConCheThree函数定义开始 //
// 删除continuumChessman中连续三个棋子的一个节点 //
bool Computer::DeleConCheThree(ThreeChessman three)
{
ContinuumChessman temp=continuumChessmanCom;
if(temp.CountThree==0)
{
std::cout<<"There is no continuumThree data in continuumChessmanCom/n";
return true;
}
while(temp.three!=NULL)
{
if(temp.three->next->THREE[0].x==three->THREE[0].x&&temp.three->next->THREE[0].y==three->THREE[0].y&&
temp.three->next->THREE[1].x==three->THREE[1].x&&temp.three->next->THREE[1].y==three->THREE[1].y&&
temp.three->next->THREE[2].x==three->THREE[2].x&&temp.three->next->THREE[2].y==three->THREE[2].y)
{
ThreeChessman TThree=temp.three->next;
temp.three->next=temp.three->next->next;
free(TThree);//释放temp.three->next所指的内存空间
continuumChessmanCom.CountThree--;
return true;
}
temp.three=temp.three->next;
}
return true;
}
/
// DeleConCheThree函数定义结束 //
// 删除continuumChessman中连续三个棋子的一个节点 //
/
// AddToContinuumChessman函数定义开始 //
// 添加一个点到ContinuumChessman数据成员中 //
bool Computer::AddToContinuumChessman(int x,int y)
{
ContinuumChessman temp;
temp=continuumChessmanCom;
/*
// if(temp.CountTwo==0&&temp.CountThree==0&&temp.CountFour==0)//如果没有连续的点,判断可不可以与其他的分散点组成连续的点
// {
/没有连续的点-开始
for(int i=0;i<ComCount;i++)
{
if(x-1==ComExistChessman[i].x&&y==ComExistChessman[i].y)
{
while(temp.two->next!=NULL) temp.two=temp.two->next;
TwoChessman tempTwo=(TwoChessman)malloc(sizeof(struct two));
tempTwo->TWO[0]=ComExistChessman[i];
tempTwo->TWO[1].x=x;
tempTwo->TWO[1].y=y;
tempTwo->next=NULL;
temp.two->next=tempTwo;
continuumChessmanCom.CountTwo++;
}
}
for(int i=0;i<ComCount;i++)
{
if(x+1==ComExistChessman[i].x&&y==ComExistChessman[i].y)
{
while(temp.two->next!=NULL) temp.two=temp.two->next;
TwoChessman tempTwo=(TwoChessman)malloc(sizeof(struct two));
tempTwo->TWO[0].x=x;
tempTwo->TWO[0].y=y;
tempTwo->TWO[1]=ComExistChessman[i];
tempTwo->next=NULL;
temp.two->next=tempTwo;
continuumChessmanCom.CountTwo++;
}
}
for(int i=0;i<ComCount;i++)
{
if(x==ComExistChessman[i].x&&y-1==ComExistChessman[i].y)
{
while(temp.two->next!=NULL) temp.two=temp.two->next;
TwoChessman tempTwo=(TwoChessman)malloc(sizeof(struct two));
tempTwo->TWO[0]=ComExistChessman[i];
tempTwo->TWO[1].x=x;
tempTwo->TWO[1].y=y;
tempTwo->next=NULL;
temp.two->next=tempTwo;
continuumChessmanCom.CountTwo++;
}
}
for(int i=0;i<ComCount;i++)
{
if(x==ComExistChessman[i].x&&y+1==ComExistChessman[i].y)
{
while(temp.two->next!=NULL) temp.two=temp.two->next;
TwoChessman tempTwo=(TwoChessman)malloc(sizeof(struct two));
tempTwo->TWO[0].x=x;
tempTwo->TWO[0].y=y;
tempTwo->TWO[1]=ComExistChessman[i];
tempTwo->next=NULL;
temp.two->next=tempTwo;
continuumChessmanCom.CountTwo++;
}
}
for(int i=0;i<ComCount;i++)
{
if(x-1==ComExistChessman[i].x&&y-1==ComExistChessman[i].y)
{
while(temp.two->next!=NULL) temp.two=temp.two->next;
TwoChessman tempTwo=(TwoChessman)malloc(sizeof(struct two));
tempTwo->TWO[0]=ComExistChessman[i];
tempTwo->TWO[1].x=x;
tempTwo->TWO[1].y=y;
tempTwo->next=NULL;
temp.two->next=tempTwo;
continuumChessmanCom.CountTwo++;
}
}
for(int i=0;i<ComCount;i++)
{
if(x+1==ComExistChessman[i].x&&y+1==ComExistChessman[i].y)
{
while(temp.two->next!=NULL) temp.two=temp.two->next;
TwoChessman tempTwo=(TwoChessman)malloc(sizeof(struct two));
tempTwo->TWO[0].x=x;
tempTwo->TWO[0].y=y;
tempTwo->TWO[1]=ComExistChessman[i];
tempTwo->next=NULL;
temp.two->next=tempTwo;
continuumChessmanCom.CountTwo++;
}
}
for(int i=0;i<ComCount;i++)
{
if(x+1==ComExistChessman[i].x&&y-1==ComExistChessman[i].y)
{
while(temp.two->next!=NULL) temp.two=temp.two->next;
TwoChessman tempTwo=(TwoChessman)malloc(sizeof(struct two));
tempTwo->TWO[0].x=x;
tempTwo->TWO[0].y=y;
tempTwo->TWO[1]=ComExistChessman[i];
tempTwo->next=NULL;
temp.two->next=tempTwo;
continuumChessmanCom.CountTwo++;
}
}
for(int i=0;i<ComCount;i++)
{
if(x-1==ComExistChessman[i].x&&y+1==ComExistChessman[i].y)
{
while(temp.two->next!=NULL) temp.two=temp.two->next;
TwoChessman tempTwo=(TwoChessman)malloc(sizeof(struct two));
tempTwo->TWO[0].x=x;
tempTwo->TWO[0].y=y;
tempTwo->TWO[1]=ComExistChessman[i];
tempTwo->next=NULL;
temp.two->next=tempTwo;
continuumChessmanCom.CountTwo++;
}
}
/没有连续的点-结束
//}
// else//有连续的点
// {
///两个连续的棋子-开始
temp=continuumChessmanCom;
TwoChessman DeleTwo[NUM*NUM];
int DeleCountTwo=0;
if(temp.two->TWO[0].x==2*NUM&&temp.two->TWO[0].y==2*NUM)//跳过头节点
temp.two=temp.two->next;
while(temp.two!=NULL)
{
if((temp.two->TWO[1].x-temp.two->TWO[0].x)==(temp.two->TWO[0].x-x)&&
(temp.two->TWO[1].y-temp.two->TWO[0].y)==(temp.two->TWO[0].y-y))//新的点加在temp.two->TWO[0]的前面
{
while(temp.three->next!=NULL)temp.three=temp.three->next;
ThreeChessman tempThree_0=(ThreeChessman)malloc(sizeof(struct three));
tempThree_0->THREE[0].x=x;
tempThree_0->THREE[0].y=y;
tempThree_0->THREE[1]=temp.two->TWO[0];
tempThree_0->THREE[2]=temp.two->TWO[1];
tempThree_0->next=NULL;
temp.three->next=tempThree_0;
continuumChessmanCom.CountThree++;
//加入到删除列表
DeleTwo[DeleCountTwo]=temp.two;
DeleCountTwo++;
//加入到删除列表
// DeleConCheTwo(temp.two); //删除temp.two这个链表节点
// goto LABLE1;
// continuumChessmanCom.CountTwo--;
}
if((temp.two->TWO[0].x-temp.two->TWO[1].x)==(temp.two->TWO[1].x-x)&&
(temp.two->TWO[0].y-temp.two->TWO[1].y)==(temp.two->TWO[1].y-y))//新的点加在temp.two->TWO[1]的后面
{
while(temp.three->next!=NULL)temp.three=temp.three->next;
ThreeChessman tempThree_1=(ThreeChessman)malloc(sizeof(struct three));
tempThree_1->THREE[0]=temp.two->TWO[0];
tempThree_1->THREE[1]=temp.two->TWO[1];
tempThree_1->THREE[2].x=x;
tempThree_1->THREE[2].y=y;
tempThree_1->next=NULL;
temp.three->next=tempThree_1;
continuumChessmanCom.CountThree++;
//加入到删除列表
DeleTwo[DeleCountTwo]=temp.two;
DeleCountTwo++;
//加入到删除列表
// DeleConCheTwo(temp.two); //删除temp.two这个链表节点
// continuumChessmanCom.CountTwo--;
}
temp.two=temp.two->next;
}//while
while(DeleCountTwo!=0)
{
DeleConCheTwo(DeleTwo[DeleCountTwo-1]);
DeleCountTwo--;
}
///两个连续的棋子-结束
//LABLE1:
///三个连续的棋子-开始
temp=continuumChessmanCom;
ThreeChessman DeleThree[NUM*NUM];
int DeleCountThree=0;
if(temp.three->THREE[0].x==2*NUM&&temp.three->THREE[0].y==2*NUM)
temp.three=temp.three->next;
while(temp.three!=NULL)
{
if((temp.three->THREE[2].x-temp.three->THREE[1].x)==(temp.three->THREE[0].x-x)&&
(temp.three->THREE[2].y-temp.three->THREE[1].y)==(temp.three->THREE[0].y-y))//新的点加在temp.two->TWO[0]的前面
{
while(temp.four->next!=NULL)temp.four=temp.four->next;
FourChessman tempFour_0=(FourChessman)malloc(sizeof(struct four));
tempFour_0->FOUR[0].x=x;
tempFour_0->FOUR[0].y=y;
tempFour_0->FOUR[1]=temp.three->THREE[0];
tempFour_0->FOUR[2]=temp.three->THREE[1];
tempFour_0->FOUR[3]=temp.three->THREE[2];
tempFour_0->next=NULL;
temp.four->next=tempFour_0;
continuumChessmanCom.CountFour++;
//加入到删除列表
DeleThree[DeleCountThree]=temp.three;
DeleCountThree++;
//加入到删除列表
// DeleConCheThree(temp.three); //删除temp.three这个链表节点
// goto LABLE2;
// continuumChessmanCom.CountThree--;
}
if((temp.three->THREE[0].x-temp.three->THREE[1].x)==(temp.three->THREE[2].x-x)&&
(temp.three->THREE[0].y-temp.three->THREE[1].y)==(temp.three->THREE[2].y-y))//新的点加在temp.two->TWO[2]的后面
{
while(temp.four->next!=NULL)temp.four=temp.four->next;
FourChessman tempFour_0=(FourChessman)malloc(sizeof(struct four));
tempFour_0->FOUR[0]=temp.three->THREE[0];
tempFour_0->FOUR[1]=temp.three->THREE[1];
tempFour_0->FOUR[2]=temp.three->THREE[2];
tempFour_0->FOUR[3].x=x;
tempFour_0->FOUR[3].y=y;
tempFour_0->next=NULL;
temp.four->next=tempFour_0;
continuumChessmanCom.CountFour++;
//加入到删除列表
DeleThree[DeleCountThree]=temp.three;
DeleCountThree++;
//加入到删除列表
// DeleConCheThree(temp.three); //删除temp.three这个链表节点
// continuumChessmanCom.CountThree--;
}
temp.three=temp.three->next;
}//while
while(DeleCountThree!=0)
{
DeleConCheThree(DeleThree[DeleCountThree-1]);
DeleCountThree--;
}
///三个连续的棋子-结束
//LABLE2:
///四个连续的棋子-开始
temp=continuumChessmanCom;
if(temp.four->FOUR[0].x==2*NUM&&temp.four->FOUR[0].y==2*NUM)
temp.four=temp.four->next;
while(temp.four!=NULL)
{
if((temp.four->FOUR[3].x-temp.four->FOUR[2].x)==(temp.four->FOUR[0].x-x)&&
(temp.four->FOUR[3].y-temp.four->FOUR[2].y)==(temp.four->FOUR[0].y-y))//新的点加在temp.two->TWO[0]的前面
{
Result=1;
return true;//结束程序的函数
}
if((temp.four->FOUR[0].x-temp.four->FOUR[1].x)==(temp.four->FOUR[3].x-x)&&
(temp.four->FOUR[0].y-temp.four->FOUR[1].y)==(temp.four->FOUR[3].y-y))//新的点加在temp.two->TWO[2]的后面
{
Result=1;
return true;//结束程序的函数
}
temp.four=temp.four->next;
}
///四个连续的棋子-结束
另一种算法//
int left=0,right=0,top=0,bottom=0,leftT=0,leftB=0,rightT=0,rightB=0,count=0,maxC=0,ConNum=0;
ContinuumComNum(x,y,left,right,top,bottom,leftT,leftB,rightT,rightB);
// maxC=Max(left+right,top+bottom,leftT+rightB,leftB+rightT,count); //返回各排中的最大值,计算有个并列为最大值 (当全部都相等是count为5)
/
if(left+right==4||top+bottom==4||leftT+rightB==4||leftB+rightT==4)
{
Result=1;
return true;//结束程序的函数
}
///8888888888888888888888888888888888888888888/
/
if(left+right==3)
{
while(temp.four->next!=NULL)temp.four=temp.four->next;
FourChessman tempFour_0=(FourChessman)malloc(sizeof(struct four));
while(left>0)
{
int i=0;
tempFour_0->FOUR[i].x=x-left;
tempFour_0->FOUR[i].y=y;
left--;
i++;
}
while(right>=0)
{
int i=3;
tempFour_0->FOUR[i].x=x+right;
tempFour_0->FOUR[i].y=y;
right--;
i++;
}
tempFour_0->next=NULL;
temp.four->next=tempFour_0;
continuumChessmanCom.CountFour++;
}
/
if(top+bottom==3)
{
while(temp.four->next!=NULL)temp.four=temp.four->next;
FourChessman tempFour_0=(FourChessman)malloc(sizeof(struct four));
while(bottom>0)
{
int i=0;
tempFour_0->FOUR[i].x=x;
tempFour_0->FOUR[i].y=y+bottom;
bottom--;
i++;
}
while(top>=0)
{
int i=3;
tempFour_0->FOUR[i].x=x;
tempFour_0->FOUR[i].y=y-top;
top--;
i++;
}
tempFour_0->next=NULL;
temp.four->next=tempFour_0;
continuumChessmanCom.CountFour++;
}
/
if(leftT+rightB==3)
{
while(temp.four->next!=NULL)temp.four=temp.four->next;
FourChessman tempFour_0=(FourChessman)malloc(sizeof(struct four));
while(leftT>0)
{
int i=0;
tempFour_0->FOUR[i].x=x-leftT;
tempFour_0->FOUR[i].y=y-leftT;
leftT--;
i++;
}
while(rightB>=0)
{
int i=3;
tempFour_0->FOUR[i].x=x+rightB;
tempFour_0->FOUR[i].y=y+rightB;
rightB--;
i++;
}
tempFour_0->next=NULL;
temp.four->next=tempFour_0;
continuumChessmanCom.CountFour++;
}
//
if(leftB+rightT==3)
{
while(temp.four->next!=NULL)temp.four=temp.four->next;
FourChessman tempFour_0=(FourChessman)malloc(sizeof(struct four));
while(leftB>0)
{
int i=0;
tempFour_0->FOUR[i].x=x-leftB;
tempFour_0->FOUR[i].y=y+leftB;
leftB--;
i++;
}
while(rightT>=0)
{
int i=3;
tempFour_0->FOUR[i].x=x+rightT;
tempFour_0->FOUR[i].y=y-rightT;
rightT--;
i++;
}
tempFour_0->next=NULL;
temp.four->next=tempFour_0;
continuumChessmanCom.CountFour++;
}
///
//8888888888888888888888888888888888888888///
/
if(left+right==2)
{
while(temp.three->next!=NULL)temp.three=temp.three->next;
ThreeChessman tempThree_0=(ThreeChessman)malloc(sizeof(struct three));
while(left>0)
{
int i=0;
tempThree_0->THREE[i].x=x-left;
tempThree_0->THREE[i].y=y;
left--;
i++;
}
while(right>=0)
{
int i=2;
tempThree_0->THREE[i].x=x+right;
tempThree_0->THREE[i].y=y;
right--;
i++;
}
tempThree_0->next=NULL;
temp.three->next=tempThree_0;
continuumChessmanCom.CountThree++;
}
/
if(top+bottom==2)
{
while(temp.three->next!=NULL)temp.three=temp.three->next;
ThreeChessman tempThree_0=(ThreeChessman)malloc(sizeof(struct three));
while(bottom>0)
{
int i=0;
tempThree_0->THREE[i].x=x;
tempThree_0->THREE[i].y=y+bottom;
bottom--;
i++;
}
while(top>=0)
{
int i=2;
tempThree_0->THREE[i].x=x;
tempThree_0->THREE[i].y=y-top;
top--;
i++;
}
tempThree_0->next=NULL;
temp.three->next=tempThree_0;
continuumChessmanCom.CountFour++;
}
/
if(leftT+rightB==2)
{
while(temp.three->next!=NULL)temp.three=temp.three->next;
ThreeChessman tempThree_0=(ThreeChessman)malloc(sizeof(struct three));
while(leftT>0)
{
int i=0;
tempThree_0->THREE[i].x=x-leftT;
tempThree_0->THREE[i].y=y-leftT;
leftT--;
i++;
}
while(rightB>=0)
{
int i=2;
tempThree_0->THREE[i].x=x+rightB;
tempThree_0->THREE[i].y=y+rightB;
rightB--;
i++;
}
tempThree_0->next=NULL;
temp.three->next=tempThree_0;
continuumChessmanCom.CountThree++;
}
//
if(leftB+rightT==2)
{
while(temp.three->next!=NULL)temp.three=temp.three->next;
ThreeChessman tempThree_0=(ThreeChessman)malloc(sizeof(struct three));
while(leftB>0)
{
int i=0;
tempThree_0->THREE[i].x=x-leftB;
tempThree_0->THREE[i].y=y+leftB;
leftB--;
i++;
}
while(rightT>=0)
{
int i=2;
tempThree_0->THREE[i].x=x+rightT;
tempThree_0->THREE[i].y=y-rightT;
rightT--;
i++;
}
tempThree_0->next=NULL;
temp.three->next=tempThree_0;
continuumChessmanCom.CountThree++;
}
///
//8888888888888888888888888888888888888888///
/
/
if(left+right==1)
{
while(temp.two->next!=NULL)temp.two=temp.two->next;
TwoChessman tempTwo_0=(TwoChessman)malloc(sizeof(struct two));
while(left>0)
{
int i=0;
tempTwo_0->TWO[i].x=x-left;
tempTwo_0->TWO[i].y=y;
left--;
i++;
}
while(right>=0)
{
int i=1;
tempTwo_0->TWO[i].x=x+right;
tempTwo_0->TWO[i].y=y;
right--;
i++;
}
tempTwo_0->next=NULL;
temp.two->next=tempTwo_0;
continuumChessmanCom.CountTwo++;
}
/
if(top+bottom==1)
{
while(temp.two->next!=NULL)temp.two=temp.two->next;
TwoChessman tempTwo_0=(TwoChessman)malloc(sizeof(struct two));
while(bottom>0)
{
int i=0;
tempTwo_0->TWO[i].x=x;
tempTwo_0->TWO[i].y=y+bottom;
bottom--;
i++;
}
while(top>=0)
{
int i=1;
tempTwo_0->TWO[i].x=x;
tempTwo_0->TWO[i].y=y-top;
top--;
i++;
}
tempTwo_0->next=NULL;
temp.two->next=tempTwo_0;
continuumChessmanCom.CountFour++;
}
/
if(leftT+rightB==1)
{
while(temp.two->next!=NULL)temp.two=temp.two->next;
TwoChessman tempTwo_0=(TwoChessman)malloc(sizeof(struct two));
while(leftT>0)
{
int i=0;
tempTwo_0->TWO[i].x=x-leftT;
tempTwo_0->TWO[i].y=y-leftT;
leftT--;
i++;
}
while(rightB>=0)
{
int i=1;
tempTwo_0->TWO[i].x=x+rightB;
tempTwo_0->TWO[i].y=y+rightB;
rightB--;
i++;
}
tempTwo_0->next=NULL;
temp.two->next=tempTwo_0;
continuumChessmanCom.CountTwo++;
}
//
if(leftB+rightT==1)
{
while(temp.two->next!=NULL)temp.two=temp.two->next;
TwoChessman tempTwo_0=(TwoChessman)malloc(sizeof(struct two));
while(leftB>0)
{
int i=0;
tempTwo_0->TWO[i].x=x-leftB;
tempTwo_0->TWO[i].y=y+leftB;
leftB--;
i++;
}
while(rightT>=0)
{
int i=1;
tempTwo_0->TWO[i].x=x+rightT;
tempTwo_0->TWO[i].y=y-rightT;
rightT--;
i++;
}
tempTwo_0->next=NULL;
temp.two->next=tempTwo_0;
continuumChessmanCom.CountTwo++;
}
///
return true;
}
/
// AddToContinuumChessman函数定义结束 //
// 添加一个点到ContinuumChessman数据成员中 //
/
// JudgeComNext函数定义开始 //
// 判断一个坐标是否已 //
// 经被加入到ComNext里面 //
///
bool Computer::JudgeComNext(int x,int y)
{
NextPri temp=ComNext;
for(int i=0;i<MAXPRI-1;i++)
{
while(temp.PRI[i]!=NULL)
{
if(temp.PRI[i]->point.x==x&&temp.PRI[i]->point.y==y)
return false;
temp.PRI[i]=temp.PRI[i]->next;
}
}
return true;
}
// JudgeComNext函数定义结束 //
// 判断一个坐标是否已 //
// 经被加入到ComNext里面 //
///
*/
//窗口算法.cpp
#include"五子棋核心算法.h"
char tempNum;
Computer com;
HMENU hMenu;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("兰兰学下棋") ;
HWND hwnd;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style= CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName= szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),szAppName, MB_ICONERROR) ;
return 0 ;
}
hMenu=LoadMenu(hInstance,MAKEINTRESOURCE(IDM_MENU));
hwnd = CreateWindow( szAppName, // window class name
TEXT ("兰兰学下棋"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT,// initial x position
CW_USEDEFAULT,// initial y position
730,// initial x size
600,// initial y size
NULL, // parent window handle
hMenu, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
// RECT rect ;
static int cxClient,cyClient;
static int spaceBetween;
static int cxPos,cyPos;
int tempX,tempY;
static int x=0,y=0;
static int cxChar,cyChar;
TEXTMETRIC tm ;
CString tempI;
CString step;
switch (message)
{
case WM_CREATE:
return 0 ;
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam)-HIWORD(lParam)%NUM;
spaceBetween=((NUM-2)*cyClient/NUM)/(NUM-1);
return 0;
case WM_LBUTTONDOWN:
cxPos=LOWORD(lParam);
cyPos=HIWORD(lParam);
tempX=cxPos/spaceBetween;
tempY=cyPos/spaceBetween;
if(cxPos<tempX*spaceBetween+spaceBetween/2&&
cyPos<tempY*spaceBetween+spaceBetween/2)
{
if(com.JudgeCoordinate(tempX-1,tempY-1))
{
if(com.Result==NOWIN)
{
com.CoordinateR[tempX-1][tempY-1]=OPPCHESSMAN;
com.AddNewChessmanOpp(tempX-1,tempY-1);
com.CoordinateSNum[tempX-1][tempY-1]=com.ComCount+com.OppCount;//各个棋子的先后序号
::InvalidateRect(hwnd,NULL,TRUE);
if(!com.JudgeOppWin())
{
com.AddDefend();
com.AttackNextPri();
com.AddComNewCh();
com.JudgeComWin();
::InvalidateRect(hwnd,NULL,TRUE);
}
}
}
}
if(cxPos<tempX*spaceBetween+spaceBetween/2&&
cyPos>tempY*spaceBetween+spaceBetween/2)
{
if(com.JudgeCoordinate(tempX-1,tempY))
{
if(com.Result==NOWIN)
{
com.CoordinateR[tempX-1][tempY]=OPPCHESSMAN;
com.AddNewChessmanOpp(tempX-1,tempY);
com.CoordinateSNum[tempX-1][tempY]=com.ComCount+com.OppCount;//各个棋子的先后序号
::InvalidateRect(hwnd,NULL,TRUE);
if(!com.JudgeOppWin())
{
com.AddDefend();
com.AttackNextPri();
com.AddComNewCh();
com.JudgeComWin();
::InvalidateRect(hwnd,NULL,TRUE);
}
}
}
}
if(cxPos>tempX*spaceBetween+spaceBetween/2&&
cyPos<tempY*spaceBetween+spaceBetween/2)
{
if(com.JudgeCoordinate(tempX,tempY-1))
{
if(com.Result==NOWIN)
{
com.CoordinateR[tempX][tempY-1]=OPPCHESSMAN;
com.AddNewChessmanOpp(tempX,tempY-1);
com.CoordinateSNum[tempX][tempY-1]=com.ComCount+com.OppCount;//各个棋子的先后序号
::InvalidateRect(hwnd,NULL,TRUE);
if(!com.JudgeOppWin())
{
com.AddDefend();
com.AttackNextPri();
com.AddComNewCh();
com.JudgeComWin();
::InvalidateRect(hwnd,NULL,TRUE);
}
}
}
}
if(cxPos>tempX*spaceBetween+spaceBetween/2&&
cyPos>tempY*spaceBetween+spaceBetween/2)
{
if(com.JudgeCoordinate(tempX,tempY))
{
if(com.Result==NOWIN)
{
com.CoordinateR[tempX][tempY]=OPPCHESSMAN;
com.AddNewChessmanOpp(tempX,tempY);
com.CoordinateSNum[tempX][tempY]=com.ComCount+com.OppCount;//各个棋子的先后序号
::InvalidateRect(hwnd,NULL,TRUE);
if(!com.JudgeOppWin())
{
com.AddDefend();
com.AttackNextPri();
com.AddComNewCh();
com.JudgeComWin();
::InvalidateRect(hwnd,NULL,TRUE);
}
}
}
}
::InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_PAINT:
///获取字体的宽度-开始/
hdc = GetDC (hwnd) ;
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cyChar = tm.tmHeight+tm.tmExternalLeading;
ReleaseDC (hwnd, hdc) ;
///获取字体的宽度-结束/
hdc = BeginPaint (hwnd, &ps) ;
Rectangle(hdc,spaceBetween-1,spaceBetween-1,spaceBetween*NUM,spaceBetween*NUM);
::TextOut(hdc,spaceBetween*4/10,spaceBetween*4/10,TEXT("0"),1);
for(int i=0;i<=NUM-1;i++)
{
MoveToEx(hdc,spaceBetween,spaceBetween+spaceBetween*i,NULL);
LineTo(hdc,spaceBetween*NUM,spaceBetween+spaceBetween*i);
MoveToEx(hdc,spaceBetween+spaceBetween*i,spaceBetween,NULL);
LineTo(hdc,spaceBetween+spaceBetween*i,spaceBetween*NUM);
tempI.Format("%d",i);
if(i!=0&&i<=9)
{
TextOut(hdc,spaceBetween*(i+1)-cxChar/2,spaceBetween*2/10,tempI,1);
TextOut(hdc,spaceBetween*2/10,spaceBetween*(i+1)-cxChar,tempI,1);
}
if(i!=0&&i>9)
{
TextOut(hdc,spaceBetween*(i+1)-cxChar,spaceBetween*2/10,tempI,2);
TextOut(hdc,spaceBetween*2/10,spaceBetween*(i+1)-cxChar,tempI,2);
}
}//绘制棋盘横竖线条
HBRUSH hBrush;
hBrush=(HBRUSH)SelectObject(hdc,GetStockObject(BLACK_BRUSH));//保存原来的画刷用于恢复
Rectangle(hdc,4*spaceBetween-spaceBetween*1/5,4*spaceBetween-spaceBetween*1/5,
4*spaceBetween+spaceBetween*1/5,4*spaceBetween+spaceBetween*1/5);
Rectangle(hdc,12*spaceBetween-spaceBetween*1/5,4*spaceBetween-spaceBetween*1/5,
12*spaceBetween+spaceBetween*1/5,4*spaceBetween+spaceBetween*1/5);
Rectangle(hdc,4*spaceBetween-spaceBetween*1/5,12*spaceBetween-spaceBetween*1/5,
4*spaceBetween+spaceBetween*1/5,12*spaceBetween+spaceBetween*1/5);
Rectangle(hdc,12*spaceBetween-spaceBetween*1/5,12*spaceBetween-spaceBetween*1/5,
12*spaceBetween+spaceBetween*1/5,12*spaceBetween+spaceBetween*1/5);
SelectObject(hdc,hBrush);//恢复原来的画刷
::TextOut(hdc,spaceBetween*(NUM+1),spaceBetween*2,TEXT("Player time:"),12);
::TextOut(hdc,spaceBetween*(NUM+1),spaceBetween*3,TEXT("Computer time:"),14);
::TextOut(hdc,spaceBetween*(NUM+1),spaceBetween*5,TEXT("Step:"),5);
if(com.Result==0)
step.Format("%d",com.ComCount+com.OppCount);
else
step.Format("%d",com.ComCount+com.OppCount+1);
if(com.ComCount+com.OppCount<10)
::TextOut(hdc,spaceBetween*(NUM+1)+5*cxChar,spaceBetween*5,step,1);
else if(com.ComCount+com.OppCount>=10&&com.ComCount+com.OppCount<100)
::TextOut(hdc,spaceBetween*(NUM+1)+5*cxChar,spaceBetween*5,step,2);
else
::TextOut(hdc,spaceBetween*(NUM+1)+5*cxChar,spaceBetween*5,step,3);
for(int i=0;i<NUM;i++)
for(int j=0;j<NUM;j++)
{
if(com.CoordinateR[i][j]!=NOCHESSMAN)
{
int tempCooX=(i+1)*spaceBetween;
int tempCooY=(j+1)*spaceBetween;
CString snum;
snum.Format("%d",com.CoordinateSNum[i][j]);//com.GetComCount()+com.GetOppCount());
if(com.CoordinateR[i][j]==COMCHESSMAN)//如果是电脑的棋子
{
HBRUSH hBrush;
hBrush=(HBRUSH)SelectObject(hdc,GetStockObject(BLACK_BRUSH));//保存原来的画刷用于恢复
Ellipse(hdc,tempCooX-(spaceBetween/2)*18/20,tempCooY-(spaceBetween/2)*18/20,
tempCooX+(spaceBetween/2)*18/20,tempCooY+(spaceBetween/2)*18/20);
SelectObject(hdc,hBrush);//恢复原来的画刷
//改变字体颜色和字体背景颜色-开始
HDC hdc2;
hdc2 = GetDC (hwnd) ;
::SetBkColor(hdc2,RGB(0,0,0));
::SetTextColor(hdc2,RGB(255,255,255));
if(com.CoordinateSNum[i][j]<10)
::TextOut(hdc2,tempCooX-cxChar/2-1,tempCooY-cyChar/2,snum,1);
else if(com.CoordinateSNum[i][j]>=10&&com.CoordinateSNum[i][j]<100)
::TextOut(hdc2,tempCooX-cxChar-1,tempCooY-cyChar/2,snum,2);
else
::TextOut(hdc2,tempCooX-cxChar*3/2,tempCooY-cyChar/2,snum,3);
::SetTextColor(hdc2,RGB(0,0,0));
::SetBkColor(hdc2,RGB(255,255,255));
::ReleaseDC(hwnd,hdc2);
//改变字体颜色和字体背景颜色-结束
}
else//如果是对手的棋子
{
Ellipse(hdc,tempCooX-(spaceBetween/2)*18/20,tempCooY-(spaceBetween/2)*18/20,
tempCooX+(spaceBetween/2)*18/20,tempCooY+(spaceBetween/2)*18/20);
//改变字体颜色和字体背景颜色-开始
HDC hdc2;
hdc2 = GetDC (hwnd) ;
::SetBkColor(hdc2,RGB(255,255,255));
::SetTextColor(hdc2,RGB(0,0,0));
if(com.CoordinateSNum[i][j]<10)
::TextOut(hdc2,tempCooX-cxChar/2-1,tempCooY-cyChar/2,snum,1);
else if(com.CoordinateSNum[i][j]>=10&&com.CoordinateSNum[i][j]<100)
::TextOut(hdc2,tempCooX-cxChar-1,tempCooY-cyChar/2,snum,2);
else
::TextOut(hdc2,tempCooX-cxChar*3/2,tempCooY-cyChar/2,snum,3);
::SetTextColor(hdc2,RGB(255,255,255));
::SetBkColor(hdc2,RGB(0,0,0));
::ReleaseDC(hwnd,hdc2);
//改变字体颜色和字体背景颜色-结束
}
}
}
EndPaint (hwnd, &ps);
if(com.Result==COMWIN)
MessageBox (NULL, TEXT ("计算机赢了!"), TEXT ("GAME OVER"), 0) ;
else if(com.Result==OPPWIN)
MessageBox (NULL, TEXT ("您赢了!"), TEXT ("GAME OVER"), 0) ;
return 0 ;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_OPEN:
com.DeleComExistChessman();
com.DeleOppExistChessman();
com.DeleComNext();
com.DeleCoordinateR();
com.Result=0;
::InvalidateRect(hwnd,NULL,TRUE);
break;
default:
break;
}
return 0;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}