forked from len0rd/rockbox
This brings the puzzles source in sync with Simon's branch, commit fd304c5 (from March 2024), with some added Rockbox-specific compatibility changes: https://www.franklinwei.com/git/puzzles/commit/?h=rockbox-devel&id=516830d9d76bdfe64fe5ccf2a9b59c33f5c7c078 There are quite a lot of backend changes, including a new "Mosaic" puzzle. In addition, some new frontend changes were necessary: - New "Preferences" menu to access the user preferences system. - Enabled spacebar input for several games. Change-Id: I94c7df674089c92f32d5f07025f6a1059068af1e
73 lines
1.3 KiB
C
73 lines
1.3 KiB
C
#include <assert.h>
|
|
#include <string.h>
|
|
|
|
#include "puzzles.h"
|
|
|
|
/* horrific and doesn't check overflow. */
|
|
static long factx(long x, long y)
|
|
{
|
|
long acc = 1, i;
|
|
|
|
for (i = y; i <= x; i++)
|
|
acc *= i;
|
|
return acc;
|
|
}
|
|
|
|
void reset_combi(combi_ctx *combi)
|
|
{
|
|
int i;
|
|
combi->nleft = combi->total;
|
|
for (i = 0; i < combi->r; i++)
|
|
combi->a[i] = i;
|
|
}
|
|
|
|
combi_ctx *new_combi(int r, int n)
|
|
{
|
|
long nfr, nrf;
|
|
combi_ctx *combi;
|
|
|
|
assert(r <= n);
|
|
assert(n >= 1);
|
|
|
|
combi = snew(combi_ctx);
|
|
memset(combi, 0, sizeof(combi_ctx));
|
|
combi->r = r;
|
|
combi->n = n;
|
|
|
|
combi->a = snewn(r, int);
|
|
memset(combi->a, 0, r * sizeof(int));
|
|
|
|
nfr = factx(n, r+1);
|
|
nrf = factx(n-r, 1);
|
|
combi->total = (int)(nfr / nrf);
|
|
|
|
reset_combi(combi);
|
|
return combi;
|
|
}
|
|
|
|
/* returns NULL when we're done otherwise returns input. */
|
|
combi_ctx *next_combi(combi_ctx *combi)
|
|
{
|
|
int i = combi->r - 1, j;
|
|
|
|
if (combi->nleft == combi->total)
|
|
goto done;
|
|
else if (combi->nleft <= 0)
|
|
return NULL;
|
|
|
|
while (combi->a[i] == combi->n - combi->r + i)
|
|
i--;
|
|
combi->a[i] += 1;
|
|
for (j = i+1; j < combi->r; j++)
|
|
combi->a[j] = combi->a[i] + j - i;
|
|
|
|
done:
|
|
combi->nleft--;
|
|
return combi;
|
|
}
|
|
|
|
void free_combi(combi_ctx *combi)
|
|
{
|
|
sfree(combi->a);
|
|
sfree(combi);
|
|
}
|