Bugfixes and code cleanup
parent
6627896dba
commit
4ca3513991
353
Roofus.c
353
Roofus.c
|
@ -1,4 +1,4 @@
|
|||
#define VERSION "1.03"
|
||||
#define VERSION "1.04"
|
||||
|
||||
#define N 9
|
||||
#define USERNAME_LENGTH 10
|
||||
|
@ -9,187 +9,194 @@
|
|||
#include <string.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
void restoregrid ();
|
||||
void menu ();
|
||||
void play ();
|
||||
void start ();
|
||||
void move_ptr (int direction);
|
||||
void nolow ();
|
||||
void settings ();
|
||||
void ranking ();
|
||||
void rules ();
|
||||
void printm ();
|
||||
int win ();
|
||||
void restoregrid();
|
||||
void menu();
|
||||
void play();
|
||||
void start();
|
||||
void move_ptr(int direction);
|
||||
void nolow();
|
||||
void settings();
|
||||
void ranking();
|
||||
void rules();
|
||||
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 () {
|
||||
initscr ();
|
||||
noecho ();
|
||||
cbreak ();
|
||||
keypad (stdscr, true);
|
||||
int main() {
|
||||
initscr();
|
||||
noecho();
|
||||
cbreak();
|
||||
keypad(stdscr, true);
|
||||
|
||||
if (has_colors () == TRUE) {
|
||||
start_color ();
|
||||
init_pair (1, COLOR_BLACK, COLOR_WHITE);
|
||||
if (has_colors() == TRUE) {
|
||||
start_color();
|
||||
init_pair(1, COLOR_BLACK, COLOR_WHITE);
|
||||
}
|
||||
srand (time (NULL));
|
||||
fp = fopen ("roofus.txt", "a+");
|
||||
srand(time(NULL));
|
||||
fp = fopen("roofus.txt", "a+");
|
||||
if (fp == NULL) {
|
||||
printw ("Error opening file");
|
||||
refresh ();
|
||||
printw("Error opening file");
|
||||
refresh();
|
||||
getch();
|
||||
endwin();
|
||||
return 1;
|
||||
} else {
|
||||
restoregrid ();
|
||||
menu ();
|
||||
restoregrid();
|
||||
menu();
|
||||
fclose(fp);
|
||||
}
|
||||
fclose (fp);
|
||||
endwin ();
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// restore last grid size
|
||||
void restoregrid () {
|
||||
void restoregrid() {
|
||||
int gridtemp, length;
|
||||
fseek (fp, 0, SEEK_END);
|
||||
length = ftell (fp);
|
||||
fseek (fp, (length - 2), SEEK_SET); // go to grid size character
|
||||
gridtemp = fgetc (fp) - '0'; // convert char to int
|
||||
fseek(fp, 0, SEEK_END);
|
||||
length = ftell(fp);
|
||||
fseek(fp, (length - 2), SEEK_SET); // go to grid size character
|
||||
gridtemp = fgetc(fp) - '0'; // convert char to int
|
||||
if (gridtemp < 3 || gridtemp > 9)
|
||||
grid = 5;
|
||||
else
|
||||
grid = gridtemp;
|
||||
}
|
||||
|
||||
void menu () {
|
||||
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);
|
||||
refresh ();
|
||||
switch (getch ()) {
|
||||
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);
|
||||
refresh();
|
||||
switch (getch()) {
|
||||
case '1': play(); break;
|
||||
case '2': ranking(); break;
|
||||
case '3': settings(); break;
|
||||
case '4': rules(); break;
|
||||
case 'q': return;
|
||||
case '1': play (); break;
|
||||
case '2': ranking (); break;
|
||||
case '3': settings (); break;
|
||||
case '4': rules (); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void play () {
|
||||
void play() {
|
||||
char user[USERNAME_LENGTH];
|
||||
int direction;
|
||||
int end = 0;
|
||||
char *text = malloc (sizeof (text) * N);
|
||||
char *text = malloc(sizeof(text) * N);
|
||||
moves = 0;
|
||||
clear();
|
||||
my_x = grid / 2;
|
||||
my_y = grid / 2;
|
||||
start ();
|
||||
ptr_x = grid / 2;
|
||||
ptr_y = grid / 2;
|
||||
start();
|
||||
while (end == 0) {
|
||||
printw ("\nArrow to move, q to quit\n");
|
||||
printw("\nArrow to move, q to quit\n");
|
||||
refresh();
|
||||
direction = getch ();
|
||||
direction = getch();
|
||||
if (direction == 'q') {
|
||||
printw ("\nAre you sure? [y/n] ");
|
||||
refresh ();
|
||||
if (getch () == 'y') {
|
||||
printw("\nAre you sure? [y/n] ");
|
||||
refresh();
|
||||
if (getch() == 'y') {
|
||||
return;
|
||||
} else {
|
||||
direction = 0;
|
||||
}
|
||||
}
|
||||
if (direction != 0) {
|
||||
move_ptr (direction);
|
||||
printm ();
|
||||
end = win ();
|
||||
move_ptr(direction);
|
||||
printm();
|
||||
end = win();
|
||||
if (end != 0) {
|
||||
echo ();
|
||||
printw ("Enter your name: ");
|
||||
refresh ();
|
||||
getnstr (user, USERNAME_LENGTH); // read at most
|
||||
noecho ();
|
||||
echo();
|
||||
printw("Enter your name: ");
|
||||
refresh();
|
||||
getnstr(user, USERNAME_LENGTH); // read at most
|
||||
noecho();
|
||||
}
|
||||
} else {
|
||||
printm ();
|
||||
printm();
|
||||
}
|
||||
}
|
||||
sprintf (text, "%li %d %s %d\n", time (NULL), moves, user, grid);
|
||||
fputs (text, fp);
|
||||
free (text);
|
||||
sprintf(text, "%li %d %s %d\n", time(NULL), moves, user, grid);
|
||||
fputs(text, fp);
|
||||
free(text);
|
||||
return;
|
||||
}
|
||||
|
||||
void start () {
|
||||
void start() {
|
||||
int i, j;
|
||||
for (i = 0; i < grid; i++) {
|
||||
for (j = 0; j < grid; j++) {
|
||||
matrix[i][j] = rand () % 19 + 1;
|
||||
matrix[i][j] = rand() % 19 + 1;
|
||||
}
|
||||
}
|
||||
nolow ();
|
||||
printm ();
|
||||
nolow();
|
||||
printm();
|
||||
}
|
||||
|
||||
// move pointer
|
||||
void move_ptr (int direction) {
|
||||
int xinit = my_x, yinit = my_y;
|
||||
void move_ptr(int direction) {
|
||||
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;
|
||||
|
@ -197,43 +204,43 @@ void move_ptr (int direction) {
|
|||
}
|
||||
if (del == 1)
|
||||
matrix[yinit][xinit] = 0;
|
||||
nolow ();
|
||||
moves++;
|
||||
nolow();
|
||||
if (xinit != ptr_x || yinit != ptr_y)
|
||||
moves++;
|
||||
}
|
||||
|
||||
// if 1 or 2 -> random number
|
||||
void nolow () {
|
||||
void nolow() {
|
||||
int i, j;
|
||||
for (i = 0; i < grid; i++) {
|
||||
for (j = 0; j < grid; j++) {
|
||||
if ( matrix[i][j] < 3 && matrix[i][j] > 0)
|
||||
matrix[i][j] = rand () % 10 - 10;
|
||||
matrix[i][j] = rand() % 10 - 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void settings () {
|
||||
int menu = 0;
|
||||
while (menu == 0) {
|
||||
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;
|
||||
void settings() {
|
||||
while (1) {
|
||||
clear();
|
||||
printw("> Settings\n\n");
|
||||
printw("Menu [q]\nGrid size? [3-9] ");
|
||||
refresh();
|
||||
switch (getch()) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ranking () {
|
||||
void ranking() {
|
||||
int i = 0, j;
|
||||
struct res {
|
||||
int moves;
|
||||
|
@ -246,9 +253,9 @@ void ranking () {
|
|||
char *time_string[1000];
|
||||
int grid_val = grid;
|
||||
int printed;
|
||||
rewind (fp);
|
||||
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].res_time, &result[i].moves, result[i].user, &result[i].grid);
|
||||
i++;
|
||||
} while (result[i-1].moves > 0);
|
||||
for (i = 0; result[i].moves != 0; i++) {
|
||||
|
@ -261,76 +268,72 @@ void ranking () {
|
|||
}
|
||||
}
|
||||
do {
|
||||
clear ();
|
||||
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)
|
||||
printw ("Best results:\n\n");
|
||||
clear();
|
||||
if (grid_val >= 3 && grid_val <= 9)
|
||||
printw("Best results (%dx%d matrix):\n\n", grid_val, grid_val);
|
||||
else if (grid_val == 1)
|
||||
printw("Best results:\n\n");
|
||||
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 ();
|
||||
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();
|
||||
printed++;
|
||||
}
|
||||
}
|
||||
if (printed == 0) {
|
||||
printw ("Nothing to show...\n");
|
||||
refresh ();
|
||||
printw("Nothing to show...\n");
|
||||
refresh();
|
||||
}
|
||||
printw ("\nMenu [q]; To filter by matrix size [3-9]; No filter [1] ");
|
||||
refresh ();
|
||||
printw("\nMenu [q]; To filter by matrix size [3-9]; No filter [1] ");
|
||||
refresh();
|
||||
do {
|
||||
grid_val = getch () - '0';
|
||||
grid_val = getch() - '0';
|
||||
} while ((grid_val < 3 || grid_val > 9) && grid_val != 1 && grid_val != 'q'-'0');
|
||||
} while (grid_val != ('q'-'0'));
|
||||
}
|
||||
|
||||
void rules () {
|
||||
clear ();
|
||||
printw ("> Rules\n\n");
|
||||
printw ("Delete numbers on the table by moving the pointer.\n");
|
||||
printw ("That's all you need to know\n");
|
||||
refresh ();
|
||||
getch ();
|
||||
void rules() {
|
||||
clear();
|
||||
printw("> Rules\n\n");
|
||||
printw("Delete numbers on the table by moving the pointer.\n");
|
||||
printw("That's all you need to know\n");
|
||||
refresh();
|
||||
getch();
|
||||
}
|
||||
|
||||
// print matrix
|
||||
void printm () {
|
||||
void printm() {
|
||||
int i, j;
|
||||
clear ();
|
||||
printw ("\n\n");
|
||||
clear();
|
||||
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) {
|
||||
attron (COLOR_PAIR (1));
|
||||
} else {
|
||||
printw ("> ");
|
||||
}
|
||||
for (j = 0; j < grid; j++) {
|
||||
if (i == ptr_y && j == ptr_x) {
|
||||
if (has_colors() == TRUE)
|
||||
attron(COLOR_PAIR(1));
|
||||
else
|
||||
printw("> ");
|
||||
}
|
||||
if (matrix[i][j] != 0)
|
||||
printw ("%2d", matrix[i][j]);
|
||||
printw("%2d", matrix[i][j]);
|
||||
else {
|
||||
if (has_colors () == FALSE) {
|
||||
printw (" ");
|
||||
} else {
|
||||
printw (" .");
|
||||
}
|
||||
}
|
||||
if (has_colors () == TRUE) {
|
||||
attroff (COLOR_PAIR (1));
|
||||
if (has_colors() == FALSE)
|
||||
printw(" ");
|
||||
else
|
||||
printw(" .");
|
||||
}
|
||||
if (has_colors() == TRUE)
|
||||
attroff(COLOR_PAIR(1));
|
||||
printw("\t");
|
||||
}
|
||||
printw ("\n\n");
|
||||
printw("\n\n");
|
||||
}
|
||||
refresh ();
|
||||
refresh();
|
||||
}
|
||||
|
||||
int win () {
|
||||
int win() {
|
||||
int i, j, count = 0;
|
||||
for (i = 0; i < grid; i++) {
|
||||
for (j = 0; j < grid; j++) {
|
||||
|
@ -339,13 +342,13 @@ int win () {
|
|||
}
|
||||
}
|
||||
if (count >= grid * (grid - 1)) {
|
||||
printw ("\nYou win! Moves: %d\n", moves);
|
||||
refresh ();
|
||||
getchar ();
|
||||
printw("\nYou win! Moves: %d\n", moves);
|
||||
refresh();
|
||||
getchar();
|
||||
return 1;
|
||||
} else {
|
||||
printw ("\nYou still need to delete %d numbers. Moves: %d", grid * (grid - 1) - count, moves);
|
||||
refresh ();
|
||||
printw("\nYou still need to delete %d numbers. Moves: %d", grid * (grid - 1) - count, moves);
|
||||
refresh();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue