Code cleanup
This commit is contained in:
parent
4ca3513991
commit
4d705243a9
195
Roofus.c
195
Roofus.c
|
@ -1,4 +1,4 @@
|
||||||
#define VERSION "1.04"
|
#define VERSION "1.05"
|
||||||
|
|
||||||
#define N 9
|
#define N 9
|
||||||
#define USERNAME_LENGTH 10
|
#define USERNAME_LENGTH 10
|
||||||
|
@ -21,9 +21,12 @@ void rules();
|
||||||
void printm();
|
void printm();
|
||||||
int win();
|
int win();
|
||||||
|
|
||||||
|
struct point{
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
} ptr;
|
||||||
|
|
||||||
int matrix[N][N] = {0};
|
int matrix[N][N] = {0};
|
||||||
int ptr_x;
|
|
||||||
int ptr_y;
|
|
||||||
int moves;
|
int moves;
|
||||||
int grid;
|
int grid;
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
@ -50,17 +53,15 @@ int main() {
|
||||||
restoregrid();
|
restoregrid();
|
||||||
menu();
|
menu();
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
endwin();
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
endwin();
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// restore last grid size
|
// restore last grid size
|
||||||
void restoregrid() {
|
void restoregrid() {
|
||||||
int gridtemp, length;
|
int gridtemp;
|
||||||
fseek(fp, 0, SEEK_END);
|
fseek(fp, -2, SEEK_END); // go to grid size character
|
||||||
length = ftell(fp);
|
|
||||||
fseek(fp, (length - 2), SEEK_SET); // go to grid size character
|
|
||||||
gridtemp = fgetc(fp) - '0'; // convert char to int
|
gridtemp = fgetc(fp) - '0'; // convert char to int
|
||||||
if (gridtemp < 3 || gridtemp > 9)
|
if (gridtemp < 3 || gridtemp > 9)
|
||||||
grid = 5;
|
grid = 5;
|
||||||
|
@ -89,53 +90,59 @@ void menu() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void play() {
|
void play() {
|
||||||
|
int direction, end = 0, replay;
|
||||||
char user[USERNAME_LENGTH];
|
char user[USERNAME_LENGTH];
|
||||||
int direction;
|
|
||||||
int end = 0;
|
|
||||||
char *text = malloc(sizeof(text) * N);
|
char *text = malloc(sizeof(text) * N);
|
||||||
moves = 0;
|
do {
|
||||||
clear();
|
replay = 0;
|
||||||
ptr_x = grid / 2;
|
moves = 0;
|
||||||
ptr_y = grid / 2;
|
ptr.x = ptr.y = grid / 2;
|
||||||
start();
|
clear();
|
||||||
while (end == 0) {
|
start();
|
||||||
printw("\nArrow to move, q to quit\n");
|
while (end == 0) {
|
||||||
refresh();
|
printw("\n[q] Quit. [r] Restart\nArrow to move ");
|
||||||
direction = getch();
|
|
||||||
if (direction == 'q') {
|
|
||||||
printw("\nAre you sure? [y/n] ");
|
|
||||||
refresh();
|
refresh();
|
||||||
if (getch() == 'y') {
|
direction = getch();
|
||||||
return;
|
if (direction == 'q') {
|
||||||
} else {
|
printw("\nQuit? [y/n] ");
|
||||||
direction = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (direction != 0) {
|
|
||||||
move_ptr(direction);
|
|
||||||
printm();
|
|
||||||
end = win();
|
|
||||||
if (end != 0) {
|
|
||||||
echo();
|
|
||||||
printw("Enter your name: ");
|
|
||||||
refresh();
|
refresh();
|
||||||
getnstr(user, USERNAME_LENGTH); // read at most
|
if (getch() == 'y') {
|
||||||
noecho();
|
break;
|
||||||
|
} else
|
||||||
|
direction = 0;
|
||||||
}
|
}
|
||||||
} else {
|
if (direction == 'r') {
|
||||||
printm();
|
printw("\nRestart? [y/n] ");
|
||||||
|
refresh();
|
||||||
|
if (getch() == 'y') {
|
||||||
|
replay = 1;
|
||||||
|
break;
|
||||||
|
} else
|
||||||
|
direction = 0;
|
||||||
|
}
|
||||||
|
if (direction != 0) {
|
||||||
|
move_ptr(direction);
|
||||||
|
printm();
|
||||||
|
end = win();
|
||||||
|
if (end == 1) {
|
||||||
|
echo();
|
||||||
|
printw("Enter your name: ");
|
||||||
|
refresh();
|
||||||
|
getnstr(user, USERNAME_LENGTH); // read at most
|
||||||
|
noecho();
|
||||||
|
sprintf(text, "%li %d %s %d\n", time(NULL), moves, user, grid);
|
||||||
|
fputs(text, fp);
|
||||||
|
}
|
||||||
|
} else printm();
|
||||||
}
|
}
|
||||||
}
|
} while (replay == 1);
|
||||||
sprintf(text, "%li %d %s %d\n", time(NULL), moves, user, grid);
|
|
||||||
fputs(text, fp);
|
|
||||||
free(text);
|
free(text);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void start() {
|
void start() {
|
||||||
int i, j;
|
for (int i = 0; i < grid; i++) {
|
||||||
for (i = 0; i < grid; i++) {
|
for (int j = 0; j < grid; j++) {
|
||||||
for (j = 0; j < grid; j++) {
|
|
||||||
matrix[i][j] = rand() % 19 + 1;
|
matrix[i][j] = rand() % 19 + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -145,58 +152,58 @@ void start() {
|
||||||
|
|
||||||
// move pointer
|
// move pointer
|
||||||
void move_ptr(int direction) {
|
void move_ptr(int direction) {
|
||||||
int xinit = ptr_x, yinit = ptr_y;
|
int xinit = ptr.x, yinit = ptr.y;
|
||||||
int i, j, del = 0;
|
int i, j, del = 0;
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case KEY_LEFT:
|
case KEY_LEFT:
|
||||||
if (ptr_x != 0) {
|
if (ptr.x != 0) {
|
||||||
ptr_x--;
|
ptr.x--;
|
||||||
for (j = 0; j < xinit; j++) {
|
for (j = 0; j < xinit; j++) {
|
||||||
if (matrix[ptr_y][j] != 0) {
|
if (matrix[ptr.y][j] != 0) {
|
||||||
matrix[ptr_y][j] = matrix[ptr_y][j] - matrix[ptr_y][xinit];
|
matrix[ptr.y][j] = matrix[ptr.y][j] - matrix[ptr.y][xinit];
|
||||||
if (matrix[ptr_y][j] == 0)
|
if (matrix[ptr.y][j] == 0)
|
||||||
del = 1;
|
del = 1;
|
||||||
if (matrix[ptr_y][j] < 0 && matrix[yinit][xinit] != 0)
|
if (matrix[ptr.y][j] < 0 && matrix[yinit][xinit] != 0)
|
||||||
matrix[ptr_y][j] = - matrix[ptr_y][j];
|
matrix[ptr.y][j] = - matrix[ptr.y][j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case KEY_RIGHT:
|
case KEY_RIGHT:
|
||||||
if (ptr_x < grid - 1) {
|
if (ptr.x < grid - 1) {
|
||||||
ptr_x++;
|
ptr.x++;
|
||||||
for (j = ptr_x; j < grid; j++) {
|
for (j = ptr.x; j < grid; j++) {
|
||||||
if (matrix[ptr_y][j] != 0) {
|
if (matrix[ptr.y][j] != 0) {
|
||||||
matrix[ptr_y][j] = matrix[ptr_y][j] - matrix[yinit][xinit];
|
matrix[ptr.y][j] = matrix[ptr.y][j] - matrix[yinit][xinit];
|
||||||
if (matrix[ptr_y][j] == 0)
|
if (matrix[ptr.y][j] == 0)
|
||||||
del = 1;
|
del = 1;
|
||||||
if (matrix[ptr_y][j] < 0 && matrix[yinit][xinit] != 0)
|
if (matrix[ptr.y][j] < 0 && matrix[yinit][xinit] != 0)
|
||||||
matrix[ptr_y][j] = - matrix[ptr_y][j];
|
matrix[ptr.y][j] = - matrix[ptr.y][j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case KEY_UP:
|
case KEY_UP:
|
||||||
if (ptr_y > 0) {
|
if (ptr.y > 0) {
|
||||||
ptr_y--;
|
ptr.y--;
|
||||||
for (i = ptr_y; i >= 0; i--) {
|
for (i = ptr.y; i >= 0; i--) {
|
||||||
if (matrix[i][ptr_x] != 0) {
|
if (matrix[i][ptr.x] != 0) {
|
||||||
matrix[i][ptr_x] = matrix[i][ptr_x] - matrix[yinit][xinit];
|
matrix[i][ptr.x] = matrix[i][ptr.x] - matrix[yinit][xinit];
|
||||||
if (matrix[i][ptr_x] == 0)
|
if (matrix[i][ptr.x] == 0)
|
||||||
del = 1;
|
del = 1;
|
||||||
if (matrix[i][ptr_x] < 0 && matrix[yinit][xinit] != 0)
|
if (matrix[i][ptr.x] < 0 && matrix[yinit][xinit] != 0)
|
||||||
matrix[i][ptr_x] = - matrix[i][ptr_x];
|
matrix[i][ptr.x] = - matrix[i][ptr.x];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case KEY_DOWN:
|
case KEY_DOWN:
|
||||||
if (ptr_y < grid - 1) {
|
if (ptr.y < grid - 1) {
|
||||||
ptr_y++;
|
ptr.y++;
|
||||||
for (i = ptr_y; i < grid; i++) {
|
for (i = ptr.y; i < grid; i++) {
|
||||||
if(matrix[i][ptr_x] != 0) {
|
if(matrix[i][ptr.x] != 0) {
|
||||||
matrix[i][ptr_x] = matrix[i][ptr_x] - matrix[yinit][xinit];
|
matrix[i][ptr.x] = matrix[i][ptr.x] - matrix[yinit][xinit];
|
||||||
if (matrix[i][ptr_x] == 0)
|
if (matrix[i][ptr.x] == 0)
|
||||||
del = 1;
|
del = 1;
|
||||||
if (matrix[i][ptr_x] < 0 && matrix[yinit][xinit] != 0)
|
if (matrix[i][ptr.x] < 0 && matrix[yinit][xinit] != 0)
|
||||||
matrix[i][ptr_x] = - matrix[i][ptr_x];
|
matrix[i][ptr.x] = - matrix[i][ptr.x];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
@ -205,15 +212,14 @@ void move_ptr(int direction) {
|
||||||
if (del == 1)
|
if (del == 1)
|
||||||
matrix[yinit][xinit] = 0;
|
matrix[yinit][xinit] = 0;
|
||||||
nolow();
|
nolow();
|
||||||
if (xinit != ptr_x || yinit != ptr_y)
|
if (xinit != ptr.x || yinit != ptr.y)
|
||||||
moves++;
|
moves++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if 1 or 2 -> random number
|
// if 1 or 2 -> random number
|
||||||
void nolow() {
|
void nolow() {
|
||||||
int i, j;
|
for (int i = 0; i < grid; i++) {
|
||||||
for (i = 0; i < grid; i++) {
|
for (int j = 0; j < grid; j++) {
|
||||||
for (j = 0; j < grid; j++) {
|
|
||||||
if ( matrix[i][j] < 3 && matrix[i][j] > 0)
|
if ( matrix[i][j] < 3 && matrix[i][j] > 0)
|
||||||
matrix[i][j] = rand() % 10 - 10;
|
matrix[i][j] = rand() % 10 - 10;
|
||||||
}
|
}
|
||||||
|
@ -246,16 +252,16 @@ void ranking() {
|
||||||
int moves;
|
int moves;
|
||||||
int grid;
|
int grid;
|
||||||
char user[10];
|
char user[10];
|
||||||
time_t res_time;
|
time_t playtime;
|
||||||
};
|
};
|
||||||
struct res result[1000];
|
struct res result[1000];
|
||||||
struct res temp;
|
struct res temp;
|
||||||
char *time_string[1000];
|
|
||||||
int grid_val = grid;
|
int grid_val = grid;
|
||||||
int printed;
|
int printed;
|
||||||
|
|
||||||
rewind(fp);
|
rewind(fp);
|
||||||
do {
|
do {
|
||||||
fscanf(fp, "%li %d %s %d", &result[i].res_time, &result[i].moves, result[i].user, &result[i].grid);
|
fscanf(fp, "%li %d %s %d", &result[i].playtime, &result[i].moves, result[i].user, &result[i].grid);
|
||||||
i++;
|
i++;
|
||||||
} while (result[i-1].moves > 0);
|
} while (result[i-1].moves > 0);
|
||||||
for (i = 0; result[i].moves != 0; i++) {
|
for (i = 0; result[i].moves != 0; i++) {
|
||||||
|
@ -276,16 +282,12 @@ void ranking() {
|
||||||
printed = 0;
|
printed = 0;
|
||||||
for (i = 0; result[i].moves != 0; i++) {
|
for (i = 0; result[i].moves != 0; i++) {
|
||||||
if ((result[i].grid == grid_val || grid_val == 1) && printed < 9) {
|
if ((result[i].grid == grid_val || grid_val == 1) && printed < 9) {
|
||||||
time_string[i] = ctime(&result[i].res_time);
|
printw("# %d - %3d moves\t- (%dx%d matrix) - %s\t- %s", printed + 1, result[i].moves, result[i].grid, result[i].grid, result[i].user, ctime(&result[i].playtime));
|
||||||
printw("# %d - %3d moves\t- (%dx%d matrix) - %s\t- %s", printed + 1, result[i].moves, result[i].grid, result[i].grid, result[i].user, time_string[i]);
|
|
||||||
refresh();
|
|
||||||
printed++;
|
printed++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (printed == 0) {
|
if (printed == 0)
|
||||||
printw("Nothing to show...\n");
|
printw("Nothing to show...\n");
|
||||||
refresh();
|
|
||||||
}
|
|
||||||
printw("\nMenu [q]; To filter by matrix size [3-9]; No filter [1] ");
|
printw("\nMenu [q]; To filter by matrix size [3-9]; No filter [1] ");
|
||||||
refresh();
|
refresh();
|
||||||
do {
|
do {
|
||||||
|
@ -305,12 +307,11 @@ void rules() {
|
||||||
|
|
||||||
// print matrix
|
// print matrix
|
||||||
void printm() {
|
void printm() {
|
||||||
int i, j;
|
|
||||||
clear();
|
clear();
|
||||||
printw("\n\n");
|
printw("\n\n");
|
||||||
for (i = 0; i < grid; i++) {
|
for (int i = 0; i < grid; i++) {
|
||||||
for (j = 0; j < grid; j++) {
|
for (int j = 0; j < grid; j++) {
|
||||||
if (i == ptr_y && j == ptr_x) {
|
if (i == ptr.y && j == ptr.x) {
|
||||||
if (has_colors() == TRUE)
|
if (has_colors() == TRUE)
|
||||||
attron(COLOR_PAIR(1));
|
attron(COLOR_PAIR(1));
|
||||||
else
|
else
|
||||||
|
@ -334,9 +335,9 @@ void printm() {
|
||||||
}
|
}
|
||||||
|
|
||||||
int win() {
|
int win() {
|
||||||
int i, j, count = 0;
|
int count = 0;
|
||||||
for (i = 0; i < grid; i++) {
|
for (int i = 0; i < grid; i++) {
|
||||||
for (j = 0; j < grid; j++) {
|
for (int j = 0; j < grid; j++) {
|
||||||
if (matrix[i][j] == 0)
|
if (matrix[i][j] == 0)
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue