Skip to content

Commit 40c2ff4

Browse files
committed
minor cleanups
1 parent 483f1bf commit 40c2ff4

2 files changed

Lines changed: 347 additions & 380 deletions

File tree

ocean/hex/hex.c

Lines changed: 81 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,86 +3,89 @@
33
#include <stdio.h>
44
#include <time.h>
55

6-
void demo()
7-
{
8-
Hex env = {0};
9-
allocate_chex(&env);
10-
c_reset(&env);
11-
c_render(&env);
12-
env.random_opponent = false;
13-
14-
int tick = 0;
15-
while(!WindowShouldClose())
16-
{
17-
bool move_made = false;
18-
19-
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
20-
{
21-
Vector2 mouse = GetMousePosition();
22-
23-
int screen_width = GetScreenWidth();
24-
int screen_height = GetScreenHeight();
25-
float radius = 22.0f;
26-
float sqrt3 = 1.73205f;
27-
float hex_width = sqrt3 * radius;
28-
float hex_height = 2.0f * radius;
29-
30-
float total_width = hex_width * BOARD_SIZE + hex_width * 0.5f * BOARD_SIZE;
31-
float total_height = hex_height * 0.75f * BOARD_SIZE;
32-
33-
float start_x = screen_width / 2.0f - total_width / 2.0f + hex_width / 2.0f;
34-
float start_y = screen_height / 2.0f - total_height / 2.0f + hex_height / 2.0f;
35-
36-
// Inverse map:
37-
int r = (int)roundf((mouse.y - start_y) / (hex_height * 0.75f));
38-
int c = (int)roundf((mouse.x - start_x) / hex_width - r * 0.5f);
39-
40-
if(r >= 0 && r < BOARD_SIZE && c >= 0 && c < BOARD_SIZE)
41-
{
42-
env.actions[0] = r * BOARD_SIZE + c;
43-
move_made = true;
44-
}
45-
}
46-
47-
if(move_made)
48-
{
49-
c_step(&env);
50-
}
51-
52-
53-
c_render(&env);
54-
tick++;
55-
}
56-
57-
free_allocated_chex(&env);
58-
c_close(&env);
6+
void allocate(Hex* env) {
7+
env->observations = (float*)calloc(2 * TOTAL_CELLS, sizeof(float));
8+
env->actions = (float*)calloc(1, sizeof(float));
9+
env->terminals = (float*)calloc(1, sizeof(float));
10+
env->rewards = (float*)calloc(1, sizeof(float));
5911
}
6012

61-
void speed_test()
62-
{
63-
Hex env = {0};
64-
allocate_chex(&env);
65-
c_reset(&env);
66-
clock_t start = clock();
67-
68-
int num_steps = 1000000;
69-
for(int i = 0; i < num_steps; i++)
70-
{
71-
env.actions[0] = compute_legal_move(&env);
72-
c_step(&env);
73-
}
74-
clock_t end = clock();
75-
double elapsed = (double)(end - start) / CLOCKS_PER_SEC;
76-
printf("Time for %d steps: %.2f seconds\n", num_steps, elapsed);
77-
printf("SPS: %.2fM\n", num_steps / elapsed / 1e6);
78-
79-
free_allocated_chex(&env);
80-
c_close(&env);
13+
void free_allocated(Hex* env) {
14+
free(env->actions);
15+
free(env->observations);
16+
free(env->terminals);
17+
free(env->rewards);
8118
}
8219

83-
int main()
84-
{
85-
demo();
86-
// speed_test();
87-
return 0;
20+
void demo() {
21+
Hex env = {0};
22+
allocate(&env);
23+
c_reset(&env);
24+
c_render(&env);
25+
env.random_opponent = false;
26+
27+
while(!WindowShouldClose()) {
28+
bool move_made = false;
29+
30+
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
31+
Vector2 mouse = GetMousePosition();
32+
33+
int screen_width = GetScreenWidth();
34+
int screen_height = GetScreenHeight();
35+
float radius = 22.0f;
36+
float sqrt3 = 1.73205f;
37+
float hex_width = sqrt3 * radius;
38+
float hex_height = 2.0f * radius;
39+
40+
float total_width = hex_width * BOARD_SIZE + hex_width * 0.5f * BOARD_SIZE;
41+
float total_height = hex_height * 0.75f * BOARD_SIZE;
42+
43+
float start_x = screen_width / 2.0f - total_width / 2.0f + hex_width / 2.0f;
44+
float start_y = screen_height / 2.0f - total_height / 2.0f + hex_height / 2.0f;
45+
46+
// Inverse map:
47+
int r = (int)roundf((mouse.y - start_y) / (hex_height * 0.75f));
48+
int c = (int)roundf((mouse.x - start_x) / hex_width - r * 0.5f);
49+
50+
if(r >= 0 && r < BOARD_SIZE && c >= 0 && c < BOARD_SIZE) {
51+
env.actions[0] = r * BOARD_SIZE + c;
52+
move_made = true;
53+
}
54+
}
55+
56+
if(move_made) {
57+
c_step(&env);
58+
}
59+
60+
c_render(&env);
61+
}
62+
63+
free_allocated(&env);
64+
c_close(&env);
65+
}
66+
67+
void speed_test() {
68+
Hex env = {0};
69+
allocate(&env);
70+
c_reset(&env);
71+
clock_t start = clock();
72+
73+
int num_steps = 1000000;
74+
for(int i = 0; i < num_steps; i++) {
75+
env.actions[0] = compute_legal_move(&env);
76+
c_step(&env);
77+
}
78+
clock_t end = clock();
79+
double elapsed = (double)(end - start) / CLOCKS_PER_SEC;
80+
printf("Time for %d steps: %.2f seconds\n", num_steps, elapsed);
81+
printf("SPS: %.2fM\n", num_steps / elapsed / 1e6);
82+
83+
free_allocated(&env);
84+
c_close(&env);
85+
}
86+
87+
int main() {
88+
demo();
89+
// speed_test();
90+
return 0;
8891
}

0 commit comments

Comments
 (0)