Compare commits

...

11 Commits

Author SHA1 Message Date
Racoonicorn 55d0f73cf1 Linked lists 2017-01-11 23:03:03 +01:00
Racoonicorn 4d705243a9 Code cleanup 2017-01-10 22:03:28 +01:00
Racoonicorn 4ca3513991 Bugfixes and code cleanup 2016-12-28 21:19:25 +01:00
Racoonicorn 6627896dba Code cleanup 2016-12-27 14:00:59 +01:00
Racoonicorn 7100764299 Update README.md 2016-12-27 13:59:04 +01:00
Racoonicorn a6b1e1454c Merge pull request #3 from nico202/master
Colored selection (if terminal supports it)
2016-12-26 18:30:46 +01:00
Nicolò Balzarotti 188261ceaa Colored selection (if terminal supports it) 2016-12-26 18:25:16 +01:00
Nicolò Balzarotti 9632d192df fix: ncurses init in main 2016-12-26 16:42:59 +01:00
Nicolò Balzarotti c8146c4273 nix: add default.nix (to compile with nix-shell) 2016-12-26 16:42:28 +01:00
Nicolò Balzarotti e2e4ed8f93 Port to ncurses 2016-12-26 16:42:18 +01:00
Racoonicorn f61bb0e60f Update and rename roofus.c to Roofus.c 2016-12-26 16:38:52 +01:00
4 changed files with 411 additions and 331 deletions

View File

@ -1,3 +1,21 @@
# Roofus.c
Clear the table
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`

376
Roofus.c Normal file
View File

@ -0,0 +1,376 @@
#define VERSION "1.10"
#define N 9
#define USERNAME_LENGTH 10
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
FILE *fp;
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 play();
void start();
void move_ptr(int direction);
void nolow();
void settings();
void ranking();
void rules();
void printm();
int win();
ptrNode InsertInList(ptrNode List, struct res elem);
ptrNode InsertFirst(ptrNode List, struct res elem);
int EmptyList(ptrNode List);
int main() {
initscr();
noecho();
cbreak();
keypad(stdscr, true);
if (has_colors() == TRUE) {
start_color();
init_pair(1, COLOR_BLACK, COLOR_WHITE);
}
srand(time(NULL));
fp = fopen("roofus.txt", "a+");
if (fp == NULL) {
printw("Error opening file");
refresh();
getch();
endwin();
return 1;
} else {
importdata();
menu();
fclose(fp);
endwin();
return 0;
}
}
void importdata() {
int lenght;
struct res temp;
// import last grid size
fseek(fp, -2, SEEK_END);
current.grid = fgetc(fp) - '0';
if (current.grid < 3 || current.grid > 9)
current.grid = 5;
lenght = ftell(fp);
rewind(fp);
// import results
while (ftell(fp) < lenght) {
fscanf(fp, "%li %d %s %d", &temp.playtime, &temp.moves, temp.user, &temp.grid);
List = InsertInList(List, temp);
}
}
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", current.grid, current.grid, VERSION);
refresh();
switch (getch()) {
case '1': play(); break;
case '2': ranking(); break;
case '3': settings(); break;
case '4': rules(); break;
case 'q': return;
}
}
}
void play() {
int direction, end = 0, replay;
char *text = malloc(sizeof(text) * N);
do {
replay = 0;
current.moves = 0;
ptr.x = ptr.y = current.grid / 2;
start();
while (end == 0) {
printw("\n[q] Quit. [r] Restart\nArrow to move ");
refresh();
direction = getch();
if (direction == 'q') {
printw("\nQuit? [y/n] ");
refresh();
if (getch() == 'y')
break;
else
direction = 0;
}
if (direction == 'r') {
printw("\nRestart? [y/n] ");
refresh();
if (getch() == 'y') {
replay = 1;
break;
} else
direction = 0;
}
if (direction != 0) {
move_ptr(direction);
printm();
end = win();
if (end == 1) {
current.playtime = time(NULL);
printw("Enter your name: ");
refresh();
echo();
getnstr(current.user, USERNAME_LENGTH); // read at most
noecho();
sprintf(text, "%li %d %s %d\n", current.playtime, current.moves, current.user, current.grid);
fputs(text, fp);
List = InsertInList(List, current);
}
} else
printm();
}
} while (replay == 1);
free(text);
return;
}
void start() {
for (int i = 0; i < current.grid; i++) {
for (int j = 0; j < current.grid; j++) {
matrix[i][j] = rand() % 19 + 1;
}
}
nolow();
printm();
}
// move pointer
void move_ptr(int direction) {
int xinit = ptr.x, yinit = ptr.y;
int i, j, del = 0;
switch (direction) {
case KEY_LEFT:
if (ptr.x != 0) {
ptr.x--;
for (j = 0; j < xinit; j++) {
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[ptr.y][j] < 0 && matrix[yinit][xinit] != 0)
matrix[ptr.y][j] = - matrix[ptr.y][j];
}
}
} break;
case KEY_RIGHT:
if (ptr.x < current.grid - 1) {
ptr.x++;
for (j = ptr.x; j < current.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[ptr.y][j] < 0 && matrix[yinit][xinit] != 0)
matrix[ptr.y][j] = - matrix[ptr.y][j];
}
}
} break;
case KEY_UP:
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][ptr.x] < 0 && matrix[yinit][xinit] != 0)
matrix[i][ptr.x] = - matrix[i][ptr.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)
matrix[yinit][xinit] = 0;
nolow();
if (xinit != ptr.x || yinit != ptr.y)
current.moves++;
}
// if 1 or 2 -> random number
void nolow() {
for (int i = 0; i < current.grid; i++) {
for (int j = 0; j < current.grid; j++) {
if ( matrix[i][j] < 3 && matrix[i][j] > 0)
matrix[i][j] = rand() % 10 - 10;
}
}
}
void settings() {
while (1) {
clear();
printw("> Settings\n\n");
printw("Menu [q]\nGrid size? [3-9] ");
refresh();
switch (getch()) {
case 'q': return;
case '3': current.grid = 3; return;
case '4': current.grid = 4; return;
case '5': current.grid = 5; return;
case '6': current.grid = 6; return;
case '7': current.grid = 7; return;
case '8': current.grid = 8; return;
case '9': current.grid = 9; return;
default: break;
}
}
}
void ranking() {
int grid_rank = current.grid;
int printed;
ptrNode ptrList;
do {
printed = 0;
clear();
if (grid_rank >= 3 && grid_rank <= 9)
printw("Best results (%dx%d matrix):\n\n", grid_rank, grid_rank);
else
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++;
}
}
if (printed == 0)
printw("Nothing to show...\n");
printw("\nMenu [q]; To filter by matrix size [3-9]; No filter [1] ");
refresh();
do {
grid_rank = getch() - '0';
} while ((grid_rank < 3 || grid_rank > 9) && grid_rank != 1 && grid_rank != 'q'-'0');
} while (grid_rank != ('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();
}
// print matrix
void printm() {
clear();
printw("\n\n");
for (int i = 0; i < current.grid; i++) {
for (int j = 0; j < current.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]);
else {
if (has_colors() == FALSE)
printw(" ");
else
printw(" .");
}
if (has_colors() == TRUE)
attroff(COLOR_PAIR(1));
printw("\t");
}
printw("\n\n");
}
refresh();
}
int win() {
int count = 0;
for (int i = 0; i < current.grid; i++) {
for (int j = 0; j < current.grid; j++) {
if (matrix[i][j] == 0)
count++;
}
}
if (count >= current.grid * (current.grid - 1)) {
printw("\nYou win! Moves: %d\n", current.moves);
refresh();
getchar();
return 1;
} else {
printw("\nYou still need to delete %d numbers. Moves: %d", current.grid * (current.grid - 1) - count, current.moves);
refresh();
return 0;
}
}
ptrNode InsertInList(ptrNode List, struct res elem) {
if (EmptyList(List) || List->result.moves >= elem.moves)
return InsertFirst(List, elem);
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;
}

16
default.nix Normal file
View File

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

330
roofus.c
View File

@ -1,330 +0,0 @@
#define VERSION "0.23"
#define N 9
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
void menu ();
int play ();
void start ();
void settings ();
void printm ();
void move (char *dir);
void nolow ();
void ranking ();
int win ();
int error ();
int matrix[N][N] = {0};
int my_x;
int my_y;
int moves;
int grid = 5;
FILE *fp;
int main () {
srand (time(NULL));
fp = fopen ("roofus.txt", "a+");
if (fp == NULL) {
printf ("Error opening file");
}else{
menu ();
}
fclose (fp);
printf ("\n\n\n\nRoofus v%s", VERSION);
return 0;
}
void menu (){
char mode[5];
int menu = 1;
while (menu == 1) {
system("clear");
printf ("Select mode:\n> 0: Play\n> 1: Settings\n> 2: Ranking\n> 3: Exit\n");
scanf ("%5s", mode);
switch (mode[0]) {
case '0': menu = play (); break;
case '1': settings (); break;
case '2': ranking (); break;
case '3': return;
}
}
}
void settings () {
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 exit = 0;
int end = 0;
char *text = malloc (sizeof (text) * N);
my_x = grid / 2;
my_y = grid / 2;
moves = 0;
system("clear");
start ();
printm ();
while (end == 0) {
printf ("\nLeft: a. Up: w. Right: d. Down: s. Menu: m. Exit: 0");
printf ("\nDirection? ");
scanf ("%5s", dir);
getchar ();
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] ");
scanf ("%c", &exit);
if (exit == 'y') {
return 1;
} else {
dir[0] = '1';
getchar ();
}
}
move (dir);
printm ();
end = win ();
if (end != 0) {
printf ("Enter your name: ");
scanf ("%10s", user);
}
}
sprintf (text, "%li %d %s %d\n", time(NULL), moves, user, grid);
fputs (text, fp);
free (text);
return 1;
}
void start () {
int i, j;
for (i = 0; i < grid; i++) {
for (j = 0; j < grid; j++) {
matrix[i][j] = rand () % 19 + 1;
}
}
nolow ();
}
void move (char *dir) {
int xinit, yinit;
int stop = 0, del = 0;
int i, j;
xinit = my_x;
yinit = my_y;
// move pointer
switch (dir[0]) {
case 'a':
if (my_x != 0) {
my_x--;
} else {
stop = error ();
}
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++) {
if (matrix[my_y][j] != 0) {
matrix[my_y][j] = matrix[my_y][j] - matrix[my_y][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 == 'w'){
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)
del = 1;
if (matrix[i][my_x] < 0 && matrix[yinit][xinit] != 0)
matrix[i][my_x] = -matrix[i][my_x];
}
}
}
if (*dir == 's'){
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)
del = 1;
if (matrix[i][my_x] < 0 && matrix[yinit][xinit] != 0)
matrix[i][my_x] = -matrix[i][my_x];
}
}
}
if (del == 1)
matrix[yinit][xinit] = 0;
nolow ();
moves++;
}
}
// if 1 or 2 -> random number
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;
}
}
}
// print matrix
void printm () {
int i, j;
system("clear");
printf ("\n\n");
for (i = 0; i < grid; i++) {
for (j = 0;j < grid; j++) {
if (i == my_y && j == my_x)
printf ("> ");
if (matrix[i][j] != 0)
printf ("%d\t", matrix[i][j]);
else
printf (" \t");
}
printf ("\n\n");
}
}
int win () {
int i, j, count=0;
for (i = 0; i < grid; i++) {
for (j = 0; j < grid; j++) {
if (matrix[i][j] == 0)
count++;
}
}
if (count >= grid * (grid - 1)) {
printf ("\nYou win! Moves: %d\n", moves);
getchar ();
return 1;
}else{
printf ("\nYou still need to delete %d numbers. Moves: %d", grid * (grid - 1) - count, moves);
return 0;
}
}
int error () {
printf ("ERROR!!!");
return 1;
}