#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
char qipan[16][16];//设置棋盘变量
int loopa1,loopa2,loopput1,loopput2;
//棋子&电脑变量
char qizi='#';
int cps=0,cpx=0,cpy=0,cpfx=0,playx=0,playy=0;
//更新棋盘
int putqipan()
{
clrscr();//清除屏幕函数
printf("输入1~9或a~f的字符组成一个坐标来放置棋子,两坐标值以空格隔开,如2 6,表示在横坐标为2,纵坐标为6的位置放置一个棋子\n以5颗棋子相连为胜利条件!\n");
//把棋盘各点显示在屏幕上
for (loopput1=0;loopput1<16;loopput1++)
{
for (loopput2=0;loopput2<32;loopput2++)
{
if (loopput2%2==0) printf("%c",qipan[loopput1][loopput2/2]);
else printf(" ");//控制棋盘长度
}
printf("\n");
}
printf("输入坐标>>>>>");
}
//ai下棋指令
bool chessdown()//指定方向下棋
{
int icpx,icpy;
icpx=cpx;
icpy=cpy;
switch (cpfx)
{
case 0:icpx++;break;
case 1:icpx--;break;
case 2:icpy++;break;
case 3:icpy--;break;
case 4:icpx++,icpy++;break;
case 5:icpx++,icpy--;break;
case 6:icpx--,icpy++;break;
case 7:icpx--,icpy--;break;
}
if (qipan[icpx][icpy]!='*'&&qipan[icpx][icpy]!='#'&&(icpx>0&&icpx<=15)&&(icpy>0&&icpy<=15))
{
cpx=icpx;
cpy=icpy;
qipan[cpx][cpy]=qizi;
return true;
}
else return false;
}
bool antidown()//反方向下棋
{
int icpx,icpy;
icpx=cpx;
icpy=cpy;
while (1)
{
switch (cpfx)
{
case 0:icpx--;break;
case 1:icpx++;break;
case 2:icpy--;break;
case 3:icpy++;break;
case 4:icpx--,icpy--;break;
case 5:icpx--,icpy++;break;
case 6:icpx++,icpy--;break;
case 7:icpx++,icpy++;break;
}
if (qipan[icpx][icpy]==qizi);
else if (qipan[icpx][icpy]!='*'&&qipan[icpx][icpy]!='#'&&(icpx>0&&icpx<=15)&&(icpy>0&&icpy<=15))
{
cpx=icpx;
cpy=icpy;
qipan[cpx][cpy]=qizi;
return true;
break;
}
else
{
cps=0;
return false;
break;
}
}
}
bool defence()//阻止对方胜利
{
int qh1,qh2,qs1,qs2,qp1,qp2,qn1,qn2,qi;
qh1=qh2=qs1=qs2=qp1=qp2=qn1=qn2=0;
char qz;
bool stop=false;
if (qizi=='#') qz='*';
else qz='#';
//两侧封杀
for (qi=1;qipan[playx][playy-qi]==qz;qi++)
qh1++;
for (qi=1;qipan[playx][playy+qi]==qz;qi++)
qh2++;
//横向档棋
if (qh1+qh2+1>=3)
{
if (qipan[playx][playy-qh1-1]!='*'&&qipan[playx][playy-qh1-1]!='#'&&(playx>0&&playx<=15)&&(playy-qh1-1>0&&playy-qh1-1<=15))
{
qipan[playx][playy-qh1-1]=qizi;
stop=true;
return true;
}
else if (qipan[playx][playy+qh2+1]!='*'&&qipan[playx][playy+qh2+1]!='#'&&(playx>0&&playx<=15)&&(playy+qh2+1>0&&playy+qh2+1<=15))
{
qipan[playx][playy+qh2+1]=qizi;
stop=true;
return true;
}
}
if (stop==false )
{
for (qi=1;qipan[playx-qi][playy]==qz;qi++)
qs1++;
for (qi=1;qipan[playx+qi][playy]==qz;qi++)
qs2++;
if (qs1+qs2+1>=3)//竖向挡棋
{
if (qipan[playx-qs1-1][playy]!='*'&&qipan[playx-qs1-1][playy]!='#'&&(playx-qs1-1>0&&playx-qs1-1<=15)&&(playy>0&&play
y<=15))
{
qipan[playx-qs1-1][playy]=qizi;
stop=true;
return true;
}
else if (qipan[playx+qs2+1][playy]!='*'&&qipan[playx+qs2+1][playy]!='#'&&(playx*qs2+1>0&&playx+qs2+1<=15)&&(playy>0&&playy<=15))
{
qipan[playx+qs2+1][playy]=qizi;
stop=true;
return true;
}
}
}
if (stop==false )
{
for (qi=1;qipan[playx-qi][playy-qi]==qz;qi++)
qp1++;
for (qi=1;qipan[playx+qi][playy+qi]==qz;qi++)
qp2++;
if (qp1+qp2+1>=3)//左斜挡棋
{
if (qipan[playx-qp1-1][playy-qp1-1]!='*'&&qipan[playx-qp1-1][playy-qp1-1]!='#'&&(playx-qp1-1>0&&playx-qp1-1<=15)&&(playy-qp1-1>0&&playy-qp1-1<=15))
{
qipan[playx-qp1-1][playy-qp1-1]=qizi;
stop=true;
return true;
}
else if (qipan[playx+qp2+1][playy+qp2+1]!='*'&&qipan[playx+qp2+1][playy+qp2+1]!='#'&&(playx+qp2+1>0&&playx+qp2+1<=15)&&(playy+qp2+1>0&&playy+qp2+1<=15))
{
qipan[playx+qp2+1][playy+qp2+1]=qizi;
stop=true;
return true;
}
}
}
if (stop==false )
{
for (qi=1;qipan[playx+qi][playy-qi]==qz;qi++)
qn1++;
for (qi=1;qipan[playx-qi][playy+qi]==qz;qi++)
qn2++;
if (qn1+qn2+1>=3)//右斜挡棋
{
if (qipan[playx+qn1+1][playy-qn1-1]!='*'&&qipan[playx+qn1+1][playy-qn1-1]!='#'&&(playx+qn1+1>0&&playx+qn1+1<=15)&&(playy-qn1-1>0&&playy-qn1-1<=15))
{
qipan[playx+qn1+1][playy-qn1-1]=qizi;
stop=true;
return true;
}
else if (qipan[playx-qn2-1][playy+qn2+1]!='*'&&qipan[playx-qn2-1][playy+qn2+1]!='#'&&(playx-qn2-1>0&&playx-qn2-1<=15)&&(playy+qn2+1>0&&playy+qn2+1<=15))
{
qipan[playx-qn2-1][playy+qn2+1]=qizi;
stop=true;
return true;
}
}
}
qh1=qh2=qs1=qs2=qp1=qp2=qn1=qn2=0;
//空位封杀
int space1=0,space2=0;
//横向空位封杀
for (qi=1;qipan[playx][playy-qi]!=qizi&&qi<5;qi++)
{
if (qipan[playx][playy-qi]==qz)qh1++;
if (qipan[playx][playy-qi]!='*'&&qipan[playx][playy-qi]!='#')space1++;
}
for (qi=1;qipan[playx][playy+qi]!=qizi&&qi<5;qi++)
{
if (qipan[playx][playy+qi]==qz)qh2++;
if (qipan[playx][playy+qi]!='*'&&qipan[playx][playy+qi]!='#')space2++;
}
if (qh1+qh2+1>=3)
{
for (qi=1;qi<5;qi++)
{
if (qipan[playx][playy-qi]!='*'&&qipan[playx][playy-qi]!='#'&&(playx>0&&playx<=15)&&(playy-qi>0&&playy-qi<=15)&&space1<=space2)
{
qipan[playx][playy-qi]=qizi;
stop=true;
return true;
break;
}
else if (qipan[playx][playy+qi]!='*'&&qipan[playx][playy+qi]!='#'&&(playx>0&&playx<=15)&&(playy+qi>0&&playy+qi<=15)&&space2<=space1)
{
qipan[playx][playy+qi]=qizi;
stop=true;
return true;
break;
}
}
}
if (stop==false )
{
//纵向空位封杀
space1=space2=0;
for (qi=1;qipan[playx-qi][playy]!=qizi&&qi<5;qi++)
{
if (qipan[playx-qi][playy]==qz)qs1++;
if (qipan[playx-qi][playy]!='*'&&qipan[playx-qi][playy]!='#')space1++;
}
for (qi=1;qipan[playx+qi][playy]!=qizi&&qi<5;qi++)
{
if (qipan[playx+qi][playy]==qz)qs2++;
if (qipan[playx+qi][playy]!='*'&&qipan[playx+qi][playy]!='#')space2++;
}
if (qs1+qs2+1>=3)
{电脑c语言编译器
f
or (qi=1;qi<5;qi++)
{
if (qipan[playx-qi][playy]!='*'&&qipan[playx-qi][playy]!='#'&&(playx-qi>0&&playx-qi<=15)&&(playy>0&&playy<=15)&&space1<=space2)
{
qipan[playx-qi][playy]=qizi;
stop=true;
return true;
break;
}
else if (qipan[playx+qi][playy]!='*'&&qipan[playx+qi][playy]!='#'&&(playx+qi>0&&playx+qi<=15)&&(playy>0&&playy<=15)&&space2<=space1)
{
qipan[playx+qi][playy]=qizi;
stop=true;
return true;
break;
}
}
}
}
if (stop==false )
{
//左斜空位封杀
space1=space2=0;
for (qi=1;qipan[playx-qi][playy-qi]!=qizi&&qi<5;qi++)
{
if (qipan[playx-qi][playy-qi]==qz)qp1++;
if (qipan[playx-qi][playy-qi]!='*'&&qipan[playx-qi][playy-qi]!='#')space1++;
}
for (qi=1;qipan[playx+qi][playy+qi]!=qizi&&qi<5;qi++)
{
if (qipan[playx+qi][playy+qi]==qz)qp2++;
if (qipan[playx+qi][playy+qi]!='*'&&qipan[playx+qi][playy+qi]!='#')space2++;
}
if (qp1+qp2+1>=3)
{
for (qi=1;qi<5;qi++)
{
if (qipan[playx-qi][playy-qi]!='*'&&qipan[playx-qi][playy-qi]!='*'&&(playx-qi>0&&playx-qi<=15)&&(playy-qi>0&&playy-qi<=15)&&space1<=space2)
{
qipan[playx-qi][playy-qi]=qizi;
stop=true;
return true;
break;
}
else if (qipan[playx+qi][playy+qi]!='*'&&qipan[playx+qi][playy+qi]!='*'&&(playx+qi>0&&playx+qi<=15)&&(playy+qi>0&&playy+qi<=15)&&space2<=space1)
{
qipan[playx+qi][playy+qi]=qizi;
stop=true;
return true;
break;
}
}
}
}
if (stop==false )
{
//右斜空位封杀
space1=space2=0;
for (qi=1;qipan[playx+qi][playy-qi]!=qizi&&qi<5;qi++)
{
if (qipan[playx+qi][playy-qi]==qz)qn1++;
if (qipan[playx+qi][playy-qi]!='*'&&qipan[playx+qi][playy-qi]!='#')space1++;
}
for (qi=1;qipan[playx-qi][playy+qi]!=qizi&&qi<5;qi++)
{
if (qipan[playx-qi][playy+qi]==qz)qn2++;
if (qipan[playx-qi][playy+qi]!='*'&&qipan[playx-qi][playy+qi]!='#')space2++;
}
if (qn1+qn2+1>=3)
{
for (qi=1;qi<5;qi++)
{
if (qipan[playx+qi][playy-qi]!='*'&&qipan[playx+qi][playy-qi]!='*'&&(playx+qi>0&&playx+qi<=15)&&(playy-qi>0&&playy-qi<=15)&&space1<=space2)
{
qipan[playx+qi][playy-qi]=qizi;
stop=true;
return true;
break;
}
else if (qipan[playx-qi][playy+qi]!='*'&&qipan[playx-qi][playy+qi]!='*'&&(playx-qi>0&&playx-qi<=15)&&(playy+qi>0&&playy+qi<=15)&&space2<=space1)
{
qipan[playx-qi][playy+qi]=qizi;
stop=true;
return true;
break;
}
}
}
}
if (stop==false ) return false;
}
//电脑下棋
void computer()
{
int s;
if (cps==0)
{
while (1)
{
cpx=1+rand()%15;
cpy=1+rand()%15;
if (qipan[cpx][cpy]!='*'&&qipan[cpx][cpy]!='#')
{
cps=1;
srand( (unsigned)time( NULL ) );
qipan[cpx][cpy]=qizi;
cpfx=rand()%8;
break;
}
}
}
else if (cps>=1)
{
if (defence()==false )
if (chessdown()==false )
if (antidown()==false ) computer();
}
}
//胜利判断
bool succ(int x,int y)
{
int qh,qs,qp,qn,qi;
qh=qs=qp=qn=1;
for (qi=1;qipa
n[x][y-qi]==qizi;qi++)
qh++;
for (qi=1;qipan[x][y+qi]==qizi;qi++)
qh++;
for (qi=1;qipan[x-qi][y]==qizi;qi++)
qs++;
for (qi=1;qipan[x+qi][y]==qizi;qi++)
qs++;
for (qi=1;qipan[x-qi][y-qi]==qizi;qi++)
qp++;
for (qi=1;qipan[x+qi][y+qi]==qizi;qi++)
qp++;
for (qi=1;qipan[x+qi][y-qi]==qizi;qi++)
qn++;
for (qi=1;qipan[x-qi][y+qi]==qizi;qi++)
qn++;
if (qh>=5||qs>=5||qp>=5||qn>=5)
return true;
else return false;
}
int main()
{
int px,py;
//棋盘空格填充
for (loopa1=0;loopa1<=16;loopa1++)
{
for (loopa2=0;loopa2<16;loopa2++)
{
qipan[loopa1][loopa2]=' ';
}
}
//棋盘坐标填充
for (loopa1=1;loopa1<16;loopa1++)
{
if (loopa1<10)
{
qipan[0][loopa1]='0'+loopa1;
qipan[loopa1][0]='0'+loopa1;
}
else
{
qipan[0][loopa1]='a'+loopa1-10;
qipan[loopa1][0]='a'+loopa1-10;
}
}
putqipan();
while (1)
{
scanf("%x %x",&py,&px);
if ((px>0&&px<=15)&&(py>0&&py<=15))
{
if (qipan[px][py]!='*'&&qipan[px][py]!='#')
{
playx=px;
playy=py;
qipan[px][py]=qizi;
putqipan();
if (succ(px,py)==true )
{
printf("\n%c胜利!",qizi);
break;
}
else
{
if (qizi=='#') qizi='*';
else qizi='#';
computer();
putqipan();
if (succ(cpx,cpy)==true )
{
printf("\n%c胜利!",qizi);
break;
}
else
{
if (qizi=='#') qizi='*';
else qizi='#';
}
}
}
else printf("你不能将棋子放置再已经放棋子的格子上!\n重新输入坐标>>>>>");
}
else printf("请输入合法字符1~9,a~f\n重新输入坐标>>>>>");
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论