#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 <stdlib.h>#define SIZE 3
typedef enum {CBLANK, CBLACK, CWHITE} CHESS
typedef enum {GM_WIN, GM_LOST, GM_UNKNOW, GM_ERROR} GAMEFLAGvoid init_board(CHESS board[SIZE][SIZE]) //初始化
{
int i, j
for (i = 0i <SIZEi++)
{
for (j = 0j <SIZEj++)
{
board[i][j] = CBLANK
}
}
}void print_chess(CHESS board[SIZE][SIZE]) //打印棋盘
{
int i, j
putchar(' ')
for (i=0i <SIZEi++)
{
printf("%2d", i+1)
}
putchar('培仔\n')
for (i=0i <SIZEi++)
{
printf("%-2d", i+1)
for (j=0j <SIZEj++)
{
switch (board[i][j])
{
case CWHITE:
putchar('O')
break
case CBLACK:
putchar('*')
break
case CBLANK:
putchar('_'兄中哗)
break
default:
putchar('?')
break
}
putchar(' ')
}
putchar('羡行\n')
}
}void swc(CHESS chess, int *black, int *white, int *bmax, int *wmax) //判断
{
switch (chess)
{
case CBLACK:
*white = 0
(*black)++
break
case CWHITE:
*black = 0
(*white)++
break
case CBLANK:
*black = 0
*white = 0
break
default:
break
} if (*black >*bmax) *bmax = *black
if (*white >*wmax) *wmax = *white
}
GAMEFLAG res(CHESS board[SIZE][SIZE]) //判断输赢
{
int i, j
int win[4] = {0, 0, 0, 0}
int rblack, rwhite, cblack, cwhite,
loblack = 0, lowhite = 0,
roblack = 0, rowhite = 0,
bmax = 0, wmax = 0for (i=0i <SIZEi++)
{
rblack = 0
rwhite = 0
cblack = 0
cwhite = 0 swc(board[i][i], &loblack, &lowhite, &bmax, &wmax)
swc(board[i][SIZE-i-1], &roblack, &rowhite, &bmax, &wmax) for (j=0j <SIZEj++)
{
swc(board[i][j], &rblack, &rwhite, &bmax, &wmax)
swc(board[j][i], &cblack, &cwhite, &bmax, &wmax) } } if (bmax >= 3)
{
if (wmax >= 3)
{
return GM_ERROR
}
else
{
return GM_WIN
}
}
else
{
if (wmax >= 3)
{
return GM_LOST
}
else
{
return GM_UNKNOW
}
}}int move(CHESS board[SIZE][SIZE], CHESS chs, int x, int y)
{
int bs = 1
if (board[x][y])
bs = 0
else if (y >= SIZE || y <0 || x >= SIZE || x <0)
bs = 0
else
board[x][y] = chs
return bs
}int main()
{
CHESS b[SIZE][SIZE]
char *msg[] = {"BLACK WIN!\n", "WHITE LOST!", "NOT YET", "ERROR!!"}
char *plr[] = {"NON", "BLACK", "WHITE"}
CHESS p = CBLACK
GAMEFLAG flg
init_board(b)
while ((flg = res(b)) == GM_UNKNOW)
{
int x, y, bmv = 1
system("cls")
print_chess(b)
while (bmv)
{
printf("%s回合,输入坐标:", plr[p])
scanf("%d%d", &x, &y)
bmv = !move(b,p,x-1,y-1)
}
p = (CHESS)(CWHITE + CBLACK - p)
}
printf("%s", msg[flg])
system("pause")
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)