Puzzle_

 avatar
Alexmegawin
c_cpp
2 years ago
5.2 kB
5
Indexable
#include <iostream>
#include <fstream>
#include <conio.h>
#include <windows.h>
using namespace std;



class Puzzle
{
private:
    int size;
    int** field;
    int x, y;

    int count = 0;

    bool is_win()
    {
        int k = 1;
        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                if (field[i][j] != k && !(i == size - 1 && j == size - 1))
                    return false;
                k++;
            }
        }
        return true;
    }

    void move_left()
    {
        if (y == size - 1)
            return;
        field[x][y] = field[x][y + 1];
        field[x][y + 1] = 0;
        y++;
    }

    void move_right()
    {
        if (y == 0)
            return;
        field[x][y] = field[x][y - 1];
        field[x][y - 1] = 0;
        y--;
    }

    void move_up()
    {
        if (x == size - 1)
            return;
        field[x][y] = field[x + 1][y];
        field[x + 1][y] = 0;
        x++;
    }

    void move_down()
    {
        if (x == 0)
            return;
        field[x][y] = field[x - 1][y];
        field[x - 1][y] = 0;
        x--;
    }

    void print_field()
    {
        for (int k = 0; k < size; k++)
        {
            cout << "#####";
        }
        cout << "#" << endl;
        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                if (field[i][j] == 0)
                    cout << "| " << "   ";
                else if (field[i][j] < 10)
                    cout << "| " << field[i][j] << "  ";
                else
                    cout << "| " << field[i][j] << " ";
            }
            cout << "|" << endl;
            for (int k = 0; k < size; k++)
            {
                cout << "#####";
            }
            cout << "#" << endl;
        }
    }

public:
    Puzzle(int _size)
    {
        size = _size;
        field = new int* [size];
        for (int i = 0; i < size; i++)
            field[i] = new int[size];


        int k = 1;
        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                field[i][j] = k;
                k++;
            }
        }
        x = size - 1;
        y = size - 1;
        field[x][y] = 0;


        srand(time(0));
        for (int i = 0; i < size * size * 100; i++)
        {
            int r = rand() % 4;
            switch (r)
            {
            case 0: move_left(); break;
            case 1: move_right(); break;
            case 2: move_up(); break;
            case 3: move_down(); break;
            }
        }
    }

    ~Puzzle()
    {
        for (int i = 0; i < size; i++)
            delete[] field[i];
        delete[] field;
    }

    void run_game()
    {
        char ch;
        do
        {
            if (is_win())
            {
                int a = 0;
                system("cls");
                ifstream fin;
                fin.open("record.txt");

                fin >> a;
                if (a == 0 || a > count)
                {
                    ofstream fout;
                    fout.open("record.txt");
                    fout << count;
                    fout.close();
                }
                fin.close();

                cout << "       Ты победил!";
                cout << endl << "Число ходов - " << count;
                if (count < a || a == 0)
                    cout << endl << "Это новый рекорд!";
                else
                    cout << endl << "Твой рекорд - " << a << " ходов";
                return;
            }

            system("cls");
            cout << "           Пятнашки";
            cout << endl << "     Чтобы выйти, нажмите Q" << endl << endl;
            print_field();
            cout << endl << "Число ходов - " << count << endl;
            ch = _getch();
            switch (ch)
            {
                case 'a':
                {
                    if (!(y == size - 1))
                        count++;
                    move_left();
                    break;
                }
                case 'd':
                {
                    if (!(y == 0))
                        count++;
                    move_right();
                    break;
                }
                case 'w':
                {
                    if (!(x == size - 1))
                        count++;
                    move_up();
                    break;
                }
                case 's':
                {
                   if (!(x == 0))
                       count++;
                    move_down();
                    break;
                }
            }
        } while (ch != 'q');
    }
};

int main()
{
    setlocale(LC_ALL, "Russian");

    Puzzle game(3);
    game.run_game();
    return 0;
}