📄 wuziqi.c
字号:
/*C语言大作业,我选了编五子棋,有鼠标操作,AI还没编出来,以后完成之后会更新。
另外,运行此程序需要你的C能独立运行图形程序。怎么运行呢?如下:
许你经常在用C/C++语言编写一些图形程序,但是总不能脱离C/C++语言环境独立运行,我们怎样来解决呢?下面是实现图形程序独立运行的具体步骤:
1.将驱动程序EGAVGA.BGI转换成目标文件EGAVGA.OBJ:
C:\TC>BGIOBJ EGAVGA
按同样的办法,将字体文件*.CHR转换成目标文件*.OBJ:
C:\TC>BGIOBJ TRIP
C:\TC>BGIOBJ LITT
C:\TC>BGIOBJ SANS
C:\TC>BGIOBJ GOTH
2.将上述建立的OBJ文件加入到GRAPHICS.LIB库文件中,具体方法如下:
C:\TC>TLINK C:\TC\LIB\GRAPHICS.LIB+EGAVGA
C:\TC>TLINK C:\TC\LIB\GRAPHICS.LIB+TRIP
C:\TC>TLINK C:\TC\LIB\GRAPHICS.LIB+LITT
C:\TC>TLINK C:\TC\LIB\GRAPHICS.LIB+SANS
C:\TC>TLINK C:\TC\LIB\GRAPHICS.LIB+GOTH
也可以使用TLIB、PRJ程序代替TLINK。
3.在程序中调用initgraph()函数前,应加上如下语句:
registerbgidriver(EGAVGA-driver);
它通知连接程序把EGAVGA驱动程序装入用户的执行程序中,同样在装入字体文件之前要加上如下语句:
registerbgifont(字体文件名);
如果这样做了,你就可以运行下列程序了:
*/
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
union REGS regs;
int retcode;
main()
{
void init(int luozi[][19]);
int judgewin(int luozi[][19]);
int luozi[19][19];
int *pa,*pb,*pc,button,oldb,i=0,flag=0;
int gdriver=9,gmode=2;
registerbgidriver(EGAVGA_driver);
registerbgifont(triplex_font);
registerbgifont(small_font);
registerbgifont(sansserif_font);
registerbgifont(gothic_font);
initgraph(&gdriver,&gmode,"F:\TC");
drawpic();
setmousearea();
init(luozi);
while(1)
{
domouse(pa,pb,pc);
oldb=button;
button=*pc;
flag=judgewin(luozi);
if(button==1&&oldb!=button)
{if(flag==0)
{
if(*pa>=110&&*pa<480&&*pb>=60&&*pb<430)
{if(luozi[(*pa-110)/20][(*pb-60)/20]!=0) continue;
else
{ chess(*pa,*pb,(i+1)%2);
luozi[(*pa-110)/20][(*pb-60)/20]=(i+1)%2+1;
flag=judgewin(luozi);
i++;
}}
else continue;
}
if(flag==1)
doblackwin();
if(flag==2)
dowhitewin();
}
else continue;
}
getch();
closegraph();
return 0;
}
int setmousearea()
{
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=620;
int86(0x33,®s,®s);
regs.x.ax=8;
regs.x.cx=0;
regs.x.dx=480;
int86(0x33,®s,®s);
return(retcode);
}
void drawmouse(int x,int y)
{
if(x>=110&&x<480&&y>=60&&y<430)
{setcolor(14);
setlinestyle(0,0,2);
line(x+1,y+1,x+19,y+1);
line(x+19,y+1,x+19,y+19);
line(x+19,y+19,x+1,y+19);
line(x+1,y+19,x+1,y+1);
}
else
{setcolor(15);
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);
}
}
int mouseget(int *pa,int *pb,int *pc)
{
int x0,a,y0,b;
regs.x.ax=3;
int86(0x33,®s,®s);
if(regs.x.cx>=110&®s.x.cx<480&®s.x.dx>=60&®s.x.dx<430)
{ a=(regs.x.cx-110)/20;
x0=110+a*20;
b=(regs.x.dx-60)/20;
y0=60+b*20;
*pa=x0;
*pb=y0;
*pc=regs.x.bx;
}
else
{*pa=regs.x.cx;
*pb=regs.x.dx;
*pc=regs.x.bx;
}
}
domouse(int *pa,int*pb,int *pc)
{
unsigned size;
void *save;
mouseget(pa,pb,pc);
if(*pa>=110&&*pa<480&&*pb>=60&&*pb<430)
{size=imagesize(*pa,*pb,*pa+20,*pb+20);
save=malloc(size);
getimage(*pa,*pb,*pa+20,*pb+20,save);
drawmouse(*pa,*pb);
delay(5000);
putimage(*pa,*pb,save,COPY_PUT);/*将先前储存的图像恢复到屏幕上*/
free(save);
}
else
{size=imagesize(*pa,*pb,*pa+11,*pb+11);
save=malloc(size);
getimage(*pa,*pb,*pa+11,*pb+11,save);
drawmouse(*pa,*pb);
delay(5000);
putimage(*pa,*pb,save,COPY_PUT);
free(save);
}
}
drawpic()
{
int i;
setbkcolor(0);
setcolor(15);
setlinestyle(0,0,1);
setfillstyle(1,6);
bar3d(100,50,500,450,10,1);
floodfill(200,200,15);
setfillstyle(1,7);
floodfill(200,45,15);
setfillstyle(1,8);
floodfill(505,100,15);
setcolor(0);
for(i=0;i<=18;i++)
{ line(120,70+i*20,480,70+i*20);
line(120+i*20,70,120+i*20,430);
}
settextstyle(0,0,1);
setcolor(15);
outtextxy(0,60,"TheColorNow:");
}
int chess(int x,int y,int order)
{
if(order==0)
{
setcolor(8);
setfillstyle(1,8);
pieslice(x+10,y+10,0,360,8);
setcolor(7);
setfillstyle(1,7);
pieslice(x+9,y+9,0,360,6);
setcolor(15);
setfillstyle(1,15);
pieslice(x+6,y+6,0,360,2);
setcolor(8);
setfillstyle(1,8);
pieslice(50,100,0,360,8);
}
else if(order==1)
{
setcolor(0);
setfillstyle(1,0);
pieslice(x+10,y+10,0,360,8);
setcolor(8);
setfillstyle(1,8);
pieslice(x+7,y+7,0,360,4);
setcolor(7);
setfillstyle(1,7);
pieslice(x+6,y+6,0,360,2);
setcolor(15);
setfillstyle(1,15);
pieslice(50,100,0,360,8);
}
}
int judgewin(int luozi[][19])
{
int m,n,flag=0;
for(m=0;m<=18;m++)
{for(n=0;n<=14;n++)
{if(luozi[m][n]==1&&luozi[m][n+1]==1&&luozi[m][n+2]==1&&luozi[m][n+3]==1&&luozi[m][n+4]==1)
flag=2;
if(luozi[m][n]==2&&luozi[m][n+1]==2&&luozi[m][n+2]==2&&luozi[m][n+3]==2&&luozi[m][n+4]==2)
flag=1;
else continue;
}
}
for(m=0;m<=14;m++)
{for(n=0;n<=18;n++)
{if(luozi[m][n]==1&&luozi[m+1][n]==1&&luozi[m+2][n]==1&&luozi[m+3][n]==1&&luozi[m+4][n]==1)
flag=2;
if(luozi[m][n]==2&&luozi[m+1][n]==2&&luozi[m+2][n]==2&&luozi[m+3][n]==2&&luozi[m+4][n]==2)
flag=1;
else continue;
}
}
for(m=0;m<=14;m++)
{for(n=0;n<=14;n++)
{if(luozi[m][n]==1&&luozi[m+1][n+1]==1&&luozi[m+2][n+2]==1&&luozi[m+3][n+3]==1&&luozi[m+4][n+4]==1)
flag=2;
if(luozi[m][n]==2&&luozi[m+1][n+1]==2&&luozi[m+2][n+2]==2&&luozi[m+3][n+3]==2&&luozi[m+4][n+4]==2)
flag=1;
else continue;
}
}
for(m=0;m<=14;m++)
{for(n=4;n<=18;n++)
{if(luozi[m][n]==1&&luozi[m+1][n-1]==1&&luozi[m+2][n-2]==1&&luozi[m+3][n-3]==1&&luozi[m+4][n-4]==1)
flag=2;
if(luozi[m][n]==2&&luozi[m+1][n-1]==2&&luozi[m+2][n-2]==2&&luozi[m+3][n-3]==2&&luozi[m+4][n-4]==2)
flag=1;
else continue;
}
}
return(flag);
}
doblackwin()
{
settextstyle(3,0,7);
setcolor(15);
outtextxy(110,200,"BLACK WINS!");
}
dowhitewin()
{
settextstyle(3,0,7);
setcolor(15);
outtextxy(110,200,"WHITE WINS!");
}
void init(int luozi[][19])
{
int i,j;
for(i=0;i<19;i++)
{for(j=0;j<19;j++)
{luozi[i][j]=0;}}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -