*****************************************************/
_random_position() {
- /* Returns {x, y} as percentages within the center pool,
- * keeping tiles away from edges. */
- return {
- x: 5 + Math.random() * 80,
- y: 5 + Math.random() * 75
- };
+ /* Pick the best of several random candidates by choosing the
+ * one with the most distance from existing tiles. */
+ const positions = this.state.tile_positions;
+ const existing = Object.values(positions);
+ let best = null, best_dist = -1;
+
+ for (let i = 0; i < 10; i++) {
+ const candidate = {
+ x: 5 + Math.random() * 80,
+ y: 5 + Math.random() * 75
+ };
+ let min_dist = Infinity;
+ for (const pos of existing) {
+ const dx = candidate.x - pos.x;
+ const dy = candidate.y - pos.y;
+ min_dist = Math.min(min_dist, dx * dx + dy * dy);
+ }
+ if (min_dist > best_dist) {
+ best = candidate;
+ best_dist = min_dist;
+ }
+ }
+ return best || { x: 5 + Math.random() * 80, y: 5 + Math.random() * 75 };
}
/*****************************************************