五子棋小程序.docx
- 文档编号:18529247
- 上传时间:2023-08-19
- 格式:DOCX
- 页数:15
- 大小:16.07KB
五子棋小程序.docx
《五子棋小程序.docx》由会员分享,可在线阅读,更多相关《五子棋小程序.docx(15页珍藏版)》请在冰点文库上搜索。
五子棋小程序
/*
五子棋
*/
#include
#include
#include
#include
#include
#defineLEFT0x4b00
#defineRIGHT0x4d00
#defineDOWN0x5000
#defineUP0x4800
#defineESC0x011b
#defineSPACE0x3920
#defineBILI20
#defineJZ4
#defineJS3
#defineN19
intbox[N][N];
intstep_x,step_y;
intkey;
intflag=1;
voiddraw_box();
voiddraw_cicle(intx,inty,intcolor);
voidchange();
voidjudgewho(intx,inty);
voidjudgekey();
intjudgeresult(intx,inty);
voidattentoin();
voidattention()
{
charch;
window(1,1,80,25);
textbackground(LIGHTBLUE);
textcolor(YELLOW);
clrscr();
gotoxy(15,2);
printf("游戏操作规则:
");
gotoxy(15,4);
printf("PlayRules:
");
gotoxy(15,6);
printf("1、按左右上下方向键移动棋子");
gotoxy(15,8);
printf("1.PressLeft,Right,Up,DownKeytomovePiece");
gotoxy(15,10);
printf("2、按空格确定落棋子");
gotoxy(15,12);
printf("2.PressSpacetoplacethePiece");
gotoxy(15,14);
printf("3、禁止在棋盘外按空格");
gotoxy(15,16);
printf("3.DONOTpressSpaceoutsideofthechessboard");
gotoxy(15,18);
printf("你是否接受上述的游戏规则(Y/N)");
gotoxy(15,20);
printf("DoyouaccepttheabovePlayingRules?
[Y/N]:
");
while
(1)
{
gotoxy(60,20);
ch=getche();
if(ch=='Y'||ch=='y')
break;
elseif(ch=='N'||ch=='n')
{
window(1,1,80,25);
textbackground(BLACK);
textcolor(LIGHTGRAY);
clrscr();
exit(0);
}
gotoxy(51,12);
printf("");
}
}
voiddraw_box()
{
intx1,x2,y1,y2;
setbkcolor(LIGHTBLUE);
setcolor(YELLOW);
gotoxy(7,2);
printf("Left,Right,Up,DownKEYtomove,Spacetoput,ESC-quit.");
for(x1=1,y1=1,y2=18;x1<=18;x1++)
line((x1+JZ)*BILI,(y1+JS)*BILI,(x1+JZ)*BILI,(y2+JS)*BILI);
for(x1=1,y1=1,x2=18;y1<=18;y1++)
line((x1+JZ)*BILI,(y1+JS)*BILI,(x2+JZ)*BILI,(y1+JS)*BILI);
for(x1=1;x1<=18;x1++)
for(y1=1;y1<=18;y1++)
box[x1][y1]=0;
}
voiddraw_circle(intx,inty,intcolor)
{
setcolor(color);
setlinestyle(SOLID_LINE,0,1);
x=(x+JZ)*BILI;
y=(y+JS)*BILI;
circle(x,y,8);
}
voidjudgekey()
{
inti;
intj;
switch(key)
{
caseLEFT:
if(step_x-1<0)
break;
else
{
for(i=step_x-1,j=step_y;i>=1;i--)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break;
}
if(i<1)break;
step_x=i;
judgewho(step_x,step_y);
break;
}
caseRIGHT:
if(step_x+1>18)
break;
else
{
for(i=step_x+1,j=step_y;i<=18;i++)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break;
}
if(i>18)break;
step_x=i;
judgewho(step_x,step_y);
break;
}
caseDOWN:
if((step_y+1)>18)
break;
else
{
for(i=step_x,j=step_y+1;j<=18;j++)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break;
}
if(j>18)break;
step_y=j;
judgewho(step_x,step_y);
break;
}
caseUP:
if((step_y-1)<0)
break;
else
{
for(i=step_x,j=step_y-1;j>=1;j--)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break;
}
if(j<1)break;
step_y=j;
judgewho(step_x,step_y);
break;
}
caseESC:
break;
caseSPACE:
if(step_x>=1&&step_x<=18&&step_y>=1&&step_y<=18)
{
if(box[step_x][step_y]==0)
{
box[step_x][step_y]=flag;
if(judgeresult(step_x,step_y)==1)
{
sound(1000);
delay(1000);
nosound();
gotoxy(30,4);
if(flag==1)
{
setbkcolor(BLUE);
cleardevice();
setviewport(100,100,540,380,1);
/*定义一个图形窗口*/
setfillstyle(1,2);
/*绿色以实填充*/
setcolor(YELLOW);
rectangle(0,0,439,279);
floodfill(50,50,14);
setcolor(12);
settextstyle(1,0,5);
/*三重笔划字体,水平放?
5倍*/
outtextxy(20,20,"TheWhiteWin!
");
setcolor(15);
settextstyle(3,0,5);
/*无衬笔划字体,水平放大5倍*/
outtextxy(120,120,"TheWhiteWin!
");
setcolor(14);
settextstyle(2,0,8);
getch();
closegraph();
exit(0);
}
if(flag==2)
{
setbkcolor(BLUE);
cleardevice();
setviewport(100,100,540,380,1);
/*定义一个图形窗口*/
setfillstyle(1,2);
/*绿色以实填充*/
setcolor(YELLOW);
rectangle(0,0,439,279);
floodfill(50,50,14);
setcolor(12);
settextstyle(1,0,8);
/*三重笔划字体,水平放大8倍*/
outtextxy(20,20,"TheRedWin!
");
setcolor(15);
settextstyle(3,0,5);
/*无衬笔划字体,水平放大5倍*/
outtextxy(120,120,"TheRedWin!
");
setcolor(14);
settextstyle(2,0,8);
getch();
closegraph();
exit(0);
}
}
change();
break;
}
}
else
break;
}
}
voidchange()
{
if(flag==1)
flag=2;
else
flag=1;
}
voidjudgewho(intx,inty)
{
if(flag==1)
draw_circle(x,y,15);
if(flag==2)
draw_circle(x,y,4);
}
intjudgeresult(intx,inty)
{
intj,k,n1,n2;
while
(1)
{
n1=0;
n2=0;
/*水平向左数*/
for(j=x,k=y;j>=1;j--)
{
if(box[j][k]==flag)
n1++;
else
break;
}
/*水平向右数*/
for(j=x,k=y;j<=18;j++)
{
if(box[j][k]==flag)
n2++;
else
break;
}
if(n1+n2-1>=5)
{
return
(1);
break;
}
/*垂直向上数*/
n1=0;
n2=0;
for(j=x,k=y;k>=1;k--)
{
if(box[j][k]==flag)
n1++;
else
break;
}
/*垂直向下数*/
for(j=x,k=y;k<=18;k++)
{
if(box[j][k]==flag)
n2++;
else
break;
}
if(n1+n2-1>=5)
{
return
(1);
break;
}
/*向左上方数*/
n1=0;
n2=0;
for(j=x,k=y;j>=1,k>=1;j--,k--)
{
if(box[j][k]==flag)
n1++;
else
break;
}
/*向右下方数*/
for(j=x,k=y;j<=18,k<=18;j++,k++)
{
if(box[j][k]==flag)
n2++;
else
break;
}
if(n1+n2-1>=5)
{
return
(1);
break;
}
/*向右上方数*/
n1=0;
n2=0;
for(j=x,k=y;j<=18,k>=1;j++,k--)
{
if(box[j][k]==flag)
n1++;
else
break;
}
/*向左下方数*/
for(j=x,k=y;j>=1,k<=18;j--,k++)
{
if(box[j][k]==flag)
n2++;
else
break;
}
if(n1+n2-1>=5)
{
return
(1);
break;
}
return(0);
break;
}
}
voidmain()
{
intgdriver=VGA,gmode=VGAHI;
clrscr();
attention();
initgraph(&gdriver,&gmode,"c:
\\tc");
/*setwritemode(XOR_PUT);*/
flag=1;
draw_box();
do
{
step_x=0;
step_y=0;
/*draw_circle(step_x,step_y,8);*/
judgewho(step_x-1,step_y-1);
do
{
while(bioskey
(1)==0);
key=bioskey(0);
judgekey();
}
while(key!
=SPACE&&key!
=ESC);
}
while(key!
=ESC);
closegraph();
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 五子棋 程序
![提示](https://static.bingdoc.com/images/bang_tan.gif)