sliding puzzle drawn to screen

This commit is contained in:
2024-12-20 19:53:22 +03:00
parent 40b50d1964
commit e691768ae0

View File

@@ -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;
}