All pastes #499547 Raw Edit

Stuff

public text v1 · immutable
·published 2007-05-21 03:16 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct group 
{
    char c[4];
    int enabled;
    int num;
};

static struct group groups[] = 
{
    "LRlr", 0xf, 4,
    "UDud", 0xf, 4,
    "BFbf", 0xf, 4
};

static int ngroups = sizeof groups / sizeof(struct group);

static int group_len;
static int toto;

static int myrand(int n)
{
    return random() % n;
}

static void add_scramble(char c)
{
    int mod;

    mod = myrand(3);
    if (mod == 0)
        printf("%c ", c);
    else if (mod == 1)
        printf("%c' ", c);
    else
        printf("%c2 ", c);
}

static void enable_group(int group)
{
    toto += group_len - groups[group].num;
    groups[group].enabled = (1 << group_len) - 1;
    groups[group].num = group_len;
}

static char gen_new(int *group)
{
    int i;
    int g;
    int r;
    int numen = 0;

    numen = toto;
    
    r = myrand(numen);

    for (g = 0; g < ngroups; g++)
    {
        if (r < groups[g].num)
        {
            for (i = 0; i < group_len; i++)
            {
                if (groups[g].enabled & (1 << i))
                {
                    if (r-- == 0)
                    {
                        groups[g].enabled &= ~(1 << i);
                        groups[g].num--;
                        --toto;
                        *group = g;
                        return groups[g].c[i];
                    }
                }
            }
        }
        r -= groups[g].num;
    }
    
    printf("Oh noes!\n");
    exit(1);
}

int main(int argc, char *argv[])
{
    int last_group;
    int num;
    int i;

    if (strcmp(argv[0], "./4x4") == 0)
        group_len = 4;
    else
        group_len = 2;
#if 0    
    else
    {
        fprintf(stderr, "What kind of scramble is this?\n");
        return 1;
    }
#endif    
    
    if (argc < 2)
    {
        num = 20;
    }
    else
    {
        num = atoi(argv[1]);
        if (num < 1)
            num = 1;
#if 0        
        else if (num > 100)
            num = 100;
#endif        
    }

    /* Seed the random number generator. */
    srandom((int)time(NULL));

    for (i = 0; i < ngroups; i++)
        groups[i].num = group_len;
    toto = ngroups * group_len;

    for (i = 0; i < num; i++)
    {
        int group;
        add_scramble(gen_new(&group));
        if (i > 0 && group != last_group)
        {
            /* Re-enable the last group. */
            enable_group(last_group);
        }
        last_group = group;
    }
    printf("\n");
    return 0;
}