Code cleanup
This commit is contained in:
parent
7100764299
commit
6627896dba
222
Roofus.c
222
Roofus.c
|
@ -1,4 +1,4 @@
|
||||||
#define VERSION "1.02"
|
#define VERSION "1.03"
|
||||||
|
|
||||||
#define N 9
|
#define N 9
|
||||||
#define USERNAME_LENGTH 10
|
#define USERNAME_LENGTH 10
|
||||||
|
@ -7,7 +7,6 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
|
||||||
void restoregrid ();
|
void restoregrid ();
|
||||||
|
@ -31,54 +30,48 @@ int gridset = 0;
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
|
||||||
int main () {
|
int main () {
|
||||||
initscr();
|
initscr ();
|
||||||
noecho();
|
noecho ();
|
||||||
cbreak();
|
cbreak ();
|
||||||
keypad(stdscr, true);
|
keypad (stdscr, true);
|
||||||
|
|
||||||
if (has_colors() == TRUE) {
|
if (has_colors () == TRUE) {
|
||||||
start_color();
|
start_color ();
|
||||||
init_pair(1, COLOR_BLACK, COLOR_WHITE);
|
init_pair (1, COLOR_BLACK, COLOR_WHITE);
|
||||||
}
|
}
|
||||||
srand (time(NULL));
|
srand (time (NULL));
|
||||||
fp = fopen ("roofus.txt", "a+");
|
fp = fopen ("roofus.txt", "a+");
|
||||||
if (fp == NULL) {
|
if (fp == NULL) {
|
||||||
printw ("Error opening file");
|
printw ("Error opening file");
|
||||||
refresh();
|
refresh ();
|
||||||
}else{
|
} else {
|
||||||
restoregrid ();
|
restoregrid ();
|
||||||
menu ();
|
menu ();
|
||||||
}
|
}
|
||||||
fclose (fp);
|
fclose (fp);
|
||||||
printw ("\n\n\n\nRoofus v%s", VERSION);
|
endwin ();
|
||||||
refresh ();
|
|
||||||
endwin();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// restore last grid size
|
// restore last grid size
|
||||||
void restoregrid () {
|
void restoregrid () {
|
||||||
int gridtemp, length;
|
int gridtemp, length;
|
||||||
fseek(fp, 0, SEEK_END);
|
fseek (fp, 0, SEEK_END);
|
||||||
length = ftell(fp);
|
length = ftell (fp);
|
||||||
fseek(fp, (length - 2), SEEK_SET); // go to grid size character
|
fseek (fp, (length - 2), SEEK_SET); // go to grid size character
|
||||||
gridtemp = fgetc(fp) - 48; // -48 to convert char in int
|
gridtemp = fgetc (fp) - '0'; // convert char to int
|
||||||
if (gridtemp < 3 || gridtemp > 9)
|
if (gridtemp < 3 || gridtemp > 9)
|
||||||
grid = 5;
|
grid = 5;
|
||||||
else
|
else
|
||||||
grid = gridtemp;
|
grid = gridtemp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void menu (){
|
void menu () {
|
||||||
int mode;
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
clear();
|
clear ();
|
||||||
printw ("~~~ ROOFUS ~~~\n\n[1] Play\n[2] Ranking\n[3] Settings\n[4] Rules\n[q] Quit\n\n");
|
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();
|
refresh ();
|
||||||
mode = getch();
|
switch (getch ()) {
|
||||||
|
|
||||||
switch (mode) {
|
|
||||||
case 'q': return;
|
case 'q': return;
|
||||||
case '1': play (); break;
|
case '1': play (); break;
|
||||||
case '2': ranking (); break;
|
case '2': ranking (); break;
|
||||||
|
@ -86,13 +79,11 @@ void menu (){
|
||||||
case '4': rules (); break;
|
case '4': rules (); break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* return 0; */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void play () {
|
void play () {
|
||||||
char user[USERNAME_LENGTH];
|
char user[USERNAME_LENGTH];
|
||||||
int direction;
|
int direction;
|
||||||
int exit = 0;
|
|
||||||
int end = 0;
|
int end = 0;
|
||||||
char *text = malloc (sizeof (text) * N);
|
char *text = malloc (sizeof (text) * N);
|
||||||
moves = 0;
|
moves = 0;
|
||||||
|
@ -100,36 +91,35 @@ void play () {
|
||||||
my_x = grid / 2;
|
my_x = grid / 2;
|
||||||
my_y = grid / 2;
|
my_y = grid / 2;
|
||||||
start ();
|
start ();
|
||||||
|
|
||||||
while (end == 0) {
|
while (end == 0) {
|
||||||
printw ("\nArrow to move, q to quit\n");
|
printw ("\nArrow to move, q to quit\n");
|
||||||
refresh();
|
refresh();
|
||||||
|
direction = getch ();
|
||||||
direction = getch();
|
|
||||||
if (direction == 'q') {
|
if (direction == 'q') {
|
||||||
printw ("\nAre you sure? [y/n] ");
|
printw ("\nAre you sure? [y/n] ");
|
||||||
refresh();
|
refresh ();
|
||||||
exit = getch();
|
if (getch () == 'y') {
|
||||||
if (exit == 'y') {
|
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
direction = '1';
|
direction = 0;
|
||||||
getchar ();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (direction != 0) {
|
||||||
move_ptr (direction);
|
move_ptr (direction);
|
||||||
printm ();
|
printm ();
|
||||||
end = win ();
|
end = win ();
|
||||||
if (end != 0) {
|
if (end != 0) {
|
||||||
echo();
|
echo ();
|
||||||
printw ("Enter your name: ");
|
printw ("Enter your name: ");
|
||||||
refresh();
|
refresh ();
|
||||||
getnstr(user, USERNAME_LENGTH); // read at most
|
getnstr (user, USERNAME_LENGTH); // read at most
|
||||||
|
noecho ();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
printm ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sprintf (text, "%li %d %s %d\n", time(NULL), moves, user, grid);
|
sprintf (text, "%li %d %s %d\n", time (NULL), moves, user, grid);
|
||||||
refresh();
|
|
||||||
fputs (text, fp);
|
fputs (text, fp);
|
||||||
free (text);
|
free (text);
|
||||||
return;
|
return;
|
||||||
|
@ -143,61 +133,17 @@ void start () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
nolow ();
|
nolow ();
|
||||||
printm();
|
printm ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// move pointer
|
||||||
void move_ptr (int direction) {
|
void move_ptr (int direction) {
|
||||||
int xinit = my_x, yinit = my_y;
|
int xinit = my_x, yinit = my_y;
|
||||||
int stop = 0, del = 0;
|
int i, j, del = 0;
|
||||||
int i, j;
|
|
||||||
// move_ptr pointer
|
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case KEY_LEFT:
|
case KEY_LEFT:
|
||||||
if (my_x != 0) {
|
if (my_x != 0) {
|
||||||
my_x--;
|
my_x--;
|
||||||
} else {
|
|
||||||
stop = 1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case KEY_RIGHT:
|
|
||||||
if (my_x < grid - 1) {
|
|
||||||
my_x++;
|
|
||||||
} else {
|
|
||||||
stop = 1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case KEY_UP:
|
|
||||||
if (my_y > 0) {
|
|
||||||
my_y--;
|
|
||||||
} else {
|
|
||||||
stop = 1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case KEY_DOWN:
|
|
||||||
if(my_y < grid - 1) {
|
|
||||||
my_y++;
|
|
||||||
} else {
|
|
||||||
stop = 1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
stop = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (stop == 0) {
|
|
||||||
// change values
|
|
||||||
if (direction == KEY_RIGHT) {
|
|
||||||
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)
|
|
||||||
del = 1;
|
|
||||||
if (matrix[my_y][j] < 0 && matrix[yinit][xinit] != 0)
|
|
||||||
matrix[my_y][j] = -matrix[my_y][j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (direction == KEY_LEFT) {
|
|
||||||
for (j = 0; j < xinit; j++) {
|
for (j = 0; j < xinit; j++) {
|
||||||
if (matrix[my_y][j] != 0) {
|
if (matrix[my_y][j] != 0) {
|
||||||
matrix[my_y][j] = matrix[my_y][j] - matrix[my_y][xinit];
|
matrix[my_y][j] = matrix[my_y][j] - matrix[my_y][xinit];
|
||||||
|
@ -207,8 +153,23 @@ void move_ptr (int direction) {
|
||||||
matrix[my_y][j] = - matrix[my_y][j];
|
matrix[my_y][j] = - matrix[my_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)
|
||||||
|
del = 1;
|
||||||
|
if (matrix[my_y][j] < 0 && matrix[yinit][xinit] != 0)
|
||||||
|
matrix[my_y][j] = -matrix[my_y][j];
|
||||||
}
|
}
|
||||||
if (direction == KEY_UP){
|
}
|
||||||
|
} break;
|
||||||
|
case KEY_UP:
|
||||||
|
if (my_y > 0) {
|
||||||
|
my_y--;
|
||||||
for (i = my_y; i >= 0; i--) {
|
for (i = my_y; i >= 0; i--) {
|
||||||
if (matrix[i][my_x] != 0) {
|
if (matrix[i][my_x] != 0) {
|
||||||
matrix[i][my_x] = matrix[i][my_x] - matrix[yinit][xinit];
|
matrix[i][my_x] = matrix[i][my_x] - matrix[yinit][xinit];
|
||||||
|
@ -218,8 +179,10 @@ void move_ptr (int direction) {
|
||||||
matrix[i][my_x] = -matrix[i][my_x];
|
matrix[i][my_x] = -matrix[i][my_x];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} break;
|
||||||
if (direction == KEY_DOWN){
|
case KEY_DOWN:
|
||||||
|
if (my_y < grid - 1) {
|
||||||
|
my_y++;
|
||||||
for (i = my_y; i < grid; i++) {
|
for (i = my_y; i < grid; i++) {
|
||||||
if(matrix[i][my_x] != 0) {
|
if(matrix[i][my_x] != 0) {
|
||||||
matrix[i][my_x] = matrix[i][my_x] - matrix[yinit][xinit];
|
matrix[i][my_x] = matrix[i][my_x] - matrix[yinit][xinit];
|
||||||
|
@ -229,12 +192,13 @@ void move_ptr (int direction) {
|
||||||
matrix[i][my_x] = -matrix[i][my_x];
|
matrix[i][my_x] = -matrix[i][my_x];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} break;
|
||||||
|
default: return;
|
||||||
}
|
}
|
||||||
if (del == 1)
|
if (del == 1)
|
||||||
matrix[yinit][xinit] = 0;
|
matrix[yinit][xinit] = 0;
|
||||||
nolow ();
|
nolow ();
|
||||||
moves++;
|
moves++;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if 1 or 2 -> random number
|
// if 1 or 2 -> random number
|
||||||
|
@ -249,15 +213,13 @@ void nolow () {
|
||||||
}
|
}
|
||||||
|
|
||||||
void settings () {
|
void settings () {
|
||||||
int setting;
|
|
||||||
int menu = 0;
|
int menu = 0;
|
||||||
clear();
|
|
||||||
printw ("> Settings\n\n");
|
|
||||||
while (menu == 0) {
|
while (menu == 0) {
|
||||||
printw ("Menu [q]\nGrid size? [3-9]");
|
clear ();
|
||||||
refresh();
|
printw ("> Settings\n\n");
|
||||||
setting = getch();
|
printw ("Menu [q]\nGrid size? [3-9] ");
|
||||||
switch (setting) {
|
refresh ();
|
||||||
|
switch (getch ()) {
|
||||||
case 'q': menu = 1; break;
|
case 'q': menu = 1; break;
|
||||||
case '3': grid = 3; gridset = 1; menu = 1; break;
|
case '3': grid = 3; gridset = 1; menu = 1; break;
|
||||||
case '4': grid = 4; gridset = 1; menu = 1; break;
|
case '4': grid = 4; gridset = 1; menu = 1; break;
|
||||||
|
@ -266,8 +228,7 @@ void settings () {
|
||||||
case '7': grid = 7; gridset = 1; menu = 1; break;
|
case '7': grid = 7; gridset = 1; menu = 1; break;
|
||||||
case '8': grid = 8; gridset = 1; menu = 1; break;
|
case '8': grid = 8; gridset = 1; menu = 1; break;
|
||||||
case '9': grid = 9; gridset = 1; menu = 1; break;
|
case '9': grid = 9; gridset = 1; menu = 1; break;
|
||||||
default: printw("Retry\n"); break;
|
default: break;
|
||||||
refresh();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -300,96 +261,91 @@ void ranking () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
clear();
|
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);
|
printw ("Best results (%dx%d matrix):\n\n", grid_val, grid_val);
|
||||||
refresh();
|
refresh ();
|
||||||
}
|
} else if (grid_val == 1)
|
||||||
else if (grid_val == 1)
|
|
||||||
printw ("Best results:\n\n");
|
printw ("Best results:\n\n");
|
||||||
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);
|
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]);
|
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();
|
refresh ();
|
||||||
printed++;
|
printed++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (printed == 0) {
|
if (printed == 0) {
|
||||||
printw ("Nothing to show...\n");
|
printw ("Nothing to show...\n");
|
||||||
refresh();
|
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 {
|
||||||
grid_val = getch() - '0';
|
grid_val = getch () - '0';
|
||||||
// exit with 'q' - '0'
|
|
||||||
} while ((grid_val < 3 || grid_val > 9) && grid_val != 1 && grid_val != 'q'-'0');
|
} while ((grid_val < 3 || grid_val > 9) && grid_val != 1 && grid_val != 'q'-'0');
|
||||||
} while (grid_val != ('q'-'0'));
|
} while (grid_val != ('q'-'0'));
|
||||||
}
|
}
|
||||||
|
|
||||||
void rules () {
|
void rules () {
|
||||||
clear();
|
clear ();
|
||||||
printw ("> Rules\n\n");
|
printw ("> Rules\n\n");
|
||||||
printw ("Delete numbers on the table by moving the pointer.\n");
|
printw ("Delete numbers on the table by moving the pointer.\n");
|
||||||
printw ("That's all you need to know\n");
|
printw ("That's all you need to know\n");
|
||||||
refresh();
|
refresh ();
|
||||||
getch();
|
getch ();
|
||||||
}
|
}
|
||||||
|
|
||||||
// print matrix
|
// print matrix
|
||||||
void printm () {
|
void printm () {
|
||||||
int i, j;
|
int i, j;
|
||||||
clear();
|
clear ();
|
||||||
printw ("\n\n");
|
printw ("\n\n");
|
||||||
for (i = 0; i < grid; i++) {
|
for (i = 0; i < grid; i++) {
|
||||||
for (j = 0;j < grid; j++) {
|
for (j = 0;j < grid; j++) {
|
||||||
if (i == my_y && j == my_x) {
|
if (i == my_y && j == my_x) {
|
||||||
if (has_colors() == TRUE) {
|
if (has_colors () == TRUE) {
|
||||||
attron(COLOR_PAIR(1));
|
attron (COLOR_PAIR (1));
|
||||||
} else {
|
} else {
|
||||||
printw ("> ");
|
printw ("> ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (matrix[i][j] != 0)
|
if (matrix[i][j] != 0)
|
||||||
printw ("%d", matrix[i][j]);
|
printw ("%2d", matrix[i][j]);
|
||||||
else {
|
else {
|
||||||
if (has_colors() == FALSE) {
|
if (has_colors () == FALSE) {
|
||||||
printw (" ");
|
printw (" ");
|
||||||
} else {
|
} else {
|
||||||
printw(".");
|
printw (" .");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (has_colors() == TRUE) {
|
if (has_colors () == TRUE) {
|
||||||
attroff(COLOR_PAIR(1));
|
attroff (COLOR_PAIR (1));
|
||||||
}
|
}
|
||||||
printw("\t");
|
printw("\t");
|
||||||
}
|
}
|
||||||
printw ("\n\n");
|
printw ("\n\n");
|
||||||
}
|
}
|
||||||
refresh();
|
refresh ();
|
||||||
}
|
}
|
||||||
|
|
||||||
int win () {
|
int win () {
|
||||||
int i, j, count=0;
|
int i, j, count = 0;
|
||||||
for (i = 0; i < grid; i++) {
|
for (i = 0; i < grid; i++) {
|
||||||
for (j = 0; j < grid; j++) {
|
for (j = 0; j < grid; j++) {
|
||||||
if (matrix[i][j] == 0)
|
if (matrix[i][j] == 0)
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count >= grid * (grid - 1)) {
|
if (count >= grid * (grid - 1)) {
|
||||||
printw ("\nYou win! Moves: %d\n", moves);
|
printw ("\nYou win! Moves: %d\n", moves);
|
||||||
refresh ();
|
refresh ();
|
||||||
getchar ();
|
getchar ();
|
||||||
return 1;
|
return 1;
|
||||||
}else{
|
} else {
|
||||||
printw ("\nYou still need to delete %d numbers. Moves: %d", grid * (grid - 1) - count, moves);
|
printw ("\nYou still need to delete %d numbers. Moves: %d", grid * (grid - 1) - count, moves);
|
||||||
refresh();
|
refresh ();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue