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