资源简介
本人的C语言课程设计,用了一个较好理解的算法,不过智能还不错,对于人工智能入门的人学习是很有帮助的。在wintc下编译通过。
代码片段和文件信息
#include “stdio.h“
#include “graphics.h“
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define W 0x1177
#define S 0x1f73
#define A 0x1e61
#define D 0x2064
#define SPACE 0x3920
#define ESC 0x011b
#define ENTER 0x1c0d
struct type
{
int defend;
int offend;
};//made by linju
struct point
{
int x;
int y;
int flag;
int sum;
};//made by chenjian
typedef struct point POINT;
POINT move(POINT tint s[16][16]);
POINT computer(POINT tint s[16][16]);
int init(void);
void outputMap(int a[16][16]);
void draw(int xint yint i);
void checkwin(POINT iint a[16][16]);
int getscore(int iint jint s[16][16]);
void cursor(int xint yint i);
void main()
{
int gdriver=DETECTgmode;
POINT player1player2;
int k[16][16];
int ijm;
initgraph(&gdriver&gmode“C:\\JMSOFT\\DRV“);
player1.x=player2.x=240;
player1.y=player2.y=240;
player1.flag=1;
player2.flag=-1;
for(i=0;i<=15;i++)
for(j=0;j<=15;j++)
k[i][j]=0;
m=init();
if(m==1){
outputMap(k);
while(1)
{
player1=move(player1k);
outputMap(k);
checkwin(player1k);
player2=move(player2k);
outputMap(k);
checkwin(player2k);
}
}
if(m==2){
outputMap(k);
while(1)
{
player1=move(player1k);
outputMap(k);
checkwin(player1k);
player2=computer(player2k);
outputMap(k);
checkwin(player2k);
}
}
getch();
closegraph();
}
int init()
{
int i;
int key;
int x=180y=353;
settextstyle(0 0 3);
setbkcolor(YELLOW);
setcolor(CYAN);
outtextxy(80 120 “Five In A Row“);
for(i=0;i<5;i++){
circle(140+i*8021012);
}
settextstyle(001);
setcolor(BLUE);
outtextxy(200 300 “By linju and chenjian“);
settextstyle(001);
outtextxy(200 350 “human to human battle“);
outtextxy(200 380 “human to computer battle“);
cursor(xy2);
setfillstyle(1YELLOW);
while(1)
{
key=bioskey(0);
if(key==UP||key==DOWN)
{
if(y==353)
{
bar(120320200420);
y+=30;
cursor(xy3);
}
else
{
bar(150350200420);
y-=30;
cursor(xy2);
}
}
if(key==ENTER){
cleardevice();
if(y==353) return 1;
if(y==383) return 2;
}
}
}
void checkwin(POINT iint a[16][16])
{
int k[4]={1111};
int p;
POINT t1=i;
POINT t2=i;
t1.x-=20;
while(a[(t1.y-100)/20][(t1.x-100)/20]==i.flag)//左
{
k[0]+=1;
t1.x-=20;
}
t2.x+=20;//右
while(a[(t2.y-100)/20][(t2.x-100)/20]==i.flag)
{
k[0]+=1;
t2.x+=20;
}
t1=i;
t2=i;
t1.y-=20;//竖直向上
while(a[(t1.y-100)/20][(t1.x-100)/20]==i.flag)
{
k[1]+=1;
t1.y-=20;
}
t2.y+=20;//竖直向下
while(a[(t2.y-100)/20][(t2.x-100)/20]==i.flag)
{
k[1]+=1;
t2.y+=20;
}
t1=i;
t2=i;
t1.y-=20;//这个/方向
t1.x+=20;
while(a[(t1.y-100)/20][(t1.x-100)/20]==i.flag)
{
k[2]+=
评论
共有 条评论