515 lines
13 KiB
C++
515 lines
13 KiB
C++
|
// 16.cpp : Определяет точку входа для приложения.
|
|||
|
//
|
|||
|
|
|||
|
#include "framework.h"
|
|||
|
#include "16.h"
|
|||
|
#include <stdio.h>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
enum Material {
|
|||
|
void_m,
|
|||
|
player_m,
|
|||
|
wall_m,
|
|||
|
gold_m
|
|||
|
};
|
|||
|
|
|||
|
#define WIDTH_MAP 30
|
|||
|
#define HEIGHT_MAP 20
|
|||
|
|
|||
|
#define WIDTH_1 20
|
|||
|
#define HEIGHT_1 20
|
|||
|
|
|||
|
int map[HEIGHT_MAP][WIDTH_MAP] = {
|
|||
|
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|||
|
{0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0},
|
|||
|
{0, 0, 0, 0, 0, 0, 1, 0, 0, 3, 0, 0, 0, 0, 0},
|
|||
|
{0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 3, 3, 0, 0, 0},
|
|||
|
{0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 3, 3, 0, 0, 0},
|
|||
|
|
|||
|
{0, 0, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0, 0, 0, 0},
|
|||
|
{0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 2, 0, 0, 2, 0},
|
|||
|
{0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0},
|
|||
|
{0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0},
|
|||
|
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
|||
|
};
|
|||
|
|
|||
|
int step = 0, gold = 0;
|
|||
|
|
|||
|
POINT getPlayerCoord() {
|
|||
|
|
|||
|
for (int i = 0; i < WIDTH_MAP; i++) {
|
|||
|
for (int j = 0; j < WIDTH_MAP; j++) {
|
|||
|
if (map[i][j] == 1) return { j,i };
|
|||
|
}
|
|||
|
}
|
|||
|
return { -1,-1 };
|
|||
|
}
|
|||
|
|
|||
|
void right() {
|
|||
|
POINT c = getPlayerCoord();
|
|||
|
if (c.x < 0 || map[c.y][c.x + 1] == wall_m) return;
|
|||
|
|
|||
|
if (map[c.y][c.x + 1] == gold_m) {
|
|||
|
gold += 1;
|
|||
|
}
|
|||
|
|
|||
|
map[c.y][c.x + 1] = 1;
|
|||
|
map[c.y][c.x] = void_m;
|
|||
|
step++;
|
|||
|
}
|
|||
|
|
|||
|
void left() {
|
|||
|
POINT c = getPlayerCoord();
|
|||
|
if (c.x < 0 || map[c.y][c.x - 1] == wall_m) return;
|
|||
|
|
|||
|
if (map[c.y][c.x - 1] == gold_m) {
|
|||
|
gold += 1;
|
|||
|
}
|
|||
|
|
|||
|
map[c.y][c.x - 1] = 1;
|
|||
|
map[c.y][c.x] = void_m;
|
|||
|
step++;
|
|||
|
}
|
|||
|
|
|||
|
void down() {
|
|||
|
POINT c = getPlayerCoord();
|
|||
|
if (c.x < 0 || map[c.y + 1][c.x] == wall_m) return;
|
|||
|
|
|||
|
if (map[c.y + 1][c.x] == gold_m) {
|
|||
|
gold += 1;
|
|||
|
}
|
|||
|
|
|||
|
map[c.y + 1][c.x] = 1;
|
|||
|
map[c.y][c.x] = void_m;
|
|||
|
step++;
|
|||
|
}
|
|||
|
|
|||
|
void up() {
|
|||
|
POINT c = getPlayerCoord();
|
|||
|
if (c.x < 0 || map[c.y - 1][c.x] == wall_m) return;
|
|||
|
|
|||
|
if (map[c.y - 1][c.x] == gold_m) {
|
|||
|
gold += 1;
|
|||
|
}
|
|||
|
|
|||
|
map[c.y - 1][c.x] = 1;
|
|||
|
map[c.y][c.x] = void_m;
|
|||
|
step++;
|
|||
|
}
|
|||
|
|
|||
|
void createWall() {
|
|||
|
POINT c = getPlayerCoord();
|
|||
|
if (c.x < 0 || c.x - 1 < 0) return;
|
|||
|
|
|||
|
map[c.y][c.x - 1] = wall_m;
|
|||
|
}
|
|||
|
|
|||
|
void dropGold() {
|
|||
|
POINT c = getPlayerCoord();
|
|||
|
if (c.x < 0 || c.x + 1 > WIDTH_MAP || map[c.y][c.x + 1] == gold_m || map[c.y][c.x + 1] == wall_m) return;
|
|||
|
|
|||
|
if (gold > 0) {
|
|||
|
map[c.y][c.x + 1] = gold_m;
|
|||
|
gold--;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void Z() {
|
|||
|
POINT c = getPlayerCoord();
|
|||
|
if (c.x < 0) return;
|
|||
|
|
|||
|
for (int i = c.x + 1; i < WIDTH_MAP; i++) {
|
|||
|
if (map[c.y][i] == wall_m) map[c.y][i] = void_m;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void U() {
|
|||
|
POINT c = getPlayerCoord();
|
|||
|
if (c.x < 0) return;
|
|||
|
|
|||
|
|
|||
|
for (int i = -2; i < 3; i++) {
|
|||
|
if (i == 0 || c.x+i < 0 || c.x + i >= WIDTH_MAP || c.y + i < 0 || c.y - i >= HEIGHT_MAP) continue;
|
|||
|
map[c.y + i][c.x + i] = wall_m;
|
|||
|
}
|
|||
|
|
|||
|
for (int i = -2; i < 3; i++) {
|
|||
|
if (i == 0 || c.x + i < 0 || c.x + i >= WIDTH_MAP || c.y - i < 0 || c.y + i >= HEIGHT_MAP) continue;
|
|||
|
map[c.y - i][c.x + i] = wall_m;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void D() {
|
|||
|
POINT c = getPlayerCoord();
|
|||
|
if (c.x < 0) return;
|
|||
|
|
|||
|
|
|||
|
for (int i = -2; i < 3; i++) {
|
|||
|
if (gold <= 0) break;
|
|||
|
if (i == 0 || c.x + i < 0 || c.x + i >= WIDTH_MAP || c.y + i < 0 || c.y - i >= HEIGHT_MAP) continue;
|
|||
|
if (map[c.y + i][c.x + i] == wall_m) continue;
|
|||
|
map[c.y + i][c.x + i] = gold_m;
|
|||
|
gold--;
|
|||
|
}
|
|||
|
|
|||
|
for (int i = -2; i < 3; i++) {
|
|||
|
if (gold <= 0) break;
|
|||
|
if (i == 0 || c.x + i < 0 || c.x + i >= WIDTH_MAP || c.y - i < 0 || c.y + i >= HEIGHT_MAP) continue;
|
|||
|
if (map[c.y - i][c.x + i] == wall_m) continue;
|
|||
|
map[c.y - i][c.x + i] = gold_m;
|
|||
|
gold--;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void save() {
|
|||
|
FILE* fout;
|
|||
|
fopen_s(&fout, "D:\\university\\ОАИП\\lab\\Temp\\game.bin", "wb");
|
|||
|
if (fout == NULL) {
|
|||
|
printf("");
|
|||
|
return;
|
|||
|
}
|
|||
|
int width = WIDTH_MAP, height = HEIGHT_MAP;
|
|||
|
|
|||
|
fwrite(&width, sizeof(width), 1, fout);
|
|||
|
fwrite(&height, sizeof(height), 1, fout);
|
|||
|
fwrite(&map, sizeof(map), 1, fout);
|
|||
|
fwrite(&step, sizeof(step), 1, fout);
|
|||
|
fwrite(&gold, sizeof(gold), 1, fout);
|
|||
|
fclose(fout);
|
|||
|
}
|
|||
|
|
|||
|
void load() {
|
|||
|
FILE* fin;
|
|||
|
fopen_s(&fin, "D:\\university\\ОАИП\\lab\\Temp\\game.bin", "rb");
|
|||
|
if (fin == NULL) {
|
|||
|
printf("");
|
|||
|
return;
|
|||
|
}
|
|||
|
int width, height;
|
|||
|
|
|||
|
fread(&width, sizeof(width), 1, fin);
|
|||
|
fread(&height, sizeof(height), 1, fin);
|
|||
|
fread(&map, sizeof(map), 1, fin);
|
|||
|
fread(&step, sizeof(step), 1, fin);
|
|||
|
fread(&gold, sizeof(gold), 1, fin);
|
|||
|
fclose(fin);
|
|||
|
}
|
|||
|
|
|||
|
void restartGame() {
|
|||
|
FILE* fin;
|
|||
|
fopen_s(&fin, "D:\\university\\ОАИП\\lab\\Temp\\start.bin", "rb");
|
|||
|
if (fin == NULL) {
|
|||
|
printf("");
|
|||
|
return;
|
|||
|
}
|
|||
|
int width, height;
|
|||
|
|
|||
|
fread(&width, sizeof(width), 1, fin);
|
|||
|
fread(&height, sizeof(height), 1, fin);
|
|||
|
fread(&map, sizeof(map), 1, fin);
|
|||
|
fread(&step, sizeof(step), 1, fin);
|
|||
|
fread(&gold, sizeof(gold), 1, fin);
|
|||
|
fclose(fin);
|
|||
|
}
|
|||
|
|
|||
|
void drawMap(HDC hdc) {
|
|||
|
HBRUSH materials[4] = {
|
|||
|
(HBRUSH)CreateSolidBrush(RGB(127,127,127)),
|
|||
|
(HBRUSH)CreateSolidBrush(RGB(0,0,255)),
|
|||
|
(HBRUSH)CreateSolidBrush(RGB(0,0,0)),
|
|||
|
(HBRUSH)CreateSolidBrush(RGB(255,255,0))
|
|||
|
};
|
|||
|
|
|||
|
for (int i = 0; i < HEIGHT_MAP; i++) {
|
|||
|
for (int j = 0; j < WIDTH_MAP; j++) {
|
|||
|
RECT r = { j * WIDTH_1, i * HEIGHT_1, (j + 1) * WIDTH_1, (i + 1) * HEIGHT_1 };
|
|||
|
FillRect(hdc, &r, materials[map[i][j]]);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
char text[80];
|
|||
|
sprintf_s(text, "steps = %d, gold = %d", step, gold);
|
|||
|
TextOutA(hdc, 10, HEIGHT_MAP * HEIGHT_1, (LPCSTR)text, strlen(text));
|
|||
|
|
|||
|
for (int i = 0; i < 4; i++) DeleteObject(materials[i]);
|
|||
|
}
|
|||
|
|
|||
|
void Midas(int i, int j) {
|
|||
|
if (map[i][j] == 2) {
|
|||
|
map[i][j] = 3;
|
|||
|
if (i > 0) Midas(i - 1, j);
|
|||
|
if (i < HEIGHT_MAP - 1) Midas(i + 1, j);
|
|||
|
if (j > 0) Midas(i, j - 1);
|
|||
|
if (i < WIDTH_MAP) Midas(i, j + 1);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void M() {
|
|||
|
POINT c = getPlayerCoord();
|
|||
|
Midas(c.y, c.x + 1);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
#define MAX_LOADSTRING 100
|
|||
|
|
|||
|
// Глобальные переменные:
|
|||
|
HINSTANCE hInst; // текущий экземпляр
|
|||
|
WCHAR szTitle[MAX_LOADSTRING]; // Текст строки заголовка
|
|||
|
WCHAR szWindowClass[MAX_LOADSTRING]; // имя класса главного окна
|
|||
|
|
|||
|
// Отправить объявления функций, включенных в этот модуль кода:
|
|||
|
ATOM MyRegisterClass(HINSTANCE hInstance);
|
|||
|
BOOL InitInstance(HINSTANCE, int);
|
|||
|
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
|
|||
|
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
|
|||
|
|
|||
|
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
|
|||
|
_In_opt_ HINSTANCE hPrevInstance,
|
|||
|
_In_ LPWSTR lpCmdLine,
|
|||
|
_In_ int nCmdShow)
|
|||
|
{
|
|||
|
UNREFERENCED_PARAMETER(hPrevInstance);
|
|||
|
UNREFERENCED_PARAMETER(lpCmdLine);
|
|||
|
|
|||
|
// TODO: Разместите код здесь.
|
|||
|
|
|||
|
// Инициализация глобальных строк
|
|||
|
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
|
|||
|
LoadStringW(hInstance, IDC_MY16, szWindowClass, MAX_LOADSTRING);
|
|||
|
MyRegisterClass(hInstance);
|
|||
|
|
|||
|
// Выполнить инициализацию приложения:
|
|||
|
if (!InitInstance (hInstance, nCmdShow))
|
|||
|
{
|
|||
|
return FALSE;
|
|||
|
}
|
|||
|
|
|||
|
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY16));
|
|||
|
|
|||
|
MSG msg;
|
|||
|
|
|||
|
// Цикл основного сообщения:
|
|||
|
while (GetMessage(&msg, nullptr, 0, 0))
|
|||
|
{
|
|||
|
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
|
|||
|
{
|
|||
|
TranslateMessage(&msg);
|
|||
|
DispatchMessage(&msg);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return (int) msg.wParam;
|
|||
|
}
|
|||
|
|
|||
|
//
|
|||
|
// ФУНКЦИЯ: MyRegisterClass()
|
|||
|
//
|
|||
|
// ЦЕЛЬ: Регистрирует класс окна.
|
|||
|
//
|
|||
|
ATOM MyRegisterClass(HINSTANCE hInstance)
|
|||
|
{
|
|||
|
WNDCLASSEXW wcex;
|
|||
|
|
|||
|
wcex.cbSize = sizeof(WNDCLASSEX);
|
|||
|
|
|||
|
wcex.style = CS_HREDRAW | CS_VREDRAW;
|
|||
|
wcex.lpfnWndProc = WndProc;
|
|||
|
wcex.cbClsExtra = 0;
|
|||
|
wcex.cbWndExtra = 0;
|
|||
|
wcex.hInstance = hInstance;
|
|||
|
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY16));
|
|||
|
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
|
|||
|
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
|
|||
|
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_MY16);
|
|||
|
wcex.lpszClassName = szWindowClass;
|
|||
|
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
|
|||
|
|
|||
|
return RegisterClassExW(&wcex);
|
|||
|
}
|
|||
|
|
|||
|
//
|
|||
|
// ФУНКЦИЯ: InitInstance(HINSTANCE, int)
|
|||
|
//
|
|||
|
// ЦЕЛЬ: Сохраняет маркер экземпляра и создает главное окно
|
|||
|
//
|
|||
|
// КОММЕНТАРИИ:
|
|||
|
//
|
|||
|
// В этой функции маркер экземпляра сохраняется в глобальной переменной, а также
|
|||
|
// создается и выводится главное окно программы.
|
|||
|
//
|
|||
|
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
|
|||
|
{
|
|||
|
hInst = hInstance; // Сохранить маркер экземпляра в глобальной переменной
|
|||
|
|
|||
|
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
|
|||
|
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
|
|||
|
|
|||
|
if (!hWnd)
|
|||
|
{
|
|||
|
return FALSE;
|
|||
|
}
|
|||
|
|
|||
|
ShowWindow(hWnd, nCmdShow);
|
|||
|
UpdateWindow(hWnd);
|
|||
|
|
|||
|
return TRUE;
|
|||
|
}
|
|||
|
|
|||
|
//
|
|||
|
// ФУНКЦИЯ: WndProc(HWND, UINT, WPARAM, LPARAM)
|
|||
|
//
|
|||
|
// ЦЕЛЬ: Обрабатывает сообщения в главном окне.
|
|||
|
//
|
|||
|
// WM_COMMAND - обработать меню приложения
|
|||
|
// WM_PAINT - Отрисовка главного окна
|
|||
|
// WM_DESTROY - отправить сообщение о выходе и вернуться
|
|||
|
//
|
|||
|
//
|
|||
|
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
|
|||
|
{
|
|||
|
switch (message)
|
|||
|
{
|
|||
|
case WM_COMMAND:
|
|||
|
{
|
|||
|
int wmId = LOWORD(wParam);
|
|||
|
// Разобрать выбор в меню:
|
|||
|
switch (wmId)
|
|||
|
{
|
|||
|
case IDM_ABOUT:
|
|||
|
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
|
|||
|
break;
|
|||
|
case ISave:
|
|||
|
save();
|
|||
|
break;
|
|||
|
case ILoad:
|
|||
|
load();
|
|||
|
InvalidateRect(hWnd, NULL, TRUE);
|
|||
|
break;
|
|||
|
case IDM_EXIT:
|
|||
|
DestroyWindow(hWnd);
|
|||
|
break;
|
|||
|
default:
|
|||
|
return DefWindowProc(hWnd, message, wParam, lParam);
|
|||
|
}
|
|||
|
}
|
|||
|
break;
|
|||
|
case WM_PAINT:
|
|||
|
{
|
|||
|
PAINTSTRUCT ps;
|
|||
|
HDC hdc = BeginPaint(hWnd, &ps);
|
|||
|
// TODO: Добавьте сюда любой код прорисовки, использующий HDC...
|
|||
|
|
|||
|
drawMap(hdc);
|
|||
|
|
|||
|
EndPaint(hWnd, &ps);
|
|||
|
}
|
|||
|
break;
|
|||
|
case WM_CHAR:
|
|||
|
switch (wParam) {
|
|||
|
case 'l':
|
|||
|
createWall();
|
|||
|
InvalidateRect(hWnd, NULL, TRUE);
|
|||
|
break;
|
|||
|
case 'r':
|
|||
|
dropGold();
|
|||
|
InvalidateRect(hWnd, NULL, TRUE);
|
|||
|
break;
|
|||
|
case 'z':
|
|||
|
Z();
|
|||
|
InvalidateRect(hWnd, NULL, TRUE);
|
|||
|
break;
|
|||
|
case 'u':
|
|||
|
U();
|
|||
|
InvalidateRect(hWnd, NULL, TRUE);
|
|||
|
break;
|
|||
|
case 'd':
|
|||
|
D();
|
|||
|
InvalidateRect(hWnd, NULL, TRUE);
|
|||
|
break;
|
|||
|
case 'm':
|
|||
|
M();
|
|||
|
InvalidateRect(hWnd, NULL, TRUE);
|
|||
|
break;
|
|||
|
}
|
|||
|
break;
|
|||
|
case WM_KEYDOWN:
|
|||
|
switch (wParam)
|
|||
|
{
|
|||
|
case VK_RIGHT:
|
|||
|
right();
|
|||
|
InvalidateRect(hWnd, NULL, TRUE);
|
|||
|
break;
|
|||
|
case VK_LEFT:
|
|||
|
left();
|
|||
|
InvalidateRect(hWnd, NULL, TRUE);
|
|||
|
break;
|
|||
|
case VK_UP:
|
|||
|
up();
|
|||
|
InvalidateRect(hWnd, NULL, TRUE);
|
|||
|
break;
|
|||
|
case VK_DOWN:
|
|||
|
down();
|
|||
|
InvalidateRect(hWnd, NULL, TRUE);
|
|||
|
break;
|
|||
|
case VK_F5:
|
|||
|
save();
|
|||
|
break;
|
|||
|
case VK_F9:
|
|||
|
load();
|
|||
|
InvalidateRect(hWnd, NULL, TRUE);
|
|||
|
break;
|
|||
|
case VK_F6:
|
|||
|
restartGame();
|
|||
|
InvalidateRect(hWnd, NULL, TRUE);
|
|||
|
break;
|
|||
|
}
|
|||
|
break;
|
|||
|
case WM_DESTROY:
|
|||
|
PostQuitMessage(0);
|
|||
|
break;
|
|||
|
default:
|
|||
|
return DefWindowProc(hWnd, message, wParam, lParam);
|
|||
|
}
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
// Обработчик сообщений для окна "О программе".
|
|||
|
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
|
|||
|
{
|
|||
|
UNREFERENCED_PARAMETER(lParam);
|
|||
|
switch (message)
|
|||
|
{
|
|||
|
case WM_INITDIALOG:
|
|||
|
return (INT_PTR)TRUE;
|
|||
|
|
|||
|
case WM_COMMAND:
|
|||
|
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
|
|||
|
{
|
|||
|
EndDialog(hDlg, LOWORD(wParam));
|
|||
|
return (INT_PTR)TRUE;
|
|||
|
}
|
|||
|
break;
|
|||
|
}
|
|||
|
return (INT_PTR)FALSE;
|
|||
|
}
|