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