📄 pvscom.h
字号:
#include "head.h"
/*头文件、常量定义*/
int moveTimes(int color)/* 检测行动力,color代表棋子颜色 */
{
int i,j;
int n=0;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
if (hb.a[i][j]==0&(judge1(i,j,color,0)||judge2(i,j,color,0)||judge3(i,j,color,0)||judge4(i,j,color,0)||judge5(i,j,color,0)||judge6(i,j,color,0)||judge7(i,j,color,0)||judge8(i,j,color,0)))
n++;
}
}
return n;
}
int judgeEnter(int x,int y,int color)/* 判断x,y位置能吃掉对方棋子个数,返回0则表示不能落子,color表示棋子颜色 */
{
int n=0;
if(hb.a[x][y]) return 0;
n += judge1(x,y,color,0);
n += judge2(x,y,color,0);
n += judge3(x,y,color,0);
n += judge4(x,y,color,0);
n += judge5(x,y,color,0);
n += judge6(x,y,color,0);
n += judge7(x,y,color,0);
n += judge8(x,y,color,0);
return n;
}
void drawLast(int x,int y,int color)/* 用动画显示吃掉对方棋子 */
{
if(color == 1)
drawQZ(getX(x),getY(y),8);
else
drawQZ(getX(x),getY(y),15);
if(judge1(x,y,color,0)) judge1(x,y,color,1);
if(judge2(x,y,color,0)) judge2(x,y,color,1);
if(judge3(x,y,color,0)) judge3(x,y,color,1);
if(judge4(x,y,color,0)) judge4(x,y,color,1);
if(judge5(x,y,color,0)) judge5(x,y,color,1);
if(judge6(x,y,color,0)) judge6(x,y,color,1);
if(judge7(x,y,color,0)) judge7(x,y,color,1);
if(judge8(x,y,color,0)) judge8(x,y,color,1);
}
void playGame(void)/*人机对战函数*/
{
int key,i,j;/* 接受键盘扫描码,循环变量 */
int x,y;/* 临时存储光标位置 */
int max,min;
int ch;
/* 初始化棋盘 */
hb.s1=2;
hb.s2=2;/* 初始化分数 */
for(i=0;i<SIZE;i++) /* 初始化棋盘 */
for(j=0;j<SIZE;j++)
hb.a[i][j]=0;
hb.a[3][3] = 2; /* 初始化开始的四个棋子 */
hb.a[4][4] = 2;
hb.a[4][3] = 1;
hb.a[3][4] = 1;
drawQZ(getX(3),getY(3),15);
drawQZ(getX(4),getY(4),15);
drawQZ(getX(4),getY(3),8);
drawQZ(getX(3),getY(4),8);
hb.x = 2;
hb.y = 2;
hb.read = 8;/* 表示该黑棋走 */
dispMouse(getX(hb.x),getY(hb.y),15);
while(1) /* 人机对战开始游戏 */
{
printScore();
if(ch=judgeGameover())
{
Gameover(ch);
if(judgeYorN())
{
cleardevice();/* 清屏函数 */
playFace();
playGame();
}
else
break;
}
if(hb.read == 8)
{
if(!moveTimes(1)) /* 如果玩家没有移动力,则将权限交给电脑 */
{
hb.read = 15;
continue;
}
while(1)
{
key=bioskey(0);
if(key == ESC)
{
if(judgeYorN()==0)
return;
}
else if(key==UP||key==DOWN||key==RIGHT||key==LEFT)
{
dispMouse(getX(hb.x),getY(hb.y),0);
if(key == UP && hb.y > 0)
hb.y--;
else if(key == DOWN && hb.y < (SIZE-1))
hb.y++;
else if(key == LEFT && hb.x > 0)
hb.x--;
else if(key == RIGHT && hb.x <(SIZE-1))
hb.x++;
dispMouse(getX(hb.x),getY(hb.y),15);
}
else if((key==ENTER||key==SPACE)&&hb.a[hb.x][hb.y]==0)
{
i = hb.x;j=hb.y;
if(ch = judgeEnter(hb.x,hb.y,1))
{
drawLast(hb.x,hb.y,1);
hb.read = 15;
hb.a[i][j] = 1;
hb.s1+=ch+1;
hb.s2-=ch;
break;
}
}
}
}
else if(hb.read == 15)
{
/* 四角优先战术 */
ch = 0;/* 假设四角都不能落子 */
for(i=0;i<4;i++)
{
x = aa[i][0];
y = aa[i][1];
if(ch = judgeEnter(x,y,2)) /* 判断四角能否落子,如果能罗则退出循环 */
break;
}
if(ch)/* 如果四个角能落子 */
{
drawLast(x,y,2);
hb.a[x][y] = 2;
hb.read=8;
hb.s1-=ch;
hb.s2+=ch+1;
continue;
}
min = 100;
for(i=0;i<SIZE;i++)/* 扫描棋盘情况 */
{
for(j=0;j<SIZE;j++)
{
if(i==0&&j==1&&hb.a[0][0]==0)continue;/* 尽量不在四个角的相邻格下棋 */
else if(i==1&&j==0&&hb.a[0][0]==0)continue;
else if(i==1&&j==1&&hb.a[0][0]==0)continue;
else if(i==0&&j==6&&hb.a[0][7]==0)continue;
else if(i==6&&j==0&&hb.a[0][7]==0)continue;
else if(i==1&&j==6&&hb.a[0][7]==0)continue;
else if(i==7&&j==1&&hb.a[7][0]==0)continue;
else if(i==1&&j==7&&hb.a[7][0]==0)continue;
else if(i==6&&j==1&&hb.a[7][0]==0)continue;
else if(i==7&&j==6&&hb.a[7][7]==0)continue;
else if(i==6&&j==7&&hb.a[7][7]==0)continue;
else if(i==6&&j==6&&hb.a[7][7]==0)continue;
if(judgeEnter(i,j,2))/* 如果该位置可以落子 */
{
hb.a[i][j] = 2;/* 假设在该位置落子 */
if(min>(max=moveTimes(1)))
{
x = i;
y = j;
min = max;
}
hb.a[i][j] = 0;/* 取消假设 */
}
}
}
if(min!=100)
{
sleep(1);
ch = judgeEnter(x,y,2);
drawLast(x,y,2);
hb.a[x][y] = 2;
hb.read=8;
hb.s1-=ch;
hb.s2+=ch+1;
continue;
}
else
{
max = 0;
for(i=0;i<12;i++)/* 扫描棋盘情况 */
{
if(ch = judgeEnter(bb[i][0],bb[i][1],2))/* 如果该位置可以落子 */
{
if(max<ch)
{
x = bb[i][0];
y = bb[i][1];
max = ch;
}
}
}
if(max)
{
sleep(1);
drawLast(x,y,2);
hb.a[x][y] = 2;
hb.read=8;
hb.s1-=max;
hb.s2+=max+1;
continue;
}
else
hb.read = 8;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -