Roofus/Roofus.c

377 lines
10 KiB
C
Raw Normal View History

2017-01-11 23:03:03 +01:00
#define VERSION "1.10"
2016-12-18 22:36:00 +01:00
2016-12-21 10:30:11 +01:00
#define N 9
2016-12-26 15:03:34 +01:00
#define USERNAME_LENGTH 10
2016-12-18 22:36:00 +01:00
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
2016-12-23 21:03:57 +01:00
#include <string.h>
2016-12-26 15:03:34 +01:00
#include <ncurses.h>
2017-01-11 23:03:03 +01:00
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();
2016-12-28 21:19:25 +01:00
void menu();
void play();
void start();
void move_ptr(int direction);
void nolow();
void settings();
void ranking();
void rules();
void printm();
int win();
2017-01-11 23:03:03 +01:00
ptrNode InsertInList(ptrNode List, struct res elem);
ptrNode InsertFirst(ptrNode List, struct res elem);
int EmptyList(ptrNode List);
2016-12-18 22:36:00 +01:00
2016-12-28 21:19:25 +01:00
int main() {
initscr();
noecho();
cbreak();
keypad(stdscr, true);
2016-12-26 15:52:51 +01:00
2016-12-28 21:19:25 +01:00
if (has_colors() == TRUE) {
start_color();
init_pair(1, COLOR_BLACK, COLOR_WHITE);
}
2016-12-28 21:19:25 +01:00
srand(time(NULL));
fp = fopen("roofus.txt", "a+");
2016-12-23 21:03:57 +01:00
if (fp == NULL) {
2016-12-28 21:19:25 +01:00
printw("Error opening file");
refresh();
getch();
endwin();
return 1;
2016-12-27 14:00:59 +01:00
} else {
2017-01-11 23:03:03 +01:00
importdata();
2016-12-28 21:19:25 +01:00
menu();
fclose(fp);
2017-01-10 22:03:28 +01:00
endwin();
return 0;
2016-12-23 21:03:57 +01:00
}
2016-12-20 09:58:13 +01:00
}
2017-01-11 23:03:03 +01:00
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);
}
2016-12-26 13:05:06 +01:00
}
2016-12-28 21:19:25 +01:00
void menu() {
2016-12-26 13:05:06 +01:00
while (1) {
2016-12-28 21:19:25 +01:00
clear();
printw("~~~ ROOFUS %dx%d ~~~\n\n"
"[1] Play\n"
"[2] Ranking\n"
"[3] Settings\n"
"[4] Rules\n"
2017-01-11 23:03:03 +01:00
"[q] Quit\n\nv%s\n\n", current.grid, current.grid, VERSION);
2016-12-28 21:19:25 +01:00
refresh();
switch (getch()) {
case '1': play(); break;
case '2': ranking(); break;
case '3': settings(); break;
case '4': rules(); break;
2016-12-27 14:00:59 +01:00
case 'q': return;
2016-12-20 09:58:13 +01:00
}
2016-12-23 21:03:57 +01:00
}
2016-12-20 09:58:13 +01:00
}
2016-12-28 21:19:25 +01:00
void play() {
2017-01-10 22:03:28 +01:00
int direction, end = 0, replay;
2016-12-28 21:19:25 +01:00
char *text = malloc(sizeof(text) * N);
2017-01-10 22:03:28 +01:00
do {
replay = 0;
2017-01-11 23:03:03 +01:00
current.moves = 0;
ptr.x = ptr.y = current.grid / 2;
2017-01-10 22:03:28 +01:00
start();
while (end == 0) {
printw("\n[q] Quit. [r] Restart\nArrow to move ");
2016-12-28 21:19:25 +01:00
refresh();
2017-01-10 22:03:28 +01:00
direction = getch();
if (direction == 'q') {
printw("\nQuit? [y/n] ");
refresh();
2017-01-11 23:03:03 +01:00
if (getch() == 'y')
2017-01-10 22:03:28 +01:00
break;
2017-01-11 23:03:03 +01:00
else
2017-01-10 22:03:28 +01:00
direction = 0;
2016-12-18 22:36:00 +01:00
}
2017-01-10 22:03:28 +01:00
if (direction == 'r') {
printw("\nRestart? [y/n] ");
2016-12-28 21:19:25 +01:00
refresh();
2017-01-10 22:03:28 +01:00
if (getch() == 'y') {
replay = 1;
break;
} else
direction = 0;
2016-12-27 14:00:59 +01:00
}
2017-01-10 22:03:28 +01:00
if (direction != 0) {
move_ptr(direction);
printm();
end = win();
if (end == 1) {
2017-01-11 23:03:03 +01:00
current.playtime = time(NULL);
2017-01-10 22:03:28 +01:00
printw("Enter your name: ");
refresh();
2017-01-11 23:03:03 +01:00
echo();
getnstr(current.user, USERNAME_LENGTH); // read at most
2017-01-10 22:03:28 +01:00
noecho();
2017-01-11 23:03:03 +01:00
sprintf(text, "%li %d %s %d\n", current.playtime, current.moves, current.user, current.grid);
2017-01-10 22:03:28 +01:00
fputs(text, fp);
2017-01-11 23:03:03 +01:00
List = InsertInList(List, current);
2017-01-10 22:03:28 +01:00
}
2017-01-11 23:03:03 +01:00
} else
printm();
2016-12-24 20:15:47 +01:00
}
2017-01-10 22:03:28 +01:00
} while (replay == 1);
2016-12-28 21:19:25 +01:00
free(text);
2016-12-26 13:05:06 +01:00
return;
2016-12-18 22:36:00 +01:00
}
2016-12-28 21:19:25 +01:00
void start() {
2017-01-11 23:03:03 +01:00
for (int i = 0; i < current.grid; i++) {
for (int j = 0; j < current.grid; j++) {
2016-12-28 21:19:25 +01:00
matrix[i][j] = rand() % 19 + 1;
2016-12-18 22:36:00 +01:00
}
}
2016-12-28 21:19:25 +01:00
nolow();
printm();
2016-12-18 22:36:00 +01:00
}
2016-12-27 14:00:59 +01:00
// move pointer
2016-12-28 21:19:25 +01:00
void move_ptr(int direction) {
2017-01-10 22:03:28 +01:00
int xinit = ptr.x, yinit = ptr.y;
2016-12-27 14:00:59 +01:00
int i, j, del = 0;
switch (direction) {
case KEY_LEFT:
2017-01-10 22:03:28 +01:00
if (ptr.x != 0) {
ptr.x--;
2016-12-27 14:00:59 +01:00
for (j = 0; j < xinit; j++) {
2017-01-10 22:03:28 +01:00
if (matrix[ptr.y][j] != 0) {
matrix[ptr.y][j] = matrix[ptr.y][j] - matrix[ptr.y][xinit];
if (matrix[ptr.y][j] == 0)
2016-12-27 14:00:59 +01:00
del = 1;
2017-01-10 22:03:28 +01:00
if (matrix[ptr.y][j] < 0 && matrix[yinit][xinit] != 0)
matrix[ptr.y][j] = - matrix[ptr.y][j];
2016-12-27 14:00:59 +01:00
}
2016-12-18 22:36:00 +01:00
}
2016-12-27 14:00:59 +01:00
} break;
case KEY_RIGHT:
2017-01-11 23:03:03 +01:00
if (ptr.x < current.grid - 1) {
2017-01-10 22:03:28 +01:00
ptr.x++;
2017-01-11 23:03:03 +01:00
for (j = ptr.x; j < current.grid; j++) {
2017-01-10 22:03:28 +01:00
if (matrix[ptr.y][j] != 0) {
matrix[ptr.y][j] = matrix[ptr.y][j] - matrix[yinit][xinit];
if (matrix[ptr.y][j] == 0)
2016-12-27 14:00:59 +01:00
del = 1;
2017-01-10 22:03:28 +01:00
if (matrix[ptr.y][j] < 0 && matrix[yinit][xinit] != 0)
matrix[ptr.y][j] = - matrix[ptr.y][j];
2016-12-27 14:00:59 +01:00
}
2016-12-18 22:36:00 +01:00
}
2016-12-27 14:00:59 +01:00
} break;
case KEY_UP:
2017-01-10 22:03:28 +01:00
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)
2016-12-27 14:00:59 +01:00
del = 1;
2017-01-10 22:03:28 +01:00
if (matrix[i][ptr.x] < 0 && matrix[yinit][xinit] != 0)
matrix[i][ptr.x] = - matrix[i][ptr.x];
2016-12-27 14:00:59 +01:00
}
2016-12-18 22:36:00 +01:00
}
2016-12-27 14:00:59 +01:00
} break;
case KEY_DOWN:
2017-01-11 23:03:03 +01:00
if (ptr.y < current.grid - 1) {
2017-01-10 22:03:28 +01:00
ptr.y++;
2017-01-11 23:03:03 +01:00
for (i = ptr.y; i < current.grid; i++) {
2017-01-10 22:03:28 +01:00
if(matrix[i][ptr.x] != 0) {
matrix[i][ptr.x] = matrix[i][ptr.x] - matrix[yinit][xinit];
if (matrix[i][ptr.x] == 0)
2016-12-27 14:00:59 +01:00
del = 1;
2017-01-10 22:03:28 +01:00
if (matrix[i][ptr.x] < 0 && matrix[yinit][xinit] != 0)
matrix[i][ptr.x] = - matrix[i][ptr.x];
2016-12-27 14:00:59 +01:00
}
2016-12-18 22:36:00 +01:00
}
2016-12-27 14:00:59 +01:00
} break;
default: return;
2016-12-18 22:36:00 +01:00
}
2016-12-27 14:00:59 +01:00
if (del == 1)
matrix[yinit][xinit] = 0;
2016-12-28 21:19:25 +01:00
nolow();
2017-01-10 22:03:28 +01:00
if (xinit != ptr.x || yinit != ptr.y)
2017-01-11 23:03:03 +01:00
current.moves++;
2016-12-18 22:36:00 +01:00
}
2016-12-19 14:02:06 +01:00
// if 1 or 2 -> random number
2016-12-28 21:19:25 +01:00
void nolow() {
2017-01-11 23:03:03 +01:00
for (int i = 0; i < current.grid; i++) {
for (int j = 0; j < current.grid; j++) {
2016-12-19 14:02:06 +01:00
if ( matrix[i][j] < 3 && matrix[i][j] > 0)
2016-12-28 21:19:25 +01:00
matrix[i][j] = rand() % 10 - 10;
2016-12-18 22:36:00 +01:00
}
}
}
2016-12-28 21:19:25 +01:00
void settings() {
while (1) {
clear();
printw("> Settings\n\n");
printw("Menu [q]\nGrid size? [3-9] ");
refresh();
switch (getch()) {
case 'q': return;
2017-01-11 23:03:03 +01:00
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;
2016-12-27 14:00:59 +01:00
default: break;
2016-12-26 13:05:06 +01:00
}
}
}
2016-12-28 21:19:25 +01:00
void ranking() {
2017-01-11 23:03:03 +01:00
int grid_rank = current.grid;
2016-12-26 13:05:06 +01:00
int printed;
2017-01-11 23:03:03 +01:00
ptrNode ptrList;
2016-12-26 13:05:06 +01:00
do {
2017-01-11 23:03:03 +01:00
printed = 0;
2016-12-28 21:19:25 +01:00
clear();
2017-01-11 23:03:03 +01:00
if (grid_rank >= 3 && grid_rank <= 9)
printw("Best results (%dx%d matrix):\n\n", grid_rank, grid_rank);
else
2016-12-28 21:19:25 +01:00
printw("Best results:\n\n");
2017-01-11 23:03:03 +01:00
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));
2016-12-26 13:05:06 +01:00
printed++;
}
}
2017-01-10 22:03:28 +01:00
if (printed == 0)
2016-12-28 21:19:25 +01:00
printw("Nothing to show...\n");
printw("\nMenu [q]; To filter by matrix size [3-9]; No filter [1] ");
refresh();
2016-12-26 13:05:06 +01:00
do {
2017-01-11 23:03:03 +01:00
grid_rank = getch() - '0';
} while ((grid_rank < 3 || grid_rank > 9) && grid_rank != 1 && grid_rank != 'q'-'0');
} while (grid_rank != ('q'-'0'));
2016-12-26 13:05:06 +01:00
}
2016-12-28 21:19:25 +01:00
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();
2016-12-26 13:05:06 +01:00
}
2016-12-19 14:02:06 +01:00
// print matrix
2016-12-28 21:19:25 +01:00
void printm() {
clear();
printw("\n\n");
2017-01-11 23:03:03 +01:00
for (int i = 0; i < current.grid; i++) {
for (int j = 0; j < current.grid; j++) {
2017-01-10 22:03:28 +01:00
if (i == ptr.y && j == ptr.x) {
2016-12-28 21:19:25 +01:00
if (has_colors() == TRUE)
attron(COLOR_PAIR(1));
else
printw("> ");
}
2016-12-19 14:02:06 +01:00
if (matrix[i][j] != 0)
2016-12-28 21:19:25 +01:00
printw("%2d", matrix[i][j]);
else {
2016-12-28 21:19:25 +01:00
if (has_colors() == FALSE)
printw(" ");
else
printw(" .");
}
2016-12-28 21:19:25 +01:00
if (has_colors() == TRUE)
attroff(COLOR_PAIR(1));
printw("\t");
2016-12-18 22:36:00 +01:00
}
2016-12-28 21:19:25 +01:00
printw("\n\n");
2016-12-18 22:36:00 +01:00
}
2016-12-28 21:19:25 +01:00
refresh();
2016-12-18 22:36:00 +01:00
}
2016-12-28 21:19:25 +01:00
int win() {
2017-01-10 22:03:28 +01:00
int count = 0;
2017-01-11 23:03:03 +01:00
for (int i = 0; i < current.grid; i++) {
for (int j = 0; j < current.grid; j++) {
2016-12-19 14:02:06 +01:00
if (matrix[i][j] == 0)
2016-12-18 22:36:00 +01:00
count++;
}
}
2017-01-11 23:03:03 +01:00
if (count >= current.grid * (current.grid - 1)) {
printw("\nYou win! Moves: %d\n", current.moves);
2016-12-28 21:19:25 +01:00
refresh();
getchar();
2016-12-18 22:36:00 +01:00
return 1;
2016-12-27 14:00:59 +01:00
} else {
2017-01-11 23:03:03 +01:00
printw("\nYou still need to delete %d numbers. Moves: %d", current.grid * (current.grid - 1) - count, current.moves);
2016-12-28 21:19:25 +01:00
refresh();
2016-12-18 22:36:00 +01:00
return 0;
}
}
2017-01-11 23:03:03 +01:00
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;
}