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