Port to ncurses
This commit is contained in:
parent
f61bb0e60f
commit
e2e4ed8f93
16
README.md
16
README.md
|
@ -1,3 +1,19 @@
|
||||||
# Roofus.c
|
# Roofus.c
|
||||||
|
|
||||||
Clear the table
|
Clear the table
|
||||||
|
|
||||||
|
# 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`
|
||||||
|
|
276
Roofus.c
276
Roofus.c
|
@ -1,23 +1,26 @@
|
||||||
#define VERSION "0.30"
|
#define VERSION "1.00"
|
||||||
|
|
||||||
#define N 9
|
#define N 9
|
||||||
|
#define USERNAME_LENGTH 10
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <ncurses.h>
|
||||||
|
|
||||||
void restoregrid ();
|
void restoregrid ();
|
||||||
void menu ();
|
void menu ();
|
||||||
void play ();
|
void play ();
|
||||||
void start ();
|
void start ();
|
||||||
void move (char *dir);
|
void move_ptr (int direction);
|
||||||
void nolow ();
|
void nolow ();
|
||||||
void settings ();
|
void settings ();
|
||||||
void ranking ();
|
void ranking ();
|
||||||
void rules ();
|
void rules ();
|
||||||
void printm ();
|
void printm ();
|
||||||
int win ();
|
int win ();
|
||||||
int error ();
|
|
||||||
|
|
||||||
int matrix[N][N] = {0};
|
int matrix[N][N] = {0};
|
||||||
int my_x;
|
int my_x;
|
||||||
|
@ -28,83 +31,101 @@ int gridset = 0;
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
|
||||||
int main () {
|
int main () {
|
||||||
srand (time(NULL));
|
srand (time(NULL));
|
||||||
fp = fopen ("roofus.txt", "a+");
|
fp = fopen ("roofus.txt", "a+");
|
||||||
if (fp == NULL) {
|
if (fp == NULL) {
|
||||||
printf ("Error opening file");
|
printw ("Error opening file");
|
||||||
|
refresh();
|
||||||
}else{
|
}else{
|
||||||
restoregrid ();
|
restoregrid ();
|
||||||
menu ();
|
menu ();
|
||||||
}
|
}
|
||||||
fclose (fp);
|
fclose (fp);
|
||||||
printf ("\n\n\n\nRoofus v%s", VERSION);
|
printw ("\n\n\n\nRoofus v%s", VERSION);
|
||||||
|
refresh ();
|
||||||
|
endwin();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// restore last grid size
|
// restore last grid size
|
||||||
void restoregrid () {
|
void restoregrid () {
|
||||||
int gridtemp, length;
|
int gridtemp, length;
|
||||||
fseek(fp, 0, SEEK_END);
|
fseek(fp, 0, SEEK_END);
|
||||||
length = ftell(fp);
|
length = ftell(fp);
|
||||||
fseek(fp, (length - 2), SEEK_SET); // go to grid size character
|
fseek(fp, (length - 2), SEEK_SET); // go to grid size character
|
||||||
gridtemp = fgetc(fp) - 48; // -48 to convert char in int
|
gridtemp = fgetc(fp) - 48; // -48 to convert char in int
|
||||||
if (gridtemp < 3 || gridtemp > 9)
|
if (gridtemp < 3 || gridtemp > 9)
|
||||||
grid = 5;
|
grid = 5;
|
||||||
else
|
else
|
||||||
grid = gridtemp;
|
grid = gridtemp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void menu (){
|
void menu (){
|
||||||
char mode[5];
|
int mode;
|
||||||
|
|
||||||
|
initscr();
|
||||||
|
noecho();
|
||||||
|
cbreak();
|
||||||
|
keypad(stdscr, true);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
system("clear");
|
clear();
|
||||||
printf ("~~~ ROOFUS ~~~\n\n[1] Play\n[2] Ranking\n[3] Settings\n[4] Rules\n[0] Exit\n\n");
|
printw ("~~~ ROOFUS ~~~\n\n[1] Play\n[2] Ranking\n[3] Settings\n[4] Rules\n[q] Quit\n\n");
|
||||||
scanf ("%5s", mode);
|
refresh();
|
||||||
switch (mode[0]) {
|
mode = getch();
|
||||||
case '0': return;
|
|
||||||
case '1': play (); break;
|
switch (mode) {
|
||||||
case '2': ranking (); break;
|
case 'q': return;
|
||||||
case '3': settings (); break;
|
case '1': play (); break;
|
||||||
case '4': rules (); break;
|
case '2': ranking (); break;
|
||||||
|
case '3': settings (); break;
|
||||||
|
case '4': rules (); break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/* return 0; */
|
||||||
}
|
}
|
||||||
|
|
||||||
void play () {
|
void play () {
|
||||||
char dir[5], user[10];
|
char user[USERNAME_LENGTH];
|
||||||
char exit = 0;
|
int direction;
|
||||||
|
int exit = 0;
|
||||||
int end = 0;
|
int end = 0;
|
||||||
char *text = malloc (sizeof (text) * N);
|
char *text = malloc (sizeof (text) * N);
|
||||||
moves = 0;
|
moves = 0;
|
||||||
system("clear");
|
clear();
|
||||||
my_x = grid / 2;
|
my_x = grid / 2;
|
||||||
my_y = grid / 2;
|
my_y = grid / 2;
|
||||||
start ();
|
start ();
|
||||||
printm ();
|
|
||||||
while (end == 0) {
|
while (end == 0) {
|
||||||
printf ("\nLeft [a]; Up [w]; Right [d]; Down [s]. Menu [0]");
|
printw ("\nArrow to move, q to quit\n");
|
||||||
printf ("\nDirection? ");
|
refresh();
|
||||||
scanf ("%5s", dir);
|
|
||||||
getchar ();
|
direction = getch();
|
||||||
if (dir[0] == '0') {
|
if (direction == 'q') {
|
||||||
printf ("\nAre you sure? [y/n] ");
|
printw ("\nAre you sure? [y/n] ");
|
||||||
scanf ("%c", &exit);
|
refresh();
|
||||||
|
exit = getch();
|
||||||
if (exit == 'y') {
|
if (exit == 'y') {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
dir[0] = '1';
|
direction = '1';
|
||||||
getchar ();
|
getchar ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
move (dir);
|
|
||||||
|
move_ptr (direction);
|
||||||
printm ();
|
printm ();
|
||||||
end = win ();
|
end = win ();
|
||||||
if (end != 0) {
|
if (end != 0) {
|
||||||
printf ("Enter your name: ");
|
echo();
|
||||||
scanf ("%10s", user);
|
printw ("Enter your name: ");
|
||||||
|
refresh();
|
||||||
|
getnstr(user, USERNAME_LENGTH); // read at most
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sprintf (text, "%li %d %s %d\n", time(NULL), moves, user, grid);
|
sprintf (text, "%li %d %s %d\n", time(NULL), moves, user, grid);
|
||||||
|
refresh();
|
||||||
fputs (text, fp);
|
fputs (text, fp);
|
||||||
free (text);
|
free (text);
|
||||||
return;
|
return;
|
||||||
|
@ -118,52 +139,50 @@ void start () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
nolow ();
|
nolow ();
|
||||||
|
printm();
|
||||||
}
|
}
|
||||||
|
|
||||||
void move (char *dir) {
|
void move_ptr (int direction) {
|
||||||
int xinit = my_x, yinit = my_y;
|
int xinit = my_x, yinit = my_y;
|
||||||
int stop = 0, del = 0;
|
int stop = 0, del = 0;
|
||||||
int i, j;
|
int i, j;
|
||||||
// move pointer
|
// move_ptr pointer
|
||||||
switch (dir[0]) {
|
switch (direction) {
|
||||||
case 'a':
|
case KEY_LEFT:
|
||||||
if (my_x != 0) {
|
if (my_x != 0) {
|
||||||
my_x--;
|
my_x--;
|
||||||
} else {
|
} 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;
|
stop = 1;
|
||||||
break;
|
|
||||||
default:
|
|
||||||
stop = error ();
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
case KEY_RIGHT:
|
||||||
|
if (my_x < grid - 1) {
|
||||||
|
my_x++;
|
||||||
|
} else {
|
||||||
|
stop = 1;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
if (stop == 0) {
|
if (stop == 0) {
|
||||||
// change values
|
// change values
|
||||||
if (*dir == 'd') {
|
if (direction == KEY_RIGHT) {
|
||||||
for (j = my_x; j < grid; j++) {
|
for (j = my_x; j < grid; j++) {
|
||||||
if (matrix[my_y][j] != 0) {
|
if (matrix[my_y][j] != 0) {
|
||||||
matrix[my_y][j] = matrix[my_y][j] - matrix[yinit][xinit];
|
matrix[my_y][j] = matrix[my_y][j] - matrix[yinit][xinit];
|
||||||
|
@ -174,7 +193,7 @@ void move (char *dir) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (*dir == 'a') {
|
if (direction == KEY_LEFT) {
|
||||||
for (j = 0; j < xinit; j++) {
|
for (j = 0; j < xinit; j++) {
|
||||||
if (matrix[my_y][j] != 0) {
|
if (matrix[my_y][j] != 0) {
|
||||||
matrix[my_y][j] = matrix[my_y][j] - matrix[my_y][xinit];
|
matrix[my_y][j] = matrix[my_y][j] - matrix[my_y][xinit];
|
||||||
|
@ -185,7 +204,7 @@ void move (char *dir) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (*dir == 'w'){
|
if (direction == KEY_UP){
|
||||||
for (i = my_y; i >= 0; i--) {
|
for (i = my_y; i >= 0; i--) {
|
||||||
if (matrix[i][my_x] != 0) {
|
if (matrix[i][my_x] != 0) {
|
||||||
matrix[i][my_x] = matrix[i][my_x] - matrix[yinit][xinit];
|
matrix[i][my_x] = matrix[i][my_x] - matrix[yinit][xinit];
|
||||||
|
@ -196,7 +215,7 @@ void move (char *dir) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (*dir == 's'){
|
if (direction == KEY_DOWN){
|
||||||
for (i = my_y; i < grid; i++) {
|
for (i = my_y; i < grid; i++) {
|
||||||
if(matrix[i][my_x] != 0) {
|
if(matrix[i][my_x] != 0) {
|
||||||
matrix[i][my_x] = matrix[i][my_x] - matrix[yinit][xinit];
|
matrix[i][my_x] = matrix[i][my_x] - matrix[yinit][xinit];
|
||||||
|
@ -226,23 +245,25 @@ void nolow () {
|
||||||
}
|
}
|
||||||
|
|
||||||
void settings () {
|
void settings () {
|
||||||
char setting[5];
|
int setting;
|
||||||
int menu = 0;
|
int menu = 0;
|
||||||
system("clear");
|
clear();
|
||||||
printf ("> Settings\n\n");
|
printw ("> Settings\n\n");
|
||||||
while (menu == 0) {
|
while (menu == 0) {
|
||||||
printf ("Menu [0]\nGrid size? [3-9] ");
|
printw ("Menu [q]\nGrid size? [3-9]");
|
||||||
scanf ("%5s", setting);
|
refresh();
|
||||||
switch (setting[0]) {
|
setting = getch();
|
||||||
case '0': menu = 1; break;
|
switch (setting) {
|
||||||
case '3': grid = 3; gridset = 1; menu = 1; break;
|
case 'q': menu = 1; break;
|
||||||
case '4': grid = 4; gridset = 1; menu = 1; break;
|
case '3': grid = 3; gridset = 1; menu = 1; break;
|
||||||
case '5': grid = 5; gridset = 1; menu = 1; break;
|
case '4': grid = 4; gridset = 1; menu = 1; break;
|
||||||
case '6': grid = 6; gridset = 1; menu = 1; break;
|
case '5': grid = 5; gridset = 1; menu = 1; break;
|
||||||
case '7': grid = 7; gridset = 1; menu = 1; break;
|
case '6': grid = 6; gridset = 1; menu = 1; break;
|
||||||
case '8': grid = 8; gridset = 1; menu = 1; break;
|
case '7': grid = 7; gridset = 1; menu = 1; break;
|
||||||
case '9': grid = 9; gridset = 1; menu = 1; break;
|
case '8': grid = 8; gridset = 1; menu = 1; break;
|
||||||
default: printf("Retry\n"); break;
|
case '9': grid = 9; gridset = 1; menu = 1; break;
|
||||||
|
default: printw("Retry\n"); break;
|
||||||
|
refresh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -261,7 +282,6 @@ void ranking () {
|
||||||
int grid_val = grid;
|
int grid_val = grid;
|
||||||
int printed;
|
int printed;
|
||||||
rewind (fp);
|
rewind (fp);
|
||||||
getchar ();
|
|
||||||
do {
|
do {
|
||||||
fscanf (fp, "%li %d %s %d", &result[i].res_time, &result[i].moves, result[i].user, &result[i].grid);
|
fscanf (fp, "%li %d %s %d", &result[i].res_time, &result[i].moves, result[i].user, &result[i].grid);
|
||||||
i++;
|
i++;
|
||||||
|
@ -276,55 +296,63 @@ void ranking () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
system("clear");
|
clear();
|
||||||
if (grid_val >= 3 && grid_val <= 9)
|
if (grid_val >= 3 && grid_val <= 9)
|
||||||
printf ("Best results (%dx%d matrix):\n\n", grid_val, grid_val);
|
{
|
||||||
|
printw ("Best results (%dx%d matrix):\n\n", grid_val, grid_val);
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
else if (grid_val == 1)
|
else if (grid_val == 1)
|
||||||
printf ("Best results:\n\n");
|
printw ("Best results:\n\n");
|
||||||
printed = 0;
|
printed = 0;
|
||||||
for (i = 0; result[i].moves != 0; i++) {
|
for (i = 0; result[i].moves != 0; i++) {
|
||||||
if ((result[i].grid == grid_val || grid_val == 1) && printed < 9) {
|
if ((result[i].grid == grid_val || grid_val == 1) && printed < 9) {
|
||||||
time_string[i] = ctime(&result[i].res_time);
|
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]);
|
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();
|
||||||
printed++;
|
printed++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (printed == 0) {
|
if (printed == 0) {
|
||||||
printf ("Nothing to show...\n");
|
printw ("Nothing to show...\n");
|
||||||
|
refresh();
|
||||||
}
|
}
|
||||||
|
printw ("\nMenu [q]; To filter by matrix size [3-9]; No filter [1] ");
|
||||||
|
refresh();
|
||||||
|
|
||||||
do {
|
do {
|
||||||
printf ("\nMenu [0]; To filter by matrix size [3-9]; No filter [1] ");
|
grid_val = getch() - '0';
|
||||||
scanf ("%1d", &grid_val);
|
// exit with 'q' - '0'
|
||||||
getchar();
|
} while ((grid_val < 3 || grid_val > 9) && grid_val != 1 && grid_val != 'q'-'0');
|
||||||
} while ((grid_val < 3 || grid_val > 9) && grid_val != 1 && grid_val != 0);
|
} while (grid_val != ('q'-'0'));
|
||||||
} while (grid_val != 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void rules () {
|
void rules () {
|
||||||
system("clear");
|
clear();
|
||||||
getchar ();
|
printw ("> Rules\n\n");
|
||||||
printf ("> Rules\n\n");
|
printw ("Delete numbers on the table by moving the pointer.\n");
|
||||||
printf ("Delete numbers on the table by moving the pointer.\n");
|
printw ("That's all you need to know\n");
|
||||||
printf ("That's all you need to know\n");
|
refresh();
|
||||||
getchar ();
|
getch();
|
||||||
}
|
}
|
||||||
|
|
||||||
// print matrix
|
// print matrix
|
||||||
void printm () {
|
void printm () {
|
||||||
int i, j;
|
int i, j;
|
||||||
system("clear");
|
clear();
|
||||||
printf ("\n\n");
|
printw ("\n\n");
|
||||||
for (i = 0; i < grid; i++) {
|
for (i = 0; i < grid; i++) {
|
||||||
for (j = 0;j < grid; j++) {
|
for (j = 0;j < grid; j++) {
|
||||||
if (i == my_y && j == my_x)
|
if (i == my_y && j == my_x)
|
||||||
printf ("> ");
|
printw ("> ");
|
||||||
if (matrix[i][j] != 0)
|
if (matrix[i][j] != 0)
|
||||||
printf ("%d\t", matrix[i][j]);
|
printw ("%d\t", matrix[i][j]);
|
||||||
else
|
else
|
||||||
printf (" \t");
|
printw (" \t");
|
||||||
}
|
}
|
||||||
printf ("\n\n");
|
printw ("\n\n");
|
||||||
}
|
}
|
||||||
|
refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
int win () {
|
int win () {
|
||||||
|
@ -335,17 +363,15 @@ int win () {
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count >= grid * (grid - 1)) {
|
if (count >= grid * (grid - 1)) {
|
||||||
printf ("\nYou win! Moves: %d\n", moves);
|
printw ("\nYou win! Moves: %d\n", moves);
|
||||||
|
refresh ();
|
||||||
getchar ();
|
getchar ();
|
||||||
return 1;
|
return 1;
|
||||||
}else{
|
}else{
|
||||||
printf ("\nYou still need to delete %d numbers. Moves: %d", grid * (grid - 1) - count, moves);
|
printw ("\nYou still need to delete %d numbers. Moves: %d", grid * (grid - 1) - count, moves);
|
||||||
|
refresh();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int error () {
|
|
||||||
printf ("ERROR!!!");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue