当前位置: 首页 > 工具软件 > Minesweeper > 使用案例 >

Minesweeper(c++代码)

贺懿轩
2023-12-01

原题链接

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    int a, b;
    int num = 1;
    while (cin >> a >> b)
    {
        if (a == 0 && b == 0)
            break;
        string* s = new string[a];
        for (int i = 0; i < a; i++)
        {
            cin >> s[i];
        }
        cout << "Field #" << num << ":" << endl;
        string* str = new string[a + 2];
        string s1 = "";
        for (int i = 0; i < b + 2; i++)
        {
            s1 += '0';
        }
        string s2 = s1;
        str[0] = s1;
        str[a + 1] = s2;
        for (int i = 0; i < a; i++)
        {
            str[i + 1] = '0' + s[i] + '0';
        }
        for (int y = 1; y < a + 1; y++)
        {
            for (int x = 1; x < b + 1; x++)
            {
                if (str[y][x] == '*')
                {
                    cout << '*';
                    continue;
                }
                else
                {
                    int c = 0;
                    if (str[y - 1][x - 1] == '*')
                        c++;
                    if (str[y - 1][x] == '*')
                        c++;
                    if (str[y - 1][x + 1] == '*')
                        c++;
                    if (str[y][x - 1] == '*')
                        c++;
                    if (str[y][x + 1] == '*')
                        c++;
                    if (str[y + 1][x - 1] == '*')
                        c++;
                    if (str[y + 1][x] == '*')
                        c++;
                    if (str[y + 1][x + 1] == '*')
                        c++;
                    cout << c;
                }
            }
            cout << endl;
        }
        num++;
        cout << endl;
    }
    return 0;
}
 类似资料: