运行环境:DOS,不需要汉字系统,首先要运行鼠标驱动程序,HZK16汉字库
以下是源程序:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<dos.h>
#include<graphics.h>
union REGS regs;
void inigraph();
void pushbutt();
void popbutt();
void mixbar();
void closebar();
void maxbar();
void stbar();
void menubar();
void cwin();
int inimouse();
int readmouse();
void closemouse();
void openmouse();
int mousemove();
void closewindow();
void startmenu();
int messbox();
int inputbox();
int passbox();
void strout();
void helpwin();
void writebut();
void checkbut();
void fillwhite();
void fillblack();
void computer();
void clearboard();
int getkey();
int scanchess();
int boundcheck();
int boundans();
int carrayx[24];
int carrayy[24];
int userb[24][24][4];
int machb[24][24][4];
int xmin=0,xmax=639,ymin=0,ymax=479,mbutt,mstat=0;
int mmx,mmy; /* machine X,Y */
int winstatus[20][4];
int curwin,gameflag=1;
int finish=0;
int player1=0,player2=0,compt=0;
int p1but=1,p2but=0,combut=0,pbut=1;
int mcol=15,ucol=14;
FILE *fp;
main()
{ int x,y,i,mx,my,mkey,exitst=0;
int newx,newy,mbutt,row,lnewx=0,lnewy=0,de;
int driver=DETECT,mode;
inigraph(DETECT);
if((fp=fopen("hzk16","rb"))==0)
{
printf("Can not open the file\n");
exit(1);
}
setpalette(8,0 );
setpalette(12,17);
setpalette(14,0);
setbkcolor(12);
if(inimouse(xmin,xmax,ymin,ymax)==0)
{
printf("Mouse or Mouse Driver Not install!\n");
exit(1);
}
startmenu();
for(row=0;row<24;++row){ /* init coodinate */
carrayx[row]=136+row*16;
carrayy[row]=56+row*16;
}
menubar(0,0,639,16,BLUE);
strout(3,1,WHITE,"欢乐五子棋");
popbutt(15,85,80,210,GREEN,3);
popbutt(545,85,80,210,GREEN,3);
writebut();
clearboard(); /* 清除棋板 */
pushbutt(560,230,50,18,7,1);
strout(561,231,8,"玩家一");
popbutt(560,260,50,18,7,1);
strout(561,261,8,"玩家二");
pushbutt(20,90,70,18,7,1);
strout(24,91,8,"两人对战");
while(1){
int ustatus[4]={0,0,0,0};
int mstatus[4]={0,0,0,0};
int x,y,i,uline,mline,curx,cury;
if(finish==1){
openmouse(); /* 打开鼠标 */
readmouse(&newx,&newy,&mbutt); /* 读当前鼠标位置 */
checkbut(newx,newy,mbutt);
continue;
}
for(y=0;y<24;++y)
for(x=0;x<24;++x)
for(i=0;i<4;++i){
userb[y][x][i]=0;
machb[y][x][i]=0;
}
openmouse(); /* display cursor */
readmouse(&newx,&newy,&mbutt);
if(mbutt==2)
{ int i;
closemouse();
setfillstyle(1,BROWN);
bar(lnewx-8,lnewy-8,lnewx+8,lnewy+8);
setcolor(GREEN);
line(lnewx-8,lnewy,lnewx+8,lnewy);
line(lnewx,lnewy-8,lnewx,lnewy+8);
if(mstat==1) mstat=0;
else mstat=1;
for(i=200;i<2000;i+=500);
{sound(i);
delay(500);
}
nosound();
continue;
}
checkbut(newx,newy,mbutt);
if(newx<135||newx>505||newy<55||newy>425||getpixel(newx,newy)==WHITE||getpixel(newx,newy)==BLACK) continue;
for(row=0;row<24;++row){
if(newx>carrayx[row]) continue;
else if((carrayx[row]-newx)<6){
newx=carrayx[row];
goto newyy;
}
else if((newx-carrayx[row-1])<6) {
newx=carrayx[row-1];
goto newyy;
}
if(newx<carrayx[row]) break;
}
goto nonxy;
newyy:
for(row=0;row<24;++row){
if(newy>carrayy[row]) continue;
else if((carrayy[row]-newy)<6){
newy=carrayy[row];
goto dispxy;
}
else if((newy-carrayy[row-1])<6){
newy=carrayy[row-1];
goto dispxy;
}
if(newy<carrayy[row]) break;
}
goto nonxy;
dispxy:
if(getpixel(newx,newy)==15||getpixel(newx,newy)==14) goto nonxy;
lnewx=newx;
lnewy=newy;
if(mbutt==1)
{closemouse(); /* close cursor */
if(mstat==0) {
if(p1but==0){
fillwhite(newx,newy);
delay(1500);
}
else{
fillblack(newx,newy);
delay(1500);
}
mstat=1;
popbutt(560,230,50,18,7,1);
strout(561,231,8,"玩家一");
pushbutt(560,260,50,18,7,1);
strout(561,261,8,"玩家二");
}
else {
if(p2but==0){
fillwhite(newx,newy);
delay(1500);
}
else{
fillblack(newx,newy);
delay(1500);
}
mstat=0;
pushbutt(560,230,50,18,7,1);
strout(561,231,8,"玩家一");
popbutt(560,260,50,18,7,1);
strout(561,261,8,"玩家二");
}
}
nonxy:
for(y=0;y<24;++y)
for(x=0;x<24;++x){
curx=136+x*16;
cury=56+y*16;
if(getpixel(curx,cury)==15) {
userb[y][x][0]=scanchess(curx,cury,1,15)+scanchess(curx,cury,5,15)+1;
userb[y][x][1]=scanchess(curx,cury,2,15)+scanchess(curx,cury,6,15)+1;
userb[y][x][2]=scanchess(curx,cury,3,15)+scanchess(curx,cury,7,15)+1;
userb[y][x][3]=scanchess(curx,cury,4,15)+scanchess(curx,cury,8,15)+1;
}
if(getpixel(curx,cury)==14){
machb[y][x][0]=scanchess(curx,cury,1,14)+scanchess(curx,cury,5,14)+1;
machb[y][x][1]=scanchess(curx,cury,2,14)+scanchess(curx,cury,6,14)+1;
machb[y][x][2]=scanchess(curx,cury,3,14)+scanchess(curx,cury,7,14)+1;
machb[y][x][3]=scanchess(curx,cury,4,14)+scanchess(curx,cury,8,14)+1;
}
}
uline=userb[0][0][0];
for(y=0;y<24;++y)
for(x=0;x<24;++x)
for(i=0;i<4;++i){
if(uline<userb[y][x][i]) {
uline=userb[y][x][i];
}
}
if (uline==5) {messbox(150,150,200,150,GREEN,"战争结局"," 白子玩家胜利! ",1);
closemouse();
delay(200);
finish=1;
continue;
}
mline=machb[0][0][0];
for(y=0;y<24;++y)
for(x=0;x<24;++x)
for(i=0;i<4;++i){
if(mline<machb[y][x][i]) {
mline=machb[y][x][i];
}
}
if (mline==5) {messbox(150,150,200,150,RED,"战争结局"," 黑子玩家胜利! ",1);
closemouse
();
finish=1;
delay(200);
continue;
}
}
}
/* ================仿真Windows系统函数库=======================
2001年6月26日晚 注解
初始化图形系统= void inigraph(int driver,int mode)
入口参数:driver=显示卡类型
mode =显示模式
出口参数:无
*/
void inigraph(int driver,int mode) /* initial graphics system */
{
int graphdriver=driver,graphmode=mode;
/* registerbgidriver(EGAVGA_driver); */
initgraph(&graphdriver,&graphmode," ");
cleardevice();
}
/*
弹出按钮=void popbutt(int x,int y,int lenth,int high,int color,int butttype)
入口参数:x,y =按钮左上脚坐标
lenth,high=按钮长度和高度
color =按钮颜色
buttype =按钮类型 { =任意值时为一条线
=3 为三条线
}
出口参数:无
*/
void popbutt(int x,int y,int lenth,int high,int color,int butttype) /* pop up button */
{
setcolor(color);
setfillstyle(1,color);
bar(x,y,x+lenth,y+high);
setcolor(WHITE);
line(x,y,x+lenth,y);
if(butttype==3){
line(x-1,y-1,x+lenth+1,y-1);
line(x-2,y-2,x+lenth+2,y-2); }
line(x,y,x,y+high);
if(butttype==3){
line(x-1,y-1,x-1,y+high+1);
line(x-2,y-2,x-2,y+high+2); }
setcolor(DARKGRAY);
line(x+lenth,y,x+lenth,y+high);
if(butttype==3){
line(x+lenth+1,y-1,x+lenth+1,y+high+1);
line(x+lenth+2,y-2,x+lenth+2,y+high+2); }
line(x,y+high,x+lenth,y+high);
if(butttype==3){
line(x-1,y+high+1,x+lenth+1,y+high+1);
line(x-2,y+high+2,x+lenth+2,y+high+2); }
}
/*
压入按钮=void pushbutt(int x,int y,int lenth,int high,int color,int butttype)
入口参数:x,y =按钮左上脚坐标
lenth,high=按钮长度和高度
color =按钮颜色
buttype =按钮类型 { =任意值时为一条线
=3 为三条线
}
出口参数:无
*/
void pushbutt(int x,int y,int lenth,int high,int color,int butttype)/* push button */
{
setcolor(color);
setfillstyle(1,color);
bar(x,y,x+lenth,y+high);
setcolor(DARKGRAY);
line(x,y,x+lenth,y);
if(butttype==3){
line(x-1,y-1,x+lenth+1,y-1);
line(x-2,y-2,x+lenth+2,y-2); }
line(x,y,x,y+high);
if(butttype==3){
line(x-1,y-1,x-1,y+high+1);
line(x-2,y-2,x-2,y+high+2); }
setcolor(WHITE);
line(x+lenth,y,x+lenth,y+high);
if(butttype==3){
line(x+lenth+1,y-1,x+lenth+1,y+high+1);
line(x+lenth+2,y-2,x+lenth+2,y+high+2); }
line(x,y+high,x+lenth,y+high);
if(butttype==3){
line(x-1,y+high+1,x+lenth+1,y+high+1);
line(x-2,y+high+2,x+lenth+2,y+high+2); }
}
/*
状态栏 蛌oid stbar(int x,int y,int lenth,int high,int color,int stype)
入口参数:x,y =状态栏左上脚坐标
lenth,high =状态栏长度和高度
color =状态栏颜色
stype =状态栏类型 { =任意值为普通条
=1 为加框条
}
出口参数:无
*/
void stbar(int x,int y,int lenth,int high,int color,int stype) /* status bar */
{
setfillstyle(1,color);
setcolor(color);
bar(x,y,x+lenth,y+high);
if(stype==1){
setcolor(WHITE);
rectangle(x,y,x+lenth,y+high);
rectangle(x+1,y+1,x+lenth-1,y+high-1);
setcolor(DARKGRAY);
rectangle(x,y,x+lenth,y+high);}
}
/*
菜单条 蛌oid menubar(int x,int y,int lenth,int high,int color,int butnum)
入口参数:x,y =菜单条左上脚坐标
lenth,high =菜单条长度和高度
color =菜单条颜色
butnum =菜单条类型 { =任意值为三钮(有最大化,最小化和关闭钮)
=1 只有关闭钮
}
出口参数:无
*/
void menubar(int x,int y,int lenth,int high,int color,int butnum) /* menu bar */
{
setfillstyle(1,color);
setcolor(color);
bar(x,y,x+lenth,y+high);
if(butnum==1) closebar(x+lenth-14,y+3,LIGHTGRAY);
else {
mixbar(x+lenth-42,y+3,LIGHTGRAY);
maxbar(x+lenth-28,y+3,LIGHTGRAY);
closebar(x+lenth-14,y+3,LIGHTGRAY);
}
}
/*
最小化按钮 蛌oid mixbar(int x,int y,int color)
入口参数:x,y =按钮左上脚坐标
color =按钮颜色
出口参数:无
*/
void mixbar(int x,int y,int color)
{ popbutt(x,y,12,12,color,1);
setcolor(DARKGRAY);
line(x+3,y+8,x+9,y+8);
}
/*
最大化按钮 蛌oid mixbar(int x,int y,int color)
入口参数:x,y =按钮左上脚坐标
color =按钮颜色
出口参数:无
*/
void maxbar(int x,int y,int color)
{ popbutt(x,y,12,12,color,1);
setcolor(DARKGRAY);
rectangle(x+3,y+3,x+9,y+9);
}
/*
关闭按钮 蛌oid mixbar(int x,int y,int color)
入口参数:x,y =按钮左上脚坐标
color =按钮颜色
出口参数:无
*/
void closebar(int x,int y,int color)
{ popbutt(x,y,12,12,color,1);
setcolor(DARKGRAY);
line(x+3,y+3,x+9,y+9);
line(x+3,y+9,x+9,y+3);
}
/*
创建窗口=void cwin(int x,int y,int lenth,int high,int color,int butttype)
入口参数:x,y =窗口左上脚坐标
lenth,high=窗口长度和高度
color =窗口颜色
buttype =窗口类型 { =任意值时普通
=3 为带EDIT BOX
}
出口参数:无
本函数还要重写....
*/
void cwin(int x,int y,int lenth,int high,int color,int wtype) /* create a window */
{
setfillstyle(1,color);
setcolor(color);
bar(x,y,x+lenth,y+high);
menubar(x+3,y+2,lenth-5,17,BLUE,wtype);/* wtype=1 only display close button */
setcolor(DARKGRAY);
rectangle(x,y,x+lenth,y+high);
setcolor(WHITE);
line(x,y,x+lenth,y);
line(x,y,x,y+high);
if(wtype==3){ /* if wtype=3 display a text windows and three button */
setfillstyle(1,WHITE);
bar(x+5,y+60,x+lenth-5,y+high-4);
setcolor(DARKGRAY);
line(x+5,y+60,x+lenth-3,y+60);
line(x+5,y+60,x+5,y+high-4);
}
}
/* 鼠标初始化函数
入口参数: xmin=设置X轴最小值
ymin=设置Y轴最小值
xmax=设置X轴最大值
ymax=设置Y轴最大值
出口参数: 返回 0 表示鼠标安装失败
-1 表示鼠标安装成功
*/
int inimouse(int xmin,int xmax,int ymin,int ymax) /* init mouse */
{
int retcode;
regs.x.ax=0;
int86(51,®s,®s);
retcode=regs.x.ax;
if(retcode==0)
return 0;
regs
.x.ax=7;
regs.x.cx=xmin;
regs.x.dx=xmax;
int86(51,®s,®s);
regs.x.ax=8;
regs.x.cx=ymin;
regs.x.dx=ymax;
int86(51,®s,®s);
return retcode;
}
/* 读当前鼠标压下状态
入口参数(出口参数): *mx = X坐标
*my = Y坐标
*mkey= {=1 为左键
2 为右键
返回值=-1 为其它键按下,读状态不成功
= 1 为读状态成功
注:本函数只读左右两键
*/
int readmouse(int *mx,int *my,int *mkey)
{
regs.x.ax=3;
int86(51,®s,®s);
*mkey=regs.x.bx;
if(*mkey==1) regs.x.bx=0;
else if(*mkey==2) regs.x.bx=1;
else return -1;
regs.x.ax=5;
int86(51,®s,®s);
*mx=regs.x.cx;
*my=regs.x.dx;
return 1;
}
/* 取鼠标移动的方向和距离
入口(出口): *mx = X方向移动距离
*my = Y方向移动距离
*/
int mousemove(int *mx,int *my)
{
regs.x.ax=11;
int86(51,®s,®s);
*mx=regs.x.cx;
*my=regs.x.dx;
}
/* 显示鼠标光标 */
void openmouse()
{
regs.x.ax=1;
int86(51,®s,®s);
}
/* 关闭鼠标光标 */
void closemouse()
{
regs.x.ax=2;
int86(51,®s,®s);
}
/* 关闭当前窗口
还需重写........
入口:curwindow=当前窗口代号
*/
void closewindow(int curwindow)
{
setfillstyle(1,CYAN);
setcolor(CYAN);
bar(winstatus[curwindow][0],winstatus[curwindow][1],winstatus[curwindow][0]+winstatus[curwindow][2],winstatus[curwindow][1]+winstatus[curwindow][3]);
curwin==-1;
}
/* 显示开始菜单
还需重写....
*/
void startmenu()
{ stbar(0,460,639,18,LIGHTGRAY,0);
popbutt(2,461,80,17,LIGHTGRAY,1);
setcolor(RED);
strout(9,462,DARKGRAY,"开始菜单");
}
/*
信息窗口=int messbox(int x,int y,int lenth,int high,int color,char *title,char *text,int boxtype)
入口参数:x,y =信息窗口左上脚坐标
lenth,high=信息窗口长度和高度
color =信息窗口钮颜色
*title =信息窗口标题
*text =信息窗口提示文本
boxtype =信息窗口类型{ =任意值为双选钮
=1 为只有一个确定钮
}
出口参数: 返回 1 表示用户选定确定钮
2 表示用户选定取消钮
*/
int messbox(int x,int y,int lenth,int high,int color,char *title,char *text,int boxtype)
{
void *buffer;
int mx,my,mkey,size;
char ch[3]={NULL},*s;
closemouse();
size=imagesize(x,y,x+lenth,y+high);
buffer=malloc(size);
getimage(x,y,x+lenth,y+high,buffer);
cwin(x,y,lenth,high,color,1);
strout(x+4,y+2,WHITE,title);
mx=x+4;
my=y+25;
while(*text!=NULL)
{ *s=*text;
if(mx>=x+lenth-20||*s=='\n') {mx=x+4;my=my+18;++text;continue;}
if(*s==0x20) {mx=mx+8;++text;continue;}
if(*s>='a'&&*s<='z'||*s>='A'&&*s<='Z'||*s>='0'&&*s<'||*s=='['||*s==']'">='9'||*s=='!'||*s=='@'||*s=='+'||*s=='-'||*s=='*'||*s=='/'||*s==','||*s=='.'||*s=='?'||*s=='<'||*s=='>'||*s=='['||*s==']') goto keychar;
if(*s=='\('||*s=='\)'||*s=='\\'||*s=='&'||*s=='^'||*s=='%'||*s=='$'||*s=='#'||*s=='@'||*s=='`'||*s=='~'||*s=='|'||*s=='='||*s=='_'||*s=='{'||*s=='}'||*s=='\''||*s=='"'||*s==';'||*s==':') goto keychar;
goto chinese;
keychar: ch[0]=*text;
ch[1]=NULL;
strout(mx,my,DARKGRAY,ch);
mx=mx+8;
++text;
continue;
chinese: ch[0]=*text++;
ch[1]=*text++;
strout(mx,my,DARKGRAY,ch);
mx=mx+16;
}
/* strout(x+4,y+25,DARKGRAY,text); */
if(boxtype==1){
popbutt(x+lenth/2-30,y+high-40,60,25,LIGHTGRAY,1);
setcolor(DARKGRAY);
strout(x+lenth/2-30,y+high-40+4,DARKGRAY," 确 定 ");
for(;;){
openmouse();
readmouse(&mx,&my,&mkey);
if(mx>=x+lenth/2-30&&mx<=x+lenth/2+30&&my>=y+high-40&&my<=y+high-40+25){
closemouse();
pushbutt(x+lenth/2-30,y+high-40,60,25,LIGHTGRAY,1);
setcolor(DARKGRAY);
strout(x+lenth/2-30,y+high-40+9,DARKGRAY," 确 定 ");
putimage(x,y,buffer,COPY_PUT);
free(buffer);
openmouse();
return 1;
}
}
}
popbutt(x+lenth/2-60-20,y+high-40,60,25,LIGHTGRAY,1);
setcolor(DARKGRAY);
strout(x+lenth/2-60-20,y+high-40+4,DARKGRAY," 确 定 ");
popbutt(x+lenth/2+20,y+high-40,60,25,LIGHTGRAY,1);
setcolor(DARKGRAY);
strout(x+lenth/2+20,y+high-40+4,DARKGRAY," 取 消 ");
for(;;){
openmouse();
readmouse(&mx,&my,&mkey);
if(mx>=x+lenth/2-60-20&&mx<=x+lenth/2-60-20+60&&my>=y+high-40&&my<=y+high-40+25){
closemouse();
pushbutt(x+lenth/2-60-20,y+high-40,60,25,LIGHTGRAY,1);
setcolor(DARKGRAY);
strout(x+lenth/2-60-20,y+high-40+4,DARKGRAY," 确 定 ");
putimage(x,y,buffer,COPY_PUT);
free(buffer);
openmouse();
return 1;
}
if(mx>=x+lenth/2+20&&mx<=x+lenth/2+20+60&&my>=y+high-40&&my<=y+high-40+25){
closemouse();
pushbutt(x+lenth/2+20,y+high-40,60,25,LIGHTGRAY,1);
setcolor(DARKGRAY);
strout(x+lenth/2+20,y+high-40+4,DARKGRAY," 取 消 ");
putimage(x,y,buffer,COPY_PUT);
free(buffer);
openmouse();
return 2;
}
if(mx>=x+lenth-18&&mx<=x+lenth-18+10&&my>=y+3&&my<=y+15)
{ closemouse();
putimage(x,y,buffer,COPY_PUT);
free(buffer);
openmouse();
return 2;
}
}
}
/*
输入窗口=int inputbox(int x,int y,int lenth,int high,int color,char *title,char *text,char *info)
入口参数:x,y =输入窗口左上脚坐标
lenth,high=输入窗口长度和高度
color =输入窗口钮颜色
*title =输入窗口标题
*text =输入窗口提示文本
*info =用户数据输入缓冲区(最大值255个字符)
出口参数: 返回 1 表示用户选定确定钮
2 表示用户选定取消钮
注:不能输入全角汉字字符
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |