Compare commits

..

3 Commits

Author SHA1 Message Date
Racoonicorn 140de880de Update README.md 2016-12-27 13:57:45 +01:00
Racoonicorn e4bee50b83 Update Roofus.c 2016-12-27 13:56:07 +01:00
Racoonicorn 92b233ba39 Update and rename roofus.c to Roofus.c 2016-12-26 13:05:06 +01:00
3 changed files with 279 additions and 338 deletions

View File

@ -1,21 +1,3 @@
# Roofus.c # Roofus.c (without ncurses)
Delete numbers on the table by moving the pointer. Delete numbers on the table by moving the pointer
That's all you need to know.
# How to compile
Current version requires ncures (checkout the no-ncurses branch with
`git checkout no-ncurses`
if you don't want it).
Add the path to your LD_LIBRARY_PATH and then compile with the flag -lncurses
ubuntu example:
(as root)
apt-get install ncurses-dev
(compile)
`gcc ./Roofus.c -o roofus -lncurses`

481
Roofus.c
View File

@ -1,250 +1,224 @@
#define VERSION "1.10" // Not updated version
// Version without ncurses library
#define N 9 #define N 9
#define USERNAME_LENGTH 10
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ncurses.h>
FILE *fp; void restoregrid ();
int matrix[N][N] = {0};
// structs
struct point{
int x;
int y;
} ptr;
struct res {
int moves;
int grid;
char user[USERNAME_LENGTH];
time_t playtime;
} current;
typedef struct Node {
struct res result;
struct Node* next;
} Node;
typedef Node* ptrNode;
ptrNode List;
// functions
void importdata();
void menu (); void menu ();
void play (); void play ();
void start (); void start ();
void move_ptr(int direction); void move (char *dir);
void nolow (); void nolow ();
void settings (); void settings ();
void ranking (); void ranking ();
void rules (); void rules ();
void printm (); void printm ();
int win (); int win ();
ptrNode InsertInList(ptrNode List, struct res elem); int error ();
ptrNode InsertFirst(ptrNode List, struct res elem);
int EmptyList(ptrNode List); int matrix[N][N] = {0};
int my_x;
int my_y;
int moves;
int grid;
int gridset = 0;
FILE *fp;
int main () { int main () {
initscr();
noecho();
cbreak();
keypad(stdscr, true);
if (has_colors() == TRUE) {
start_color();
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"); printf ("Error opening file");
refresh();
getch();
endwin();
return 1;
}else{ }else{
importdata(); restoregrid ();
menu (); menu ();
}
fclose (fp); fclose (fp);
endwin();
return 0; return 0;
} }
}
void importdata() { // restore last grid size
int lenght; void restoregrid () {
struct res temp; int gridtemp, length;
// import last grid size fseek(fp, 0, SEEK_END);
fseek(fp, -2, SEEK_END); length = ftell(fp);
current.grid = fgetc(fp) - '0'; fseek(fp, (length - 2), SEEK_SET); // go to grid size character
if (current.grid < 3 || current.grid > 9) gridtemp = fgetc(fp) - 48; // -48 to convert char in int
current.grid = 5; if (gridtemp < 3 || gridtemp > 9)
lenght = ftell(fp); grid = 5;
rewind(fp); else
// import results grid = gridtemp;
while (ftell(fp) < lenght) {
fscanf(fp, "%li %d %s %d", &temp.playtime, &temp.moves, temp.user, &temp.grid);
List = InsertInList(List, temp);
}
} }
void menu (){ void menu (){
char mode[5];
while (1) { while (1) {
clear(); system("clear");
printw("~~~ ROOFUS %dx%d ~~~\n\n" printf ("~~~ ROOFUS ~~~\n\n[1] Play\n[2] Ranking\n[3] Settings\n[4] Rules\n[0] Exit\n\n");
"[1] Play\n" scanf ("%5s", mode);
"[2] Ranking\n" switch (mode[0]) {
"[3] Settings\n" case '0': return;
"[4] Rules\n"
"[q] Quit\n\nv%s\n\n", current.grid, current.grid, VERSION);
refresh();
switch (getch()) {
case '1': play (); break; case '1': play (); break;
case '2': ranking (); break; case '2': ranking (); break;
case '3': settings (); break; case '3': settings (); break;
case '4': rules (); break; case '4': rules (); break;
case 'q': return;
} }
} }
} }
void play () { void play () {
int direction, end = 0, replay; char dir[5], user[10];
char exit = 0;
int end = 0;
char *text = malloc (sizeof (text) * N); char *text = malloc (sizeof (text) * N);
do { moves = 0;
replay = 0; system("clear");
current.moves = 0; my_x = grid / 2;
ptr.x = ptr.y = current.grid / 2; my_y = grid / 2;
start (); start ();
printm ();
while (end == 0) { while (end == 0) {
printw("\n[q] Quit. [r] Restart\nArrow to move "); printf ("\nLeft [a]; Up [w]; Right [d]; Down [s]. Menu [0]");
refresh(); printf ("\nDirection? ");
direction = getch(); scanf ("%5s", dir);
if (direction == 'q') { getchar ();
printw("\nQuit? [y/n] "); if (dir[0] == '0') {
refresh(); printf ("\nAre you sure? [y/n] ");
if (getch() == 'y') scanf ("%c", &exit);
break; if (exit == 'y') {
else return;
direction = 0; } else {
dir[0] = '1';
getchar ();
} }
if (direction == 'r') {
printw("\nRestart? [y/n] ");
refresh();
if (getch() == 'y') {
replay = 1;
break;
} else
direction = 0;
} }
if (direction != 0) { move (dir);
move_ptr(direction);
printm (); printm ();
end = win (); end = win ();
if (end == 1) { if (end != 0) {
current.playtime = time(NULL); printf ("Enter your name: ");
printw("Enter your name: "); scanf ("%10s", user);
refresh(); }
echo(); }
getnstr(current.user, USERNAME_LENGTH); // read at most sprintf (text, "%li %d %s %d\n", time(NULL), moves, user, grid);
noecho();
sprintf(text, "%li %d %s %d\n", current.playtime, current.moves, current.user, current.grid);
fputs (text, fp); fputs (text, fp);
List = InsertInList(List, current);
}
} else
printm();
}
} while (replay == 1);
free (text); free (text);
return; return;
} }
void start () { void start () {
for (int i = 0; i < current.grid; i++) { int i, j;
for (int j = 0; j < current.grid; 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 (); nolow ();
printm();
} }
void move (char *dir) {
int xinit = my_x, yinit = my_y;
int stop = 0, del = 0;
int i, j;
// move pointer // move pointer
void move_ptr(int direction) { switch (dir[0]) {
int xinit = ptr.x, yinit = ptr.y; case 'a':
int i, j, del = 0; if (my_x != 0) {
switch (direction) { my_x--;
case KEY_LEFT: } else {
if (ptr.x != 0) { stop = error ();
ptr.x--; }
break;
case 'd':
if (my_x < grid - 1) {
my_x++;
} else {
stop = error ();
}
break;
case 'w':
if (my_y > 0) {
my_y--;
} else {
stop = error ();
}
break;
case 's':
if(my_y < grid - 1) {
my_y++;
} else {
stop = error ();
}
break;
case 'o':
stop = 1;
break;
default:
stop = error ();
break;
}
if (stop == 0) {
// change values
if (*dir == 'd') {
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 (*dir == 'a') {
for (j = 0; j < xinit; j++) { for (j = 0; j < xinit; j++) {
if (matrix[ptr.y][j] != 0) { if (matrix[my_y][j] != 0) {
matrix[ptr.y][j] = matrix[ptr.y][j] - matrix[ptr.y][xinit]; matrix[my_y][j] = matrix[my_y][j] - matrix[my_y][xinit];
if (matrix[ptr.y][j] == 0) if (matrix[my_y][j] == 0)
del = 1; del = 1;
if (matrix[ptr.y][j] < 0 && matrix[yinit][xinit] != 0) if (matrix[my_y][j] < 0 && matrix[yinit][xinit] != 0)
matrix[ptr.y][j] = - matrix[ptr.y][j]; matrix[my_y][j] = - matrix[my_y][j];
} }
} }
} break; }
case KEY_RIGHT: if (*dir == 'w'){
if (ptr.x < current.grid - 1) { for (i = my_y; i >= 0; i--) {
ptr.x++; if (matrix[i][my_x] != 0) {
for (j = ptr.x; j < current.grid; j++) { matrix[i][my_x] = matrix[i][my_x] - matrix[yinit][xinit];
if (matrix[ptr.y][j] != 0) { if (matrix[i][my_x] == 0)
matrix[ptr.y][j] = matrix[ptr.y][j] - matrix[yinit][xinit];
if (matrix[ptr.y][j] == 0)
del = 1; del = 1;
if (matrix[ptr.y][j] < 0 && matrix[yinit][xinit] != 0) if (matrix[i][my_x] < 0 && matrix[yinit][xinit] != 0)
matrix[ptr.y][j] = - matrix[ptr.y][j]; matrix[i][my_x] = -matrix[i][my_x];
} }
} }
} break; }
case KEY_UP: if (*dir == 's'){
if (ptr.y > 0) { for (i = my_y; i < grid; i++) {
ptr.y--; if(matrix[i][my_x] != 0) {
for (i = ptr.y; i >= 0; i--) { matrix[i][my_x] = matrix[i][my_x] - matrix[yinit][xinit];
if (matrix[i][ptr.x] != 0) { if (matrix[i][my_x] == 0)
matrix[i][ptr.x] = matrix[i][ptr.x] - matrix[yinit][xinit];
if (matrix[i][ptr.x] == 0)
del = 1; del = 1;
if (matrix[i][ptr.x] < 0 && matrix[yinit][xinit] != 0) if (matrix[i][my_x] < 0 && matrix[yinit][xinit] != 0)
matrix[i][ptr.x] = - matrix[i][ptr.x]; matrix[i][my_x] = -matrix[i][my_x];
} }
} }
} break;
case KEY_DOWN:
if (ptr.y < current.grid - 1) {
ptr.y++;
for (i = ptr.y; i < current.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][ptr.x] < 0 && matrix[yinit][xinit] != 0)
matrix[i][ptr.x] = - matrix[i][ptr.x];
}
}
} break;
default: return;
} }
if (del == 1) if (del == 1)
matrix[yinit][xinit] = 0; matrix[yinit][xinit] = 0;
nolow (); nolow ();
if (xinit != ptr.x || yinit != ptr.y) moves++;
current.moves++; }
} }
// if 1 or 2 -> random number // if 1 or 2 -> random number
void nolow () { void nolow () {
for (int i = 0; i < current.grid; i++) { int i, j;
for (int j = 0; j < current.grid; j++) { for (i = 0; i < grid; i++) {
for (j = 0; j < grid; j++) {
if ( matrix[i][j] < 3 && matrix[i][j] > 0) if ( matrix[i][j] < 3 && matrix[i][j] > 0)
matrix[i][j] = rand () % 10 - 10; matrix[i][j] = rand () % 10 - 10;
} }
@ -252,125 +226,126 @@ void nolow() {
} }
void settings () { void settings () {
while (1) { char setting[5];
clear(); int menu = 0;
printw("> Settings\n\n"); system("clear");
printw("Menu [q]\nGrid size? [3-9] "); printf ("> Settings\n\n");
refresh(); while (menu == 0) {
switch (getch()) { printf ("Menu [0]\nGrid size? [3-9] ");
case 'q': return; scanf ("%5s", setting);
case '3': current.grid = 3; return; switch (setting[0]) {
case '4': current.grid = 4; return; case '0': menu = 1; break;
case '5': current.grid = 5; return; case '3': grid = 3; gridset = 1; menu = 1; break;
case '6': current.grid = 6; return; case '4': grid = 4; gridset = 1; menu = 1; break;
case '7': current.grid = 7; return; case '5': grid = 5; gridset = 1; menu = 1; break;
case '8': current.grid = 8; return; case '6': grid = 6; gridset = 1; menu = 1; break;
case '9': current.grid = 9; return; case '7': grid = 7; gridset = 1; menu = 1; break;
default: 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 () { void ranking () {
int grid_rank = current.grid; 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; int printed;
ptrNode ptrList; rewind (fp);
getchar ();
do { 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; printed = 0;
clear(); for (i = 0; result[i].moves != 0; i++) {
if (grid_rank >= 3 && grid_rank <= 9) if ((result[i].grid == grid_val || grid_val == 1) && printed < 9) {
printw("Best results (%dx%d matrix):\n\n", grid_rank, grid_rank); time_string[i] = ctime(&result[i].res_time);
else 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]);
printw("Best results:\n\n");
for (ptrList = List; ptrList != NULL; ptrList = ptrList->next) {
if ((ptrList->result.grid == grid_rank || grid_rank == 1) && printed < 9 && ptrList->result.moves != 0) {
printw("# %d - %3d moves\t- (%dx%d matrix) - %s\t- %s", printed + 1, ptrList->result.moves, ptrList->result.grid, ptrList->result.grid, ptrList->result.user, ctime(&ptrList->result.playtime));
printed++; printed++;
} }
} }
if (printed == 0) if (printed == 0) {
printw("Nothing to show...\n"); printf ("Nothing to show...\n");
printw("\nMenu [q]; To filter by matrix size [3-9]; No filter [1] "); }
refresh();
do { do {
grid_rank = getch() - '0'; printf ("\nMenu [0]; To filter by matrix size [3-9]; No filter [1] ");
} while ((grid_rank < 3 || grid_rank > 9) && grid_rank != 1 && grid_rank != 'q'-'0'); scanf ("%1d", &grid_val);
} while (grid_rank != ('q'-'0')); getchar();
} while ((grid_val < 3 || grid_val > 9) && grid_val != 1 && grid_val != 0);
} while (grid_val != 0);
} }
void rules () { void rules () {
clear(); system("clear");
printw("> Rules\n\n"); getchar ();
printw("Delete numbers on the table by moving the pointer.\n"); printf ("> Rules\n\n");
printw("That's all you need to know\n"); printf ("Delete numbers on the table by moving the pointer.\n");
refresh(); printf ("That's all you need to know\n");
getch(); getchar ();
} }
// print matrix // print matrix
void printm () { void printm () {
clear(); int i, j;
printw("\n\n"); system("clear");
for (int i = 0; i < current.grid; i++) { printf ("\n\n");
for (int j = 0; j < current.grid; j++) { for (i = 0; i < grid; i++) {
if (i == ptr.y && j == ptr.x) { for (j = 0;j < grid; j++) {
if (has_colors() == TRUE) if (i == my_y && j == my_x)
attron(COLOR_PAIR(1)); printf ("> ");
else
printw("> ");
}
if (matrix[i][j] != 0) if (matrix[i][j] != 0)
printw("%2d", matrix[i][j]); printf ("%d\t", matrix[i][j]);
else {
if (has_colors() == FALSE)
printw(" ");
else else
printw(" ."); printf (" \t");
} }
if (has_colors() == TRUE) printf ("\n\n");
attroff(COLOR_PAIR(1));
printw("\t");
} }
printw("\n\n");
}
refresh();
} }
int win () { int win () {
int count = 0; int i, j, count=0;
for (int i = 0; i < current.grid; i++) { for (i = 0; i < grid; i++) {
for (int j = 0; j < current.grid; j++) { for (j = 0; j < grid; j++) {
if (matrix[i][j] == 0) if (matrix[i][j] == 0)
count++; count++;
} }
} }
if (count >= current.grid * (current.grid - 1)) { if (count >= grid * (grid - 1)) {
printw("\nYou win! Moves: %d\n", current.moves); printf ("\nYou win! Moves: %d\n", moves);
refresh();
getchar (); getchar ();
return 1; return 1;
}else{ }else{
printw("\nYou still need to delete %d numbers. Moves: %d", current.grid * (current.grid - 1) - count, current.moves); printf ("\nYou still need to delete %d numbers. Moves: %d", grid * (grid - 1) - count, moves);
refresh();
return 0; return 0;
} }
} }
ptrNode InsertInList(ptrNode List, struct res elem) { int error () {
if (EmptyList(List) || List->result.moves >= elem.moves) printf ("ERROR!!!");
return InsertFirst(List, elem); return 1;
List->next = InsertInList(List->next, elem);
return List;
}
int EmptyList(ptrNode List) {
return List == NULL;
}
ptrNode InsertFirst(ptrNode List, struct res elem) {
ptrNode FirstNode;
FirstNode = malloc(sizeof(Node));
FirstNode->result = elem;
FirstNode->next = List;
return FirstNode;
} }

View File

@ -1,16 +0,0 @@
with import <nixpkgs> {}; {
Roofus = stdenv.mkDerivation {
name = "Roofus";
buildInputs = [
ncurses
gcc
];
shellHook = ''
unset http_proxy
export LD_LIBRARY_PATH=${ncurses}
'';
};
}