diff options
author | mRnea <akannemre@gmail.com> | 2024-12-20 19:53:22 +0300 |
---|---|---|
committer | mRnea <akannemre@gmail.com> | 2024-12-20 19:53:22 +0300 |
commit | e691768ae0572a96b61bb28ce478bc4d4162a02e (patch) | |
tree | 258678d66f1d01cd7be75502c92c0dd05406426e /sliding-puzzle | |
parent | 40b50d19645218f049b6c913011d078334358058 (diff) |
sliding puzzle drawn to screen
Diffstat (limited to 'sliding-puzzle')
-rw-r--r-- | sliding-puzzle/sliding.c | 61 |
1 files changed, 38 insertions, 23 deletions
diff --git a/sliding-puzzle/sliding.c b/sliding-puzzle/sliding.c index e43a98f..40f5d85 100644 --- a/sliding-puzzle/sliding.c +++ b/sliding-puzzle/sliding.c @@ -1,3 +1,4 @@ +#include <raylib.h> #include <stdio.h> #include <stdlib.h> #include <math.h> @@ -25,42 +26,56 @@ void indexFromCoordinate(struct Coordinate*, int*, int); void randomizeField(int*, int, int, struct Coordinate*); int checkField(int* field, int row, int column); +const int screenWidth = 800; +const int screenHeight = 450; int main() { int row, column, move_counter = 0; struct Coordinate emptycell; srand(time(NULL)); - //srand(295837); - printf("Input format is: a, b\n" - "where a and b are integers.\n\n"); - - printf("Enter row and column length: "); - scanf("%d, %d", &row, &column); + row = column = 4; + InitWindow(screenWidth, screenHeight, "Sliding Puzzle"); int* field = (int*) malloc(row * column * sizeof(int)); makeField(field, row, column, &emptycell); randomizeField(field, row, column, &emptycell); - clock_t begin = clock(); - clock_t end; - while (1) - { - printField(field, row, column); - printf("Move count: %d\n", move_counter); - if (checkField(field, row, column)) - { + int cell_size = 60; + int x, y; + char str[3]; + char action; + + while (!WindowShouldClose()){ + if (checkField(field, row, column)){ break; } - move_counter += moveCell(field, &emptycell, row, column, getAction(), 1); - system("clear"); - //system("cls"); + + action = (char) GetCharPressed(); + moveCell(field, &emptycell, row, column, action, 1); + + BeginDrawing(); + ClearBackground(RAYWHITE); + + for (int i = 0; i < row; i++){ + for (int j = 0; j < column; j++){ + x = j * cell_size; + y = i * cell_size; + DrawRectangleLines(x, y, + cell_size - 4, cell_size - 4, + GRAY); + if (field[(column * i) + j] != 0){ + snprintf(str, 3, "%d", field[(column * i) + j]); + DrawText(str, x + cell_size / 3, y + cell_size / 3, 20, BLACK); + } + } + } + + EndDrawing(); } - end = clock(); - printf("begin= %Lf, end= %Lf\n", (long double) begin / CLOCKS_PER_SEC, (long double) end / CLOCKS_PER_SEC); - long double time_spent = (long double) (end - begin) / CLOCKS_PER_SEC; - printf("Play time = %lf seconds\n\n", time_spent); + + free(field); } int checkField(int* field, int row, int column) @@ -216,8 +231,8 @@ int moveCell(int* field, struct Coordinate* emptycell, int row, int column, char return move_counter; } else {return 0;} - case 't': - exit(0); + /* case 't': */ + /* exit(0); */ default: return 0; } |