围棋要实现什么功能呢?
首先是黑棋和白棋,下面的代码,黑棋占领的位置被赋值为1,白棋是2;
其次有几个围棋中的规则(不知道是不是这样的,老师是这么告诉我的,可以指正):1.块:上下左右可以连起来的叫块,一颗棋上下左右四个方向若有棋子,就可以称作是一块,一个棋子上下左右都没有,也可以称作一块。2.气:就是一个块周围(上下左右)都没有棋的地方,那就是气。
那么要实现什么呢?就是每下一次棋,返回一方所有的块和每一块有多少气。如果没气了,就把围住的棋都取走(数组里就是清为0)。
先来看看主函数
#include
#include
#include
#define maxsize 10
#define null 0
typedef struct node{
int x;
int y;
struct node*next;
} lqnode;
typedef struct{
node *front,*rear;
}Queue;
//定义了两个结构体,一个是队列,一个是节点
int main(){
int chess[10][10];
int savechess[10][10];//设置了一个数组保存之前下个棋盘
int i,j,type=1,a,b,flag=0;
for(i=0;ix;
y=t->y;
int num=0;
while(num!=4){
num=4;//这个是循环的条件,如果说这个节点上下左右都没有可以进入队列的节点了就退出
//以下是广度优先来找连在一起的点,找到就进队列,上下左右都找
if(chess[x-1][y]==type){
r=(lqnode *)malloc(sizeof(lqnode));
r->x=x;
r->y=y+1;
q->rear->next=r;
r->next=null;
q->rear=r;
chess[x-1][y]=twtype;//标记一下这个地方已经找过了
num--;
}
if(chess[x+1][y]==type){
r=(lqnode *)malloc(sizeof(lqnode));
r->x=x+1;
r->y=y;
q->rear->next=r;
r->next=null;
q->rear=r;
chess[x+1][y]=twtype;
num--;
}
if(chess[x][y-1]==type){
r=(lqnode *)malloc(sizeof(lqnode));
r->x=x;
r->y=y-1;
q->rear->next=r;
r->next=null;
q->rear=r;
chess[x][y-1]=twtype;
num--;
}
if(chess[x][y+1]==type){
r=(lqnode *)malloc(sizeof(lqnode));
r->x=x;
r->y=y+1;
q->rear->next=r;
r->next=null;
q->rear=r;
chess[x][y+1]=twtype;
num--;
}
if(num==4){
break;再判断一遍,找不到就退出
}
t=t->next;
x=t->x;
y=t->y;
printf("%d,%d\n",x,y);
}
t=q->front->next;//先记录一下这个头
int count=0;
printf("该块有以下元素:\n");
while(q->rear!=q->front){
lqnode *s;
s=q->front->next;
x=s->x;
y=s->y;
//上下左右都找一遍,注意条件,有些在边上,比如【0】【0】这个点x-1是找不到的
if(chess[x+1][y]==0 && x+1>=0 && y>=0){
count++;
chess[x+1][y]=9; //找过的标记一下,以防再找一遍
}
if(chess[x][y+1]==0 && x>=0 && y+1>=0) {
count++;
chess[x][y+1]=9;
}
if(chess[x][y-1]==0 && x>=0 && y-1>=0) {
count++;
chess[x][y-1]=9;
}
if(chess[x-1][y]==0 && x-1>=0 && y>=0) {
count++;
chess[x-1][y]=9;
}
q->front->next=s->next;
printf("x=%d,y=%d\n",x,y);
//出队列喽
if(s->next==null){
q->rear=q->front;
}
}
printf("该块有%d口气\n",count);
if(count==0){//如果为0,就是被对方围住啦,里面所有的都要变为0,就是里面的棋子都要被拿走
while(t!=null){//这个就是上面那个t为什么要被记录下来的原因
x=t->x;
y=t->y;
chess[x][y]=0;
t=t->next;
}
}
//以下就是再循环,找到没有被记录过并且还是黑放下的棋的点,
x=-1;
y=-1;
flag=1;
for(int i=0;i |