UVa 220 - Othello(模拟)

本文分享了一个程序员在调试代码时遇到的意外错误——仅仅是因为多了一个空行导致程序无法正常运行。通过深入分析代码流程,作者最终发现了问题所在并解决了这一看似简单的bug。此案例提醒我们,在调试过程中要保持耐心和细致,有时候一些看似无关紧要的因素可能正是问题的关键。
摘要由CSDN通过智能技术生成

一道纯模拟题, 曾经写了3次, 一直WA, 完全找不出错, 寒假的时候又是找不出来错, 然后。。。惊奇地发现多了个空行, 去掉, AC。。。真是醉了。


#include <iostream>
#include <cstdio>
#include <cstring>

#define MAXN 10

using namespace std;

int legal[MAXN][MAXN];
char chess, disk[MAXN][MAXN];

char other_chess() {
    return chess == 'B' ? 'W' : 'B';
}

bool check() {
    memset(legal, 0, sizeof(legal));
    int p, q;
    char other = other_chess();
    bool ok = false;
    for(int i = 0; i < MAXN; i++)              //搜索当前棋子所在的位置
        for(int j = 0; j < MAXN; j++)
            if(disk[i][j] == chess) {
                if(i - 1 >= 0&& j - 1 >= 0&& disk[i - 1][j - 1] == other) { //如果左上有其他子
                    p = i - 1;
                    q = j - 1;
                    while(p >= 0&& q >= 0) {
                        if(disk[p][q] == other) {p--; q--; continue;}
                        if(disk[p][q] == chess) break;
                        if(disk[p][q] == '-') {legal[p][q] = 1; ok = true; break;}
                    }
                }
                if(i - 1 >= 0&& disk[i - 1][j] == other) {                  //正上
                    p = i - 1;
                    q = j;
                    while(p >= 0) {
                        if(disk[p][q] == other) {p--; continue;}
                        if(disk[p][q] == chess) break;
                        if(disk[p][q] == '-') {legal[p][q] = 1; ok = true; break;}
                    }
                }
                if(i - 1 >= 0&& j + 1 < 8&& disk[i - 1][j + 1] == other) {  //右上
                    p = i - 1;
                    q = j + 1;
                    while(p >= 0&& q < 8) {
                        if(disk[p][q] == other) {p--; q++; continue;}
                        if(disk[p][q] == chess) break;
                        if(disk[p][q] == '-') {legal[p][q] = 1; ok = true; break;}
                    }
                }
                if(j + 1 < 8&& disk[i][j + 1] == other) {                      //右
                    p = i;
                    q = j + 1;
                    while(q < 8) {
                        if(disk[p][q] == other) {q++; continue;}
                        if(disk[p][q] == chess) break;
                        if(disk[p][q] == '-') {legal[p][q] = 1; ok = true; break;}
                    }
                }
                if(i + 1 < 8&& j + 1 < 8&& disk[i + 1][j + 1] == other) {          //右下
                    p = i + 1;
                    q = j + 1;
                    while(p < 8&& q < 8) {
                        if(disk[p][q] == other) {p++; q++; continue;}
                        if(disk[p][q] == chess) break;
                        if(disk[p][q] == '-') {legal[p][q] = 1; ok = true; break;}
                    }
                }
                if(i + 1 < 8&& disk[i + 1][j] == other) {               //下
                    p = i + 1;
                    q = j;
                    while(p < 8) {
                        if(disk[p][q] == other) {p++; continue;}
                        if(disk[p][q] == chess) break;
                        if(disk[p][q] == '-') {legal[p][q] = 1; ok = true; break;}
                    }
                }
                if(i + 1 < 8&& j - 1 >= 0&& disk[i + 1][j - 1] == other) {  //左下
                    p = i + 1;
                    q = j - 1;
                    while(p < 8&& q >= 0) {
                        if(disk[p][q] == other) {p++; q--; continue;}
                        if(disk[p][q] == chess) break;
                        if(disk[p][q] == '-') {legal[p][q] = 1; ok = true; break;}
                    }
                }
                if(j - 1 >= 0&& disk[i][j - 1] == other) {             //左
                    p = i;
                    q = j - 1;
                    while(q >= 0) {
                        if(disk[p][q] == other) {q--; continue;}
                        if(disk[p][q] == chess) break;
                        if(disk[p][q] == '-') {legal[p][q] = 1; ok = true; break;}
                    }
                }
            }
    return ok;
}

void change_chess(int const x, int const y) {
    int i, j, p, q;
    char other = other_chess();
    if(x - 1 >= 0&& y - 1 >= 0&& disk[x - 1][y - 1] == other) {  //左上
        i = p = x - 1;
        j = q = y - 1;
        while(i >= 0&& j >= 0) {
            if(disk[i][j] == other) {i--; j--; continue;}
            if(disk[i][j] == '-') break;
            if(disk[i][j] == chess) {
                while(p != i&& q != j)
                    disk[p--][q--] = chess;
                break;
            }
        }
    }
    if(x - 1 >= 0&& disk[x - 1][y] == other) {  //上
        i = p = x - 1;
        j = q = y;
        while(i >= 0) {
            if(disk[i][j] == other) {i--; continue;}
            if(disk[i][j] == '-') break;
            if(disk[i][j] == chess) {
                while(p != i)
                    disk[p--][q] = chess;
                break;
            }
        }
    }
    if(x - 1 >= 0&& y + 1 < 8&& disk[x - 1][y + 1] == other) {  //右上
        i = p = x - 1;
        j = q = y + 1;
        while(i >= 0&& j < 8) {
            if(disk[i][j] == other) {i--; j++; continue;}
            if(disk[i][j] == '-') break;
            if(disk[i][j] == chess) {
                while(p != i&& q != j)
                    disk[p--][q++] = chess;
                break;
            }
        }
    }
    if(y + 1 < 8&& disk[x][y + 1] == other) {  //右
        i = p = x;
        j = q = y + 1;
        while(j < 8) {
            if(disk[i][j] == other) {j++; continue;}
            if(disk[i][j] == '-') break;
            if(disk[i][j] == chess) {
                while(q != j)
                    disk[p][q++] = chess;
                break;
            }
        }
    }
    if(x + 1 < 8&& y + 1 < 8&& disk[x + 1][y + 1] == other) {  //右下
        i = p = x + 1;
        j = q = y + 1;
        while(i < 8&& j < 8) {
            if(disk[i][j] == other) {i++; j++; continue;}
            if(disk[i][j] == '-') break;
            if(disk[i][j] == chess) {
                while(p != i&& q != j)
                    disk[p++][q++] = chess;
                break;
            }
        }
    }
    if(x + 1 < 8&& disk[x + 1][y] == other) {  //下
        i = p = x + 1;
        j = q = y;
        while(i < 8) {
            if(disk[i][j] == other) {i++; continue;}
            if(disk[i][j] == '-') break;
            if(disk[i][j] == chess) {
                while(p != i)
                    disk[p++][q] = chess;
                break;
            }
        }
    }
    if(x + 1 < 8&& y - 1 >= 0&& disk[x + 1][y - 1] == other) {  //左下
        i = p = x + 1;
        j = q = y - 1;
        while(i < 8&& j >= 0) {
            if(disk[i][j] == other) {i++; j--; continue;}
            if(disk[i][j] == '-') break;
            if(disk[i][j] == chess) {
                while(p != i&& q != j)
                    disk[p++][q--] = chess;
                break;
            }
        }
    }
    if(y - 1 >= 0&& disk[x][y - 1] == other) {  //左
        i = p = x;
        j = q = y - 1;
        while(j >= 0) {
            if(disk[i][j] == other) {j--; continue;}
            if(disk[i][j] == '-') break;
            if(disk[i][j] == chess) {
                while(q != j)
                    disk[p][q--] = chess;
                break;
            }
        }
    }
    int B = 0, W = 0;
    for(int i = 0; i < 8; i++)
        for(int j = 0; j < 8; j++)
            if(disk[i][j] == 'B') B++;
            else if(disk[i][j] == 'W') W++;
    printf("Black - %2d White - %2d\n", B, W);
}

void print_disk() {
    for(int i = 0; i < 8; i++)
        printf("%s\n", disk[i]);
    return ;
}

int main() {
//#ifndef ONLINE_JUDGE
//    freopen("my.in", "r", stdin);
//#endif // ONLINE_JUDGE
    int t, T = 0;
    scanf("%d", &t);
    getchar();
    while(t--) {
        T ? printf("\n") : T++;
        memset(disk, 0, sizeof(disk));
        for(int i = 0; i < 8; i++)
            gets(disk[i]);
        scanf("%c", &chess);
        getchar();
        while(1) {
            check();
            char cmd[MAXN];
            scanf("%s", cmd);
            getchar();
            if(cmd[0] == 'Q') break;
            if(cmd[0] == 'L') {
                bool ok = false;
                for(int i = 0; i < 8; i++) {
                    for(int j = 0; j < 8; j++) {
                        if(legal[i][j]) {
                            if(ok) printf(" ");
                            printf("(%d,%d)", i + 1, j + 1);
                            ok = true;
                        }
                    }
                }
                if(!ok) printf("No legal move.");
                printf("\n");
            }
            if(cmd[0] == 'M') {
                int x = cmd[1] - '0' - 1;
                int y = cmd[2] - '0' - 1;
                if(!check()) chess = other_chess();
                disk[x][y] = chess;
                change_chess(x, y);
                chess = other_chess();
            }
        }
        print_disk();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值