Roofus/roofus.c

310 lines
7.6 KiB
C
Raw Normal View History

2016-12-23 21:03:57 +01:00
#define VERSION "0.20"
2016-12-18 22:36:00 +01:00
2016-12-21 10:30:11 +01:00
#define N 9
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-20 09:58:13 +01:00
void menu ();
int play ();
2016-12-19 14:02:06 +01:00
void start ();
2016-12-20 09:58:13 +01:00
void settings ();
2016-12-19 14:02:06 +01:00
void printm ();
void move (char *dir);
void nolow ();
2016-12-20 09:58:13 +01:00
void ranking ();
2016-12-19 14:02:06 +01:00
int win ();
int error ();
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;
int grid = 5;
2016-12-23 21:03:57 +01:00
FILE *fp;
2016-12-18 22:36:00 +01:00
int main () {
srand (time(NULL));
2016-12-23 21:03:57 +01:00
fp = fopen ("roofus.txt", "a+");
if (fp == NULL) {
printf ("Error opening file");
}else{
menu ();
}
fclose (fp);
2016-12-20 09:58:13 +01:00
printf ("\n\n\n\nv%s", VERSION);
return 0;
}
void menu (){
char mode[5];
int menu = 1;
while (menu == 1) {
system("clear");
printf ("Select mode:\n> 0: Play\n> 1: Settings\n> 2: Ranking\n> 3: Exit\n");
scanf ("%5s", mode);
switch (mode[0]) {
case '0': menu = play (); break;
case '1': settings (); break;
case '2': ranking (); break;
case '3': return;
}
}
}
void settings () {
char setting[5];
int menu = 0;
system("clear");
while (menu == 0) {
2016-12-21 10:30:11 +01:00
printf ("Menu: press 0\nGrid size? 3, 5, 7 or 9? ");
2016-12-20 09:58:13 +01:00
scanf ("%5s", setting);
switch (setting[0]) {
case '0': menu = 1; break;
2016-12-21 10:30:11 +01:00
case '3': grid = 3; menu = 1; break;
case '5': grid = 5; menu = 1; break;
case '7': grid = 7; menu = 1; break;
case '9': grid = 9; menu = 1; break;
default: printf("Retry\n"); break;
2016-12-20 09:58:13 +01:00
}
2016-12-23 21:03:57 +01:00
}
2016-12-20 09:58:13 +01:00
}
void ranking () {
2016-12-23 21:03:57 +01:00
int i = 0, j ,a;
int best[1000] = {0};
int grid_val[1000];
time_t res_time[1000];
char *time_string[1000];
2016-12-20 09:58:13 +01:00
system("clear");
2016-12-23 21:03:57 +01:00
rewind (fp);
getchar ();
do {
fscanf (fp, "%d %li %d", &best[i], &res_time[i], &grid_val[i]);
i++;
} while (best[i-1] > 0);
for (i = 0; best[i] != 0; i++) {
for (j = i + 1; best[j] !=0; j++) {
if (best[i] > best[j]) {
a = best[i];
best[i] = best[j];
best[j] = a;
}
}
}
printf ("Best results: \n\n");
for (i=0; i < 10 && best[i] != 0; i++) {
time_string[i] = ctime(&res_time[i]);
// printf ("#%d: %3d moves\t- %dx%d matrix\t- %s", i + 1, best[i], grid_val[i], grid_val[i], time_string[i]);
printf ("#%d: %3d moves\n", i + 1, best[i]);
}
getchar ();
2016-12-20 09:58:13 +01:00
}
int play () {
char dir[5];
char exit = 0;
int end = 0;
2016-12-23 21:03:57 +01:00
char *text = malloc (sizeof (text) * N);
2016-12-21 10:30:11 +01:00
my_x = grid / 2;
my_y = grid / 2;
2016-12-23 21:03:57 +01:00
moves = 0;
2016-12-20 09:58:13 +01:00
system("clear");
2016-12-19 14:02:06 +01:00
start ();
printm ();
while (end == 0) {
2016-12-20 09:58:13 +01:00
printf ("\nLeft: a. Up: w. Right: d. Down: s. Menu: m. Exit: 0");
2016-12-19 14:02:06 +01:00
printf ("\nDirection? ");
2016-12-20 09:58:13 +01:00
scanf ("%5s", dir);
2016-12-19 14:02:06 +01:00
getchar ();
if (dir[0] == '0') {
printf ("\nExit? [y/n] ");
scanf ("%c", &exit);
if (exit == 'y') {
2016-12-20 09:58:13 +01:00
return 0;
} else {
dir[0] = '1';
getchar ();
}
}
if (dir[0] == 'm') {
printf ("\nAre you sure? [y/n] ");
scanf ("%c", &exit);
if (exit == 'y') {
return 1;
2016-12-19 14:02:06 +01:00
} else {
dir[0] = '1';
getchar ();
2016-12-18 22:36:00 +01:00
}
}
2016-12-19 14:02:06 +01:00
move (dir);
printm ();
end = win ();
2016-12-18 22:36:00 +01:00
}
2016-12-23 21:03:57 +01:00
sprintf (text, "%d %li %d\n", moves, time(NULL), grid);
fputs (text, fp);
free (text);
2016-12-20 09:58:13 +01:00
return 1;
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-18 22:36:00 +01:00
}
2016-12-19 14:02:06 +01:00
void move (char *dir) {
2016-12-18 22:36:00 +01:00
int xinit, yinit;
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-19 14:02:06 +01:00
xinit = my_x;
yinit = my_y;
2016-12-18 22:36:00 +01:00
2016-12-19 14:02:06 +01:00
// move pointer
switch (dir[0]) {
2016-12-18 22:36:00 +01:00
case 'a':
2016-12-19 14:02:06 +01:00
if (my_x != 0) {
2016-12-18 22:36:00 +01:00
my_x--;
2016-12-19 14:02:06 +01:00
} else {
stop = error ();
2016-12-18 22:36:00 +01:00
}
break;
case 'd':
2016-12-21 10:30:11 +01:00
if (my_x < grid - 1) {
2016-12-18 22:36:00 +01:00
my_x++;
2016-12-19 14:02:06 +01:00
} else {
stop = error ();
2016-12-18 22:36:00 +01:00
}
break;
case 'w':
2016-12-19 14:02:06 +01:00
if (my_y > 0) {
2016-12-18 22:36:00 +01:00
my_y--;
2016-12-19 14:02:06 +01:00
} else {
stop = error ();
2016-12-18 22:36:00 +01:00
}
break;
case 's':
2016-12-21 10:30:11 +01:00
if(my_y < grid - 1) {
2016-12-18 22:36:00 +01:00
my_y++;
2016-12-19 14:02:06 +01:00
} else {
stop = error ();
2016-12-18 22:36:00 +01:00
}
break;
case 'o':
2016-12-19 14:02:06 +01:00
stop = 1;
2016-12-18 22:36:00 +01:00
break;
default:
2016-12-19 14:02:06 +01:00
stop = error ();
2016-12-18 22:36:00 +01:00
break;
}
2016-12-19 14:02:06 +01:00
if (stop == 0) {
// change values
if (*dir == 'd') {
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-19 14:02:06 +01:00
if (*dir == 'a') {
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-19 14:02:06 +01:00
if (*dir == 'w'){
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-19 14:02:06 +01:00
if (*dir == 's'){
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-19 14:02:06 +01:00
// print matrix
void printm () {
2016-12-18 22:36:00 +01:00
int i, j;
2016-12-20 09:58:13 +01:00
system("clear");
2016-12-19 14:02:06 +01:00
printf ("\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-18 22:36:00 +01:00
printf ("> ");
2016-12-19 14:02:06 +01:00
if (matrix[i][j] != 0)
printf ("%d\t", matrix[i][j]);
2016-12-18 22:36:00 +01:00
else
2016-12-19 14:02:06 +01:00
printf (" \t");
2016-12-18 22:36:00 +01:00
}
2016-12-19 14:02:06 +01:00
printf ("\n\n");
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-21 10:30:11 +01:00
if (count >= grid * (grid - 1)) {
2016-12-20 09:58:13 +01:00
printf ("\nYou win! Moves: %d\n", moves);
getchar ();
2016-12-18 22:36:00 +01:00
return 1;
}else{
2016-12-21 10:30:11 +01:00
printf ("\nYou still need to delete %d numbers. Moves: %d", grid * (grid - 1) - count, moves);
2016-12-18 22:36:00 +01:00
return 0;
}
}
2016-12-19 14:02:06 +01:00
int error () {
printf ("ERROR!!!");
2016-12-18 22:36:00 +01:00
return 1;
}