Update roofus.c

This commit is contained in:
Racoonicorn 2016-12-19 14:02:06 +01:00 committed by GitHub
parent 5f2820b6e2
commit a1ac74174f
1 changed files with 101 additions and 96 deletions

197
roofus.c
View File

@ -1,192 +1,197 @@
#define VERSION "0.01" #define VERSION "0.02"
#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 start(); void start ();
void printm(); void printm ();
void move(char * dir); void move (char *dir);
void nolow(); void nolow ();
int win(); int win ();
int error(); int error ();
int matrix[N][N]; int matrix[N][N];
int my_x=N/2; int my_x = N / 2;
int my_y=N/2; int my_y = N / 2;
int moves = 0; int moves = 0;
int main () { int main () {
char dir[20]; char dir[20];
char exit; char exit;
int end=0; int end = 0;
srand (time(NULL)); srand (time(NULL));
start(); start ();
printm(); printm ();
while(end==0){ while (end == 0) {
printf("\nDirection? "); printf ("\nLeft: a. Up: w. Right: d. Down: s. Exit: 0");
scanf("%s", dir); printf ("\nDirection? ");
getchar(); scanf ("%s", dir);
if(dir[0]=='0'){ getchar ();
printf("\nExit? [y/n] "); if (dir[0] == '0') {
scanf("%c", &exit); printf ("\nExit? [y/n] ");
if(exit=='y'){ scanf ("%c", &exit);
printf("\n\n\n\nv%s", VERSION); if (exit == 'y') {
printf ("\n\n\n\nv%s", VERSION);
return moves; return moves;
}else{ } else {
dir[0]='1'; dir[0] = '1';
getchar(); getchar ();
} }
} }
move(dir); move (dir);
printm(); printm ();
end = win(); end = win ();
} }
return 0; return 0;
} }
void start(){ void start () {
int i, j; int i, j;
for (i=0; i<N; i++){ for (i = 0; i < N; i++) {
for (j=0;j<N; j++){ for (j = 0; j < N; j++) {
matrix[i][j]= rand () % 19 + 1; matrix[i][j] = rand () % 19 + 1;
} }
} }
nolow(); nolow ();
} }
void move(char * dir){ void move (char *dir) {
int xinit, yinit; int xinit, yinit;
int stop=0; int stop = 0;
int i, j; int i, j;
xinit=my_x; xinit = my_x;
yinit=my_y; yinit = my_y;
switch (dir[0]){ // move pointer
switch (dir[0]) {
case 'a': case 'a':
if(my_x != 0){ if (my_x != 0) {
my_x--; my_x--;
}else{ } else {
stop=error(); stop = error ();
} }
break; break;
case 'd': case 'd':
if(my_x < N-1){ if (my_x < N-1) {
my_x++; my_x++;
}else{ } else {
stop=error(); stop = error ();
} }
break; break;
case 'w': case 'w':
if(my_y >0){ if (my_y > 0) {
my_y--; my_y--;
}else{ } else {
stop=error(); stop = error ();
} }
break; break;
case 's': case 's':
if(my_y < N-1){ if(my_y < N-1) {
my_y++; my_y++;
}else{ } else {
stop=error(); stop = error ();
} }
break; break;
case 'o': case 'o':
stop=1; stop = 1;
break; break;
default: default:
stop=error(); stop = error ();
break; break;
} }
if(stop==0){ if (stop == 0) {
// change values
if(*dir == 'd'){ if (*dir == 'd') {
for (j=1+xinit;j<=4; j++){ for (j = 1 + xinit; j <= 4; j++) {
if(matrix[yinit][j]!=0){ if (matrix[yinit][j] != 0) {
matrix[yinit][j]= matrix[yinit][j] - matrix[yinit][xinit]; matrix[yinit][j] = matrix[yinit][j] - matrix[yinit][xinit];
if(matrix[yinit][j]<0 && matrix[yinit][xinit]!=0) if (matrix[yinit][j] < 0 && matrix[yinit][xinit] != 0)
matrix[yinit][j] = -matrix[yinit][j]; matrix[yinit][j] = -matrix[yinit][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[yinit][j] != 0) {
matrix[yinit][j]= matrix[yinit][j] - matrix[yinit][xinit]; matrix[yinit][j] = matrix[yinit][j] - matrix[yinit][xinit];
if(matrix[yinit][j]<0 && matrix[yinit][xinit]!=0) if (matrix[yinit][j] < 0 && matrix[yinit][xinit] != 0)
matrix[yinit][j] = -matrix[yinit][j]; matrix[yinit][j] = -matrix[yinit][j];
} }
} }
} }
if(*dir == 'w'){ if (*dir == 'w'){
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];
if(matrix[i][my_x]<0 && matrix[yinit][xinit]!=0) if (matrix[i][my_x] < 0 && matrix[yinit][xinit] != 0)
matrix[i][my_x] = -matrix[i][my_x]; matrix[i][my_x] = -matrix[i][my_x];
} }
} }
} }
if(*dir == 's'){ if (*dir == 's'){
for (i=my_y;i<N; i++){ for (i = my_y; i < N; 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];
if(matrix[i][my_x]<0 && matrix[yinit][xinit]!=0) if (matrix[i][my_x] < 0 && matrix[yinit][xinit] != 0)
matrix[i][my_x] = -matrix[i][my_x]; matrix[i][my_x] = -matrix[i][my_x];
} }
} }
} }
nolow(); nolow ();
moves++; moves++;
} }
} }
void nolow(){ // if 1 or 2 -> random number
void nolow () {
int i, j; int i, j;
for(i=0;i< N;i++){ for (i = 0; i < N; i++) {
for(j=0;j< N;j++){ for (j = 0; j < N; j++) {
if(matrix[i][j]<3 && matrix[i][j]>0) if ( matrix[i][j] < 3 && matrix[i][j] > 0)
matrix[i][j] = (rand()%10)-10; matrix[i][j] = rand () % 10 - 10;
} }
} }
} }
void printm(){ // print matrix
void printm () {
int i, j; int i, j;
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++) {
if (i==my_y && j == my_x) if (i == my_y && j == my_x)
printf ("> "); printf ("> ");
if(matrix[i][j]!=0) if (matrix[i][j] != 0)
printf("%d\t", matrix[i][j]); printf ("%d\t", matrix[i][j]);
else else
printf(" \t"); printf (" \t");
} }
printf("\n\n"); printf ("\n\n");
} }
} }
int win(){ int win () { // 20+ zeros
int i, j, count=0; int i, j, count=0;
for (i=0; i<N; i++){ for (i = 0; i < N; i++) {
for (j=0;j<N; j++){ for (j = 0; j < N; j++) {
if (matrix[i][j]==0) if (matrix[i][j] == 0)
count++; count++;
} }
} }
if (count >= N*(N-1)){ if (count >= N * (N-1)) {
printf("\nYou win! Moves: %d", moves); printf ("\nYou win! Moves: %d", moves);
return 1; return 1;
}else{ }else{
printf ("\nYou still need to delete %d numbers", N * (N-1) - count);
return 0; return 0;
} }
} }
int error(){ int error () {
printf("ERROR!!!"); printf ("ERROR!!!");
return 1; return 1;
} }