sliding puzzle drawn to screen
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
#include <raylib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
@@ -25,42 +26,56 @@ void indexFromCoordinate(struct Coordinate*, int*, int);
|
|||||||
void randomizeField(int*, int, int, struct Coordinate*);
|
void randomizeField(int*, int, int, struct Coordinate*);
|
||||||
int checkField(int* field, int row, int column);
|
int checkField(int* field, int row, int column);
|
||||||
|
|
||||||
|
const int screenWidth = 800;
|
||||||
|
const int screenHeight = 450;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int row, column, move_counter = 0;
|
int row, column, move_counter = 0;
|
||||||
struct Coordinate emptycell;
|
struct Coordinate emptycell;
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
//srand(295837);
|
|
||||||
|
|
||||||
printf("Input format is: a, b\n"
|
row = column = 4;
|
||||||
"where a and b are integers.\n\n");
|
InitWindow(screenWidth, screenHeight, "Sliding Puzzle");
|
||||||
|
|
||||||
printf("Enter row and column length: ");
|
|
||||||
scanf("%d, %d", &row, &column);
|
|
||||||
|
|
||||||
int* field = (int*) malloc(row * column * sizeof(int));
|
int* field = (int*) malloc(row * column * sizeof(int));
|
||||||
makeField(field, row, column, &emptycell);
|
makeField(field, row, column, &emptycell);
|
||||||
randomizeField(field, row, column, &emptycell);
|
randomizeField(field, row, column, &emptycell);
|
||||||
|
|
||||||
clock_t begin = clock();
|
int cell_size = 60;
|
||||||
clock_t end;
|
int x, y;
|
||||||
while (1)
|
char str[3];
|
||||||
{
|
char action;
|
||||||
printField(field, row, column);
|
|
||||||
printf("Move count: %d\n", move_counter);
|
while (!WindowShouldClose()){
|
||||||
if (checkField(field, row, column))
|
if (checkField(field, row, column)){
|
||||||
{
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
move_counter += moveCell(field, &emptycell, row, column, getAction(), 1);
|
|
||||||
system("clear");
|
action = (char) GetCharPressed();
|
||||||
//system("cls");
|
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);
|
||||||
}
|
}
|
||||||
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);
|
EndDrawing();
|
||||||
|
}
|
||||||
|
|
||||||
|
free(field);
|
||||||
}
|
}
|
||||||
|
|
||||||
int checkField(int* field, int row, int column)
|
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;
|
return move_counter;
|
||||||
}
|
}
|
||||||
else {return 0;}
|
else {return 0;}
|
||||||
case 't':
|
/* case 't': */
|
||||||
exit(0);
|
/* exit(0); */
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user