回 帖 发 新 帖 刷新版面

主题:[原创]黑白棋 游戏

/*
黑白棋 1.0 文本界面

程序 经TC2.0 运行通过
*/
#define WHITE  1
#define BLACK -1
#define NULL   0
#define PASS   1
#define OK     0
#define DEBUG  0          /*设置为 1时,可以看见包裹线  用于调试程序*/

struct {
    int color;
    int value;  /*优先级 0, 1 -> 9   1的优先级最高 9最低 0为边界值*/
    int index;  /*包裹线 坐标在 dstak 中的次序*/
}room[10][10]={
{{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}},
{{0,0,0},{0,1,0},{0,8,0},{0,2,0},{0,4,0},{0,4,0},{0,2,0},{0,8,0},{0,1,0},{0,0,0}},
{{0,0,0},{0,8,0},{0,9,0},{0,7,0},{0,6,0},{0,6,0},{0,7,0},{0,9,0},{0,8,0},{0,0,0}},
{{0,0,0},{0,2,0},{0,7,0},{0,3,1},{0,5,8},{0,5,7},{0,3,4},{0,7,0},{0,2,0},{0,0,0}},
{{0,0,0},{0,4,0},{0,6,0},{0,5,1},{0,0,0},{0,0,0},{0,5,6},{0,6,0},{0,4,0},{0,0,0}},
{{0,0,0},{0,4,0},{0,6,0},{0,5,2},{0,0,0},{0,0,0},{0,5,5},{0,6,0},{0,4,0},{0,0,0}},
{{0,0,0},{0,2,0},{0,7,0},{0,3,2},{0,5,3},{0,5,4},{0,3,3},{0,7,0},{0,2,0},{0,0,0}},
{{0,0,0},{0,8,0},{0,9,0},{0,7,0},{0,6,0},{0,6,0},{0,7,0},{0,9,0},{0,8,0},{0,0,0}},
{{0,0,0},{0,1,0},{0,8,0},{0,2,0},{0,4,0},{0,4,0},{0,2,0},{0,8,0},{0,1,0},{0,0,0}},
{{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}}
};

/*dstak[value][index]  存储包裹线 坐标*/
struct {
    int x;
    int y;
}dstak[9][8]={
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{3,3},{6,3},{6,6},{3,6},{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{4,3},{5,3},{6,4},{6,5},{5,6},{4,6},{3,5},{3,4}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}}
};
int tos[9]={0,0,4,0,8,0,0,0,0};
int com=2,you=2;

void updatescr();
int check(int x, int y, int stdc, int draw);
int search(int *px, int *py, int stdc);
void push(int x, int y, int value);
void del(int value, int index);

main()
{
    int x, y, dx, dy, count, state=OK, stdc=BLACK;
    room[4][4].color=room[5][5].color=BLACK;
    room[4][5].color=room[5][4].color=WHITE;
    system("cls");
    while(1){
        updatescr();
        if( (count=search(&x, &y, stdc)) > 0){
            if(stdc==BLACK){
                do{
                    if(count==0) printf("Input again!\n");
                    scanf("%d%d",&x,&y);
                    printf("\n");
                    if(x<1 || x>8 || y<1 || y>8){
                        printf("x,y must be in 1~8\n");
                        continue;
                    }
                    updatescr();
                }while((count=check(x,y,stdc,0))==0);
                printf("You:%2d%2d\n",x,y);
                count=check(x,y,stdc,1);
                you+=count;
                com-=count-1;
            }else{ /*WHITE*/
                printf("Com:%2d%2d\n",x,y);
                count=check(x,y,stdc,1);
                com+=count;
                you-=count-1;
            }
            /*扩展包裹线 */
            for(dx=-1; dx<=1; dx++){
                for(dy=-1; dy<=1; dy++){
                    if(dx==0 && dy==0) continue;
                    if(room[x+dx][y+dy].color==NULL && room[x+dx][y+dy].value>0 && room[x+dx][y+dy].index==0){
                        push(x+dx,y+dy,room[x+dx][y+dy].value);
                    }
                }
            }
                        del(room[x][y].value,room[x][y].index);
                        state=OK;
        }else if(state==OK){
            state=PASS;
            printf("%d PASS\n",stdc);
        }else if(state==PASS){
            break;
        }
        stdc=-stdc;
    }
    if(you>com){
        printf("You win\n");
    }else if(you<com){
        printf("Com win\n");
    }else{
        printf("Tie\n");
    }
    getch();
}

回复列表 (共4个回复)

沙发

void updatescr()
{
    int x, y;
    printf("  1 2 3 4 5 6 7 8\n");
    for(y=1; y<=8; y++){
        printf("%1d",y);
        for(x=1; x<=8; x++){
                switch(room[x][y].color){
                    case -1:
                        printf(" B");
                        break;
                    case 0:
                        printf(" .");
                        break;
                    case 1:
                        printf(" W");
                        break;
                }
                if(DEBUG && room[x][y].index>0){
                        printf("\b\b *");
                }
        }
        printf("\n");
    }
    printf("You:%2d Com:%2d\n",you,com);
}

/*当stdc色方(W/B) 在 x,y 处下子  检测能否下子
下子处必在 包裹线之上
*/
int check(int x, int y, int stdc, int draw)
{
    int i, dx, dy, cmpc, count=0;
    if(room[x][y].index==0) return 0;
    for(dx=-1; dx<=1; dx++){
        for(dy=-1; dy<=1; dy++){
            if(dx==0 && dy==0) continue;
            for(i=1; (cmpc=room[x+i*dx][y+i*dy].color)!=NULL; i++){
                if(stdc*cmpc == 1){
                    count+=i-1;
                    if(draw){
                        while(--i){
                            room[x+i*dx][y+i*dy].color=stdc;
                        }
                    }
                    break;
                }
            }
        }
    }
    if(count>0){
        count++;
        if(draw){
            room[x][y].color=stdc;
        }
    }
    return count;
}

/*当stdc 方下子时 在包裹线上搜索 可行最优点 并返回可否下子
*/
int search(int *px, int *py, int stdc)
{
    int value, index, temp, count=0;
    for(value=0; value<9; value++){
        if(tos[value]==0) continue;
        for(index=0; index<tos[value]; index++){
            temp=check(dstak[value][index].x, dstak[value][index].y, stdc, 0);
            if(count<temp){
                count=temp;
                *px=dstak[value][index].x;
                *py=dstak[value][index].y;
            }
        }
        if(count>0) break;
    }
    return count;
}

void push(int x, int y, int value)
{
    value--;
    dstak[value][tos[value]].x=x;
    dstak[value][tos[value]].y=y;
    tos[value]++;
    room[x][y].index=tos[value];
}

void del(int value, int index)
{
    value--;
    index--;
    tos[value]--;
    room[dstak[value][index].x][dstak[value][index].y].index=0;
    if(index < tos[value]){
        dstak[value][index].x=dstak[value][tos[value]].x;
        dstak[value][index].y=dstak[value][tos[value]].y;
        room[dstak[value][index].x][dstak[value][index].y].index=index+1;
    }
}
/*
无序堆栈 (自定名)
push(data);
将数据压入堆栈顶端  堆栈顶指针tos 加一
del(index);
将堆栈中index号元素删除
删除过程 堆栈顶指针tos减一
         用堆栈[tos]元素 覆盖[index]元素
         如果index指向堆栈顶 则不用覆盖
*/

板凳

输入坐标    列 行
  1 2 3 4 5 6 7 8
1 . . . . . . . .
2 . . . . . . . .
3 . . . . . . . .
4 . . . B W . . .
5 . . . W B . . .
6 . . . . . . . .
7 . . . . . . . .
8 . . . . . . . .
You: 2 Com: 2
6 4

  1 2 3 4 5 6 7 8
1 . . . . . . . .
2 . . . . . . . .
3 . . . . . . . .
4 . . . B W . . .
5 . . . W B . . .
6 . . . . . . . .
7 . . . . . . . .
8 . . . . . . . .
You: 2 Com: 2
You: 6 4
  1 2 3 4 5 6 7 8
1 . . . . . . . .
2 . . . . . . . .
3 . . . . . . . .
4 . . . B B B . .
5 . . . W B . . .
6 . . . . . . . .
7 . . . . . . . .
8 . . . . . . . .
You: 4 Com: 1
Com: 6 3
  1 2 3 4 5 6 7 8
1 . . . . . . . .
2 . . . . . . . .
3 . . . . . W . .
4 . . . B W B . .
5 . . . W B . . .
6 . . . . . . . .
7 . . . . . . . .
8 . . . . . . . .
You: 3 Com: 3

3 楼

路过

4 楼

向楼主学习学习~谢谢楼主

我来回复

您尚未登录,请登录后再回复。点此登录或注册