poj 2996 Help Me with the Game

本文介绍了一个用于解析棋盘布局的程序。该程序读取棋盘的文本表示,并将棋子位置整理为便于处理的格式。文章详细展示了如何针对黑白两方的棋子进行分类和排序的过程。

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

//做模拟题的代码总是令人恶心,或者是自己的思维还不够灵活吧,只可以一步一步地进行模拟,所以代码才会那么恶心,需要努力进行改进才可以! 
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <algorithm>
using namespace std;

vector<string> black;//黑色棋子的位置储存 
vector<string> white;//白色棋子的位置储存 
vector<string>::iterator it1;
vector<string>::iterator it2;
vector<string> answhite[6];//对白色棋子的位置按“KQRBNP”的顺序储存 ,方便对同类棋子的处理 
vector<string> ansblack[6];//对黑色棋子的位置按“KQRBNP”的顺序储存,方便对同类棋子的处理 

int main()
{
    string input1, input2;
    int i, j, k;
    black.clear(), white.clear();
    string temp;
    for (i = 0; i < 17; i++)//对于“+---+---+---+---+---+---+---+---+”的输入不理睬,对于另一种输入 ,分离出字符,形成一个字符串,储存在一个容器中! 
    {
        temp.clear();
        if (i%2 == 0)
           cin >> input1;
        else
        {
            cin >> input2;
            for (j = 0; j < 33; j++)
            {
                if (input2[j] == '|' || input2[j] == '.' || input2[j] == ':')  continue;
                else
                {
                    if (input2[j] > 'a' && input2[j] < 'z')//对于黑棋子的储存 
                    {
                        temp = toupper(input2[j]);
                        if (j == 2)
                           temp.push_back('a');
                        else if (j == 6)
                            temp.push_back('b');
                        else if (j == 10)
                            temp.push_back('c');
                        else if (j == 14)
                            temp.push_back('d');
                        else if (j == 18)
                            temp.push_back('e');
                        else if (j == 22)
                            temp.push_back('f');
                        else if (j == 26)
                            temp.push_back('g');
                        else if (j == 30)
                            temp.push_back('h');
                        if (i == 1)
                            temp.push_back('8');
                        else if (i == 3)
                            temp.push_back('7');
                        else if (i == 5)
                            temp.push_back('6');
                        else if (i == 7)
                            temp.push_back('5');
                        else if (i == 9)
                            temp.push_back('4');
                        else if (i == 11)
                            temp.push_back('3');
                        else if (i == 13)
                            temp.push_back('2');
                        else if (i == 15)
                            temp.push_back('1');
                        black.push_back(temp);
                    }
                    else if (input2[j] > 'A' && input2[j] < 'Z')//对于白棋子的储存 
                    {
                         temp = input2[j];
                         if (j == 2)
                           temp.push_back('a');
                        else if (j == 6)
                            temp.push_back('b');
                        else if (j == 10)
                            temp.push_back('c');
                        else if (j == 14)
                            temp.push_back('d');
                        else if (j == 18)
                            temp.push_back('e');
                        else if (j == 22)
                            temp.push_back('f');
                        else if (j == 26)
                            temp.push_back('g');
                        else if (j == 30)
                            temp.push_back('h');
                        if (i == 1)
                            temp.push_back('8');
                        else if (i == 3)
                            temp.push_back('7');
                        else if (i == 5)
                            temp.push_back('6');
                        else if (i == 7)
                            temp.push_back('5');
                        else if (i == 9)
                            temp.push_back('4');
                        else if (i == 11)
                            temp.push_back('3');
                        else if (i == 13)
                            temp.push_back('2');
                        else if (i == 15)
                            temp.push_back('1');
                        white.push_back(temp);
                    }
                }
            }
        }
    }
    
    
    for (it1 = white.begin(); it1 != white.end(); it1++)//将棋子按“KQRBNP”的顺序放入一个容器数组中 ,然后通过比较,得出输出的顺序! 
    {
        if ((*it1)[0] == 'K')
           answhite[0].push_back((*it1));
        else if ((*it1)[0] == 'Q')
           answhite[1].push_back((*it1));
        else if ((*it1)[0] == 'R')
           answhite[2].push_back((*it1));
        else if ((*it1)[0] == 'B')
           answhite[3].push_back((*it1));
        else if ((*it1)[0] == 'N')
           answhite[4].push_back((*it1));
        else if ((*it1)[0] == 'P')
           answhite[5].push_back((*it1));
    }
    white.clear();
    for (i = 0; i < 6; i++)
    {
        int size = answhite[i].size();
        for (j = 0; j < size; j++)
        {
           for (k = j+1; k < size; k++)
           {
               if (answhite[i][j][2] > answhite[i][k][2])
               {
                   string tmp = answhite[i][j];
                   answhite[i][j] = answhite[i][k];
                   answhite[i][k] = tmp;
               }
               else
               {
                   if (answhite[i][j][2] == answhite[i][k][2])
                   {
                       if (answhite[i][j][1] > answhite[i][k][1])
                       {
                          string tmp = answhite[i][j];
                          answhite[i][j] = answhite[i][k];
                          answhite[i][k] = tmp;
                       }
                   }
               }
           }
           if (i == 5)//对于‘P’开头的字符进行处理,使其忽略‘P’ 
           {
               string tt;
               tt.push_back(answhite[i][j][1]);
               tt.push_back(answhite[i][j][2]);
               white.push_back(tt);
           }
           else 
              white.push_back(answhite[i][j]);
        }
    }
    
    //白色棋子的输出 
    int size = white.size();
    cout << "White: ";
    for (i = 0; i < size; i++)
    {
        if (i)
           cout << ",";
        cout << white[i];
    }
    cout << endl;
    
    
    //其实黑色棋子的处理和白色棋子的处理是一样的,只是比较的顺序不同,注意就可以了! 
    for (it1 = black.begin(); it1 != black.end(); it1++)
    {
        if ((*it1)[0] == 'K')
           ansblack[0].push_back((*it1));
        else if ((*it1)[0] == 'Q')
           ansblack[1].push_back((*it1));
        else if ((*it1)[0] == 'R')
           ansblack[2].push_back((*it1));
        else if ((*it1)[0] == 'B')
           ansblack[3].push_back((*it1));
        else if ((*it1)[0] == 'N')
           ansblack[4].push_back((*it1));
        else if ((*it1)[0] == 'P')
           ansblack[5].push_back((*it1));
    }
    black.clear();
    for (i = 0; i < 6; i++)
    {
        int size = ansblack[i].size();
        for (j = 0; j < size; j++)
        {
           for (k = j+1; k < size; k++)
           {
               if (ansblack[i][j][2] < ansblack[i][k][2])
               {
                   string tmp = ansblack[i][k];
                   ansblack[i][k] = ansblack[i][j];
                   ansblack[i][j] = tmp;
               }
               else
               {
                   if (ansblack[i][j][2] == ansblack[i][k][2])
                   {
                       if (ansblack[i][j][1] > ansblack[i][k][1])
                       {
                          string tmp = ansblack[i][j];
                          ansblack[i][j] = ansblack[i][k];
                          ansblack[i][k] = tmp;
                       }
                   }
               }
           }
           if (i == 5)
           {
               string tt;
               tt.push_back(ansblack[i][j][1]);
               tt.push_back(ansblack[i][j][2]);
               black.push_back(tt);
           }
           else 
              black.push_back(ansblack[i][j]);
        }
    }
    size = black.size();
    cout << "Black: ";
    for (i = 0; i < size; i++)
    {
        if (i)
           cout << ",";
        cout << black[i];
    }
    cout << endl;
    
    system("pause");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值