/* This program is maked by nanchang*/
/* univercisty student ,The mechine */
/**/
/**/
/**/
/**/
#include<stdio.h>
#include<dos.h>
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
#include<bios.h>
#include<string.h>
#include<math.h>
#define ESC 0x11b
#define LEFT 0x4b00
#define UP 0x4800
#define RIGHT 0x4d00
#define DOWN 0x5000
#define SPACE 0x3920
#define F2 0x3c00
#define F3 0x3d00
#define F4 0x3e00
/*x0 y0 scren is the course init postion*/
int x0,y0,map[15][15];/**/
int regret[125][2];/**/
int Found_Best,First_Arrive;/**/
int Depth_TT,Best_Num;
typedef struct ThreeTree/**/
{
int i; /**/
int j; /**/
int num; /**/
struct ThreeTree *parent;/**/
struct ThreeTree *LChild;/**/
struct ThreeTree *MChild;/**/
struct ThreeTree *RChild;/**/
}TNode,*TTree; /**/
void InitGraph();/**/
void CloseGraph();/**/
void DrawMap(); /**/
void DrawChess(); /**/
void GameControl(); /**/
void DrawCourse(int Course_I,int Course_J,int flag);/**/
void DrawRegretChess(int Course_I,int Course_J); /**/
void SaveMap(int regret[][2],int Regret_Count);
void LoadMap(int regret[][2],int *Regret_Count);
int DepthTT(TTree TT);
void CreatTT(TTree TT,int mode);
int RearshBest(TTree TT,int *C_I,int *C_J);
void DestoryTT(TTree TT);
void ComputerThink(int *C_I,int *C_J,int mode);
void SetMap(TTree TT);
void ClearMap(TTree TT);
void GetComputerIJ(TTree TT,int *C_I,int *C_J);
void GetPersonIJ(TTree TT,int *C_I,int *C_J);
int LinkSS4(int C_I,int C_J);/*211110 or 211101 or 211011*/
/*210111 or 11110 or 11101 */
/*11011 or 10111*/
int LinkSS3(int C_I,int C_J);/*211100 or 211010 or 211001*/
/*210110 or 210101 or 21011*/
/*11100 or 10110 or 10011 */
/*10101*/
int LinkS5(int C_I,int C_J);/*1011101*/
int LinkS4(int C_I,int C_J);/*1010101*/
int LinkS3(int C_I,int C_J);/*1010100*/
int LinkF5(int C_I,int C_J);/*11111*/
int LinkF4(int C_I,int C_J);/*11110*/
int LinkF3(int C_I,int C_J);/*011100 or 010110*/
int LinkF2(int C_I,int C_J);/*011000 or 010100 or 010010*/
int LinkF1(int C_I,int C_J);/*1*/
void DrawTextXY(int x1,int y1,int x2,int y2,char s[]);
int PriorityGet(int C_I,int C_J,int num);
void ScanMap(int b[3][3],int *N,int mode);
main(){
InitGraph();
x0=(getmaxx()-470)/2;
y0=(getmaxy()-330)/2;
DrawMap();
GameControl();
CloseGraph();
}
void GameControl(){
int Course_I=7,Course_J=7,key,mode=1,Regret_Count=0;
int Computer_I,Computer_J,Game_Over=0,i;
char str[20];
strcpy(str,"Five Chess");
DrawTextXY(x0+350,y0+10,x0+470,y0+30,str);
strcpy(str,"");
DrawCourse(Course_I,Course_J,1);
do
{
Computer_I=-1;
Computer_J=-1;
key=bioskey(0);
switch(key){
case UP:
DrawCourse(Course_I,Course_J,0);
if(Course_I>0)Course_I--;
else Course_I=14;
break;
case LEFT:
DrawCourse(Course_I,Course_J,0);
if(Course_J>0)Course_J--;
else Course_J=14;
break;
case RIGHT:
DrawCourse(Course_I,Course_J,0);
if(Course_J<14)Course_J++;
else Course_J=0;
break;
case DOWN:
DrawCourse(Course_I,Course_J,0);
if(Course_I<14)Course_I++;
else Course_I=0;
break;
case SPACE:
if(!map[Course_I][Course_J])
{
map[Course_I][Course_J]=mode;
regret[Regret_Count][0]=Course_I;
regret[Regret_Count][1]=Course_J;
Regret_Count++;
if(LinkF5(Course_I,Course_J))
{
Game_Over=1;
break;
}
mode=3-mode;
DrawChess();
DrawTextXY(x0+325,y0+190,x0+478,y0+210," Thinking...");
ComputerThink(&Computer_I,&Computer_J,mode);
DrawTextXY(x0+325,y0+190,x0+478,y0+210," ");
if(Computer_I!=-1)
{
map[Computer_I][Computer_J]=mode;
regret[Regret_Count][0]=Computer_I;
regret[Regret_Count][1]=Computer_J;
Regret_Count++;
DrawCourse(Course_I,Course_J,0);
Course_I=Computer_I;
Course_J=Computer_J;
if(LinkF5(Course_I,Course_J))
{
Game_Over=1;
break;
}
mode=3-mode;
}
}
break;
case F2:
SaveMap(regret,Regret_Count--);
break;
case F3:
LoadMap(regret,&Regret_Count);
Course_I=regret[Regret_Count-1][0];
Course_J=regret[Regret_Count-1][1];
break;
case F4:
if(Regret_Count>0)
{
if(Regret_Count%2)mode=2;
for(i=0;i<2;i++)
{
Regret_Count--;
DrawRegretChess(regret[Regret_Count][0],regret[Regret_Count][1]);
map[regret[Regret_Count][0]][regret[Regret_Count][1]]=0;
DrawCourse(Course_I,Course_J,0);
Course_I=regret[Regret_Count][0];
Course_J=regret[Regret_Count][1];
mode=3-mode;
}
}
break;
}/*end switch*/
DrawCourse(Course_I,Course_J,1);
DrawChess();
}while(key!=ESC&&Game_Over!=1);
if(mode==1)
{
DrawTextXY(x0+350,y0+190,x0+470,y0+210,"You Win");
}
else
{
DrawTextXY(x0+350,y0+190,x0+470,y0+210,"Computer Win");
}
sleep(2);
CloseGraph();
}
void SaveMap(int regret[][2],int Regret_Count)
{
char File_Name[12],str[50];
char ch;
int i=0;
FILE *fp;
DrawTextXY(x0+350,y0+190,x0+470,y0+210,"Input File name");
ch='a';
for(i=0;ch!='\r'&&i<8;i++)
{
ch=getch();
if(i>0&&ch=='\b')
{
File_Name[i-1]=File_Name[i];
i-=2;
}
else if(ch!='\r'&&ch!='b')
{ File_Name[i]=ch;
File_Name[i+1]='\0';
}
DrawTextXY(x0+350,y0+190,x0+470,y0+210,File_Name);
}
strcat(File_Name,".map");
strcpy(str,"");
strcpy(str,"Cann't open ");
strcat(str,File_Name);
if((fp=fopen(File_Name,"a+"))==NULL)
{
DrawTextXY(x0+350,y0+190,x0+470,y0+210,str);
sleep(2);
return;
}
for(i=0;i<Regret_Count;i++)
{
fprintf(fp,"%d %d\n",regret[i][0],regret[i][1]);
}
fclose(fp);
DrawTextXY(x0+350,y0+190,x0+470,y0+210,"File success save");
sleep(2);
DrawTextXY(x0+350,y0+190,x0+470,y0+210,"");
}
void LoadMap(int regret[][2],int *Regret_Count)
{
char File_Name[12],str[50];
char ch;
int i=0,mode;
FILE *fp;
DrawTextXY(x0+350,y0+190,x0+470,y0+210,"Input File name");
ch='a';
for(i=0;ch!='\r'&&i<8;i++)
{
ch=getch();
if(i>0&&ch=='\b')
{
File_Name[i-1]=File_Name[i];
i-=2;
}
else if(ch!='\r'&&ch!='b')
{ File_Name[i]=ch;
File_Name[i+1]='\0';
}
DrawTextXY(x0+350,y0+190,x0+470,y0+210,File_Name);
}
strcat(File_Name,".map");
strcpy(str,"");
strcpy(str,"Cann't open ");
strcat(str,File_Name);
if((fp=fopen(File_Name,"r+"))==NULL)
{
DrawTextXY(x0+350,y0+190,x0+470,y0+210,str);
sleep(2);
return;
}
for(i=0;i<(*Regret_Count);i++)
{
map[regret[*Regret_Count][0]][regret[*Regret_Count][1]]=0;
}
*Regret_Count=0;
while(!feof(fp))
{
fscanf(fp,"%d%d",®ret[*Regret_Count][0],®ret[*Regret_Count][1]);
if((*Regret_Count)%2==0)mode=2;
else mode=1;
map[regret[*Regret_Count][0]][regret[*Regret_Count][1]]=mode;
(*Regret_Count)++;
}
fclose(fp);
}
void ComputerThink(int *C_I,int *C_J,int mode)
{
TTree HeadTT,T;
Found_Best=0;
First_Arrive=0;
Depth_TT=-1;
Best_Num=0;
HeadTT=(TTree)malloc(sizeof(TNode));
HeadTT->parent=NULL;
HeadTT->LChild=NULL;
HeadTT->MChild=NULL;
HeadTT->RChild=NULL;
HeadTT->i=-1;
HeadTT->j=-1;
HeadTT->num=-1;
T=HeadTT;
CreatTT(T,mode);
RearshBest(HeadTT,C_I,C_J);
DestoryTT(HeadTT);
}
int RearshBest(TTree TT,int *C_I,int *C_J)
{
int N;
if(Found_Best==1)return;
if(TT->LChild)RearshBest(TT->LChild,C_I,C_J);
if(TT->MChild)RearshBest(TT->MChild,C_I,C_J);
if(TT->RChild)RearshBest(TT->RChild,
评论1
最新资源