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

Python014

用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 <time.h>

#include <stdlib.h>

int main()

{

int i,j, a,b, res, sum=0

char ch

printf("Now ready to answer!\nEvery question you have 3 chances to answer, first chance you answer right you can got 10, second got 7, third got 5. Otherwise can not got score!\n")

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

{

printf("%dth question:", i+1)

srand(time(NULL))

a = rand()%50

b = rand()%(a>25?(50-a):a)

ch = rand()%2

if(ch) ch='+'

else ch='-'

printf("%d %c %d = ", a, ch, b)

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

{

scanf("%d", &res)

if(ch=='+')

if(res==a+b){

printf("right!\n")

switch(j){

case 0: sum+=10break

case 1: sum+=7break

case 2: sum+=5break

}

break

}

else{

printf("wrong!\n")

if(j!=2)printf("try again:")

}

else

if(res==a-b){

printf("right!\n")

switch(j){

case 0: sum+=10break

case 1: sum+=7break

case 2: sum+=5break

}

break

}

else{

printf("wrong!\n")

if(j!=2)printf("try again:")

}

}

}

printf("your score is : %d\n", sum)

return 0

}