Roofus/Roofus.c

378 lines
9.4 KiB
C
Raw Normal View History

2016-12-26 15:52:51 +01:00
#define VERSION "1.01"
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-18 22:36:00 +01:00
2016-12-26 15:03:34 +01:00
#include <ncurses.h>
2016-12-26 13:05:06 +01:00
void restoregrid ();
2016-12-20 09:58:13 +01:00
void menu ();
2016-12-26 13:05:06 +01:00
void play ();
2016-12-19 14:02:06 +01:00
void start ();
2016-12-26 15:03:34 +01:00
void move_ptr (int direction);
2016-12-19 14:02:06 +01:00
void nolow ();
2016-12-26 13:05:06 +01:00
void settings ();
2016-12-20 09:58:13 +01:00
void ranking ();
2016-12-26 13:05:06 +01:00
void rules ();
void printm ();
2016-12-19 14:02:06 +01:00
int win ();
2016-12-18 22:36:00 +01:00
2016-12-21 10:30:11 +01:00
int matrix[N][N] = {0};
2016-12-20 09:58:13 +01:00
int my_x;
int my_y;
2016-12-21 10:30:11 +01:00
int moves;
2016-12-26 13:05:06 +01:00
int grid;
int gridset = 0;
2016-12-23 21:03:57 +01:00
FILE *fp;
2016-12-18 22:36:00 +01:00
int main () {
2016-12-26 15:52:51 +01:00
initscr();
noecho();
cbreak();
keypad(stdscr, true);
2016-12-26 15:03:34 +01:00
srand (time(NULL));
2016-12-23 21:03:57 +01:00
fp = fopen ("roofus.txt", "a+");
if (fp == NULL) {
2016-12-26 15:03:34 +01:00
printw ("Error opening file");
refresh();
2016-12-23 21:03:57 +01:00
}else{
2016-12-26 13:05:06 +01:00
restoregrid ();
2016-12-23 21:03:57 +01:00
menu ();
}
fclose (fp);
2016-12-26 15:03:34 +01:00
printw ("\n\n\n\nRoofus v%s", VERSION);
refresh ();
endwin();
2016-12-20 09:58:13 +01:00
return 0;
}
2016-12-26 13:05:06 +01:00
// restore last grid size
void restoregrid () {
int gridtemp, length;
2016-12-26 15:03:34 +01:00
fseek(fp, 0, SEEK_END);
length = ftell(fp);
fseek(fp, (length - 2), SEEK_SET); // go to grid size character
gridtemp = fgetc(fp) - 48; // -48 to convert char in int
if (gridtemp < 3 || gridtemp > 9)
grid = 5;
else
grid = gridtemp;
2016-12-26 13:05:06 +01:00
}
2016-12-20 09:58:13 +01:00
void menu (){
2016-12-26 15:03:34 +01:00
int mode;
2016-12-26 13:05:06 +01:00
while (1) {
2016-12-26 15:03:34 +01:00
clear();
printw ("~~~ ROOFUS ~~~\n\n[1] Play\n[2] Ranking\n[3] Settings\n[4] Rules\n[q] Quit\n\n");
refresh();
mode = getch();
switch (mode) {
case 'q': return;
case '1': play (); break;
case '2': ranking (); break;
case '3': settings (); break;
case '4': rules (); break;
2016-12-20 09:58:13 +01:00
}
2016-12-23 21:03:57 +01:00
}
2016-12-26 15:03:34 +01:00
/* return 0; */
2016-12-20 09:58:13 +01:00
}
2016-12-26 13:05:06 +01:00
void play () {
2016-12-26 15:03:34 +01:00
char user[USERNAME_LENGTH];
int direction;
int exit = 0;
2016-12-20 09:58:13 +01:00
int end = 0;
2016-12-23 21:03:57 +01:00
char *text = malloc (sizeof (text) * N);
2016-12-26 13:05:06 +01:00
moves = 0;
2016-12-26 15:03:34 +01:00
clear();
2016-12-21 10:30:11 +01:00
my_x = grid / 2;
my_y = grid / 2;
2016-12-19 14:02:06 +01:00
start ();
2016-12-26 15:03:34 +01:00
2016-12-19 14:02:06 +01:00
while (end == 0) {
2016-12-26 15:03:34 +01:00
printw ("\nArrow to move, q to quit\n");
refresh();
direction = getch();
if (direction == 'q') {
printw ("\nAre you sure? [y/n] ");
refresh();
exit = getch();
2016-12-20 09:58:13 +01:00
if (exit == 'y') {
2016-12-26 13:05:06 +01:00
return;
2016-12-19 14:02:06 +01:00
} else {
2016-12-26 15:03:34 +01:00
direction = '1';
2016-12-19 14:02:06 +01:00
getchar ();
2016-12-18 22:36:00 +01:00
}
}
2016-12-26 15:03:34 +01:00
move_ptr (direction);
2016-12-19 14:02:06 +01:00
printm ();
end = win ();
2016-12-24 20:15:47 +01:00
if (end != 0) {
2016-12-26 15:03:34 +01:00
echo();
printw ("Enter your name: ");
refresh();
getnstr(user, USERNAME_LENGTH); // read at most
2016-12-24 20:15:47 +01:00
}
2016-12-18 22:36:00 +01:00
}
2016-12-24 20:15:47 +01:00
sprintf (text, "%li %d %s %d\n", time(NULL), moves, user, grid);
2016-12-26 15:03:34 +01:00
refresh();
2016-12-23 21:03:57 +01:00
fputs (text, fp);
free (text);
2016-12-26 13:05:06 +01:00
return;
2016-12-18 22:36:00 +01:00
}
2016-12-19 14:02:06 +01:00
void start () {
2016-12-18 22:36:00 +01:00
int i, j;
2016-12-21 10:30:11 +01:00
for (i = 0; i < grid; i++) {
for (j = 0; j < grid; j++) {
2016-12-19 14:02:06 +01:00
matrix[i][j] = rand () % 19 + 1;
2016-12-18 22:36:00 +01:00
}
}
2016-12-19 14:02:06 +01:00
nolow ();
2016-12-26 15:03:34 +01:00
printm();
2016-12-18 22:36:00 +01:00
}
2016-12-26 15:03:34 +01:00
void move_ptr (int direction) {
2016-12-26 13:05:06 +01:00
int xinit = my_x, yinit = my_y;
2016-12-23 21:03:57 +01:00
int stop = 0, del = 0;
2016-12-18 22:36:00 +01:00
int i, j;
2016-12-26 15:03:34 +01:00
// move_ptr pointer
switch (direction) {
case KEY_LEFT:
if (my_x != 0) {
my_x--;
} else {
stop = 1;
}
break;
case KEY_RIGHT:
if (my_x < grid - 1) {
my_x++;
} else {
2016-12-19 14:02:06 +01:00
stop = 1;
2016-12-18 22:36:00 +01:00
}
2016-12-26 15:03:34 +01:00
break;
case KEY_UP:
if (my_y > 0) {
my_y--;
} else {
stop = 1;
}
break;
case KEY_DOWN:
if(my_y < grid - 1) {
my_y++;
} else {
stop = 1;
}
break;
default:
stop = 1;
break;
}
2016-12-19 14:02:06 +01:00
if (stop == 0) {
// change values
2016-12-26 15:03:34 +01:00
if (direction == KEY_RIGHT) {
2016-12-21 10:30:11 +01:00
for (j = my_x; j < grid; j++) {
2016-12-20 09:58:13 +01:00
if (matrix[my_y][j] != 0) {
matrix[my_y][j] = matrix[my_y][j] - matrix[yinit][xinit];
2016-12-23 21:03:57 +01:00
if (matrix[my_y][j] == 0)
del = 1;
2016-12-20 09:58:13 +01:00
if (matrix[my_y][j] < 0 && matrix[yinit][xinit] != 0)
matrix[my_y][j] = -matrix[my_y][j];
2016-12-18 22:36:00 +01:00
}
}
}
2016-12-26 15:03:34 +01:00
if (direction == KEY_LEFT) {
2016-12-19 14:02:06 +01:00
for (j = 0; j < xinit; j++) {
2016-12-20 09:58:13 +01:00
if (matrix[my_y][j] != 0) {
matrix[my_y][j] = matrix[my_y][j] - matrix[my_y][xinit];
2016-12-23 21:03:57 +01:00
if (matrix[my_y][j] == 0)
del = 1;
2016-12-20 09:58:13 +01:00
if (matrix[my_y][j] < 0 && matrix[yinit][xinit] != 0)
2016-12-23 21:03:57 +01:00
matrix[my_y][j] = - matrix[my_y][j];
2016-12-18 22:36:00 +01:00
}
}
}
2016-12-26 15:03:34 +01:00
if (direction == KEY_UP){
2016-12-21 10:30:11 +01:00
for (i = my_y; i >= 0; i--) {
2016-12-19 14:02:06 +01:00
if (matrix[i][my_x] != 0) {
matrix[i][my_x] = matrix[i][my_x] - matrix[yinit][xinit];
2016-12-23 21:03:57 +01:00
if (matrix[i][my_x] == 0)
del = 1;
2016-12-19 14:02:06 +01:00
if (matrix[i][my_x] < 0 && matrix[yinit][xinit] != 0)
2016-12-18 22:36:00 +01:00
matrix[i][my_x] = -matrix[i][my_x];
}
}
}
2016-12-26 15:03:34 +01:00
if (direction == KEY_DOWN){
2016-12-21 10:30:11 +01:00
for (i = my_y; i < grid; i++) {
2016-12-19 14:02:06 +01:00
if(matrix[i][my_x] != 0) {
matrix[i][my_x] = matrix[i][my_x] - matrix[yinit][xinit];
2016-12-23 21:03:57 +01:00
if (matrix[i][my_x] == 0)
del = 1;
2016-12-19 14:02:06 +01:00
if (matrix[i][my_x] < 0 && matrix[yinit][xinit] != 0)
2016-12-18 22:36:00 +01:00
matrix[i][my_x] = -matrix[i][my_x];
}
}
}
2016-12-23 21:03:57 +01:00
if (del == 1)
matrix[yinit][xinit] = 0;
2016-12-19 14:02:06 +01:00
nolow ();
2016-12-18 22:36:00 +01:00
moves++;
}
}
2016-12-19 14:02:06 +01:00
// if 1 or 2 -> random number
void nolow () {
2016-12-18 22:36:00 +01:00
int i, j;
2016-12-21 10:30:11 +01:00
for (i = 0; i < grid; i++) {
for (j = 0; j < grid; j++) {
2016-12-19 14:02:06 +01:00
if ( matrix[i][j] < 3 && matrix[i][j] > 0)
matrix[i][j] = rand () % 10 - 10;
2016-12-18 22:36:00 +01:00
}
}
}
2016-12-26 13:05:06 +01:00
void settings () {
2016-12-26 15:03:34 +01:00
int setting;
2016-12-26 13:05:06 +01:00
int menu = 0;
2016-12-26 15:03:34 +01:00
clear();
printw ("> Settings\n\n");
2016-12-26 13:05:06 +01:00
while (menu == 0) {
2016-12-26 15:03:34 +01:00
printw ("Menu [q]\nGrid size? [3-9]");
refresh();
setting = getch();
switch (setting) {
case 'q': menu = 1; break;
case '3': grid = 3; gridset = 1; menu = 1; break;
case '4': grid = 4; gridset = 1; menu = 1; break;
case '5': grid = 5; gridset = 1; menu = 1; break;
case '6': grid = 6; gridset = 1; menu = 1; break;
case '7': grid = 7; gridset = 1; menu = 1; break;
case '8': grid = 8; gridset = 1; menu = 1; break;
case '9': grid = 9; gridset = 1; menu = 1; break;
default: printw("Retry\n"); break;
refresh();
2016-12-26 13:05:06 +01:00
}
}
}
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 = grid;
int printed;
rewind (fp);
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 {
2016-12-26 15:03:34 +01:00
clear();
2016-12-26 13:05:06 +01:00
if (grid_val >= 3 && grid_val <= 9)
2016-12-26 15:03:34 +01:00
{
printw ("Best results (%dx%d matrix):\n\n", grid_val, grid_val);
refresh();
}
2016-12-26 13:05:06 +01:00
else if (grid_val == 1)
2016-12-26 15:03:34 +01:00
printw ("Best results:\n\n");
2016-12-26 13:05:06 +01:00
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);
2016-12-26 15:03:34 +01:00
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, time_string[i]);
refresh();
2016-12-26 13:05:06 +01:00
printed++;
}
}
if (printed == 0) {
2016-12-26 15:03:34 +01:00
printw ("Nothing to show...\n");
refresh();
2016-12-26 13:05:06 +01:00
}
2016-12-26 15:03:34 +01:00
printw ("\nMenu [q]; To filter by matrix size [3-9]; No filter [1] ");
refresh();
2016-12-26 13:05:06 +01:00
do {
2016-12-26 15:03:34 +01:00
grid_val = getch() - '0';
// exit with 'q' - '0'
} while ((grid_val < 3 || grid_val > 9) && grid_val != 1 && grid_val != 'q'-'0');
} while (grid_val != ('q'-'0'));
2016-12-26 13:05:06 +01:00
}
void rules () {
2016-12-26 15:03:34 +01:00
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
void printm () {
2016-12-18 22:36:00 +01:00
int i, j;
2016-12-26 15:03:34 +01:00
clear();
printw ("\n\n");
2016-12-21 10:30:11 +01:00
for (i = 0; i < grid; i++) {
for (j = 0;j < grid; j++) {
2016-12-19 14:02:06 +01:00
if (i == my_y && j == my_x)
2016-12-26 15:03:34 +01:00
printw ("> ");
2016-12-19 14:02:06 +01:00
if (matrix[i][j] != 0)
2016-12-26 15:03:34 +01:00
printw ("%d\t", matrix[i][j]);
else
printw (" \t");
2016-12-18 22:36:00 +01:00
}
2016-12-26 15:03:34 +01:00
printw ("\n\n");
2016-12-18 22:36:00 +01:00
}
2016-12-26 15:03:34 +01:00
refresh();
2016-12-18 22:36:00 +01:00
}
2016-12-23 21:03:57 +01:00
int win () {
2016-12-18 22:36:00 +01:00
int i, j, count=0;
2016-12-21 10:30:11 +01:00
for (i = 0; i < grid; i++) {
for (j = 0; j < grid; j++) {
2016-12-19 14:02:06 +01:00
if (matrix[i][j] == 0)
2016-12-18 22:36:00 +01:00
count++;
}
}
2016-12-26 15:03:34 +01:00
2016-12-21 10:30:11 +01:00
if (count >= grid * (grid - 1)) {
2016-12-26 15:03:34 +01:00
printw ("\nYou win! Moves: %d\n", moves);
refresh ();
2016-12-20 09:58:13 +01:00
getchar ();
2016-12-18 22:36:00 +01:00
return 1;
}else{
2016-12-26 15:03:34 +01:00
printw ("\nYou still need to delete %d numbers. Moves: %d", grid * (grid - 1) - count, moves);
refresh();
2016-12-18 22:36:00 +01:00
return 0;
}
}