这是我们这学期的考试题目,要求是用C语言编一个五道棋的程序,用鼠标走棋.并且能数棋盘上的棋子,能判定胜败,我写的程序如下,可能有些不足的地方,还请各位指教,谢谢,或许还能为出学者提供一些帮助
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
union REGS regs;
int a[5][5]={0};
int retcode;
int r=0;
int limtmouse() /*初始化鼠标并且限制光标的范围*/
{ regs.x.ax=0;
int86(0x33,®s,®s); /*用该中断函数驱动鼠标*/
retcode=regs.x.ax;
if(retcode==0)return 0;
regs.x.ax=7;
regs.x.cx=0;
regs.x.dx=639;
int86(0x33,®s,®s);
regs.x.ax=8;
regs.x.cx=0;
regs.x.dx=479;
int86(0x33,®s,®s);
return(retcode);
}
int mouseget(int *pa,int *pb,int *pc) /*获得鼠标的光标值*/
{ regs.x.ax=3;
int86(0x33,®s,®s);
*pa=regs.x.cx;
*pb=regs.x.dx;
*pc=regs.x.bx;
}
void drawmouse(int x,int y) /*画鼠标,/
{setcolor(1);
setlinestyle(0,0,1);
line(x,y,x+8,y+3);
line(x+8,y+3,x+6,y+4);
line(x+6,y+4,x+11,y+9);
line(x+11,y+9,x+9,y+11);
line(x+9,y+11,x+4,y+6);
line(x+4,y+6,x+3,y+8);
line(x+3,y+8,x,y);
}
void drawpic() /*画棋盘*/
{ int i;
setbkcolor(12);
setfillstyle(1,2);
bar(40,40,440,450);
setlinestyle(0,0,3);
for(i=0;i<=640;i+=10)
{line(i,30,i+10,39);
line(i,440,i+10,449);
}
for(i=40;i<=440;i+=10) /*这是花边*/
{rectangle(30,i,39,i+10);
rectangle(601,i,610,i+10);
}
setcolor(4);
for(i=80;i<=400;i+=80)
{line(80,i,400,i);
line(i,80,i,400);
}
bar(5,80,30,95); /*画的两个"begain" "eng"按扭*/
bar(5,160,30,175);
setlinestyle(0,0,1);
rectangle(5,80,30,95);
rectangle(5,160,30,175);
settextstyle(0,0,1);
setcolor(4);
outtextxy(6,83,"beg");
outtextxy(6,163,"end");
setcolor(4);
settextstyle(4,0,4);
outtextxy(50,0,"wellcome to play wu dao qi!!");
}
int count(int *pn,int *pm) /*数棋盘上的棋子*/
{int i,j,n1=0,n2=0;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
{if(a[i][j]==1) n1++;
else if(a[i][j]==2) n2++;
else continue;
}
*pn=n1;
*pm=n2;
}
begjm() /*第一个界面,是为了美化游戏的*/
{ int i,j;
setbkcolor(12);
setlinestyle(0,0,4);
setfillstyle(1,0);
fillellipse(340,100,150,50);
setfillstyle(1,15);
fillellipse(280,90,20,15);
fillellipse(400,90,20,15);
setfillstyle(1,13);
setcolor(13);
fillellipse(340,130,30,10);
setcolor(14);
setfillstyle(1,2);
bar(130,150,550,400);
for(i=200;i>=100;i-=10)
for(j=100;j<=400;j+=20)
{line(i,j,i-10,j+20);
}
for(i=500;i<600;i+=10)
for(j=100;j<420;j+=20)
{line(i,j,i+10,j+20);
}
setcolor(6);
settextstyle(0,0,6);
outtextxy(200,200,"Please");
setcolor(13);
settextstyle(0,0,2);
outtextxy(135,300,"enter any key to begain!!!");
getch();
}
void printscore(int n1,int n2) /*在棋盘的右边输出分数*/
{char tp[3],fp[3];
setfillstyle(1,3);
bar(440,40,600,440);
setfillstyle(1,r);
bar3d(460,120,560,240,30,1);
bar3d(460,300,560,420,30,1);
sprintf(tp,"%d",n1);
sprintf(fp,"%d",n2);
setcolor(r+1);
settextstyle(0,0,5);
outtextxy(500,180,tp);
outtextxy(500,360,fp);
settextstyle(1,0,2);
outtextxy(500,100,"white");
outtextxy(500,280,"green");
r++;
if(r>16) r=0;
if((n1+n2)==25&&n1>n2) /*当棋盘下完后判定胜败*/
{setfillstyle(1,3);
setcolor(4);
bar(50,50,440,440);
settextstyle(1,0,4);
outtextxy(150,200,"The wight win!!!");
}
else if((n1+n2)==25&&n1<n2)
{setfillstyle(1,3);
bar(50,50,440,440);
setcolor(4);
settextstyle(1,0,4);
outtextxy(150,200,"The green win!!!");
}
}
smath(int x,int y,int t,void *p1,void *p2,void *p3,void *p4,void *p5,void *p6,void *p7,void *p8,void *p9) /*判定并吃掉棋子*/
{
int b1,b2,b3,b4,c1,c2,c3,c4;
b1=a[x-1][y];
b2=a[x-2][y];
b3=a[x+1][y];
b4=a[x+2][y];
c1=a[x][y-1];
c2=a[x][y-2];
c3=a[x][y+1];
c4=a[x][y+2];
if(b1==t&&b2!=t&&b2!=0&&x>=2) /*the up case*/
{if(x==2)
{if(y!=0&&y!=4) {putimage(y*80+40,40,p2,COPY_PUT);a[0][y]=0;}
else if(y==0) {putimage(40,40,p1,COPY_PUT);a[0][0]=0;}
else if(y==4) {putimage(360,40,p3,COPY_PUT);a[0][4]=0;}
}
else if(x>2)
{if(y!=0&&y!=4) {putimage(y*80+40,(x-2)*80+40,p9,COPY_PUT);a[x-2][y]=0;}
else if(y==0) {putimage(40,(x-2)*80+40,p8,COPY_PUT); a[x-2][0]=0;}
else if(y==4) {putimage(360,(x-2)*80+40,p4,COPY_PUT);a[x-2][4]=0;}
}
}
if(b3==t&&b4!=t&&b4!=0&&x<=2) /*the down case*/
{if(x==2)
{if(y!=0&&y!=4) {putimage(y*80+40,360,p6,COPY_PUT);a[4][y]=0;}
else if(y==0) {putimage(40,360,p7,COPY_PUT);a[4][0]=0;}
else if(y==4) {putimage(360,360,p5,COPY_PUT);a[4][4]=0;}
}
else if(x<2)
{if(y!=0&&y!=4) {putimage(y*80+40,(x+2)*80+40,p9,COPY_PUT);a[x+2][y]=0;}
else if(y==0) {putimage(40,(x+2)*80+40,p8,COPY_PUT); a[x+2][0]=0;}
else if(y==4) {putimage(360,(x+2)*80+40,p4,COPY_PUT);a[x+2][4]=0;}
}
}
if(b1!=0&&b3!=0&&x>0&&x<4) /*the middle case 1*/
{if(x==2)
{if(b1!=t&&b3==t)
{if(y!=0&&y!=4) {putimage(y*80+40,(x-1)*80+40,p9,COPY_PUT);a[1][y]=0;}
else if(y==0) {putimage(40,(x-1)*80+40,p8,COPY_PUT);a[1][0]=0;}
else if(y==4) {putimage(360,(x-1)*80+40,p4,COPY_PUT);a[1][4]=0;}
}
else if(b3!=t&&b1==t)
{if(y!=0&&y!=4) {putimage(y*80+40,(x+1)*80+40,p9,COPY_PUT); a[3][y]=0;}
else if(y==0) {putimage(40,(x+1)*80+40,p8,COPY_PUT);a[3][0]=0;}
else if(y==4) {putimage(360,(x+1)*80+40,p4,COPY_PUT);a[3][4]=0;}
}
}
else if(x==3)
{if(b1!=t&&b3==t)
{if(y!
=0&&y!=4) {putimage(y*80+40,(x-1)*80+40,p9,COPY_PUT);a[2][y]=0;}
else if(y==0) {putimage(40,200,p8,COPY_PUT);a[2][0]=0;}
else if(y==4) {putimage(360,200,p4,COPY_PUT);a[2][4]=0;}
}
else if(b3!=t&&b1==t)
{if(y!=0&&y!=4) {putimage(y*80+40,360,p6,COPY_PUT); a[x+1][y]=0;}
else if(y==0) {putimage(40,360,p7,COPY_PUT);a[4][0]=0;}
else if(y==4) {putimage(360,360,p4,COPY_PUT);a[4][4]=0;}
}
}
else if(x==1)
{if(b1!=t&&b3==t)
{if(y!=0&&y!=4) {putimage(y*80+40,40,p2,COPY_PUT);a[0][y]=0;}
else if(y==0) {putimage(40,40,p1,COPY_PUT);a[0][0]=0;}
else if(y==4) {putimage(360,40,p3,COPY_PUT);a[0][4]=0;}
}
else if(b3!=t&&b1==t)
{if(y!=0&&y!=4) {putimage(y*80+40,200,p9,COPY_PUT); a[2][y]=0;}
else if(y==0) {putimage(40,200,p8,COPY_PUT);a[2][0]=0;}
else if(y==4) {putimage(360,200,p4,COPY_PUT);a[2][4]=0;}
}
}}
if(c1==t&&c2!=0&&c2!=t&&y>=2) /*the left case*/
{if(y==2)
{if(x!=0&&x!=4) {putimage(40,x*80+40,p8,COPY_PUT);a[x][0]=0;}
else if(x==0) {putimage(40,40,p1,COPY_PUT);a[0][0]=0;}
else if(x==4) {putimage(40,360,p7,COPY_PUT);a[4][0]=0;}
}
else if(y>2)
{if(x!=0&&x!=4) {putimage((y-2)*80+40,x*80+40,p9,COPY_PUT);a[x][y-2]=0;}
else if(x==0) {putimage((y-2)*80+40,40,p2,COPY_PUT); a[0][y-2]=0;}
else if(x==4) {putimage((y-2)*80+40,360,p6,COPY_PUT);a[4][y-2]=0;}
}
}
if(c3==t&&c4!=0&&c4!=t&&y<=2) /*the right case*/
{if(y==2)
{if(x!=0&&x!=4) {putimage(360,x*80+40,p4,COPY_PUT);a[x][4]=0;}
else if(x==0) {putimage(360,40,p3,COPY_PUT);a[0][4]=0;}
else if(x==4) {putimage(360,360,p5,COPY_PUT);a[4][4]=0;}
}
else if(y<2)
{if(x!=0&&x!=4) {putimage((y+2)*80+40,x*80+40,p9,COPY_PUT);a[x][y+2]=0;}
else if(x==0) {putimage((y+2)*80+40,40,p2,COPY_PUT); a[0][y+2]=0;}
else if(x==4) {putimage((y+2)*80+40,360,p6,COPY_PUT);a[4][y+2]=0;}
}
}
if(c1!=0&&c3!=0&&y>0&&y<4) /*the x middle case*/
{if(y==2)
{if(c1!=t&&c3==t)
{if(x!=0&&x!=4) {putimage(120,x*80+40,p9,COPY_PUT);a[x][1]=0;}
else if(x==0) {putimage(120,40,p2,COPY_PUT);a[0][1]=0;}
else if(x==4) {putimage(120,360,p6,COPY_PUT);a[4][1]=0;}
}
else if(c3!=t&&c1==t)
{if(x!=0&&x!=4) {putimage(280,x*80+40,p9,COPY_PUT); a[x][3]=0;}
else if(x==0) {putimage(280,40,p2,COPY_PUT);a[0][3]=0;}
else if(x==4) {putimage(280,360,p6,COPY_PUT);a[4][3]=0;}
}
}
else if(y==3)
{if(c1!=t&&c3==t)
{if(x!=0&&x!=4) {putimage(200,x*80+40,p9,COPY_PUT);a[x][2]=0;}
else if(x==0) {putimage(200,40,p2,COPY_PUT);a[0][2]=0;}
else if(x==4) {putimage(200,360,p6,COPY_PUT);a[4][2]=0;}
}
else if(c3!=t&&c1==t)
{if(x!=0&&x!=4) {putimage(360,x*80+40,p4,COPY_PUT); a[x][4]=0;}
else if(x==0) {putimage(360,40,p3,COPY_PUT);a[0][4]=0;}
else if(x==4) {putimage(360,360,p5,COPY_PUT);a[4][4]=0;}
}
}
else if(y==1)
{if(c1!=t&&c3==t)
{if(x!=0&&x!=4) {putimage(40,x*80+40,p8,COPY_PUT);a[x][0]=0;}
else if(x==0) {putimage(40,40,p1,COPY_PUT);a[0][0]=0;}
else if(x==4) {putimage(40,360,p7,COPY_PUT);a[4][0]=0;}
}
else if(c3!=t&&c1==t)
{if(x!=0&&x!=4) {putimage(200,x*80+40,p9,COPY_PUT); a[x][2]=0;}
else if(x==0) {putimage(200,40,p2,COPY_PUT);a[0][2]=0;}
else if(x==4) {putimage(200,360,p6,COPY_PUT);a[4][2]=0;}
}
}
}
}
forbegain() /*当点击开始后方能下棋*/
{int x,y,z,*px,*py,*pz;
px=&x;
py=&y;
pz=&z;
for(;;)
{domouse(px,py,pz);
if(x>5&&x<30)
{if(y>80&&y<95&&z==1)
{setfillstyle(1,14);
bar(5,80,30,95);
settextstyle(0,0,1);
outtextxy(5,80,"beg");
break;
}
else if(y>160&&y<175&&z==1)
{setfillstyle(1,14);
settextstyle(0,0,1);
outtextxy(5,160,"end");
delay(50000);
closegraph();
exit(0);
}
}
}}
domouse(int *pa,int *pb,int *pc) /*控制鼠标的函数*/
{unsigned size;
void *save;
mouseget(pa,pb,pc); /*获得鼠标的值*/
size=imagesize(*pa,*pb,*pa+11,*pb+11);
save=malloc(size); /*开辟这么大的空间*/
getimage(*pa,*pb,*pa+11,*pb+11,save); /*存储位图*/
drawmouse(*pa,*pb); /*画鼠标*/
delay(50000/8); /*停留50000/8豪秒*/
putimage(*pa,*pb,save,COPY_PUT); /*将刚才的位图放在画鼠标的位置*/
free(save);
}
control() /*整个下棋的控制函数*/
{
int x0,y0;
void *p1,*p2,*p3,*p4,*p5,*p6,*p7,*p8,*p9;
int x,y,z,*mx,*my,*state,d,b,n1=0,n2=0,i,j,kbit=0;
unsigned size,s1,s2,s3,s4,s5,s6,s7,s8,s9;
void *save;
mx=&x;
my=&y;
state=&z;
limtmouse();
cleardevice();
drawpic();
forbegain();
s1=imagesize(40,40,120,120);
s2=imagesize(200,40,280,120);
s3=imagesize(360,40,440,120);
s4=imagesize(360,200,440,280);
s5=imagesize(360,360,440,440);
s6=imagesize(200,360,280,440);
s7=imagesize(40,360,120,440);
s8=imagesize(40,200,120,280);
s9=imagesize(200,200,280,280);
p1=malloc(s1);
p2=malloc(s2);
p3=malloc(s3);
p4=malloc(s4);
p5=malloc(s5);
p6=malloc(s6);
p7=malloc(s7);
p8=malloc(s8);
p9=malloc(s9);
getimage(40,40,120,120,p1);
getimage(200,40,280,120,p2);
getimage(360,40,440,120,p3);
getimage(360,200,440,280,p4);
getimage(360,360,440,440,p5);
getimage(200,360,280,440,p6);
getimage(40,360,120,440,p7);
getimage(40,200,120,280,p8);
getimage(200,200,280,280,p9);
do
{
domouse(mx,my,state);
if(z==0) continue;
else if(x>40&&x<440&&y>40&&y<440)
{ b=(x-40)/8
0;
d=(y-40)/80;
x0=80+b*80;
y0=80+d*80;
i=(y0-80)/80;j=(x0-80)/80;
if(z==1&&kbit!=1&&a[i][j]==0)
{setfillstyle(1,15);
setcolor(15);
fillellipse(x0,y0,20,20); /*画棋子*/
a[i][j]=1;kbit=1;
smath(i,j,1,p1,p2,p3,p4,p5,p6,p7,p8,p9);
count(&n1,&n2);
printscore(n1,n2);
}
else if(z==2&&kbit!=2&&a[i][j]==0)
{setfillstyle(1,11);
setcolor(11);
fillellipse(x0,y0,20,20);
a[i][j]=2;kbit=2;
smath(i,j,2,p1,p2,p3,p4,p5,p6,p7,p8,p9);
count(&n1,&n2);
printscore(n1,n2);
}
}
else if(x>5&&x<30&&y>160&&y<175&&z==1)break;
}while(1);
}
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc"); /*初始画图界面*/
begjm();
control();
closegraph();
}
希望能给大家带来点什么?
我的QQ:123834937 欢迎您的加入成为我的好友
电子邮件:kindwjyou@126.com
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |