NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Исправь код
Продолжи код

#include <iostream>
#include <vector>
#include <random>

using namespace std;

// Функция создания поля матрицей NxM
vector<vector<int>> createBoard(int N, int M) {
    vector<vector<int>> board(N, vector<int>(M));
    return board;
}

// Функция заполнения поля случайными минами
void fillMines(vector<vector<int>>& board, int numMines) {
    random_device rd;
    mt19937 gen(rd());
    uniform_int_distribution<> distr(0, board.size() - 1);

    int count = 0;
    while (count < numMines) {
        int row = distr(gen);
        int col = distr(gen);
        if (board[row][col] != -1) {
            board[row][col] = -1;
            count++;
        }
    }
}

// Функция подсчета количества мин в соседних ячейках
void countMines(vector<vector<int>>& board) {
    for (int i = 0; i < board.size(); i++) {
        for (int j = 0; j < board[0].size(); j++) {
            if (board[i][j] == -1) continue;

            int count = 0;
            for (int k = -1; k <= 1; k++) {
                for (int l = -1; l <= 1; l++) {
                    if (i + k < 0 i + k >= board.size() j + l < 0 || j + l >= board[0].size()) continue;
                    if (board[i + k][j + l] == -1) count++;
                }
            }

            board[i][j] = count;
        }
    }
}

// Функция открытия ячеек
void openCell(vector<vector<int>>& board, int row, int col) {
    if (board[row][col] == -1) return;

    board[row][col] += 10;

    if (board[row][col] == 10) {
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                if (row + i < 0 row + i >= board.size() col + j < 0 || col + j >= board[0].size()) continue;
                if (board[row + i][col + j] < 10) {
                    openCell(board, row + i, col + j);
                }
            }
        }
    }
}

// Функция проверки выбранной ячейки на наличие мин
bool checkCell(vector<vector<int>>& board, int row, int col) {
    return board[row][col] == -1;
}

// Функция проверки оставшихся незаполненных ячеек на поле
bool checkEmpty(vector<vector<int>>& board) {
    for (int i = 0; i < board.size(); i++) {
        for (int j = 0; j < board[0].size(); j++) {
            if (board[i][j] < 0 || board[i][j] >= 10) continue;
            return true;
        }
    }
    return false;
}

// Функция маркировки ячейки как бомбы
void markCell(vector<vector<int>>& board, int row, int col) {
    board[row][col] = 9;
}

int main() {
    int N, M, numMines;
    cout << "Enter number of rows: ";
    cin >> N;
    cout << "Enter number of cols: ";
    cin >> M;
    cout << "Enter number of mines: ";
    cin >> numMines;

    vector<vector<int>> board = createBoard(N, M);
    fillMines(board, numMines);
    countMines(board);

    bool gameOver = false;
    while (!gameOver) {
        for (int i = 0; i < board.size(); i++) {
            for (int j = 0; j < board[0].size(); j++) {
                if (board[i][j] == -1) {
                    cout << "*";
                } else if (board[i][j] == 9) {
                    cout << "X";
                } else if (board[i][j] >= 10) {
                    cout << board[i][j] - 10;
                } else {
                    cout << ".";
                }
            }
            cout << endl;
        }

        int row, col;
        cout << "Enter row and col (starting from 0) to open or mark cell (format: row col state (0 - open, 1 - mark)): ";
        cin >> row >> col >> state;

        if (state == 0) {
            // проверяем, есть ли в выбранной ячейке мина
            if (checkCell(board, row, col)) {
                cout << "Game over!" << endl;
                gameOver = true;
            } else {
                openCell(board, row, col);
                // проверяем, остались ли еще незаполненные ячейки
                if (!checkEmpty(board)) {
                    cout << "Congratulations! You won!" << endl;
                    gameOver = true;
                }
            }
        } else if (state == 1) {
            markCell(board, row, col);
        } else {
            cout << "Invalid state input" << endl;
        }
    }

    return 0;
}
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.