Untitled

 avatar
unknown
c_cpp
a year ago
3.6 kB
2
Indexable
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
const int SIZE = 4; // Размер поля

void initializeBoard(int board[][SIZE]) {
    int count = 1;
    for (int i = 0; i < SIZE; i++) {
        for (int j=0; j<SIZE; j++) {
            board[i][j] = count++;
        }
    }
    board[SIZE - 1][SIZE - 1] = 0; // Пустая клетка
}

void shuffleBoard(int board[][SIZE]) {
    srand(time(0));
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            int x = rand() % SIZE;
            int y = rand() % SIZE;
            swap(board[i][j], board[x][y]);
        }
    }
}

void printBoard(int board[][SIZE]) {
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            if (board[i][j] == 0) {
                cout << setw(4) << " ";
            } else {
                cout << setw(4) << left << board[i][j];
            }
        }
        cout << endl;
    }
}

bool isSolved(int board[][SIZE]) {
    int count = 1;
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            if (board[i][j] != count++ % (SIZE*SIZE)) {
                return false;
            }
        }
    }
    return true;
}

void playGame() {
    int board[SIZE][SIZE];
    initializeBoard(board);
    shuffleBoard(board);

    while (true) {
        
      cout << "\033[2J\033[1;1H"; 
        printBoard(board);

        if (isSolved(board)) {
            cout << "Поздравляю, Вы победили!" << endl;
            break;
        }

        int move;
        cout << "Введите число для перемещения (0 для выхода): ";
        cin >> move;

        if (move == 0) {
            cout << "Игра завершена." << endl;
            break;
        }

        // Поиск выбранной клетки
        int row, col;
        bool found = false;
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                if (board[i][j] == move) {
                    row = i;
                    col = j;
                    found = true;
                    break;
                }
            }
            if (found) {
                break;
            }
        }

        // Проверка возможности перемещения
        if ((row > 0 && board[row - 1][col] == 0) || (row < SIZE - 1 && board[row + 1][col] == 0) ||
            (col > 0 && board[row][col - 1] == 0) || (col < SIZE - 1 && board[row][col + 1] == 0)) {
            // Перемещение клетки
// Перемещение клетки
int x, y;
do {
    x = row;
    y = col;
    cout << "Введите направление перемещения (1-вверх, 2-вниз, 3-влево, 4-вправо): ";
    cin >> move;

    switch (move) {
        case 1: // Вверх
            x--;
            break;
        case 2: // Вниз
            x++;
            break;
        case 3: // Влево
            y--;
            break;
        case 4: // Вправо
            y++;
            break;
        default:
            cout << "Некорректное направление." << endl;
            break;
    }
} while (x < 0 || x >= SIZE || y < 0 || y >= SIZE);

// Проверяем, что клетка, в которую перемещаем, пустая
if (board[x][y] == 0) {
    swap(board[row][col], board[x][y]);
} else {
    cout << "Невозможно переместить выбранную клетку." << endl;
} }}}
int main() {
    playGame();

    return 0;
}
Leave a Comment