Linked lists

This commit is contained in:
Racoonicorn 2017-01-11 23:03:03 +01:00 committed by GitHub
parent 4d705243a9
commit 55d0f73cf1
1 changed files with 113 additions and 92 deletions

205
Roofus.c
View File

@ -1,4 +1,4 @@
#define VERSION "1.05" #define VERSION "1.10"
#define N 9 #define N 9
#define USERNAME_LENGTH 10 #define USERNAME_LENGTH 10
@ -9,7 +9,32 @@
#include <string.h> #include <string.h>
#include <ncurses.h> #include <ncurses.h>
void restoregrid(); 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 menu();
void play(); void play();
void start(); void start();
@ -20,16 +45,9 @@ void ranking();
void rules(); void rules();
void printm(); void printm();
int win(); int win();
ptrNode InsertInList(ptrNode List, struct res elem);
struct point{ ptrNode InsertFirst(ptrNode List, struct res elem);
int x; int EmptyList(ptrNode List);
int y;
} ptr;
int matrix[N][N] = {0};
int moves;
int grid;
FILE *fp;
int main() { int main() {
initscr(); initscr();
@ -50,7 +68,7 @@ int main() {
endwin(); endwin();
return 1; return 1;
} else { } else {
restoregrid(); importdata();
menu(); menu();
fclose(fp); fclose(fp);
endwin(); endwin();
@ -58,15 +76,21 @@ int main() {
} }
} }
// restore last grid size void importdata() {
void restoregrid() { int lenght;
int gridtemp; struct res temp;
fseek(fp, -2, SEEK_END); // go to grid size character // import last grid size
gridtemp = fgetc(fp) - '0'; // convert char to int fseek(fp, -2, SEEK_END);
if (gridtemp < 3 || gridtemp > 9) current.grid = fgetc(fp) - '0';
grid = 5; if (current.grid < 3 || current.grid > 9)
else current.grid = 5;
grid = gridtemp; 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() { void menu() {
@ -77,7 +101,7 @@ void menu() {
"[2] Ranking\n" "[2] Ranking\n"
"[3] Settings\n" "[3] Settings\n"
"[4] Rules\n" "[4] Rules\n"
"[q] Quit\n\nv%s\n\n", grid, grid, VERSION); "[q] Quit\n\nv%s\n\n", current.grid, current.grid, VERSION);
refresh(); refresh();
switch (getch()) { switch (getch()) {
case '1': play(); break; case '1': play(); break;
@ -91,13 +115,11 @@ void menu() {
void play() { void play() {
int direction, end = 0, replay; int direction, end = 0, replay;
char user[USERNAME_LENGTH];
char *text = malloc(sizeof(text) * N); char *text = malloc(sizeof(text) * N);
do { do {
replay = 0; replay = 0;
moves = 0; current.moves = 0;
ptr.x = ptr.y = grid / 2; ptr.x = ptr.y = current.grid / 2;
clear();
start(); start();
while (end == 0) { while (end == 0) {
printw("\n[q] Quit. [r] Restart\nArrow to move "); printw("\n[q] Quit. [r] Restart\nArrow to move ");
@ -106,9 +128,9 @@ void play() {
if (direction == 'q') { if (direction == 'q') {
printw("\nQuit? [y/n] "); printw("\nQuit? [y/n] ");
refresh(); refresh();
if (getch() == 'y') { if (getch() == 'y')
break; break;
} else else
direction = 0; direction = 0;
} }
if (direction == 'r') { if (direction == 'r') {
@ -125,15 +147,18 @@ void play() {
printm(); printm();
end = win(); end = win();
if (end == 1) { if (end == 1) {
echo(); current.playtime = time(NULL);
printw("Enter your name: "); printw("Enter your name: ");
refresh(); refresh();
getnstr(user, USERNAME_LENGTH); // read at most echo();
getnstr(current.user, USERNAME_LENGTH); // read at most
noecho(); noecho();
sprintf(text, "%li %d %s %d\n", time(NULL), moves, user, grid); 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(); } else
printm();
} }
} while (replay == 1); } while (replay == 1);
free(text); free(text);
@ -141,8 +166,8 @@ void play() {
} }
void start() { void start() {
for (int i = 0; i < grid; i++) { for (int i = 0; i < current.grid; i++) {
for (int j = 0; j < grid; j++) { for (int j = 0; j < current.grid; j++) {
matrix[i][j] = rand() % 19 + 1; matrix[i][j] = rand() % 19 + 1;
} }
} }
@ -169,9 +194,9 @@ void move_ptr(int direction) {
} }
} break; } break;
case KEY_RIGHT: case KEY_RIGHT:
if (ptr.x < grid - 1) { if (ptr.x < current.grid - 1) {
ptr.x++; ptr.x++;
for (j = ptr.x; j < grid; j++) { for (j = ptr.x; j < current.grid; j++) {
if (matrix[ptr.y][j] != 0) { if (matrix[ptr.y][j] != 0) {
matrix[ptr.y][j] = matrix[ptr.y][j] - matrix[yinit][xinit]; matrix[ptr.y][j] = matrix[ptr.y][j] - matrix[yinit][xinit];
if (matrix[ptr.y][j] == 0) if (matrix[ptr.y][j] == 0)
@ -195,9 +220,9 @@ void move_ptr(int direction) {
} }
} break; } break;
case KEY_DOWN: case KEY_DOWN:
if (ptr.y < grid - 1) { if (ptr.y < current.grid - 1) {
ptr.y++; ptr.y++;
for (i = ptr.y; i < grid; i++) { for (i = ptr.y; i < current.grid; i++) {
if(matrix[i][ptr.x] != 0) { if(matrix[i][ptr.x] != 0) {
matrix[i][ptr.x] = matrix[i][ptr.x] - matrix[yinit][xinit]; matrix[i][ptr.x] = matrix[i][ptr.x] - matrix[yinit][xinit];
if (matrix[i][ptr.x] == 0) if (matrix[i][ptr.x] == 0)
@ -213,13 +238,13 @@ void move_ptr(int direction) {
matrix[yinit][xinit] = 0; matrix[yinit][xinit] = 0;
nolow(); nolow();
if (xinit != ptr.x || yinit != ptr.y) 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 < grid; i++) { for (int i = 0; i < current.grid; i++) {
for (int j = 0; j < grid; j++) { for (int j = 0; j < current.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;
} }
@ -234,55 +259,32 @@ void settings() {
refresh(); refresh();
switch (getch()) { switch (getch()) {
case 'q': return; case 'q': return;
case '3': grid = 3; return; case '3': current.grid = 3; return;
case '4': grid = 4; return; case '4': current.grid = 4; return;
case '5': grid = 5; return; case '5': current.grid = 5; return;
case '6': grid = 6; return; case '6': current.grid = 6; return;
case '7': grid = 7; return; case '7': current.grid = 7; return;
case '8': grid = 8; return; case '8': current.grid = 8; return;
case '9': grid = 9; return; case '9': current.grid = 9; return;
default: break; default: break;
} }
} }
} }
void ranking() { void ranking() {
int i = 0, j; int grid_rank = current.grid;
struct res {
int moves;
int grid;
char user[10];
time_t playtime;
};
struct res result[1000];
struct res temp;
int grid_val = grid;
int printed; int printed;
ptrNode ptrList;
rewind(fp);
do { do {
fscanf(fp, "%li %d %s %d", &result[i].playtime, &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 {
clear();
if (grid_val >= 3 && grid_val <= 9)
printw("Best results (%dx%d matrix):\n\n", grid_val, grid_val);
else if (grid_val == 1)
printw("Best results:\n\n");
printed = 0; printed = 0;
for (i = 0; result[i].moves != 0; i++) { clear();
if ((result[i].grid == grid_val || grid_val == 1) && printed < 9) { if (grid_rank >= 3 && grid_rank <= 9)
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, ctime(&result[i].playtime)); 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++; printed++;
} }
} }
@ -291,9 +293,9 @@ void ranking() {
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_rank = getch() - '0';
} while ((grid_val < 3 || grid_val > 9) && grid_val != 1 && grid_val != 'q'-'0'); } while ((grid_rank < 3 || grid_rank > 9) && grid_rank != 1 && grid_rank != 'q'-'0');
} while (grid_val != ('q'-'0')); } while (grid_rank != ('q'-'0'));
} }
void rules() { void rules() {
@ -309,8 +311,8 @@ void rules() {
void printm() { void printm() {
clear(); clear();
printw("\n\n"); printw("\n\n");
for (int i = 0; i < grid; i++) { for (int i = 0; i < current.grid; i++) {
for (int j = 0; j < grid; j++) { for (int j = 0; j < current.grid; j++) {
if (i == ptr.y && j == ptr.x) { if (i == ptr.y && j == ptr.x) {
if (has_colors() == TRUE) if (has_colors() == TRUE)
attron(COLOR_PAIR(1)); attron(COLOR_PAIR(1));
@ -336,20 +338,39 @@ void printm() {
int win() { int win() {
int count = 0; int count = 0;
for (int i = 0; i < grid; i++) { for (int i = 0; i < current.grid; i++) {
for (int j = 0; j < grid; j++) { for (int j = 0; j < current.grid; j++) {
if (matrix[i][j] == 0) if (matrix[i][j] == 0)
count++; count++;
} }
} }
if (count >= grid * (grid - 1)) { if (count >= current.grid * (current.grid - 1)) {
printw("\nYou win! Moves: %d\n", moves); printw("\nYou win! Moves: %d\n", current.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", current.grid * (current.grid - 1) - count, current.moves);
refresh(); refresh();
return 0; 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;
}