Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <cstdlib>
- #include <cstdio>
- #include <ctime>
- #include <cmath>
- #define WND_WIDTH 1000
- #define WND_HEIGHT 800
- #define FIELDS 4
- #define BTN_SIZE 60
- #define GAME_START_X 20
- #define GAME_START_Y 20
- struct field {
- HWND Button;
- int Value;
- int PosX, PosY;
- int Width, Height;
- };
- HWND hwnd ,MovesInfo;
- HINSTANCE hInstance;
- field Field[(FIELDS*FIELDS)];
- int NumberOfMoves = 0;
- void DrawGameField(int i) {
- char buff[255];
- DestroyWindow(Field[i].Button);
- sprintf_s(buff, "%d", Field[i].Value);
- Field[i] = {
- CreateWindowEx(NULL, "Button", ((Field[i].Value != 0) ? TEXT(buff) : TEXT("")), WS_CHILD | WS_VISIBLE,Field[i].PosX , Field[i].PosY, BTN_SIZE, BTN_SIZE, hwnd, (HMENU)(Field[i].Value), hInstance, NULL),
- Field[i].Value,Field[i].PosX , Field[i].PosY,BTN_SIZE,BTN_SIZE
- };
- }
- bool IsInArray(int Value) {
- for (int i = 0; i < ((FIELDS*FIELDS)); i++) {
- if (Field[i].Value == Value) return true;
- }
- return false;
- }
- void InitGame() {
- int PositionX = GAME_START_X, PositionY = GAME_START_Y;
- int NewValue;
- for (int i = 0; i < ((FIELDS*FIELDS) - 1);) {
- NewValue = (rand() % (FIELDS*FIELDS));
- if (!IsInArray(NewValue)) {
- Field[i].Value = NewValue;
- Field[i].PosX = PositionX;
- Field[i].PosY = PositionY;
- DrawGameField(i);
- if (PositionX == (GAME_START_X + (BTN_SIZE * (FIELDS - 1)))) {
- PositionX = GAME_START_Y;
- PositionY += BTN_SIZE;
- }
- else {
- PositionX += BTN_SIZE;
- }
- i++;
- }
- }
- Field[(FIELDS*FIELDS) - 1].Value = 0;
- Field[(FIELDS*FIELDS) - 1].PosX = PositionX;
- Field[(FIELDS*FIELDS) - 1].PosY = PositionY;
- DrawGameField((FIELDS*FIELDS) - 1);
- }
- void MakeMove() {
- char buff[255];
- sprintf_s(buff, "Ruchy: %d", ++NumberOfMoves);
- SetWindowText(MovesInfo, buff);
- for (int i = 0; i < ((FIELDS*FIELDS)); i++) {
- DrawGameField(i);
- }
- }
- void SwapFields(field &F1, field &F2) {
- int FieldBuff = F1.Value;
- F1.Value = F2.Value;
- F2.Value = FieldBuff;
- MakeMove();
- }
- bool CheckWin() {
- if (Field[0].Value == 1) {
- for (int i = 0; i < (FIELDS*FIELDS) - 2; i++) {
- if (Field[i].Value + 1 != Field[i + 1].Value) return false;
- }
- if (MessageBox(NULL, TEXT("WIN"), TEXT("WIN"), MB_YESNO) == IDYES) {
- for (int i = 0; i < ((FIELDS*FIELDS)); i++) {
- Field[i].Value = (FIELDS*FIELDS) + 10;
- }
- MakeMove();
- InitGame();
- }
- return true;
- }
- return false;
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
- switch (msg) {
- case WM_COMMAND:
- for (int i = 0; i < (FIELDS*FIELDS); i++) {
- if (wParam == Field[i].Value) {
- if (Field[i].Value != 0) {
- if (i - (FIELDS) >= 0 && Field[i - (FIELDS)].Value == 0) { // Pole nad puste
- SwapFields(Field[i], Field[i - (FIELDS)]);
- }
- else if (i + (FIELDS) < (FIELDS*FIELDS) && Field[i + (FIELDS)].Value == 0) { // Pole pod puste
- SwapFields(Field[i], Field[i + (FIELDS)]);
- }
- else if (i % FIELDS != 0 && i - 1 >= 0 && Field[i - 1].Value == 0) { // Pole na lewo puste
- SwapFields(Field[i], Field[i - 1]);
- }
- else if ((i + 1) % FIELDS != 0 && i + 1 < (FIELDS*FIELDS) && Field[i + 1].Value == 0) { // Pole na prawo puste
- SwapFields(Field[i], Field[i + 1]);
- }
- break;
- }
- CheckWin();
- }
- }
- break;
- case WM_CLOSE: DestroyWindow(hwnd); break;
- case WM_DESTROY: PostQuitMessage(0); break;
- default: return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE HInstance, HINSTANCE hPrevInstance, LPSTR ilCmdLine, int nCmdShow) {
- srand(time(NULL));
- WNDCLASSEX window;
- MSG msg;
- TCHAR Class_Name[] = TEXT("OKNO_TEST"), Title[] = TEXT("Tytul");
- window.cbClsExtra = NULL;
- window.cbSize = sizeof(WNDCLASSEX);
- window.cbWndExtra = NULL;
- window.hbrBackground = (HBRUSH)(COLOR_WINDOW );
- window.hCursor = LoadCursor(NULL, IDC_ARROW);
- window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- window.hInstance = hInstance;
- window.lpfnWndProc = WndProc;
- window.lpszClassName = Class_Name;
- window.lpszMenuName = 0;
- window.style = CS_VREDRAW | CS_HREDRAW;
- RegisterClassEx(&window);
- hInstance - HInstance;
- hwnd = CreateWindowEx(WS_EX_WINDOWEDGE, Class_Name, Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, WND_WIDTH, WND_HEIGHT, NULL, NULL, hInstance, NULL);
- InitGame();
- MovesInfo = CreateWindowEx(NULL, "STATIC", TEXT("Ruchy: 0"), WS_CHILD | WS_VISIBLE, GAME_START_X+(FIELDS*BTN_SIZE)+10, GAME_START_Y, 100, 50, hwnd, NULL, hInstance, NULL);
- ShowWindow(hwnd, nCmdShow);
- UpdateWindow(hwnd);
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- UnregisterClass(Class_Name, hInstance);
- return (int)msg.wParam;
- }
Add Comment
Please, Sign In to add comment