Update and rename roofus.c to Roofus.c

This commit is contained in:
Racoonicorn 2016-12-26 13:05:06 +01:00 committed by Racoonicorn
parent 4187233457
commit f61bb0e60f
1 changed files with 119 additions and 98 deletions

View File

@ -1,4 +1,4 @@
#define VERSION "0.23" #define VERSION "0.30"
#define N 9 #define N 9
#include <stdio.h> #include <stdio.h>
@ -6,14 +6,16 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
void restoregrid ();
void menu (); void menu ();
int play (); void play ();
void start (); void start ();
void settings ();
void printm ();
void move (char *dir); void move (char *dir);
void nolow (); void nolow ();
void settings ();
void ranking (); void ranking ();
void rules ();
void printm ();
int win (); int win ();
int error (); int error ();
@ -21,7 +23,8 @@ int matrix[N][N] = {0};
int my_x; int my_x;
int my_y; int my_y;
int moves; int moves;
int grid = 5; int grid;
int gridset = 0;
FILE *fp; FILE *fp;
int main () { int main () {
@ -30,6 +33,7 @@ int main () {
if (fp == NULL) { if (fp == NULL) {
printf ("Error opening file"); printf ("Error opening file");
}else{ }else{
restoregrid ();
menu (); menu ();
} }
fclose (fp); fclose (fp);
@ -37,119 +41,56 @@ int main () {
return 0; 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
gridtemp = fgetc(fp) - 48; // -48 to convert char in int
if (gridtemp < 3 || gridtemp > 9)
grid = 5;
else
grid = gridtemp;
}
void menu (){ void menu (){
char mode[5]; char mode[5];
int menu = 1; while (1) {
while (menu == 1) {
system("clear"); system("clear");
printf ("Select mode:\n> 0: Play\n> 1: Settings\n> 2: Ranking\n> 3: Exit\n"); printf ("~~~ ROOFUS ~~~\n\n[1] Play\n[2] Ranking\n[3] Settings\n[4] Rules\n[0] Exit\n\n");
scanf ("%5s", mode); scanf ("%5s", mode);
switch (mode[0]) { switch (mode[0]) {
case '0': menu = play (); break; case '0': return;
case '1': settings (); break; case '1': play (); break;
case '2': ranking (); break; case '2': ranking (); break;
case '3': return; case '3': settings (); break;
case '4': rules (); break;
} }
} }
} }
void settings () { void play () {
char setting[5];
int menu = 0;
system("clear");
while (menu == 0) {
printf ("Menu: press 0\nGrid size? 3, 5, 7 or 9? ");
scanf ("%5s", setting);
switch (setting[0]) {
case '0': menu = 1; break;
case '3': grid = 3; menu = 1; break;
case '5': grid = 5; menu = 1; break;
case '7': grid = 7; menu = 1; break;
case '9': grid = 9; menu = 1; break;
default: printf("Retry\n"); break;
}
}
}
void ranking () {
int i = 0, j;
struct res {
int moves;
int grid;
char user[10];
time_t res_time;
};
struct res result[1000];
struct res temp;
char *time_string[1000];
int grid_val = 1;
int printed;
rewind (fp);
getchar ();
do {
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++) {
for (j = i + 1; result[j].moves != 0; j++) {
if (result[i].moves > result[j].moves) {
temp = result[i];
result[i] = result[j];
result[j] = temp;
}
}
}
do {
system("clear");
printf ("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);
printf ("# %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]);
printed++;
}
}
if (printed == 0) {
printf ("Nothing to show...\n");
}
printf ("\n0: Menu; 3, 5, 7 or 9 to filter by matrix size; 1: No filter ");
scanf ("%d", &grid_val);
getchar();
} while (grid_val != 0);
}
int play () {
char dir[5], user[10]; char dir[5], user[10];
char exit = 0; char exit = 0;
int end = 0; int end = 0;
char *text = malloc (sizeof (text) * N); char *text = malloc (sizeof (text) * N);
my_x = grid / 2;
my_y = grid / 2;
moves = 0; moves = 0;
system("clear"); system("clear");
my_x = grid / 2;
my_y = grid / 2;
start (); start ();
printm (); printm ();
while (end == 0) { while (end == 0) {
printf ("\nLeft: a. Up: w. Right: d. Down: s. Menu: m. Exit: 0"); printf ("\nLeft [a]; Up [w]; Right [d]; Down [s]. Menu [0]");
printf ("\nDirection? "); printf ("\nDirection? ");
scanf ("%5s", dir); scanf ("%5s", dir);
getchar (); getchar ();
if (dir[0] == '0') { if (dir[0] == '0') {
printf ("\nExit? [y/n] ");
scanf ("%c", &exit);
if (exit == 'y') {
return 0;
} else {
dir[0] = '1';
getchar ();
}
}
if (dir[0] == 'm') {
printf ("\nAre you sure? [y/n] "); printf ("\nAre you sure? [y/n] ");
scanf ("%c", &exit); scanf ("%c", &exit);
if (exit == 'y') { if (exit == 'y') {
return 1; return;
} else { } else {
dir[0] = '1'; dir[0] = '1';
getchar (); getchar ();
@ -166,7 +107,7 @@ int play () {
sprintf (text, "%li %d %s %d\n", time(NULL), moves, user, grid); sprintf (text, "%li %d %s %d\n", time(NULL), moves, user, grid);
fputs (text, fp); fputs (text, fp);
free (text); free (text);
return 1; return;
} }
void start () { void start () {
@ -180,13 +121,9 @@ void start () {
} }
void move (char *dir) { void move (char *dir) {
int xinit, yinit; int xinit = my_x, yinit = my_y;
int stop = 0, del = 0; int stop = 0, del = 0;
int i, j; int i, j;
xinit = my_x;
yinit = my_y;
// move pointer // move pointer
switch (dir[0]) { switch (dir[0]) {
case 'a': case 'a':
@ -288,6 +225,90 @@ void nolow () {
} }
} }
void settings () {
char setting[5];
int menu = 0;
system("clear");
printf ("> Settings\n\n");
while (menu == 0) {
printf ("Menu [0]\nGrid size? [3-9] ");
scanf ("%5s", setting);
switch (setting[0]) {
case '0': 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;
default: printf("Retry\n"); break;
}
}
}
void ranking () {
int i = 0, j;
struct res {
int moves;
int grid;
char user[10];
time_t res_time;
};
struct res result[1000];
struct res temp;
char *time_string[1000];
int grid_val = grid;
int printed;
rewind (fp);
getchar ();
do {
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++) {
for (j = i + 1; result[j].moves != 0; j++) {
if (result[i].moves > result[j].moves) {
temp = result[i];
result[i] = result[j];
result[j] = temp;
}
}
}
do {
system("clear");
if (grid_val >= 3 && grid_val <= 9)
printf ("Best results (%dx%d matrix):\n\n", grid_val, grid_val);
else if (grid_val == 1)
printf ("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);
printf ("# %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]);
printed++;
}
}
if (printed == 0) {
printf ("Nothing to show...\n");
}
do {
printf ("\nMenu [0]; To filter by matrix size [3-9]; No filter [1] ");
scanf ("%1d", &grid_val);
getchar();
} while ((grid_val < 3 || grid_val > 9) && grid_val != 1 && grid_val != 0);
} while (grid_val != 0);
}
void rules () {
system("clear");
getchar ();
printf ("> Rules\n\n");
printf ("Delete numbers on the table by moving the pointer.\n");
printf ("That's all you need to know\n");
getchar ();
}
// print matrix // print matrix
void printm () { void printm () {
int i, j; int i, j;