summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-26 00:43:29 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-26 00:43:29 -0700
commit6d3583f9811d2c7b184d0197c78e6f20cfc93549 (patch)
treef44a42e043bba8159bb7634563d45097c537a5b0
parent656be88ba3b0db383101449d7d2f09348601d7d7 (diff)
downloaddartboat-6d3583f9811d2c7b184d0197c78e6f20cfc93549.tar.gz
dartboat-6d3583f9811d2c7b184d0197c78e6f20cfc93549.tar.xz
move difficulty-to-stdev logic to lib
-rw-r--r--comp.c38
-rw-r--r--comp.h1
-rw-r--r--dartboat.c9
-rw-r--r--web/web_opts.c37
4 files changed, 45 insertions, 40 deletions
diff --git a/comp.c b/comp.c
index 012eea1..6b4ec26 100644
--- a/comp.c
+++ b/comp.c
@@ -9,6 +9,42 @@
double horizontal_stdev = 16, vertical_stdev = 16;
+#define NUM_DIFF_PRESETS (sizeof(DIFF_PRESETS) / sizeof(*DIFF_PRESETS))
+static struct { int diff; double stdev; } DIFF_PRESETS[] = {
+ { 0, 66.80}, // 10
+ { 8, 40.24}, // 20
+ {16, 28.14}, // 30
+ {24, 21.35}, // 40
+ {32, 17.20}, // 50
+ {40, 14.34}, // 60
+ {48, 12.15}, // 70
+ {56, 10.36}, // 80
+ {64, 8.83}, // 90
+ {72, 7.48}, // 100
+ {80, 6.32}, // 110
+ {88, 5.34}, // 120
+ {99, 0.00}, // 167
+};
+
+// optional function to set stdevs using precomputed 0-99 difficulty values.
+// fixed-step interpolation is done between presets.
+void comp_set_difficulty(int diff)
+{
+ if (diff < DIFF_PRESETS[0].diff)
+ diff = DIFF_PRESETS[0].diff;
+ else if (diff > DIFF_PRESETS[NUM_DIFF_PRESETS - 1].diff)
+ diff = DIFF_PRESETS[NUM_DIFF_PRESETS - 1].diff;
+
+ size_t ind = 1;
+ while (ind < NUM_DIFF_PRESETS - 1 && DIFF_PRESETS[ind].diff <= diff)
+ ind++;
+
+ double step = (DIFF_PRESETS[ind - 1].stdev - DIFF_PRESETS[ind].stdev) /
+ (DIFF_PRESETS[ind].diff - DIFF_PRESETS[ind - 1].diff);
+ horizontal_stdev = vertical_stdev = DIFF_PRESETS[ind - 1].stdev -
+ ((diff - DIFF_PRESETS[ind - 1].diff) * step);
+}
+
static double drand()
{
return (double)rand() / RAND_MAX;
@@ -63,7 +99,7 @@ static struct pcoords throw_dart(struct pcoords target, struct ccoords *cc)
return cart_to_pol(*cc);
}
-struct segment next_dart(int rem, int darts_in_hand)
+static struct segment next_dart(int rem, int darts_in_hand)
{
char *c = NULL;
if (rem <= NUM_CHECKOUTS)
diff --git a/comp.h b/comp.h
index 4792516..792eb3c 100644
--- a/comp.h
+++ b/comp.h
@@ -7,6 +7,7 @@ extern double horizontal_stdev, vertical_stdev;
struct ccoords pol_to_cart(struct pcoords c);
+void comp_set_difficulty(int diff);
void comp_visit(struct leg *l);
#endif
diff --git a/dartboat.c b/dartboat.c
index fdaf6d1..952b5b7 100644
--- a/dartboat.c
+++ b/dartboat.c
@@ -138,21 +138,22 @@ void test_averages()
{
int rounds = 100000;
- for (int stdev = 4; stdev < 25; ++stdev) {
- horizontal_stdev = vertical_stdev = stdev;
+ for (int diff = 0; diff <= 99; ++diff) {
+ comp_set_difficulty(diff);
int darts = 0;
for (int i = 0; i < rounds; ++i) {
struct leg *l = leg_init(501);
while (l->rem > 0)
comp_visit(l);
- leg_free(l);
darts += (l->n_visits - 1) * 3 +
l->visits[l->n_visits-1].n_darts;
+ leg_free(l);
}
- printf("%d %f\n", stdev, (double)(501*rounds)/darts*3);
+ printf("%d %f %f\n", diff, horizontal_stdev,
+ (double)(501 * rounds) / darts * 3);
}
}
diff --git a/web/web_opts.c b/web/web_opts.c
index b593306..b880fd3 100644
--- a/web/web_opts.c
+++ b/web/web_opts.c
@@ -16,51 +16,18 @@
int delay_ms = 1000;
static int difficulty = 32;
-static struct { int diff; double stdev; } DIFF_PRESETS[] = {
- { 0, 66.80}, // 10
- { 8, 40.24}, // 20
- {16, 28.14}, // 30
- {24, 21.35}, // 40
- {32, 17.20}, // 50
- {40, 14.34}, // 60
- {48, 12.15}, // 70
- {56, 10.36}, // 80
- {64, 8.83}, // 90
- {72, 7.48}, // 100
- {80, 6.32}, // 110
- {88, 5.34}, // 120
- {99, 0.00}, // 167
-};
-#define NUM_DIFF_PRESETS (sizeof(DIFF_PRESETS) / sizeof(*DIFF_PRESETS))
static void set_delay(char *val)
{
delay_ms = atoi(val);
}
-static void set_stdev(int diff)
-{
- if (diff < DIFF_PRESETS[0].diff)
- diff = DIFF_PRESETS[0].diff;
- else if (diff > DIFF_PRESETS[NUM_DIFF_PRESETS - 1].diff)
- diff = DIFF_PRESETS[NUM_DIFF_PRESETS - 1].diff;
-
- size_t ind = 1;
- while (ind < NUM_DIFF_PRESETS - 1 && DIFF_PRESETS[ind].diff <= diff)
- ind++;
-
- double step = (DIFF_PRESETS[ind - 1].stdev - DIFF_PRESETS[ind].stdev) /
- (DIFF_PRESETS[ind].diff - DIFF_PRESETS[ind - 1].diff);
- horizontal_stdev = vertical_stdev = DIFF_PRESETS[ind - 1].stdev -
- ((diff - DIFF_PRESETS[ind - 1].diff) * step);
-}
-
static void set_difficulty(char *val)
{
int diff = atoi(val);
difficulty = diff < 0 ? 0 : diff > 99 ? 99 : diff;
- set_stdev(difficulty);
+ comp_set_difficulty(difficulty);
}
static char *prefix_opt(char *opt)
@@ -105,7 +72,7 @@ void opts_init()
{
read_delay();
if (!read_difficulty())
- set_stdev(difficulty);
+ comp_set_difficulty(difficulty);
char buf[64];
sprintf(buf, "%d", delay_ms);