五子棋程序(turbo c2下编译)

dirtysalt 2004-12-09 02:58:49
只是单机版,不知道有没有什么方法在dos下进行网络通讯,这样可以做出个联机对站(虽然比较弱质)
/*格局分布,横纵均30行,每行相隔15个坐标单位*/
#include<stdio.h>
#include <graphics.h>
#include <conio.h>
#include <alloc.h>
#include <mem.h>
#define redside 2
#define whiteside 3
char dot[31][31];/*布局的情况*/
int turn=0;/*红方下 还是 白方下*/
int curposx,curposy;/*当前位置*/
void far *imagebuffer[9][4];
int x[]={15,30,450,15,450,15,30,450,225};
int y[]={15,15,15,30,30,450,450,450,225};
/*打印边框*/
void printborder(int x,int y){
setcolor(WHITE);
line(x*15-5,y*15-5,x*15-2,y*15-5);
line(x*15-5,y*15-5,x*15-5,y*15-2);
line(x*15-5,y*15+5,x*15-2,y*15+5);
line(x*15-5,y*15+5,x*15-5,y*15+2);
line(x*15+5,y*15-5,x*15+2,y*15-5);
line(x*15+5,y*15-5,x*15+5,y*15-2);
line(x*15+5,y*15+5,x*15+2,y*15+5);
line(x*15+5,y*15+5,x*15+5,y*15+2);
}
/*判断位置所对应的类型*/
int getpostype(int x,int y){
if((x==1)&&(y==1)){
return 0;
}else
if((x>1)&&(x<30)&&(y==1)){
return 1;
}else
if((x==30)&&(y==1)){
return 2;
}else
if((x==1)&&(y<30)&&(y>1)){
return 3;
}else
if((x==30)&&(y<30)&&(y>1)){
return 4;
}else
if((x==1)&&(y==30)){
return 5;
}else
if((x>1)&&(x<30)&&(y==30)){
return 6;
}else
if((x==30)&&(y==30)){
return 7;
}
else
return 8;
}
/*初始化棋盘和当前位置*/
void initenv(){
int numberline,numbercolumn;
int loop1,loop2;
/*initialize the dot*/
for(loop1=0;loop1<=30;loop1++){
for(loop2=0;loop2<=30;loop2++){
dot[loop1][loop2]=0;
}
}
/*init the image of the map,it has 30rows and 30 columns,and the distance is 15*/
setbkcolor(BLACK);
setcolor(BLUE);
for(numberline=15;numberline<=450;numberline+=15){
line(15,numberline,450,numberline);
}
for(numbercolumn=15;numbercolumn<=450;numbercolumn+=15){
line(numbercolumn,15,numbercolumn,450);
}
turn=redside;
curposx=15;
curposy=15;
}
/*0:左上角边 1:上边 2:右上角边 3:左边 4:右边 5:左下角 6:下边 7:右下角 8:center*/
/* 0:无坐标 1:有坐标 2:red 3:white*/
void initimage(){
int size=0;
int loop1;
for(loop1=0;loop1<=8;loop1++){
size=imagesize(x[loop1]-5,y[loop1]-5,x[loop1]+5,y[loop1]+5);
imagebuffer[loop1][0]=farmalloc(size);
getimage(x[loop1]-5,y[loop1]-5,x[loop1]+5,y[loop1]+5,imagebuffer[loop1][0]);


printborder(x[loop1]/15,y[loop1]/15);
size=imagesize(x[loop1]-5,y[loop1]-5,x[loop1]+5,y[loop1]+5);
imagebuffer[loop1][1]=farmalloc(size);
getimage(x[loop1]-5,y[loop1]-5,x[loop1]+5,y[loop1]+5,imagebuffer[loop1][1]);
putimage(x[loop1]-5,y[loop1]-5,imagebuffer[loop1][0],COPY_PUT);


setfillstyle(SOLID_FILL,RED);
fillellipse(x[loop1],y[loop1],5,5);
size=imagesize(x[loop1]-5,y[loop1]-5,x[loop1]+5,y[loop1]+5);
imagebuffer[loop1][2]=farmalloc(size);
getimage(x[loop1]-5,y[loop1]-5,x[loop1]+5,y[loop1]+5,imagebuffer[loop1][2]);
putimage(x[loop1]-5,y[loop1]-5,imagebuffer[loop1][0],COPY_PUT);


setfillstyle(SOLID_FILL,WHITE);
fillellipse(x[loop1],y[loop1],5,5);
size=imagesize(x[loop1]-5,y[loop1]-5,x[loop1]+5,y[loop1]+5);
imagebuffer[loop1][3]=farmalloc(size);
getimage(x[loop1]-5,y[loop1]-5,x[loop1]+5,y[loop1]+5,imagebuffer[loop1][3]);
putimage(x[loop1]-5,y[loop1]-5,imagebuffer[loop1][0],COPY_PUT);
}
putimage(x[8]-5,y[8]-5,imagebuffer[8][1],COPY_PUT);
}
/*检查当前红方 或是 白方有没有胜利*/
int check(){
int win,loop,i;
for(i=0;i<=4;i++){
if(((curposx+i)<=30)&&(((curposx+i-4)>=1))){
for(loop=curposx+i-4;loop<=curposx+i;loop++){
if(dot[loop][curposy]!=turn){
win=0;
break;
}
else
win=1;
}
if(win==0)
continue;
if(win==1)
goto over;
}
}
for(i=0;i<=4;i++){
if(((curposy+i)<=30)&&(((curposy+i-4)>=1))){
for(loop=curposy+i-4;loop<=curposy+i;loop++){
if(dot[curposx][loop]!=turn){
win=0;
break;
}
else
win=1;
}
if(win==0)
continue;
if(win==1)
goto over;
}
}
for(i=0;i<=4;i++){
if(((curposy+i)<=30)&&((curposx-i)>=1)&&((curposy+i-4)>=1)&&((curposx+4-i)<=30)){
for(loop=(-i);loop<=(4-i);loop++){
if(dot[curposx+loop][curposy-loop]!=turn){
win=0;
break;
}
else
win=1;
}
if(win==0)
continue;
if(win==1)
goto over;
}
}
for(i=0;i<=4;i++){
if(((curposx+i)<=30)&&((curposy+i)<=30)&&((curposx+i-4)>=1)&&((curposy+i-4)>=1)){
for(loop=(-i);loop<=(4-i);loop++){
if(dot[curposx-loop][curposy-loop]!=turn){
win=0;
break;
}
else
win=1;
}
if(win==0)
continue;
if(win==1)
goto over;
}
}
if(win==0)
return 0;
over:
return 1;
}
/*按下enter 确定的动作*/
int putdown(){
int win=0;int postype;
if(dot[curposx][curposy]==0){
dot[curposx][curposy]=turn;
postype=getpostype(curposx,curposy);
putimage(curposx*15-5,curposy*15-5,imagebuffer[postype][0],COPY_PUT);
switch(turn){
case redside:
{
setfillstyle(SOLID_FILL,RED);
if(check()==1)
win=1;
turn=whiteside;
break;
}
case whiteside:
{
setfillstyle(SOLID_FILL,WHITE);
if(check()==1)
win=1;
turn=redside;
break;
}
}
fillellipse(curposx*15,curposy*15,5,5);
}
return win;
}
/*up动作*/
void up(){
int turncopy,postype[2];
if((curposy-1)>=1){
postype[0]=getpostype(curposx,curposy);
postype[1]=getpostype(curposx,curposy-1);
turncopy=dot[curposx][curposy];
putimage(curposx*15-5,curposy*15-5,imagebuffer[postype[0]][turncopy],COPY_PUT);
putimage(curposx*15-5,curposy*15-20,imagebuffer[postype[1]][1],COPY_PUT);
curposy-=1;
}
}
/*down动作*/
void down(){
int turncopy,postype[2];
if((curposy+1)<=30){
postype[0]=getpostype(curposx,curposy);
postype[1]=getpostype(curposx,curposy+1);
turncopy=dot[curposx][curposy];
putimage(curposx*15-5,curposy*15-5,imagebuffer[postype[0]][turncopy],COPY_PUT);
putimage(curposx*15-5,curposy*15+10,imagebuffer[postype[1]][1],COPY_PUT);
curposy+=1;
}
}
/*left动作*/
void left(){
int turncopy,postype[2];
if((curposx-1)>=1){
postype[0]=getpostype(curposx,curposy);
postype[1]=getpostype(curposx-1,curposy);
turncopy=dot[curposx][curposy];
putimage(curposx*15-5,curposy*15-5,imagebuffer[postype[0]][turncopy],COPY_PUT);
putimage(curposx*15-20,curposy*15-5,imagebuffer[postype[1]][1],COPY_PUT);
curposx-=1;
}
}
/*right动作*/
void right(){
int turncopy,postype[2];
if((curposx+1)<=30){
postype[0]=getpostype(curposx,curposy);
postype[1]=getpostype(curposx+1,curposy);
turncopy=dot[curposx][curposy];
putimage(curposx*15-5,curposy*15-5,imagebuffer[postype[0]][turncopy],COPY_PUT);
putimage(curposx*15+10,curposy*15-5,imagebuffer[postype[1]][1],COPY_PUT);
curposx+=1;
}
}
void main(){
int graphdriver=DETECT,graphmode;
int a=0;
initgraph(&graphdriver,&graphmode,"G:\\turboc2\\bgi");/*此处硬编码,为bgi的路径*/
initenv();
initimage();
a=getch();
while(a!=101){
switch (a)
{
case 13:{
if(putdown()==1){
if(turn==redside){
printf("white win\n");
}
if(turn==whiteside){
printf("red win\n");
}
getch();
clearviewport();
initenv();
putimage(220,220,imagebuffer[8][1],COPY_PUT);
}
break;
}
case 119:up();break; /*up*/
case 97:left();break; /*left*/
case 100:right();break; /*right*/
case 115:down();break; /*down*/
}
a=getch();
}
getch();
closegraph();
}
...全文
152 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanweiqi 2004-12-09
  • 打赏
  • 举报
回复
..
laoshiok 2004-12-09
  • 打赏
  • 举报
回复
我是培训学校的,现招聘计算机等级考试教师,教二级C、VB、VF三级网络技术。
联系电话:13930647373
171ok@sina.com
bailindf 2004-12-09
  • 打赏
  • 举报
回复
考下来,慢慢看。。。。

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧