<commit_before>#include <iostream>
#include <ctime>
#include <cstdlib>
#include <termios.h>
#include <stdio.h>
#include <fstream>
#include <cmath>


using namespace std;

// 全局变量
int uncovered[52][52] = {0};  // 已翻开的部分（需要在递归中进行操作）
bool firstuncover = true; // 递归时用于记录这是不是第一次翻开
int g_row = 10, g_col = 10, g_mine = 10;

// 用于显示帮助
void dispHelp()
{
    system("clear");
    printf("%c[1;31m", 27); // Red Bold
    cout << "             MINE SWEEPER" << endl;
    printf("%c[0;30m", 27); // Black
    cout << "Welcome to Mine Sweeper! These are the" << endl;
    cout << "commands you will use durning the play." << endl << endl;
    cout << "Up key     - Move the cursor up" << endl;
    cout << "Down key   - Move the cursor down" << endl;
    cout << "Left key   - Move the cursor left" << endl;
    cout << "Right key  - Move the cursor right" << endl;
    cout << "F          - Flag the current cell" << endl;
    cout << "V          - Questionmark the current cell" << endl;
    cout << "C          - Uncover the cell" << endl;
    cout << "Q          - Quit the current game" << endl;
    cout << "S          - Save current game" << endl;
    cout << "L          - Load game" << endl;
    cout << "             Game archive is at your user folder" << endl << endl;
    cout << "    ...Press any key to return...";
    
    // 用于控制输出不需要回车
    struct termios stored_settings;
    struct termios new_settings;
    tcgetattr (0, &stored_settings);
    new_settings = stored_settings;
    new_settings.c_lflag &= (~ICANON);
    new_settings.c_cc[VTIME] = 0;
    new_settings.c_cc[VMIN] = 1;
    tcsetattr (0, TCSANOW, &new_settings);
    
    getchar();
    
    // 结束控制输出不需要回车
    tcsetattr (0, TCSANOW, &stored_settings);
    
    return;
}

// 存盘功能
int saveGame(int (*uncovered)[52], int (*numdistb)[52], int (*mine)[52], int (*flagged)[52], int (*marked)[52], int nummine, int cur_row, int cur_col, int col, int row, long startTime, int currentleft, int usedflag, long currentTime)
{
    // 打开文件
    ofstream archivefile ("ms-archive.txt");
    int temp;
    if (archivefile.is_open()) // 判断是否成功打开文件
    {
        archivefile << row << endl << col << endl;
        
        for (int i = 0; i <= 51; i++)
        {
            for (int j = 0; j <= 51; j++)
            {
                temp = uncovered[i][j];
                archivefile << temp << endl;
            }
        }
        
        for (int i = 0; i <= 51; i++)
        {
            for (int j = 0; j <= 51; j++)
            {
                temp = numdistb[i][j];
                archivefile << temp << endl;
            }
        }
        
        for (int i = 0; i <= 51; i++)
        {
            for (int j = 0; j <= 51; j++)
            {
                temp = mine[i][j];
                archivefile << temp << endl;
            }
        }
        
        for (int i = 0; i <= 51; i++)
        {
            for (int j = 0; j <= 51; j++)
            {
                temp = flagged[i][j];
                archivefile << temp << endl;
            }
        }
        
        for (int i = 0; i <= 51; i++)
        {
            for (int j = 0; j <= 51; j++)
            {
                temp = marked[i][j];
                archivefile << temp << endl;
            }
        }
        
        archivefile << nummine << endl;
        archivefile << cur_row << endl << cur_col << endl;
        archivefile << startTime << endl;
        archivefile << currentleft << endl << usedflag << endl;
        archivefile << currentTime << endl;
        
        archivefile.close();
        return 1;
    }
    else
    {
        archivefile.close();
        return 0;
    }
}

// 用于设置难度
void setHard()
{
    system("clear");
    cout << "Set the size of the checkerboard" << endl;
    printf("%c[1;34m", 27); // Bold Blue
    cout << "Row: ";
    printf("%c[0;30m", 27); // Black
    cin >> g_row;
    while (g_row <= 3)
    {
        system("clear");
        cout << "Too small checkerboard!" << endl;
        printf("%c[1;34m", 27); // Bold Blue
        cout << "Retype the row size: " << endl;
        printf("%c[0;30m", 27); // Black
        cin >> g_row;
    }
    while (g_row >= 26)
    {
        system("clear");
        cout << "Too big checkerboard!" << endl;
        printf("%c[1;34m", 27); // Bold Blue
        cout << "Retype the row size: " << endl;
        printf("%c[0;30m", 27); // Black
        cin >> g_row;
    }
    system("clear");
    cout << "Set the size of the checkerboard" << endl;
    printf("%c[1;34m", 27); // Bold Blue
    cout << "Col: ";
    printf("%c[0;30m", 27); // Black
    cin >> g_col;
    while (g_col <= 3)
    {
        system("clear");
        cout << "Too small checkerboard!" << endl;
        printf("%c[1;34m", 27); // Bold Blue
        cout << "Retype the column size: " << endl;
        printf("%c[0;30m", 27); // Black
        cin >> g_col;
    }
    while (g_col >= 26)
    {
        system("clear");
        cout << "Too big checkerboard!" << endl;
        printf("%c[1;34m", 27); // Bold Blue
        cout << "Retype the col size: " << endl;
        printf("%c[0;30m", 27); // Black
        cin >> g_col;
    }
    system("clear");
    cout << "Set the quantity of mines" << endl;
    printf("%c[1;34m", 27); // Bold Blue
    cout << "Quantity of mines: ";
    printf("%c[0;30m", 27); // Black
    cin >> g_mine;
    
    // 输入的地雷数不应该大于棋盘大小的一半
    while ((g_mine > g_col * g_row / 2) || (g_mine <= 0))
    {
        if (g_mine <= 0)
        {
            system("clear");
            cout << "The quantity can't be zero!" << endl;
            printf("%c[1;34m", 27); // Bold Blue
            cout << "Retype the quantity of mines: " << endl;
            printf("%c[0;30m", 27); // Black
            cin >> g_mine;
        }
        else if (g_mine > g_col * g_row / 2)
        {
            system("clear");
            cout << "Too many mines! Try a smaller number." << endl;
            printf("%c[1;34m", 27); // Bold Blue
            cout << "Retype the quantity of mines: " << endl;
            printf("%c[0;30m", 27); // Black
            cin >> g_mine;
        }
    }
    
    system("clear");
    cout << "Hardness set!" << endl;
    cout << "Checkerboard size: " << g_row << "*" << g_col << endl;
    cout << "Quantity of mines: " << g_mine << endl << endl;
    cout << "...Press any key to return..." << endl;
    
    // 用于控制输出不需要回车
    struct termios stored_settings;
    struct termios new_settings;
    tcgetattr (0, &stored_settings);
    new_settings = stored_settings;<commit_msg>add error handling<commit_after>#include <iostream>
#include <ctime>
#include <cstdlib>
#include <termios.h>
#include <stdio.h>
#include <fstream>
#include <cmath>


using namespace std;

// 全局变量
int uncovered[52][52] = {0};  // 已翻开的部分（需要在递归中进行操作）
bool firstuncover = true; // 递归时用于记录这是不是第一次翻开
int g_row = 10, g_col = 10, g_mine = 10;

// 用于显示帮助
void dispHelp()
{
    system("clear");
    printf("%c[1;31m", 27); // Red Bold
    cout << "             MINE SWEEPER" << endl;
    printf("%c[0;30m", 27); // Black
    cout << "Welcome to Mine Sweeper! These are the" << endl;
    cout << "commands you will use durning the play." << endl << endl;
    cout << "Up key     - Move the cursor up" << endl;
    cout << "Down key   - Move the cursor down" << endl;
    cout << "Left key   - Move the cursor left" << endl;
    cout << "Right key  - Move the cursor right" << endl;
    cout << "F          - Flag the current cell" << endl;
    cout << "V          - Questionmark the current cell" << endl;
    cout << "C          - Uncover the cell" << endl;
    cout << "Q          - Quit the current game" << endl;
    cout << "S          - Save current game" << endl;
    cout << "L          - Load game" << endl;
    cout << "             Game archive is at your user folder" << endl << endl;
    cout << "    ...Press any key to return...";
    
    // 用于控制输出不需要回车
    struct termios stored_settings;
    struct termios new_settings;
    tcgetattr (0, &stored_settings);
    new_settings = stored_settings;
    new_settings.c_lflag &= (~ICANON);
    new_settings.c_cc[VTIME] = 0;
    new_settings.c_cc[VMIN] = 1;
    tcsetattr (0, TCSANOW, &new_settings);
    
    getchar();
    
    // 结束控制输出不需要回车
    tcsetattr (0, TCSANOW, &stored_settings);
    
    return;
}

// 存盘功能
int saveGame(int (*uncovered)[52], int (*numdistb)[52], int (*mine)[52], int (*flagged)[52], int (*marked)[52], int nummine, int cur_row, int cur_col, int col, int row, long startTime, int currentleft, int usedflag, long currentTime)
{
    // 打开文件
    ofstream archivefile ("ms-archive.txt");
    int temp;
    if (archivefile.is_open()) // 判断是否成功打开文件
    {
        archivefile << row << endl << col << endl;
        
        for (int i = 0; i <= 51; i++)
        {
            for (int j = 0; j <= 51; j++)
            {
                temp = uncovered[i][j];
                archivefile << temp << endl;
            }
        }
        
        for (int i = 0; i <= 51; i++)
        {
            for (int j = 0; j <= 51; j++)
            {
                temp = numdistb[i][j];
                archivefile << temp << endl;
            }
        }
        
        for (int i = 0; i <= 51; i++)
        {
            for (int j = 0; j <= 51; j++)
            {
                temp = mine[i][j];
                archivefile << temp << endl;
            }
        }
        
        for (int i = 0; i <= 51; i++)
        {
            for (int j = 0; j <= 51; j++)
            {
                temp = flagged[i][j];
                archivefile << temp << endl;
            }
        }
        
        for (int i = 0; i <= 51; i++)
        {
            for (int j = 0; j <= 51; j++)
            {
                temp = marked[i][j];
                archivefile << temp << endl;
            }
        }
        
        archivefile << nummine << endl;
        archivefile << cur_row << endl << cur_col << endl;
        archivefile << startTime << endl;
        archivefile << currentleft << endl << usedflag << endl;
        archivefile << currentTime << endl;
        
        archivefile.close();
        return 1;
    }
    else
    {
        archivefile.close();
        return 0;
    }
}

// 用于设置难度
void setHard()
{
    system("clear");
    cout << "Set the size of the checkerboard" << endl;
    printf("%c[1;34m", 27); // Bold Blue
    cout << "Row: ";
    printf("%c[0;30m", 27); // Black
    while (1)
    {
        cin >> g_row;
        if (cin.fail())
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(),'\n');
            cout << "Error" << endl;
            continue;
        }
        break;
    }
    while (g_row <= 3)
    {
        system("clear");
        cout << "Too small checkerboard!" << endl;
        printf("%c[1;34m", 27); // Bold Blue
        cout << "Retype the row size: " << endl;
        printf("%c[0;30m", 27); // Black
        while (1)
        {
            cin >> g_row;
            if (cin.fail())
            {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(),'\n');
                cout << "Error" << endl;
                continue;
            }
            break;
        }
    }
    while (g_row >= 26)
    {
        system("clear");
        cout << "Too big checkerboard!" << endl;
        printf("%c[1;34m", 27); // Bold Blue
        cout << "Retype the row size: " << endl;
        printf("%c[0;30m", 27); // Black
        while (1)
        {
            cin >> g_row;
            if (cin.fail())
            {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(),'\n');
                cout << "Error" << endl;
                continue;
            }
            break;
        }
    }
    system("clear");
    cout << "Set the size of the checkerboard" << endl;
    printf("%c[1;34m", 27); // Bold Blue
    cout << "Col: ";
    printf("%c[0;30m", 27); // Black
    while (1)
    {
        cin >> g_col;
        if (cin.fail())
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(),'\n');
            cout << "Error" << endl;
            continue;
        }
        break;
    }
    while (g_col <= 3)
    {
        system("clear");
        cout << "Too small checkerboard!" << endl;
        printf("%c[1;34m", 27); // Bold Blue
        cout << "Retype the column size: " << endl;
        printf("%c[0;30m", 27); // Black
        while (1)
        {
            cin >> g_col;
            if (cin.fail())
            {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(),'\n');
                cout << "Error" << endl;
                continue;
            }
            break;
        }
    }
    while (g_col >= 26)
    {
        system("clear");
        cout << "Too big checkerboard!" << endl;
        printf("%c[1;34m", 27); // Bold Blue
        cout << "Retype the col size: " << endl;
        printf("%c[0;30m", 27); // Black
        while (1)
        {
            cin >> g_col;
            if (cin.fail())
            {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(),'\n');
                cout << "Error" << endl;
                continue;
            }
            break;
        }
    }
    system("clear");
    cout << "Set the quantity of mines" << endl;
    printf("%c[1;34m", 27); // Bold Blue
    cout << "Quantity of mines: ";
    printf("%c[0;30m", 27); // Black
    while (1)
    {
        cin >> g_mine;
        if (cin.fail())
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(),'\n');
            cout << "Error" << endl;
            continue;
        }
        break;
    }
    
    // 输入的地雷数不应该大于棋盘大小的一半
    while ((g_mine > g_col * g_row / 2) || (g_mine <= 0))
    {
        if (g_mine <= 0)
        {
            system("clear");
            cout << "The quantity can't be zero!" << endl;
            printf("%c[1;34m", 27); // Bold Blue
            cout << "Retype the quantity of mines: " << endl;
            printf("%c[0;30m", 27); // Black
            while (1)
            {
                cin >> g_mine;
                if (cin.fail())
                {
                    cin.clear();
                    cin.ignore(numeric_limits<streamsize>::max(),'\n');
                    cout << "Error" << endl;
                    continue;
                }
                break;
            }
        }
        else if (g_mine > g_col * g_row / 2)
        {
            system("clear");
            cout << "Too many mines! Try a smaller number." << endl;
            printf("%c[1;34m", 27); // Bold Blue
            cout << "Retype the quantity of mines: " << endl;
            printf("%c[0;30m", 27); // Black
            while (1)
            {
                cin >> g_mine;
                if (cin.fail())
                {
                    cin.clear();
                    cin.ignore(numeric_limits<streamsize>::max(),'\n');
                    cout << "Error" << endl;
                    continue;
                }
                break;
            }
        }
    }
    
    system("clear");
    cout << "Hardness set!" << endl;
    cout << "Checkerboard size: " << g_row << "*" << g_col << endl;
    cout << "Quantity of mines: " << g_mine << endl << endl;
    cout << "...Press any key to return..." << endl;
    
    // 用于控制输出不需要回车
    struct termios stored_settings;
    struct termios new_settings;
    tcgetattr (0, &stored_settings);
    new_settings = stored_settings;
{'fraction_non_alphanumeric': 0.15958815958815958, 'fraction_numerical': 0.02232947232947233, 'mean_word_length': 1.4817949536889172, 'pattern_counts': {'":': 0, '<': 510, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3849008', 'n_tokens_mistral': 5305, 'n_tokens_neox': 5044, 'n_words': 1322}