用C语言. 编写一个文本界面的围棋打谱程序

用C语言. 编写一个文本界面的围棋打谱程序,第1张

这是一个简单的程序,会自动计算提子,但不会数目。其它的运行一次估计就差不多会用了。稍微写了点注释。

#include<stdio.h>

#include<stdlib.h>

char board[21][21]

char move[5][2]={{-1,0},{1,0},{0,-1},{0,1},{0,0}}

void initBoard()//初始化棋盘

void showBoard()//输出棋盘

char set(int x,int y,char color)//下子

void process(int xx,int yy)//计算提子

int main()

{

FILE * fptr=NULL

char pufile[256]={0}

char op

int s

int x,y,r

char color

char win

int cnt

start:

s=8

while(s!=1 &&s!=2)

{

printf("选择模式:\n1---下棋\n2---看棋谱\n0---退出\n")

printf("下棋模式下,下子请输入s x y(x,y为位置),认输输入g,和棋输入h\n选择:")

scanf("%d",&s)

if(s==0) return 0

//Egg1

if(s==10) printf("Programmer: swordlance :)\n")

//Egg1 end

}

getchar()

printf("输入棋谱路径:")

gets(pufile)

if(s==1) fptr=fopen(pufile,"w")

else fptr=fopen(pufile,"r")

if(!fptr)

{

printf("文件无法打开(创建)!\n")

system("PAUSE")

return -1

}

initBoard()

cnt=0

color='B'

while(op!='g')

{

system("CLS")

showBoard()

printf("(第%d手)",++cnt)

if(s==1)

{

printf("%c 方:",color)

scanf("%c",&op)

//printf("[%c]",op)

if(op=='s')

{

scanf("%d %d",&x,&y)

getchar()

if(set(x,y,color)!=0)

{

printf("该处不能落子!\n")

cnt--

system("PAUSE")

}

else

{

process(x,y)

fprintf(fptr,"%d %d\n",x,y)

if(color=='B') color='W'

else color='B'

}

}

else if(op=='g')

{

printf("%c 方认输。\n",color)

if(color=='B') fprintf(fptr,"0 1\n")

else fprintf(fptr,"0 -1\n")

fflush(fptr)

fclose(fptr)

system("PAUSE")

goto start

}

else if(op=='h')

{

printf("和棋。\n")

fprintf(fptr,"0 0\n")

fflush(fptr)

fclose(fptr)

system("PAUSE")

goto start

}

else

{

printf("参数错误,下子请输入s x y(x,y为位置),认输输入 g,和棋输入h")

cnt--

system("PAUSE")

}

}

else

{

fscanf(fptr,"%d %d",&x,&y)

if(x==0)

{

if(y>0) printf("W 方胜!\n")

else if(y<0) printf("B 方胜!\n")

else printf("和棋!\n")

system("PAUSE")

goto start

}

else

{

printf("%c 方落子(%d,%d)\n",color,x,y)

set(x,y,color)

process(x,y)

if(color=='B') color='W'

else color='B'

}

system("PAUSE")

}

}

system("PAUSE")

return 0

}

void initBoard()

{

int i,j

board[0][0]='O'

for(i=1i<=19i++) board[0][i]='-'

board[0][20]='O'

for(i=1i<=19i++)

{

board[i][0]='|'

for(j=1j<=19j++) board[i][j]='+'

board[i][20]='|'

}

board[20][0]='O'

for(i=1i<=19i++) board[20][i]='-'

board[20][20]='O'

board[4][4]=board[4][10]=board[4][16]=

board[10][4]=board[10][10]=board[10][16]=

board[16][4]=board[16][10]=board[16][16]='*'

}

void showBoard()

{

int i,j

for(i=0i<=20i++)

{

for(j=0j<=20j++)

{

printf("%c",board[i][j])

}

printf("\n")

}

}

char set(int x,int y,char color)

{

if(board[x][y]=='W' || board[x][y]=='B') return -1//不能落子

else board[x][y]=color

return 0

}

//计算提子

void process(int xx,int yy)

{

char his[21][21]={0}//记录算过的棋子以节约效率

char Q[400][2]={0}//某一片棋

int e//Q的长度。

char mcolor//这片棋的颜色

char ecolor//另一种颜色

int QI=0//气数

int i,j,k,l,m

int x,y

for(m=0m<5m++)

{

i=xx+move[m][0]//为了能够完成打劫,先算别人再算自己

j=yy+move[m][1]

if(his[i][j]==0 &&(board[i][j]=='W' || board[i][j]=='B')) //该位置有子开始算气

{

QI=0

his[i][j]=1

mcolor=board[i][j]

ecolor=(board[i][j]=='W'?'B':'W')

//printf("m=%c e=%c\n",mcolor,ecolor)

Q[0][0]=i

Q[0][1]=j

e=1

for(k=0k<ek++)

{

for(l=0l<4l++)

{

x=Q[k][0]+move[l][0]

y=Q[k][1]+move[l][1]

//printf("x=%d y=%d\n",x,y)

//system("PAUSE")

if(x>0 &&y>0 &&x<20 &&y<20 &&his[x][y]==0)

{

if(board[x][y]==mcolor)//己方,长气

{

Q[e][0]=x

Q[e][1]=y

e++

his[x][y]=1

}

else

{

if(board[x][y]=='+') QI++//空地,加气,忽略重复计算

}

}

}

}

//printf("QI=%d\n",QI)

//system("PAUSE")

if(!QI)//死棋,提子

{

for(k=0k<ek++)

{

board[Q[k][0]][Q[k][1]]='+'

his[Q[k][0]][Q[k][1]]=0

}

}

}

}

}

#include"stdio.h"#include"conio.h"intmain(void){intsintfintmprintf("请输入一个时间:\n")scanf("%i%i%i",&s,&f,&m)if(m==59){m=0f=f++printf("%i%i%i\n",s,f,m)}else{m=m++printf("%i%i%i\n",s,f,m)}getch()}

我写的围棋游戏程序中的数气函数,注解不多.

int CMyView::pointqi(int x, int y, int bow, int e)

{

int i,j,n=1// n is man number of the mass

int qi=0,kong=0 int pivalue,inpivalue

int mass[2][100]

int countqi[19][19]//after be counted as qi,make this point 1.

int realeye=0

for(i=0i<=18i++)

{

for(j=0j<=18j++)

{

countqi[i][j]=0

}

}

eat=0

mass[0][1]=x

mass[1][1]=y

if(bow==10)

{ pivalue=1000inpivalue=1}

else

{ pivalue=1inpivalue=1000}

for(i=1i<=ni++)

{

if(pigroup[mass[0][i]-1][mass[1][i]]==pivalue &&mass[0][i]-1>=0)

{

for(j=1j<=nj++) //confirm that the new man is not belong to mass.

{

if(mass[0][i]-1 != mass[0][j] || mass[1][i] != mass[1][j] )

{

if(j!= n)

continue

else

{

n++

mass[0][n]=mass[0][i]-1

mass[1][n]=mass[1][i]

break

}

}

else

break

}

}

if(pigroup[mass[0][i]+1][mass[1][i]]==pivalue&&mass[0][i]+1<=18)

{

for(j=1j<=nj++) //confirm that the new man is not belong to mass.

{

if(mass[0][i]+1 != mass[0][j] || mass[1][i] != mass[1][j] )

{

if(j!= n)

continue

else

{

n++

mass[0][n]=mass[0][i]+1

mass[1][n]=mass[1][i]

break

}

}

else

break

}

}

if(pigroup[mass[0][i]][mass[1][i]-1]==pivalue&&mass[1][i]-1>=0)

{

for(j=1j<=nj++) //confirm that the new man is not belong to mass.

{

if(mass[0][i] != mass[0][j] || mass[1][i]-1 != mass[1][j] )

{

if(j!= n)

continue

else

{

n++

mass[0][n]=mass[0][i]

mass[1][n]=mass[1][i]-1

break

}

}

else

break

}

}

if(pigroup[mass[0][i]][mass[1][i]+1]==pivalue&&mass[1][i]+1<=18)

{

for(j=1j<=nj++) //confirm that the new man is not belong to mass.

{

if(mass[0][i] != mass[0][j] || mass[1][i]+1 != mass[1][j] )

{

if(j!= n)

continue

else

{

n++

mass[0][n]=mass[0][i]

mass[1][n]=mass[1][i]+1

break

}

}

else

break

}

}

}

for(j=1j<=nj++) //check for all man of this mass whether qi exists.

{

if(pigroup[mass[0][j]-1][mass[1][j]]==0&&mass[0][j]-1>=0&&countqi[mass[0][j]-1][mass[1][j]]==0)

{ qi++

countqi[mass[0][j]-1][mass[1][j]]=1

if(judgerealeye(mass[0][j]-1,mass[1][j],bow)==1)

realeye=realeye+1

}

if(pigroup[mass[0][j]+1][mass[1][j]]==0&&mass[0][j]+1<=18&&countqi[mass[0][j]+1][mass[1][j]]==0)

{ qi++

countqi[mass[0][j]+1][mass[1][j]]=1

if(judgerealeye(mass[0][j]+1,mass[1][j],bow)==1)

realeye=realeye+1

}

if(pigroup[mass[0][j]][mass[1][j]-1]==0 &&mass[1][j]-1>=0&&countqi[mass[0][j]][mass[1][j]-1]==0)

{ qi++

countqi[mass[0][j]][mass[1][j]-1]=1

if(judgerealeye(mass[0][j],mass[1][j]-1,bow)==1)

realeye=realeye+1

}

if(pigroup[mass[0][j]][mass[1][j]+1]==0&&mass[1][j]+1<=18&&countqi[mass[0][j]][mass[1][j]+1]==0)

{ qi++

countqi[mass[0][j]][mass[1][j]+1]=1

if(judgerealeye(mass[0][j],mass[1][j]+1,bow)==1)

realeye=realeye+1

}

}

if(qi!=0)

{

eat=0

if(e==2)

return realeye

else

return qi

} //qi is not 0.

if(qi==0) // if any man is eat, make the value of pigroup[][] 0.

{

eat=1

eaten[k]=1

if(e==1)

{

for(i=1i<=ni++)

{

pigroup[mass[0][i]][mass[1][i]]=0

beeat[mass[0][i]][mass[1][i]]=1

}

}

}

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/7800246.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-10
下一篇 2023-04-10

发表评论

登录后才能评论

评论列表(0条)

保存