Update roofus.c

This commit is contained in:
Racoonicorn 2016-12-20 09:58:13 +01:00 committed by GitHub
parent a1ac74174f
commit 32ca67ace1
1 changed files with 85 additions and 22 deletions

107
roofus.c
View File

@ -1,41 +1,102 @@
#define VERSION "0.02" #define VERSION "0.10"
#define N 5 #define N 5
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
void menu ();
int play ();
void start (); void start ();
void settings ();
void printm (); void printm ();
void move (char *dir); void move (char *dir);
void nolow (); void nolow ();
void ranking ();
int win (); int win ();
int error (); int error ();
int matrix[N][N]; int matrix[N][N];
int my_x = N / 2; int my_x;
int my_y = N / 2; int my_y;
int moves = 0; int moves = 0;
int main () { int main () {
char dir[20];
char exit;
int end = 0;
srand (time(NULL)); srand (time(NULL));
menu ();
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) {
printf ("Not available. Press 0\n");
scanf ("%5s", setting);
switch (setting[0]) {
case '0': menu = 1; break;
case '1': break;
case '2': return;
}
}
}
void ranking () {
char back[5];
system("clear");
printf ("Not available. Press 0\n");
scanf ("%5s", back);
}
int play () {
char dir[5];
char exit = 0;
int end = 0;
my_x = N / 2;
my_y = N / 2;
system("clear");
start (); start ();
printm (); printm ();
while (end == 0) { while (end == 0) {
printf ("\nLeft: a. Up: w. Right: d. Down: s. Exit: 0"); printf ("\nLeft: a. Up: w. Right: d. Down: s. Menu: m. Exit: 0");
printf ("\nDirection? "); printf ("\nDirection? ");
scanf ("%s", dir); scanf ("%5s", dir);
getchar (); getchar ();
if (dir[0] == '0') { if (dir[0] == '0') {
printf ("\nExit? [y/n] "); printf ("\nExit? [y/n] ");
scanf ("%c", &exit); scanf ("%c", &exit);
if (exit == 'y') { if (exit == 'y') {
printf ("\n\n\n\nv%s", VERSION); return 0;
return moves; } else {
dir[0] = '1';
getchar ();
}
}
if (dir[0] == 'm') {
printf ("\nAre you sure? [y/n] ");
scanf ("%c", &exit);
if (exit == 'y') {
return 1;
} else { } else {
dir[0] = '1'; dir[0] = '1';
getchar (); getchar ();
@ -45,7 +106,7 @@ int main () {
printm (); printm ();
end = win (); end = win ();
} }
return 0; return 1;
} }
void start () { void start () {
@ -106,20 +167,20 @@ void move (char *dir) {
if (stop == 0) { if (stop == 0) {
// change values // change values
if (*dir == 'd') { if (*dir == 'd') {
for (j = 1 + xinit; j <= 4; j++) { for (j = my_x; j < N; j++) {
if (matrix[yinit][j] != 0) { if (matrix[my_y][j] != 0) {
matrix[yinit][j] = matrix[yinit][j] - matrix[yinit][xinit]; matrix[my_y][j] = matrix[my_y][j] - matrix[yinit][xinit];
if (matrix[yinit][j] < 0 && matrix[yinit][xinit] != 0) if (matrix[my_y][j] < 0 && matrix[yinit][xinit] != 0)
matrix[yinit][j] = -matrix[yinit][j]; matrix[my_y][j] = -matrix[my_y][j];
} }
} }
} }
if (*dir == 'a') { if (*dir == 'a') {
for (j = 0; j < xinit; j++) { for (j = 0; j < xinit; j++) {
if (matrix[yinit][j] != 0) { if (matrix[my_y][j] != 0) {
matrix[yinit][j] = matrix[yinit][j] - matrix[yinit][xinit]; matrix[my_y][j] = matrix[my_y][j] - matrix[my_y][xinit];
if (matrix[yinit][j] < 0 && matrix[yinit][xinit] != 0) if (matrix[my_y][j] < 0 && matrix[yinit][xinit] != 0)
matrix[yinit][j] = -matrix[yinit][j]; matrix[my_y][j] = -matrix[my_y][j];
} }
} }
} }
@ -160,6 +221,7 @@ void nolow () {
// print matrix // print matrix
void printm () { void printm () {
int i, j; int i, j;
system("clear");
printf ("\n\n"); printf ("\n\n");
for (i = 0; i < N; i++) { for (i = 0; i < N; i++) {
for (j = 0;j < N; j++) { for (j = 0;j < N; j++) {
@ -183,10 +245,11 @@ int win () { // 20+ zeros
} }
} }
if (count >= N * (N-1)) { if (count >= N * (N-1)) {
printf ("\nYou win! Moves: %d", moves); printf ("\nYou win! Moves: %d\n", moves);
getchar ();
return 1; return 1;
}else{ }else{
printf ("\nYou still need to delete %d numbers", N * (N-1) - count); printf ("\nYou still need to delete %d numbers. Moves: %d", N * (N-1) - count, moves);
return 0; return 0;
} }
} }