220:Othello

45 篇文章 0 订阅
16 篇文章 0 订阅

Othello


我的这道题的代码还是比较简洁的哈哈。

version 1:

#include<cstdio>
#include<cstring>
using namespace std;
int n,wn,bn;
int dxs[3] = {0,1,-1};
int dys[3] = {0,1,-1};
char c,gamer,cmd[5],game[9][9];
char readchar(){
    for(;;){
        c = getchar();
        if(c != '\n' && c != '\r') return c;
    }
}
int isLegal(int x,int y){
    if(game[x][y] != '-') return 0;
    for(int i = 0;i < 3;i++){
        for(int j = 0;j < 3;j++){
            if(!dxs[i] && !dys[j]) continue;
            int dx = dxs[i],dy = dys[j];
            int mark = 0,cx = x,cy = y;
            while(cx+dx>0 && cx+dx<9 && cy+dy>0 && cy+dy<9){
                cx += dx,cy += dy;
                if(game[cx][cy] == '-') break;
                if(game[cx][cy] != gamer) mark = 1;
                else{
                    if(mark) return 1;
                    else break;
                }
            }
        }
    }
    return 0;
}
void possMove(){
    int first = 1;
    for(int i = 1;i < 9;i++){
        for(int j = 1;j < 9;j++){
            if(isLegal(i,j)){
                if(!first) putchar(' ');
                printf("(%d,%d)",i,j);
                first = 0;
            }
        }
    }
    if(first) printf("No legal move.");
    putchar('\n');
}
void move(char cmd[]){
    int x = cmd[1] - '0',y = cmd[2] - '0';
    if(!isLegal(x,y)) gamer = (gamer == 'B' ? 'W' : 'B');
    game[x][y] = gamer;
    gamer == 'B' ? bn++ : wn++;
    for(int i = 0;i < 3;i++){
        for(int j = 0;j < 3;j++){
            if(!dxs[i] && !dys[j]) continue;
            int dx = dxs[i],dy = dys[j];
            int mark = 0,cx = x,cy = y;
            while(cx+dx>0 && cx+dx<9 && cy+dy>0 && cy+dy<9){
                cx += dx,cy += dy;
                if(game[cx][cy] == '-') break;
                if(game[cx][cy] != gamer) mark = 1;
                else{
                    if(mark){
                        while(cx - dx != x || cy - dy != y){
                        //while(cx-dx>0 && cx-dx<9 && cy-dy>0 && cy-dy<9 && game[cx-dx][cy-dy] != gamer){
                            cx -= dx,cy -= dy;
                            game[cx][cy] = gamer;
                            if(gamer == 'W') wn++,bn--;
                            else wn--,bn++;
                        }
                        break;
                    }
                    else break;
                }
            }
        }
    }
    printf("Black - %2d White - %2d\n",bn,wn);
    gamer = (gamer == 'B' ? 'W' : 'B');
}
int main(){
    int kase = 0;
    scanf("%d",&n);
    while(n--){
        wn = bn = 0;
        for(int i = 1;i < 9;i++){
            for(int j = 1;j < 9;j++){
                c = readchar();
                game[i][j] = c;
                if(c == 'W') wn++;
                if(c == 'B') bn++;
            }
        }
        gamer = readchar();
        if(++kase > 1) putchar('\n');
        while(scanf("%s",cmd) && cmd[0] != 'Q'){
            if(cmd[0] == 'L') possMove();
            else move(cmd);
        }
        for(int i = 1;i < 9;i++){
            for(int j = 1;j < 9;j++){
                putchar(game[i][j]);
            }
            putchar('\n');
        }
    }
    return 0;
}

version 2:

#include<cstdio>
#include<cstring>
using namespace std;
int n,wn,bn,cmark = 0;//cmark 使用search函数检查是否合法还是进行移动
int dxs[3] = {0,1,-1};//每个棋子检查8个方向
int dys[3] = {0,1,-1};
char c,gamer,cmd[5],game[9][9];
char readchar(){
    for(;;){
        c = getchar();
        if(c != '\n' && c != '\r') return c;
    }
}
int search(int x,int y){
    for(int i = 0;i < 3;i++){
        for(int j = 0;j < 3;j++){
            if(!dxs[i] && !dys[j]) continue;
            int dx = dxs[i],dy = dys[j];
            int mark = 0,cx = x,cy = y;
            while(cx+dx>0 && cx+dx<9 && cy+dy>0 && cy+dy<9){
                cx += dx,cy += dy;
                if(game[cx][cy] == '-') break;
                if(game[cx][cy] != gamer) mark = 1;
                else{
                    if(mark){
                        if(!cmark) return 1;//只是检查合法的话直接返回就好
                        while(cx - dx != x || cy - dy != y){//原路返回,将棋子置反
                            cx -= dx,cy -= dy;
                            game[cx][cy] = gamer;
                            if(gamer == 'W') wn++,bn--;
                            else wn--,bn++;
                        }
                        break;
                    }else break;
                }
            }
        }
    }
    return 0;
}
int isLegal(int x,int y){
    if(game[x][y] != '-') return 0;
    if(search(x,y)) return 1;
    return 0;
}
void possMove(){
    int first = 1;
    for(int i = 1;i < 9;i++){
        for(int j = 1;j < 9;j++){
            if(isLegal(i,j)){
                if(!first) putchar(' ');
                printf("(%d,%d)",i,j);
                first = 0;
            }
        }
    }
    if(first) printf("No legal move.");
    putchar('\n');
}
void move(char cmd[]){
    int x = cmd[1] - '0',y = cmd[2] - '0';
    if(!isLegal(x,y)) gamer = (gamer == 'B' ? 'W' : 'B');
    cmark = 1;//cmark只在移动的时候置1
    game[x][y] = gamer;
    gamer == 'B' ? bn++ : wn++;
    search(x,y);
    cmark = 0;
    printf("Black - %2d White - %2d\n",bn,wn);
    gamer = (gamer == 'B' ? 'W' : 'B');//移动之后更换gamer
}
int main(){
    int kase = 0;
    scanf("%d",&n);
    while(n--){
        wn = bn = 0;
        for(int i = 1;i < 9;i++){
            for(int j = 1;j < 9;j++){
                c = readchar();//输入要注意
                game[i][j] = c;
                if(c == 'W') wn++;
                if(c == 'B') bn++;
            }
        }
        gamer = readchar();
        if(++kase > 1) putchar('\n');
        while(scanf("%s",cmd) && cmd[0] != 'Q'){
            if(cmd[0] == 'L') possMove();
            else move(cmd);
        }
        for(int i = 1;i < 9;i++){
            for(int j = 1;j < 9;j++){
                putchar(game[i][j]);
            }
            putchar('\n');
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值