Code cleanup

This commit is contained in:
Racoonicorn 2016-12-27 14:00:59 +01:00 committed by GitHub
parent 7100764299
commit 6627896dba
1 changed files with 144 additions and 188 deletions

126
Roofus.c
View File

@ -1,4 +1,4 @@
#define VERSION "1.02"
#define VERSION "1.03"
#define N 9
#define USERNAME_LENGTH 10
@ -7,7 +7,6 @@
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
void restoregrid ();
@ -50,8 +49,6 @@ int main () {
menu ();
}
fclose (fp);
printw ("\n\n\n\nRoofus v%s", VERSION);
refresh ();
endwin ();
return 0;
}
@ -62,7 +59,7 @@ void restoregrid () {
fseek (fp, 0, SEEK_END);
length = ftell (fp);
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)
grid = 5;
else
@ -70,15 +67,11 @@ void restoregrid () {
}
void menu () {
int mode;
while (1) {
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 ();
mode = getch();
switch (mode) {
switch (getch ()) {
case 'q': return;
case '1': play (); break;
case '2': ranking (); break;
@ -86,13 +79,11 @@ void menu (){
case '4': rules (); break;
}
}
/* return 0; */
}
void play () {
char user[USERNAME_LENGTH];
int direction;
int exit = 0;
int end = 0;
char *text = malloc (sizeof (text) * N);
moves = 0;
@ -100,24 +91,20 @@ void play () {
my_x = grid / 2;
my_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] ");
refresh ();
exit = getch();
if (exit == 'y') {
if (getch () == 'y') {
return;
} else {
direction = '1';
getchar ();
direction = 0;
}
}
if (direction != 0) {
move_ptr (direction);
printm ();
end = win ();
@ -126,10 +113,13 @@ void play () {
printw ("Enter your name: ");
refresh ();
getnstr (user, USERNAME_LENGTH); // read at most
noecho ();
}
} else {
printm ();
}
}
sprintf (text, "%li %d %s %d\n", time (NULL), moves, user, grid);
refresh();
fputs (text, fp);
free (text);
return;
@ -146,58 +136,14 @@ void start () {
printm ();
}
// move pointer
void move_ptr (int direction) {
int xinit = my_x, yinit = my_y;
int stop = 0, del = 0;
int i, j;
// move_ptr pointer
int i, j, del = 0;
switch (direction) {
case KEY_LEFT:
if (my_x != 0) {
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++) {
if (matrix[my_y][j] != 0) {
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];
}
}
} 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--) {
if (matrix[i][my_x] != 0) {
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];
}
}
}
if (direction == KEY_DOWN){
} 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];
@ -229,13 +192,14 @@ void move_ptr (int direction) {
matrix[i][my_x] = -matrix[i][my_x];
}
}
} break;
default: return;
}
if (del == 1)
matrix[yinit][xinit] = 0;
nolow ();
moves++;
}
}
// if 1 or 2 -> random number
void nolow () {
@ -249,15 +213,13 @@ void nolow () {
}
void settings () {
int setting;
int menu = 0;
while (menu == 0) {
clear ();
printw ("> Settings\n\n");
while (menu == 0) {
printw ("Menu [q]\nGrid size? [3-9] ");
refresh ();
setting = getch();
switch (setting) {
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;
@ -266,8 +228,7 @@ void settings () {
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;
default: printw("Retry\n"); break;
refresh();
default: break;
}
}
}
@ -301,12 +262,10 @@ 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++) {
@ -323,10 +282,8 @@ void ranking () {
}
printw ("\nMenu [q]; To filter by matrix size [3-9]; No filter [1] ");
refresh ();
do {
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 != ('q'-'0'));
}
@ -355,7 +312,7 @@ void printm () {
}
}
if (matrix[i][j] != 0)
printw ("%d", matrix[i][j]);
printw ("%2d", matrix[i][j]);
else {
if (has_colors () == FALSE) {
printw (" ");
@ -381,7 +338,6 @@ int win () {
count++;
}
}
if (count >= grid * (grid - 1)) {
printw ("\nYou win! Moves: %d\n", moves);
refresh ();