c语言五子棋游戏编程代码自动(c语言实现简单的五子棋代码)

http://www.itjxue.com  2023-03-05 11:21  来源:未知  点击次数: 

求一个用C语言编写五子棋游戏的全部代码.

这个程序还需要两个文件,告诉我你邮箱,我发给你

源程序前半部分

/*加载头文件*/

#includestdio.h

#includestdlib.h

#includegraphics.h

#includebios.h

#includeconio.h

/*编译预处理,定义按键码*/

#define LEFT 0x4b00

#define RIGHT 0x4d00

#define DOWN 0x5000

#define UP 0x4800

/*若想在游戏中途退出, 可按 Esc 键*/

#define ESC 0x011b

/*SPACE键表示落子*/

#define SPACE 0x3920

/*设置偏移量*/

#define OFFSET 20

#define OFFSET_x 4

#define OFFSET_y 3

/*定义数组大小*/

#define N 19

/*定义全局变量*/

int status[N][N]; /*定义的数组,保存状态*/

int step_x,step_y;/*行走的坐标*/

int key ; /*获取按下的键盘的键*/

int flag; /*玩家标志*/

/*自定义函数原型*/

void DrawBoard();

void DrawCircle(int x,int y,int color);

void Alternation();

void JudgePlayer(int x,int y);

void Done();

int ResultCheck(int x,int y);

void WelcomeInfo();

void ShowMessage();

/*定义函数*/

/*显示欢迎信息函数*/

void WelcomeInfo()

{

char ch ;

/*移动光标到指定位置*/

gotoxy(12,4);

/*显示欢迎信息*/

printf("Welcome you to gobang word!");

gotoxy(12,6);

printf("1.You can use the up,down,left and right key to move the chessman,");

gotoxy(12,8);

printf(" and you can press Space key to enter after you move it !");

gotoxy(12,10);

printf("2.You can use Esc key to exit the game too !");

gotoxy(12,12);

c语言基础,求五子棋详细代码

/*一个月就想做五子棋,有点难啊,希望你能看懂,这是代码*/

#include?stdlib.h

#include?stdio.h

#include?conio.h

#include?string.h

#define?MAXIMUS?15?//定义棋盘大小

int?p[MAXIMUS][MAXIMUS];//存储对局信息

char?buff[MAXIMUS*2+1][MAXIMUS*4+3];//输出缓冲器

int?Cx,Cy;//当前光标位置

int?Now;//当前走子的玩家,1代表黑,2代表白

int?wl,wp;//当前写入缓冲器的列数和行数位置

char*?showText;//在棋盘中央显示的文字信息

int?count;//回合数

char*?Copy(char*?strDest,const?char*?strSrc)//修改过的字符串复制函数,会忽略末端的\0

{

????char*?strDestCopy?=?strDest;

????while?(*strSrc!='\0')

????{

????????*strDest++=*strSrc++;

????}

????return?strDestCopy;

}

void?Initialize()//初始化一个对局函数

{

????int?i,j;//循环变量

????showText="";//重置显示信息

????count=0;//回合数归零

????for(i=0;iMAXIMUS;i++)//重置对局数据

????{

????????for(j=0;jMAXIMUS;j++)

????????{

????????????p[i][j]=0;

????????}

????}

????Cx=Cy=MAXIMUS/2;//重置光标到中央

????Now=1;//重置当前为黑方

}

char*?getStyle(int?i,int?j)//获得棋盘中指定坐标交点位置的字符,通过制表符拼成棋盘

{

????if(p[i][j]==1)//1为黑子

????????return?"●";

????else?if(p[i][j]==2)//2为白子

????????return?"○";

????else?if(i==0j==0)//以下为边缘棋盘样式

????????return?"┏";

????else?if(i==MAXIMUS-1j==0)

????????return?"┓";

????else?if(i==MAXIMUS-1j==MAXIMUS-1)

????????return?"┛";

????else?if(i==0j==MAXIMUS-1)

????????return?"┗";

????else?if(i==0)

????????return?"┠";

????else?if(i==MAXIMUS-1)

????????return?"┨";

????else?if(j==0)

????????return?"┯";

????else?if(j==MAXIMUS-1)

????????return?"┷";

????return?"┼";//中间的空位

}

char*?getCurse(int?i,int?j)//获得指定坐标交点位置左上格的样式,通过制表符来模拟光标的显示

{

????if(i==Cx)

????{

????????if(j==Cy)

????????????return?"┏";

????????else?if?(j==Cy+1)

????????????return?"┗";

????}

????else?if(i==Cx+1)

????{

????????if(j==Cy)

????????????return?"┓";

????????else?if?(j==Cy+1)

????????????return?"┛";

????}

????return?" ";//如果不在光标附近则为空

}

void?write(char*?c)//向缓冲器写入字符串

{

????Copy(buff[wl]+wp,c);

????wp+=strlen(c);

}

void?ln()//缓冲器写入位置提行

{

????wl+=1;

????wp=0;

}

void?Display()//将缓冲器内容输出到屏幕

{

????int?i,l=strlen(showText);//循环变量,中间文字信息的长度

????int?Offset=MAXIMUS*2+2-l/2;//算出中间文字信息居中显示所在的横坐标位置

????if(Offset%2==1)//如果位置为奇数,则移动到偶数,避免混乱

????{

????????Offset--;

????}

????Copy(buff[MAXIMUS]+Offset,showText);//讲中间文字信息复制到缓冲器

????if(l%2==1)//如果中间文字长度为半角奇数,则补上空格,避免混乱

????{

????????*(buff[MAXIMUS]+Offset+l)=0x20;

????}

????system("cls");//清理屏幕,准备写入

????for(i=0;iMAXIMUS*2+1;i++)//循环写入每一行

????{

????????printf("%s",buff[i]);

????????if(iMAXIMUS*2)//写入完每一行需要换行

????????????printf("\n");

????}

}

void?Print()//将整个棋盘算出并储存到缓冲器,然后调用Display函数显示出来

{

????int?i,j;//循环变量

????wl=0;

????wp=0;

????for(j=0;j=MAXIMUS;j++)//写入出交点左上角的字符,因为需要打印棋盘右下角,所以很以横纵各多一次循环

????{

????????for(i=0;i=MAXIMUS;i++)

????????{

????????????write(getCurse(i,j));//写入左上角字符

????????????if(j==0||j==MAXIMUS)//如果是棋上下盘边缘则没有连接的竖线,用空格填充位置

????????????{

????????????????if(i!=MAXIMUS)

????????????????????write(" ");

????????????}

????????????else//如果在棋盘中间则用竖线承接上下

????????????{

????????????????if(i==0||i==MAXIMUS-1)//左右边缘的竖线更粗

????????????????????write("┃");

????????????????else?if(i!=MAXIMUS)//中间的竖线

????????????????????write("│");

????????????}

????????}

????????if(j==MAXIMUS)//如果是最后一次循环,则只需要处理边侧字符,交点要少一排

????????{

????????????break;

????????}

????????ln();//提行开始打印交点内容

????????write(" ");//用空位补齐位置

????????for(i=0;iMAXIMUS;i++)//按横坐标循环正常的次数

????????{

????????????write(getStyle(i,j));//写入交点字符

????????????if(i!=MAXIMUS-1)//如果不在最右侧则补充一个横线承接左右

????????????{

????????????????if(j==0||j==MAXIMUS-1)

????????????????{

????????????????????write("━");//上下边缘的横线更粗

????????????????}

????????????????else

????????????????{

????????????????????write("—");//中间的横线

????????????????}

????????????}

????????}

????????ln();//写完一行后提行

????}

????Display();//将缓冲器内容输出到屏幕

}

int?Put()//在当前光标位置走子,如果非空,则返回0表示失败

{

????if(p[Cx][Cy]==0)

????{

????????p[Cx][Cy]=Now;//改变该位置数据

????????return?1;//返回1表示成功

????}

????else

????{

????????return?0;

????}

}

int?Check()//胜负检查,即判断当前走子位置有没有造成五连珠的情况

{

????int?w=1,x=1,y=1,z=1,i;//累计横竖正斜反邪四个方向的连续相同棋子数目

????for(i=1;i5;i++)//向下检查

????????if(Cy+iMAXIMUSp[Cx][Cy+i]==Now)

????????????w++;

????????else?

????????????break;

????for(i=1;i5;i++)//向上检查

????????if(Cy-i0p[Cx][Cy-i]==Now)

????????????w++;

????????else?

????????????break;

????if(w=5)//若果达到5个则判断当前走子玩家为赢家

????????return?Now;

????for(i=1;i5;i++)//向右检查

????????if(Cx+iMAXIMUSp[Cx+i][Cy]==Now)

????????????x++;

????????else?

????????????break;

????for(i=1;i5;i++)//向左检查

????????if(Cx-i0p[Cx-i][Cy]==Now)

????????????x++;

????????else?

????????????break;

????if(x=5)//若果达到5个则判断当前走子玩家为赢家

????????return?Now;

????for(i=1;i5;i++)//向右下检查

????????if(Cx+iMAXIMUSCy+iMAXIMUSp[Cx+i][Cy+i]==Now)

????????????y++;

????????else?

????????????break;

????for(i=1;i5;i++)//向左上检查

????????if(Cx-i0Cy-i0p[Cx-i][Cy-i]==Now)

????????????y++;

????????else?

????????????break;

????if(y=5)//若果达到5个则判断当前走子玩家为赢家

????????return?Now;

????for(i=1;i5;i++)//向右上检查

????????if(Cx+iMAXIMUSCy-i0p[Cx+i][Cy-i]==Now)

????????????z++;

????????else?

????????????break;

????for(i=1;i5;i++)//向左下检查

????????if(Cx-i0Cy+iMAXIMUSp[Cx-i][Cy+i]==Now)

????????????z++;

????????else?

????????????break;

????if(z=5)//若果达到5个则判断当前走子玩家为赢家

????????return?Now;

????return?0;//若没有检查到五连珠,则返回0表示还没有玩家达成胜利

}

int?RunGame()//进行整个对局,返回赢家信息(虽然有用上)

{

????int?input;//输入变量

????int?victor;//赢家信息

????Initialize();//初始化对局

????while(1)//开始无限回合的死循环,直到出现胜利跳出

????{

????????Print();//打印棋盘

????????input=getch();//等待键盘按下一个字符

????????if(input==27)//如果是ESC则退出程序

????????{

????????????exit(0);

????????}

????????else?if(input==0x20)//如果是空格则开始走子

????????{

????????????if(Put())//如果走子成功则判断胜负

????????????{

????????????????victor=Check();

????????????????Now=3-Now;//轮换当前走子玩家

????????????????count++;

????????????????if(victor==1)//如果黑方达到胜利,显示提示文字并等待一次按键,返回胜利信息

????????????????{

????????????????????showText="黑方获得了胜利!";

????????????????????Print();

????????????????????if(getch()==0xE0)

????????????????????{

????????????????????????getch();

????????????????????}

????????????????????return?Now;

????????????????}

????????????????else?if(victor==2)//如果白方达到胜利,显示提示文字并等待一次按键,返回胜利信息

????????????????{

????????????????????showText="白方获得了胜利!";

????????????????????Display();

????????????????????if(getch()==0xE0)

????????????????????{

????????????????????getch();

????????????????????}

????????????????????return?Now;

????????????????}

????????????????else?if(count==MAXIMUS*MAXIMUS)//如果回合数达到了棋盘总量,即棋盘充满,即为平局

????????????????{

????????????????????showText="平局!";

????????????????????Display();

????????????????????if(getch()==0xE0)

????????????????????{

????????????????????????getch();

????????????????????}

????????????????????return?0;

????????????????}

????????????}

????????}

????????else?if(input==0xE0)//如果按下的是方向键,会填充两次输入,第一次为0xE0表示按下的是控制键

????????{

????????????input=getch();//获得第二次输入信息

????????????switch(input)//判断方向键方向并移动光标位置

????????????{

????????????????case?0x4B:

????????????????Cx--;

????????????????break;

????????????????case?0x48:

????????????????Cy--;

????????????????break;

????????????????case?0x4D:

????????????????Cx++;

????????????????break;

????????????????case?0x50:

????????????????Cy++;

????????????????break;

????????????}

????????????if(Cx0)//如果光标位置越界则移动到对侧

????????????????Cx=MAXIMUS-1;

????????????if(Cy0)

????????????????Cy=MAXIMUS-1;

????????????if(CxMAXIMUS-1)

????????????????Cx=0;

????????????if(CyMAXIMUS-1)

????????????????Cy=0;

????????}

????}

}

int?main()//主函数

{

????system("title?简易五子棋?——Etsnarl制作");//设置标题

????system("mode?con?cols=63?lines=32");//设置窗口大小

????system("color?E0");//设置颜色

????while(1)//循环执行游戏

????{

????????RunGame();

????}

????return?0;

}

c语言的五子棋代码(博弈算法)

#include?stdio.h

#include?bios.h

#include?ctype.h

#include?conio.h

#include?dos.h

#define?CROSSRU?????0xbf???/*右上角点*/

#define?CROSSLU?????0xda???/*左上角点*/

#define?CROSSLD?????0xc0???/*左下角点*/

#define?CROSSRD?????0xd9???/*右下角点*/

#define?CROSSL???????0xc3???/*左边*/

#define?CROSSR???????0xb4???/*右边*/

#define?CROSSU???????0xc2???/*上边*/

#define?CROSSD???????0xc1???/*下边*/

#define?CROSS???????0xc5???/*十字交叉点*/

/*定义棋盘左上角点在屏幕上的位置*/

#define?MAPXOFT?????5

#define?MAPYOFT?????2

/*定义1号玩家的操作键键码*/

#define?PLAY1UP?????0x1157/*上移--'W'*/

#define?PLAY1DOWN???0x1f53/*下移--'S'*/

#define?PLAY1LEFT???0x1e41/*左移--'A'*/

#define?PLAY1RIGHT???0x2044/*右移--'D'*/

#define?PLAY1DO?????0x3920/*落子--空格键*/

/*定义2号玩家的操作键键码*/

#define?PLAY2UP?????0x4800/*上移--方向键up*/

#define?PLAY2DOWN???0x5000/*下移--方向键down*/

#define?PLAY2LEFT???0x4b00/*左移--方向键left*/

#define?PLAY2RIGHT???0x4d00/*右移--方向键right*/

#define?PLAY2DO?????0x1c0d/*落子--回车键Enter*/

/*若想在游戏中途退出,?可按?Esc?键*/

#define?ESCAPE???????0x011b

/*定义棋盘上交叉点的状态,?即该点有无棋子?*/

/*若有棋子,?还应能指出是哪个玩家的棋子???*/

#define?CHESSNULL???0???/*没有棋子*/

#define?CHESS1???????'O'/*一号玩家的棋子*/

#define?CHESS2???????'X'/*二号玩家的棋子*/

/*定义按键类别*/

#define?KEYEX99v?????????0/*退出键*/

#define?KEYFALLCHESS???1/*落子键*/

#define?KEYMOVECURSOR???2/*光标移动键*/

#define?KEYINVALID?????3/*无效键*/

/*定义符号常量:?真,?假?---?真为1,?假为0?*/

#define?TRUE?????????1

#define?FALSE???????0

/**********************************************************/

/*?定义数据结构???????????????????????????????????????????*/

/*棋盘交叉点坐标的数据结构*/

struct?point

{

int?x,y;

};

或者下面这个:

#include?graphics.h

#include?stdlib.h

#include?stdio.h

#include?conio.h

#define?N?15

#define?B?7

#define?STOP?-10000

#define?OK?1

#define?NO?0

#define?UP?328

#define?DOWN?336

#define?LEFT?331

#define?RIGHT?333

int?a[N+1][N+1];

int?zx,zy;

int?write=1,biaoji=0;

struct?zn{

long?sum;

int?y;

int?x;

}w[N+1][N+1],max,max1;

void?cbar(int?i,int?x,int?y,int?r);

void?map(int?a[][]);

int?getkey();

int?key();

void?zuobiao(int?x,int?y,int?i);

int?tu(int?a[][],int?write);

int?wtu(int?a[][],int?write);

int?zhineng(int?a[][]);

int?zh5(int?y,int?x,int?a[][]);

long?zzh5(int?b[][],int?i);

main()

{

int?i,j;

int?gdriver=DETECT;

int?gmode;

initgraph(gdriver,gmode,"");

zx=(N+1)/2;

zy=(N+1)/2;

for(i=1;i=N;i++)

for(j=1;j=N;j++)

a[i][j]=0;

map(a);

i=1;

while(i)

{

int?k,n;

k=wtu(a,write);

if(k==STOP)?goto?end;

map(a);

n=zhineng(a);

if(n==STOP)?goto?end;

map(a);

}

end:

;

}

int?zhineng(int?a[N+1][N+1])

{

int?i,j;

int?k;

max.sum=-1;

for(i=0;i=N;i++)

for(j=0;j+N;j++)

{

w[i][j].sum=0;

w[i][j].x=i;

w[i][j].y=j;

}

for(i=1;i=N-4;i++)

for(j=1;j=N-4;j++)

{

k=zh5(i,j,a);

if(k==STOP)?return?(STOP);

}

for(i=1;i=N;i++)

for(j=1;j=N;j++)

{

if(max.sumw[i][j].sum)

{

max.sum=w[i][j].sum;

max.y=i;

max.x=j;

}

else?if(max.sum==w[i][j].sum)

{

if(((max.y-zy)*(max.y-zy)+(max.x-zx)*(max.x-zx))((i-zy)*(i-zy)+(j-zx)*(j-zx)))

max.sum=w[i][j].sum;

max.y=i;

max.x=j;

}

}

if(a[max.y][max.x]==0)

{

a[max.y][max.x]=-1;

zy=max.y;

zx=max.x;

}

}

int?zh5(int?y,int?x,int?a[N+1][N+1])

{

int?i,j;

int?b[6][6];

long?c[13];

long?d[6][6];

long?temp;

for(i=y;i=y+4;i++)

for(j=x;j=x+4;j++)

b[i+1-y][j+1-x]=a[i][j];

c[1]=b[1][1]+b[1][2]+b[1][3]+b[1][4]+b[1][5];

c[2]=b[2][1]+b[2][2]+b[2][3]+b[2][4]+b[2][5];

c[3]=b[3][1]+b[3][2]+b[3][3]+b[3][4]+b[3][5];

c[4]=b[4][1]+b[4][2]+b[4][3]+b[4][4]+b[4][5];

c[5]=b[5][1]+b[5][2]+b[5][3]+b[5][4]+b[5][5];

c[6]=b[1][1]+b[2][1]+b[3][1]+b[4][1]+b[5][1];

c[7]=b[1][2]+b[2][2]+b[3][2]+b[4][2]+b[5][2];

c[8]=b[1][3]+b[2][3]+b[3][3]+b[4][3]+b[5][3];

c[9]=b[1][4]+b[2][4]+b[3][4]+b[4][4]+b[5][4];

c[10]=b[1][5]+b[2][5]+b[3][5]+b[4][5]+b[5][5];

c[11]=b[1][1]+b[2][2]+b[3][3]+b[4][4]+b[5][5];

c[12]=b[1][5]+b[2][4]+b[3][3]+b[4][2]+b[5][1];

for(i=1;i=12;i++)

{

switch(c[i])

{

case?5:biaoji=1;return(STOP);

case?-5:biaoji=-1;return(STOP);

case?-4:c[i]=100000;break;

case?4:c[i]=100000;break;

case?-3:c[i]=150;break;

case?3:c[i]=150;break;

case?-2:c[i]=120;break;

case?2:c[i]=100;break;

case?-1:c[i]=1;break;

case?1:c[i]=1;break;

default:?c[i]=0;

}

}

for(i=1;i=12;i++)

{

if(c[i]==150)

c[i]+=zzh5(b,i);

}

for(i=1;i=5;i++)

for(j=1;j=5;j++)

d[i][j]=0;

for(i=1;i=5;i++)

for(j=1;j=5;j++)

{

if(i==j)?d[i][j]+=c[11];

if((i+j)==6)?d[i][j]+=c[12];

d[i][j]+=c[i]+c[j+5];

}

for(i=1;i=5;i++)

for(j=1;j=5;j++)

{

if(b[i][j]!=0)

d[i][j]=-2;

}

max1.sum=-1;

max1.y=0;

max1.x=0;

for(i=1;i=5;i++)

for(j=1;j=5;j++)

{

if(max1.sumd[i][j])

{

max1.sum=d[i][j];

max1.y=i;

max1.x=j;

w[i+y-1][j+x-1].sum+=max1.sum;

}

else?if(max1.sum==d[i][j])

{

if(((i+y-1-zy)*(i+y-1-zy)+(j+x-1-zx)*(j+x-1-zx))((max1.y+y-1-zy)*(max1.y+y-1-zy)+(max1.x+x-1-zx)*(max1.x+x-1-zx)))

{

max1.sum=d[i][j];

max1.y=i;

max1.x=j;

}

}

}

}

long?zzh5(int?b[6][6],int?n)

{

int?i,j,k,l,m;

switch(n)

{

case?1:i=b[1][1];j=b[1][2];k=b[1][3];l=b[1][4];m=b[1][5];break;

case?2:i=b[2][1];j=b[2][2];k=b[2][3];l=b[2][4];m=b[2][5];break;

case?3:i=b[3][1];j=b[3][2];k=b[3][3];l=b[3][4];m=b[3][5];break;

case?4:i=b[4][1];j=b[4][2];k=b[4][3];l=b[4][4];m=b[4][5];break;

case?5:i=b[5][1];j=b[5][2];k=b[5][3];l=b[5][4];m=b[5][5];break;

case?6:i=b[1][1];j=b[2][1];k=b[3][1];l=b[4][1];m=b[5][1];break;

case?7:i=b[1][2];j=b[2][2];k=b[3][2];l=b[4][2];m=b[5][2];break;

case?8:i=b[1][3];j=b[2][3];k=b[3][3];l=b[4][3];m=b[5][3];break;

case?9:i=b[1][4];j=b[2][4];k=b[3][4];l=b[4][4];m=b[5][4];break;

case?10:i=b[1][5];j=b[2][5];k=b[3][5];l=b[4][5];m=b[5][5];break;

case?11:i=b[1][1];j=b[2][2];k=b[3][3];l=b[4][4];m=b[5][5];break;

case?12:i=b[1][5];j=b[2][4];k=b[3][3];l=b[4][2];m=b[5][1];break;

}

if((i==0j==1k==1l==1m==0))

return?(900);

if((i==0j==-1k==-1l==-1m==0))

return(1000);

if((i==0j==0k==1l==1m==1)||(i==1j==1k==1l==0m==0))

return(20);

if((i==0j==0k==-1l==-1m==-1)||(i==-1j==-1k==-1l==0m==0))

return(20);

if((i==-1j==1k==1l==1m==1)||(i==1j==-1k==1l==1m==1)||(i==1j==1k==-1l==1m==1)||(i==1j==1k==1l==-1m==1)||(i==1j==1k==1l==1m==-1))

return(-60);

if((i==1j==-1k==-1l==-1m==-1)||(i==-1j==1k==-1l==-1m==-1)||(i==-1j==1k==-1l==-1m==-1)||(i==-1j==-1k==-1l==1m==-1)||(i==-1j==-1k==-1l==-1m==1))

return(-60);

}

int?wtu(int?a[N+1][N+1],int?write)

{

int?i=1;

map(a);

zuobiao(zx,zy,1);

while(i)

{

int?k;

k=tu(a,write);

if(k==OK)?i=0;

if(k==STOP)?return?(STOP);

}

}

int?getkey()

{

int?key,lo,hi;

key=bioskey(0);

lo=key0x00ff;

hi=(key0xff00)8;

return((lo==0)???hi+256:lo);

}

int?key()

{

int?k;

k=getkey();

switch(k)

{

case?27:?return?(STOP);

case?13:

case?'?':?return?(OK);

case?328:?return?(UP);

case?336:?return?(DOWN);

case?331:?return?(LEFT);

case?333:?return?(RIGHT);

default:?return?(NO);

}

}

void?zuobiao(int?x,int?y,int?i)

{

int?r;

if(i!=0)

{

setcolor(GREEN);

for(r=1;r=5;r++)

circle(75+25*x,25+25*y,r);

}

else

{

if(a[zy][zx]==1)

{

setcolor(8);

for(r=1;r=5;r++)

circle(75+25*x,25+25*y,r);

}

else?if(a[zy][zx]==-1)

{

setcolor(WHITE);

for(r=1;r=5;r++)

circle(75+25*x,25+25*y,r);

}

else

{

setcolor(B);

for(r=1;r=5;r++)

circle(75+25*x,25+25*y,r);

setcolor(RED);?line(75+25*zx-5,25+25*zy,75+25*x+5,25+25*zy);

line(75+25*zx,25+25*zy-5,75+25*zx,25+25*zy+5);

}

}

}

int?tu(int?a[N+1][N+1],int?write)

{

int?k;

re:

k=key();

if(k==OK)

{

if(a[zy][zx]==0)

{

a[zy][zx]=write;

}

else

goto?re;

}

if(k==STOP)?return(STOP);

if(k==NO)?goto?re;

if(k==UP)

{

int?i,j;

if(zy==1)?j=zy;

else?j=zy-1;

zuobiao(zx,zy,0);

zuobiao(zx,j,1);

zy=j;

goto?re;

}

if(k==DOWN)

{

int?i,j;

if(zy==N)?j=zy;

else?j=zy+1;

zuobiao(zx,zy,0);

zuobiao(zx,j,1);

zy=j;

goto?re;

}

if(k==LEFT)

{

int?i,j;

if(zx==1)?i=zx;

else?i=zx-1;

zuobiao(zx,zy,0);

zuobiao(i,zy,1);

zx=i;

goto?re;

}

if(k==RIGHT)

{

int?i,j;

if(zx==N)?i=zx;

else?i=zx+1;

zuobiao(zx,zy,0);

zuobiao(i,zy,1);

zx=i;

goto?re;

}

}

void?cbar(int?i,int?x,int?y,int?r)

{

if(i!=0)

{

if(i==1)

setcolor(8);

else?if(i==-1)

setcolor(WHITE);

for(i=1;i=r;i++)

{

circle(x,y,i);

}

}

}

void?map(int?a[N+1][N+1])

{

int?i,j;

cleardevice();

setbkcolor(B);

setcolor(RED);

for(i=0;iN;i++)

{

line(100,50+25*i,75+N*25,50+25*i);

line(100+25*i,50,100+25*i,25+N*25);

}

for(i=1;i=N;i++)

for(j=1;j=N;j++)

cbar(a[i][j],75+25*j,25+25*i,10);

}

c语言五子棋代码,

package day17.gobang;

import java.util.Arrays;

public class GoBangGame {

public static final char BLANK='*';

public static final char BLACK='@';

public static final char WHITE='O';

public static final int MAX = 16;

private static final int COUNT = 5;

//棋盘

private char[][] board;

public GoBangGame() {

}

//开始游戏

public void start() {

board = new char[MAX][MAX];

//把二维数组都填充‘*’

for(char[] ary: board){

Arrays.fill(ary, BLANK);

}

}

public char[][] getChessBoard(){

return board;

}

public void addBlack(int x, int y) throws ChessExistException{

//@

//char blank = '*';

//System.out.println( x +"," + y + ":" + board[y][x] + "," + BLANK);

if(board[y][x] == BLANK){// x, y 位置上必须是空的才可以添棋子

board[y][x] = BLACK;

return;

}

throw new ChessExistException("已经有棋子了!");

}

public void addWhite(int x, int y)

throws ChessExistException{

if(board[y][x] == BLANK){// x, y 位置上必须是空的才可以添棋子

board[y][x] = WHITE;

return;

}

throw new ChessExistException("已经有棋子了!");

}

//chess 棋子:'@'/'O'

public boolean winOnY(char chess, int x, int y){

//先找到y方向第一个不是 blank的棋子

int top = y;

while(true){

if(y==0 || board[y-1][x]!=chess){

//如果y已经是棋盘的边缘, 或者的前一个不是chess

//就不再继续查找了

break;

}

y--;

top = y;

}

//向回统计所有chess的个数,如果是COUNT个就赢了

int count = 0;

y = top;

while(true){

if(y==MAX || board[y][x]!=chess){

//如果找到头 或者 下一个子不是chess 就不再继续统计了

break;

}

count++;

y++;

}

return count==COUNT;

}

//chess 棋子:'@'/'O'

public boolean winOnX(char chess, int x, int y){

//先找到x方向第一个不是 blank的棋子

int top = x;

while(true){

if(x==0 || board[y][x-1]!=chess){

//如果x已经是棋盘的边缘, 或者的前一个不是chess

//就不再继续查找了

break;

}

x--;

top = x;

}

//向回统计所有chess的个数,如果是COUNT个就赢了

int count = 0;

x = top;

while(true){

if(x==MAX || board[y][x]!=chess){

//如果找到头 或者 下一个子不是chess 就不再继续统计了

break;

}

count++;

x++;

}

return count==COUNT;

}

//chess 棋子:'@'/'O'

public boolean winOnXY(char chess, int x, int y){

//先找MAX向第一个不是 blank的棋子

int top = y;

int left = x;

while(true){

if(x==0 || y==0 || board[y-1][x-1]!=chess){

//如果x已经是棋盘的边缘, 或者的前一个不是chess

//就不再继续查找了

break;

}

x--;

y--;

top = y;

left=x;

}

//向回统计所有chess的个数,如果是COUNT个就赢了

int count = 0;

x = left;

y = top;

while(true){

if(x==MAX || y==MAX || board[y][x]!=chess){

//如果找到头 或者 下一个子不是chess 就不再继续统计了

break;

}

count++;

x++;

y++;

}

return count==COUNT;

}

//chess 棋子:'@'/'O'

public boolean winOnYX(char chess, int x, int y){

//先找到x方向第一个不是 blank的棋子

int top = y;

int left = x;

while(true){

if(x==MAX-1 || y==0 || board[y-1][x+1]!=chess){

//如果x已经是棋盘的边缘, 或者的前一个不是chess

//就不再继续查找了

break;

}

x++;

y--;

top = y;

left=x;

}

//向回统计所有chess的个数,如果是COUNT个就赢了

int count = 0;

x = left;

y = top;

while(true){

if(x==0 || y==MAX || board[y][x]!=chess){

//如果找到头 或者 下一个子不是chess 就不再继续统计了

break;

}

count++;

x--;

y++;

}

return count==COUNT;

}

public boolean whiteIsWin(int x, int y) {

//在任何一个方向上赢了,都算赢

return winOnY(WHITE, x, y) ||

winOnX(WHITE, x, y) ||

winOnXY(WHITE, x, y) ||

winOnYX(WHITE, x, y);

}

public boolean blackIsWin(int x, int y) {

return winOnY(BLACK, x, y) ||

winOnX(BLACK, x, y) ||

winOnXY(BLACK, x, y) ||

winOnYX(BLACK, x, y);

}

}

怎样用C语言编一个简单的五子棋游戏?

#include graphics.h

#include stdio.h

#include MATH.H

IMAGE* IMG;

IMAGE* IMG2;

IMAGE* IMG3;

IMAGE* whole;

bool mark = false;

int x = 0, y = 0;

int flag[15][15];

void show()

{

outtextxy(550, 100, "白方:");

outtextxy(550, 150, " 箭头移动");

outtextxy(550, 200, " 回车键落子");

outtextxy(550, 250, "黑方:");

outtextxy(550, 300, " ADWS移动");

outtextxy(550, 350, " 空格键落子");

}

int success1(int dir1, int dir2)

{

int number = 0;

int temp_x = x, temp_y = y;

while (((temp_x / 35 + dir1) = 0 (temp_x / 35 + dir1) 15) ((temp_y / 35 + dir2) = 0 (temp_y / 35 + dir2) 15) (flag[(temp_x / 35 + dir1)][(temp_y / 35 + dir2)] == 1))

{

temp_x = temp_x + dir1 * 35;

temp_y = temp_y + dir2 * 35;

++number;

}

return number;

}

int success2(int dir1, int dir2)

{

int number = 0;

int temp_x = x, temp_y = y;

while (((temp_x / 35 + dir1) = 0 (temp_x / 35 + dir1) 15) ((temp_y / 35 + dir2) = 0 (temp_y / 35 + dir2) 15) (flag[(temp_x / 35 + dir1)][(temp_y / 35 + dir2)] == 2))

{

temp_x = temp_x + dir1 * 35;

temp_y = temp_y + dir2 * 35;

++number;

}

return number;

}

int success1()

{

int number = 0;

number = success1(0, -1) + success1(0, 1);//上下

if (number 4)

{

number = success1(-1, 0) + success1(1, 0);//左右

if (number 4)

{

number = success1(-1, -1) + success1(1, 1);//左上右下

if (number 4)

{

number = success1(-1, 1) + success1(1, -1);//左下右上

}

}

}

return number;

}

int success2()

{

int number = 0;

number = success2(0, -1) + success2(0, 1);//上下

if (number 4)

{

number = success2(-1, 0) + success2(1, 0);//左右

if (number 4)

{

number = success2(-1, -1) + success2(1, 1);//左上右下

if (number 4)

{

number = success2(-1, 1) + success2(1, -1);//左下右上

}

}

}

return number;

}

void control()

{

char key = 0;

while (key != 27)

{

Sleep(10);

if (kbhit())

{

key = getch();

switch (key)

{

case VK_LEFT:

if (mark)

break;

if (x 0)

x = x - 35;

break;

case 'a':

case 'A':

if (!mark)

break;

if (x 0)

x = x - 35;

break;

case VK_RIGHT:

if (mark)

break;

if (x 490)

x = x + 35;

break;

case 'd':

case 'D':

if (!mark)

break;

if (x 490)

x = x + 35;

break;

case VK_UP:

if (mark)

break;

if (y 0)

y = y - 35;

break;

case 'w':

case 'W':

if (!mark)

break;

if (y 0)

y = y - 35;

break;

case VK_DOWN:

if (mark)

break;

if (y 490)

y = y + 35;

break;

case 's':

case 'S':

if (!mark)

break;

if (y 490)

y = y + 35;

break;

case VK_RETURN:

if (mark)

break;

if (flag[x / 35][y / 35] == 0)

{

putimage(whole, x + 6, y + 6, 31, 32, IMG2, 0, 0);

flag[x / 35][y / 35] = 1;

if (success1() = 4)

{

outtextxy(600, 50, "黑方 胜!");

key = 27;

}

mark = true;

}

break;

case VK_SPACE:

if (!mark)

break;

if (flag[x / 35][y / 35] == 0)

{

putimage(whole, x + 6, y + 6, 31, 31, IMG3, 0, 0);

flag[x / 35][y / 35] = 2;

if (success2() = 4)

{

outtextxy(600, 50, "白方 胜!");

key = 27;

}

mark = false;

}

break;

default:

break;

}

putimage(0, 0, whole);

putimage_transparent(NULL, IMG, x + 20, y + 20, 0x0, 0, 0, 20, 20);

}

}

}

void main()

{

setinitmode(0);

initgraph(800, 538);

SetWindowText(GetHWnd(), "五子棋20110327");

setcolor(0xffffff);

setfont(36, 0, "楷体_GB2312");

IMAGE* IMG1 = new IMAGE;

getimage(IMG1, "JPG", MAKEINTRESOURCE(102));//棋盘

putimage(0, 0, IMG1);

IMG2 = new IMAGE;

getimage(IMG2, "JPG", MAKEINTRESOURCE(103));//黑棋

IMG3 = new IMAGE;

getimage(IMG3, "JPG", MAKEINTRESOURCE(104));//白棋

IMG = new IMAGE;

getimage(IMG, "GIF", MAKEINTRESOURCE(101));//手形

whole = new IMAGE;

getimage(whole, 0, 0, 537, 537);

putimage_transparent(NULL, IMG, x + 20, y + 20, 0x0, 0, 0, 20, 20);

show();

control();

delete IMG1;

delete IMG2;

delete IMG3;

delete whole;

getch();

getch();

closegraph();

}

(责任编辑:IT教学网)

更多

推荐导航代码文章