lundi 19 octobre 2020

C Array Randomly Shrinks

I'm writing a solution to the 'flea problem' on Project Euler (https://projecteuler.net/problem=213). The 'movement' of the 'fleas' and all that works fine. However, after a few turns (different amount every time) the array shrink to half of the original size (30 to 15 or 16, 20 to 10 or 11).

Any help would be appreciated.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main() {
//https://projecteuler.net/problem=213

int grid[30][30];
int size = sizeof(grid) / sizeof(grid[0]);

for(int x = 0; x < size; x++) { //Setting the fleas down.
    for (int y = 0; y < size; y++) {
        grid[x][y] = 1;
    }
}

srand(time(NULL)); //Setting the random stuff

for(int turn = 1; turn <= 50; turn++) {
    for(int x = 0; x < size; x++) { //Showing the full grid every turn.
        for (int y = 0; y < size; y++) {
            printf("%d", grid[x][y]);
        }
        printf("\n");
    }

    printf("\n\n\n");

    for(int x = 0; x < size; x++) {
        for(int y = 0; y < size; y++) {
            for(int flea = 0; flea < grid[x][y]; flea++) { //Moving the fleas.
                grid[x][y]--; //Taking them off of their cell.

                int direction = rand() % 4; // 3 is up, 2 is down, 1 is left, 0 is right

                while (x == 0 && y == 0 && (direction == 3 || direction == 1)) { //Grab a new direction if it's at the corner and wants to go off.
                    direction = rand() % 4;
                }
                while (x == 0 && y == size && (direction == 3 || direction == 0)) {
                    direction = rand() % 4;
                }
                while (x == size && y == 0 && (direction == 2 || direction == 1)) {
                    direction = rand() % 4;
                }
                while (x == size && y == size && (direction == 2 || direction == 0)) {
                    direction = rand() % 4;
                }

                while (x == 0 && direction == 3) { //Same, but for the edge.
                    direction = rand() % 4;
                }
                while (x == size && direction == 2) {
                    direction = rand() % 4;
                }
                while (y == 0 && direction == 1) {
                    direction = rand() % 4;
                }
                while (y == size && direction == 0) {
                    direction = rand() % 4;
                }

                switch (direction) {
                    case 3: //Up
                        grid[x][y - 1]++;

                        break;

                    case 2: //Down
                        grid[x][y + 1]++;

                        break;

                    case 1: //Left
                        grid[x - 1][y]++;

                        break;

                    case 0: //Right
                        grid[x + 1][y]++;

                        break;
                }
            }
        }
    }
}

int empty = 0;

for (int x = 0; x < size; x++) { //Counting the empties.
    for (int y = 0; y < size; y++) {
        if(grid[x][y] == 0) {
            empty++;
        }
    }
}

printf("%d", empty);

return 0;
}



Aucun commentaire:

Enregistrer un commentaire