Bugfixes and code cleanup

master
Racoonicorn 2016-12-28 21:19:25 +01:00 committed by GitHub
parent 6627896dba
commit 4ca3513991
1 changed files with 178 additions and 175 deletions

129
Roofus.c
View File

@ -1,4 +1,4 @@
#define VERSION "1.03"
#define VERSION "1.04"
#define N 9
#define USERNAME_LENGTH 10
@ -22,11 +22,10 @@ void printm ();
int win();
int matrix[N][N] = {0};
int my_x;
int my_y;
int ptr_x;
int ptr_y;
int moves;
int grid;
int gridset = 0;
FILE *fp;
int main() {
@ -44,11 +43,14 @@ int main () {
if (fp == NULL) {
printw("Error opening file");
refresh();
getch();
endwin();
return 1;
} else {
restoregrid();
menu();
}
fclose(fp);
}
endwin();
return 0;
}
@ -69,14 +71,19 @@ void restoregrid () {
void menu() {
while (1) {
clear();
printw ("~~~ ROOFUS %dx%d ~~~\n\n[1] Play\n[2] Ranking\n[3] Settings\n[4] Rules\n[q] Quit\n\nv%s\n\n", grid, grid, VERSION);
printw("~~~ ROOFUS %dx%d ~~~\n\n"
"[1] Play\n"
"[2] Ranking\n"
"[3] Settings\n"
"[4] Rules\n"
"[q] Quit\n\nv%s\n\n", grid, grid, VERSION);
refresh();
switch (getch()) {
case 'q': return;
case '1': play(); break;
case '2': ranking(); break;
case '3': settings(); break;
case '4': rules(); break;
case 'q': return;
}
}
}
@ -88,8 +95,8 @@ void play () {
char *text = malloc(sizeof(text) * N);
moves = 0;
clear();
my_x = grid / 2;
my_y = grid / 2;
ptr_x = grid / 2;
ptr_y = grid / 2;
start();
while (end == 0) {
printw("\nArrow to move, q to quit\n");
@ -138,58 +145,58 @@ void start () {
// move pointer
void move_ptr(int direction) {
int xinit = my_x, yinit = my_y;
int xinit = ptr_x, yinit = ptr_y;
int i, j, del = 0;
switch (direction) {
case KEY_LEFT:
if (my_x != 0) {
my_x--;
if (ptr_x != 0) {
ptr_x--;
for (j = 0; j < xinit; j++) {
if (matrix[my_y][j] != 0) {
matrix[my_y][j] = matrix[my_y][j] - matrix[my_y][xinit];
if (matrix[my_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[my_y][j] < 0 && matrix[yinit][xinit] != 0)
matrix[my_y][j] = - matrix[my_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 (my_x < grid - 1) {
my_x++;
for (j = my_x; j < grid; j++) {
if (matrix[my_y][j] != 0) {
matrix[my_y][j] = matrix[my_y][j] - matrix[yinit][xinit];
if (matrix[my_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[my_y][j] < 0 && matrix[yinit][xinit] != 0)
matrix[my_y][j] = -matrix[my_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 (my_y > 0) {
my_y--;
for (i = my_y; i >= 0; i--) {
if (matrix[i][my_x] != 0) {
matrix[i][my_x] = matrix[i][my_x] - matrix[yinit][xinit];
if (matrix[i][my_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][my_x] < 0 && matrix[yinit][xinit] != 0)
matrix[i][my_x] = -matrix[i][my_x];
if (matrix[i][ptr_x] < 0 && matrix[yinit][xinit] != 0)
matrix[i][ptr_x] = - matrix[i][ptr_x];
}
}
} break;
case KEY_DOWN:
if (my_y < grid - 1) {
my_y++;
for (i = my_y; i < grid; i++) {
if(matrix[i][my_x] != 0) {
matrix[i][my_x] = matrix[i][my_x] - matrix[yinit][xinit];
if (matrix[i][my_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][my_x] < 0 && matrix[yinit][xinit] != 0)
matrix[i][my_x] = -matrix[i][my_x];
if (matrix[i][ptr_x] < 0 && matrix[yinit][xinit] != 0)
matrix[i][ptr_x] = - matrix[i][ptr_x];
}
}
} break;
@ -198,6 +205,7 @@ void move_ptr (int direction) {
if (del == 1)
matrix[yinit][xinit] = 0;
nolow();
if (xinit != ptr_x || yinit != ptr_y)
moves++;
}
@ -213,21 +221,20 @@ void nolow () {
}
void settings() {
int menu = 0;
while (menu == 0) {
while (1) {
clear();
printw("> Settings\n\n");
printw("Menu [q]\nGrid size? [3-9] ");
refresh();
switch (getch()) {
case 'q': menu = 1; break;
case '3': grid = 3; gridset = 1; menu = 1; break;
case '4': grid = 4; gridset = 1; menu = 1; break;
case '5': grid = 5; gridset = 1; menu = 1; break;
case '6': grid = 6; gridset = 1; menu = 1; break;
case '7': grid = 7; gridset = 1; menu = 1; break;
case '8': grid = 8; gridset = 1; menu = 1; break;
case '9': grid = 9; gridset = 1; menu = 1; break;
case 'q': return;
case '3': grid = 3; return;
case '4': grid = 4; return;
case '5': grid = 5; return;
case '6': grid = 6; return;
case '7': grid = 7; return;
case '8': grid = 8; return;
case '9': grid = 9; return;
default: break;
}
}
@ -262,10 +269,9 @@ void ranking () {
}
do {
clear();
if (grid_val >= 3 && grid_val <= 9) {
if (grid_val >= 3 && grid_val <= 9)
printw("Best results (%dx%d matrix):\n\n", grid_val, grid_val);
refresh ();
} else if (grid_val == 1)
else if (grid_val == 1)
printw("Best results:\n\n");
printed = 0;
for (i = 0; result[i].moves != 0; i++) {
@ -304,25 +310,22 @@ void printm () {
printw("\n\n");
for (i = 0; i < grid; i++) {
for (j = 0; j < grid; j++) {
if (i == my_y && j == my_x) {
if (has_colors () == TRUE) {
if (i == ptr_y && j == ptr_x) {
if (has_colors() == TRUE)
attron(COLOR_PAIR(1));
} else {
else
printw("> ");
}
}
if (matrix[i][j] != 0)
printw("%2d", matrix[i][j]);
else {
if (has_colors () == FALSE) {
if (has_colors() == FALSE)
printw(" ");
} else {
else
printw(" .");
}
}
if (has_colors () == TRUE) {
if (has_colors() == TRUE)
attroff(COLOR_PAIR(1));
}
printw("\t");
}
printw("\n\n");