UVA - 220 Othello(黑白棋)

本文介绍了一个使用C++实现的模拟棋盘博弈程序,通过复杂的逻辑判断实现棋子的移动和翻转,展示了如何处理棋盘状态和玩家操作,以及如何判断合法的移动位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

就是模拟。。
代码:

#include <bits/stdc++.h>
using namespace std;

int xx[] = {-1,-1,-1, 1,1,1,0, 0};
int yy[] = {-1, 0, 1,-1,0,1,1,-1};
char mmp[10][10];
int fff[2][10][10];
void move(int x, int y, int p)
{
    char ch;
    if(p == 0)
        ch = 'W';
    else
        ch = 'B';
    mmp[x][y] = ch;
    for(int i=0; i<8; i++)
    {
        if(fff[p][x][y] & 1<<i)
        {
            int tx = x-xx[i];
            int ty = y-yy[i];
            while(tx>=0 && ty>=0
                    && tx<8 && ty<8
                    && mmp[tx][ty] != ch)
            {
                mmp[tx][ty] = ch;
                tx -= xx[i];
                ty -= yy[i];
            }
        }
    }
}
int Print(bool isp, int p)
{
    bool flag = false;
    for(int i=0; i<8; i++)
        for(int j=0; j<8; j++)
            if(fff[p][i][j])
            {
                if(flag && isp)
                    cout<<" ";
                flag = true;
                if(isp)
                    cout<<'('<<i+1<<','<<j+1<<")";
                else
                    return flag;
            }
    if(isp)
    {
        if(flag)
            cout<<endl;
        else
            cout<<"No legal move."<<endl;
    }
    return flag;
}

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        memset(mmp, 0, sizeof(mmp));
        for(int i=0; i<8; i++)
            cin>>mmp[i];
        char ch[5];
        int p;
        cin>>ch;
        if(ch[0] == 'W')
            p = 0;
        else
            p = 1;
        while(ch[0] != 'Q')
        {
            memset(fff, 0, sizeof(fff));
            for(int i=0; i<8; i++)
            {
                for(int j=0; j<8; j++)
                    if(mmp[i][j] == 'W')
                    {
                        for(int k=0; k<8; k++)
                        {
                            int tx = i+xx[k];
                            int ty = j+yy[k];
                            int num = 0;
                            while(tx>=0 && ty>=0
                                    && tx<8 && ty<8
                                    && mmp[tx][ty] == 'B')
                            {
                                tx += xx[k];
                                ty += yy[k];
                                ++num;
                            }
                            if(num>=1 && mmp[tx][ty] == '-')
                                fff[0][tx][ty] |= 1<<k;
                        }
                    }
                    else if(mmp[i][j] == 'B')
                    {
                        for(int k=0; k<8; k++)
                        {
                            int tx = i+xx[k];
                            int ty = j+yy[k];
                            int num = 0;
                            while(tx>=0 && ty>=0
                                    && tx<8 && ty<8
                                    && mmp[tx][ty] == 'W')
                            {
                                tx += xx[k];
                                ty += yy[k];
                                ++num;
                            }
                            if(num>=1 && mmp[tx][ty] == '-')
                                fff[1][tx][ty] |= 1<<k;
                        }
                    }
            }
            cin>>ch;
            if(ch[0] == 'L')
                Print(true, p);
            else if(ch[0] == 'M')
            {
                if(!Print(false, p))
                    p ^= 1;
                move(ch[1]-'0'-1, ch[2]-'0'-1, p);
                p ^= 1;
                int wnum = 0;
                int bnum = 0;
                for(int i=0; i<8; i++)
                    for(int j=0; j<8; j++)
                    {
                        if(mmp[i][j] == 'W')
                            ++wnum;
                        if(mmp[i][j] == 'B')
                            ++bnum;
                    }
                printf("Black - %2d White - %2d\n", bnum, wnum);
            }
        }
        for(int i=0; i<8; i++)
            cout<<mmp[i]<<endl;
        if(n != 0)
            cout<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值