| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5322 人关注过本帖, 1 人收藏
标题:[原创]五子棋程序
只看楼主 加入收藏
zhangyibao
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2006-5-24
收藏(1)
 问题点数:0 回复次数:13 
[原创]五子棋程序
这个是我用最基础(只要会循环,函数,画图,数组即可)的C语言编的一个五子棋程序,发上来目的是请求大家的讨论以求得完美!!!!
搜索更多相关主题的帖子: 五子棋 C语言 画图 
2006-08-01 10:37
zhangyibao
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2006-5-24
收藏
得分:0 
欢迎大家参与讨论!!!!!!!!!!!!!
2006-08-01 10:38
zhangyibao
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2006-5-24
收藏
得分:0 

/************************************************************************************/
/***********************作品名称:〈超 级 五 子 棋〉*********************************/
/***************************学校: 八 一 农 垦 大 学*************************/
/***************************专业:通 信 工 程****************************************/
/***************************作者: Z.Y.B ****************************************/
/************************************************************************************/
/******************************头函数及自定义函数************************************/
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
#define PI 3.1415926
void INIT();/************图形初始化子函数********************************************/
void St();/**************开始画面子函数**********************************************/
void Ed();/**************结束画面子函数**********************************************/
void INITQP(int c1,int c2);/**********画棋盘函数*************************************/
void HUAZI(int x,int y,int q);/********画棋子函数************************************/
void message(int f,int f0);/**********显示信息子函数*********************************/
void sd(int x,int y,int p);/***********显示当前玩家**********************************/
void SJ();/****************************显示日期**************************************/
void wg();/********************画棋盘网格********************************************/
void sounds(int f);/***************响铃函数******************************************/
void XL(int f);/*****************笑脸************************************************/
char ZouQi();/**********走棋函数*****************************************************/
int PY(int py[21][21]);/************判赢函数*****************************************/
int mx[3];/**********定义一个全程变量保存玩家得分和优先走棋标志**********************/
/*********主函数********/
void main()
{
char c,c0;
mx[0]=0,mx[1]=0,mx[2]=1;

St();
INIT();
INITQP(mx[0],mx[1]);

c0=getch();
while(c0==121)
{
closegraph();
INIT();
INITQP(mx[0],mx[1]);
c=ZouQi();
if(c==113)break;
if(c==99)c0=121;
else break;
}
cleardevice();
Ed();
closegraph();
printf("\n\n\n\n\n\n\n\n\n\n\n Please press any key to EXIT!!!!");
getch();

}
/*图形初始化*/
void INIT()
{
int gd=DETECT,gm;
registerbgidriver(EGAVGA_driver);
initgraph(&gd,&gm,"");
cleardevice();
}
/**************开始画面**************/
void St()
{
int x1,y1,x2,y2,r1,r2,r0=100;
float x,h=PI/3;
INIT();
setbkcolor(0);

setcolor(10);
settextstyle(0,0,2);
outtextxy(263,350,"WELCOM!");

for(r0=100;r0>=0;r0-=20)
for(x=0;x<=5*PI/3;x+=h)
{
x1=320+r0*cos(x);
y1=240+r0*sin(x);
r1=10;
setcolor(4);
setfillstyle(1,4);
circle(x1,y1,r1);
floodfill(x1,y1,4);

delay(1000000);
x+=h;
x2=320+r0*cos(x);
y2=240+r0*sin(x);
r2=10;
setcolor(10);
setfillstyle(1,10);
circle(x2,y2,r2);
floodfill(x2,y2,10);
delay(1000000);
}
printf("\n\n\n\n\n Please press any key to EXIT!!!!");
getch();

closegraph();
}
/**************结束画面**************/
void Ed()
{
int x1,y1,x2,y2,r1,r2,r0;
float x,h=PI/3;
setbkcolor(0);

setcolor(10);
settextstyle(0,0,2);
outtextxy(260,350,"Bye!Bye!");

for(r0=0;r0<=100;r0+=20)
for(x=0;x<=5*PI/3;x+=h)
{
x1=320+r0*cos(x);
y1=240+r0*sin(x);
r1=10;
setcolor(4);
setfillstyle(1,4);
circle(x1,y1,r1);
floodfill(x1,y1,4);

delay(1000000);
x+=h;
x2=320+r0*cos(x);
y2=240+r0*sin(x);
r2=10;
setcolor(10);
setfillstyle(1,10);
circle(x2,y2,r2);
floodfill(x2,y2,10);
delay(1000000);
}
}
/*画棋盘函数*/
void INITQP(int c1,int c2)
{
int i;
unsigned char *s1="\0";
unsigned char *s2="\0";
setbkcolor(0);

setcolor(1);
setfillstyle(1,1);
bar(120,40,520,440);
floodfill(320,240,1);
wg();

printf(" Please don't go out of the boundary!\n");
printf(" *********五子棋*********\n");
printf("游戏说明:\n");
printf("Player1:\n");
printf("up--W\n");
printf("down--S\n");
printf("left--A\n");
printf("right--D\n");
printf("Yes--NULL\n\n\n\n");
printf("Player2:\n");
printf("up--I\n");
printf("down--K\n");
printf("left--J\n");
printf("right--L\n");
printf("Yes--Enter\n\n\n");
printf("Start--Y\n");
printf("Continue--C\n");
printf("Q--Exit"); /*****游戏注释****/

sprintf(s1,"%d",c1);
setcolor(10);
setfillstyle(1,10);
ellipse(20,180,0,360,20,10);
floodfill(20,180,10);
setcolor(4);
settextstyle(0,0,2);
outtextxy(13,175,s1); /***player1得分****/

sprintf(s2,"%d",c2);
setcolor(4);
setfillstyle(1,4);
ellipse(20,310,0,360,20,10);
floodfill(20,310,4);
setcolor(10);
settextstyle(0,0,2);
outtextxy(13,305,s2); /***player2得分****/

setcolor(4);
settextstyle(1,0,8);
outtextxy(330,450,"Designed by ZHANG YI BAO");
outtextxy(435,460,"2006--5--28");
SJ();
}
/*画棋子函数*/
void HUAZI(int x,int y,int q)
{
setcolor(q);
setfillstyle(1,q);
circle(x,y,10);
floodfill(x,y,q);
}
/*显示信息*/
void message(int f,int f0)
{
switch(f)
{
case 1:
setcolor(0);
settextstyle(1,0,3);
outtextxy(540,60,"Player2 go!");
outtextxy(540,60,"Player2 win!");

setcolor(10);
settextstyle(1,0,3);
if(f0==0)
{outtextxy(540,60,"Player1 go!");}
else {outtextxy(540,60,"Player1 win!");sounds(1);XL(1);}
break;
case 2:
setcolor(0);
settextstyle(1,0,3);
outtextxy(540,60,"Player1 go!");
outtextxy(540,60,"Player1 win!");

setcolor(10);
settextstyle(1,0,3);
if(f0==0)
{outtextxy(540,60,"Player2 go!");}
else {outtextxy(540,60,"Player2 win!");sounds(2);XL(2);}
break;
}
}
/***********当前玩家***************/
void sd(int x,int y,int p)
{
do
{
setcolor(p);
setfillstyle(1,p);
circle(x,y,5);
floodfill(x,y,p);

delay(1000);
setcolor(1);
setfillstyle(1,1);
circle(x,y,5);
floodfill(x,y,1);
}while(!kbhit());
}
/***画棋盘网格***/
void wg()
{
int i;
setcolor(13);
setlinestyle(2,0,1);
for(i=0;i<21;i++)
{
line(120,40+i*20,520,40+i*20);
line(120+i*20,40,120+i*20,440);
}
}
/**********走棋函数*********/
char ZouQi()
{
int i,j,t;
int x1,y1,x2,y2;
int py[21][21]={0};
char c,k1,k2;

x1=140;y1=60;
x2=x1+360;y2=y1;

while(1)
{
if(mx[2]==1)
{
message(1,0);
sd(x1,y1,4);
while(1)
{
k1=getch();
/********玩家1的按键判断************/
switch(k1)
{
case 97:sd(x1-20,y1,4);x1=x1-20;if(x1==120)closegraph();wg();break; /*******左移(A)*******/
case 100:sd(x1+20,y1,4);x1=x1+20;if(x1==520)closegraph();wg();break; /*******右移(D)*******/
case 119:sd(x1,y1-20,4);y1=y1-20;if(y1==40)closegraph();wg();break; /*******上移(W)*******/
case 115:sd(x1,y1+20,4);y1=y1+20;if(y1==440)closegraph();wg();break; /*******下移(S)*******/
case 113:c=113;goto R;break; /*******退出(Q)*******/
case 32:HUAZI(x1,y1,4);x1=x1+20;wg();i=(x1-120)/20;j=(y1-40)/20;py[i][j]=1;t=PY(py);
if(t==1){c=getch();t=0;mx[0]+=1;mx[2]=2;goto R;} /*******判断输赢及退出还是继续(Q/C)*******/
}
if(k1==32) break;
}
}

message(2,0);
sd(x2,y2,10);
while(1)
{
k2=getch();
/********玩家2的按键判断************/
switch(k2)
{
case 105:sd(x2,y2-20,10);y2=y2-20;if(y2==40)closegraph();wg();break; /*******上移(I)*******/
case 107:sd(x2,y2+20,10);y2=y2+20;if(y2==440)closegraph();wg();break; /*******下移(K)*******/
case 106:sd(x2-20,y2,10);x2=x2-20;if(x2==120)closegraph();wg();break; /*******左移(J)*******/
case 108:sd(x2+20,y2,10);x2=x2+20;if(x2==520)closegraph();wg();break; /*******右移(L)*******/
case 113:c=113;goto R;break; /*******退出(Q)*******/
case '\r':HUAZI(x2,y2,10);x2+=20;wg();i=(x2-120)/20;j=(y2-40)/20;py[i][j]=2;t=PY(py);
if(t==1){c=getch();t=0;mx[1]+=1;mx[2]=1;goto R;} /*******判断输赢及退出还是继续(Q/C)*******/
}
if(k2=='\r')break;
}
mx[2]=1;
}
R:return(c); /*********返回键值**********/
}
/************判赢函数**************/
int PY(int py[21][21])
{
int i,j,k,t=0,p1=0,p2=0;
/*******判行*********/
for(i=0;i<21;i++)
for(j=0;j<17;j++)
{
for(k=0;k<5;k++)
{
if(py[i][j+k]==1)p1++;
if(py[i][j+k]==2)p2++;
}
if(p1==5){message(1,1);t=1;}
if(p2==5){message(2,1);t=1;}
p1=0;p2=0;
}
/**********判列**********/
for(j=0;j<21;j++)
for(i=0;i<21;i++)
{
for(k=0;k<5;k++)
{
if(py[i+k][j]==1)p1++;
if(py[i+k][j]==2)p2++;
}
if(p1==5){message(1,1);t=1;}
if(p2==5){message(2,1);t=1;}
p1=0;p2=0;
}
/**************判对角(左上-右下)*************/
for(j=0;j<21;j++)
for(i=0;i<16;i++)
{
for(k=0;k<5;k++)
{
if(py[i+k][i+j+k]==1)p1++;
if(py[i+k][i+j+k]==2)p2++;
}
if(p1==5){message(1,1);t=1;}
if(p2==5){message(2,1);t=1;}
p1=0;p2=0;
}

for(j=1;j<21;j++)
for(i=j;i<21;i++)
{
for(k=0;k<5;k++)
{
if(py[i+k][i-j+k]==1)p1++;
if(py[i+k][i-j+k]==2)p2++;
}
if(p1==5){message(1,1);t=1;}
if(p2==5){message(2,1);t=1;}
p1=0;p2=0;
}
/*****************判对角(左下-右上)**************/
for(j=20;j>=0;j--)
for(i=0;i<21;i++)
{
for(k=0;k<5;k++)
{
if(py[i+k][j-i-k]==1)p1++;
if(py[i+k][j-i-k]==2)p2++;
}
if(p1==5){message(1,1);t=1;}
if(p2==5){message(2,1);t=1;}
p1=0;p2=0;
}

for(j=21;j<41;j++)
for(i=j-20;i<21;i++)
{
for(k=0;k<5;k++)
{
if(py[i+k][j-i-k]==1)p1++;
if(py[i+k][j-i-k]==2)p2++;
}
if(p1==5){message(1,1);t=1;}
if(p2==5){message(2,1);t=1;}
p1=0;p2=0;
}

return(t);/**************返回优胜标志************/
}
/***************响铃函数***************/
void sounds(int f)
{
if(f==1)
{
sound(2000);delay(1000000);sound(1500);delay(100000);sound(500);delay(10000);nosound();
}
if(f==2)
{
sound(500);delay(1000000);sound(750);delay(100000);sound(2000);delay(10000);nosound();
}
}
/*****************笑脸****************/
void XL(int f)
{
int c1,c2,i;
if(f==1){c1=4;c2=10;}
else {c1=10;c2=4;}
do{
setcolor(c1);
setfillstyle(1,c1);
circle(580,90,20);
floodfill(580,90,c1);
setcolor(c2);
arc(570,90,60,120,10);
arc(590,90,60,120,10);
arc(580,90,240,340,10);
arc(580,102,150,360,3);

delay(1000000);
setcolor(0);
setfillstyle(1,0);
circle(580,90,20);
floodfill(580,90,0);
arc(570,90,60,120,10);
arc(590,90,60,120,10);
arc(580,90,240,340,10);
arc(580,102,150,360,3);
}while(!kbhit());
}
/************显示日期*************/
void SJ()
{
unsigned char *riqi="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
struct date d;
int year,mon,day;
getdate(&d);
sprintf(riqi,"%4d-%02d-%02d",d.da_year,d.da_mon,d.da_day);

setcolor(6);
setlinestyle(0,0,1);
setfillstyle(1,6);
bar3d(530,400,610,435,4,1);
floodfill(550,420,6);

setcolor(15);
settextstyle(3,0,8);
outtextxy(530,415,riqi);
}

2006-08-01 10:41
zhangyibao
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2006-5-24
收藏
得分:0 

这个程序是我花了一周的时间编的!欢迎大家指导!

2006-09-02 11:02
vbc
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:352
专家分:147
注 册:2006-12-30
收藏
得分:0 

是不是可以人机对战的???


清远鹏程万里人才网:[url=http://www.]http://www.[/url]zq.,qy.
2006-12-30 21:53
vbc
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:352
专家分:147
注 册:2006-12-30
收藏
得分:0 
不可以人机对战的就不要放上来了.

清远鹏程万里人才网:[url=http://www.]http://www.[/url]zq.,qy.
2006-12-30 22:10
mn51pj
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2006-12-1
收藏
得分:0 
。。。

都是强人
2006-12-30 22:25
vbc
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:352
专家分:147
注 册:2006-12-30
收藏
得分:0 
等我编好人机对战再来给你看看..

清远鹏程万里人才网:[url=http://www.]http://www.[/url]zq.,qy.
2006-12-30 22:39
卧龙孔明
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:59
帖 子:3872
专家分:684
注 册:2006-10-13
收藏
得分:0 
先支持一下楼主

希望楼主继续学习努力,早日写出一个有人工智能的人机对战版来.

My Blog: www.aiexp.info
虽然我的路是从这里开始的,但是这里不再是乐土.感谢曾经影响过,引导过,帮助过我的董凯,飞燕,leeco,starwing,Rockcarry,soft_wind等等等等.别了,BCCN.
2006-12-31 10:01
ffaannggqq
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2006-12-23
收藏
得分:0 
我顶一下

2006-12-31 14:21
快速回复:[原创]五子棋程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.055064 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved