add: data generator

This commit is contained in:
h z
2025-01-29 21:24:10 +00:00
parent 34e9b95145
commit 05d375d521
5 changed files with 101 additions and 7 deletions

View File

@@ -0,0 +1,56 @@
import random
from Board import Board
from DataGenerator import DataGenerator
def _circle_generator(cx, cy, px, py):
yield cx + px, cy + py
yield cx - px, cy + py
yield cx + px, cy - py
yield cx - px, cy - py
yield cx + py, cy + px
yield cx - py, cy + px
yield cx + py, cy - px
yield cx - py, cy - px
def circle_generator(cx, cy, radius):
x = 0
y = radius
d = 1 - radius
for tx, ty in _circle_generator(cx, cy, x, y):
yield tx, ty
while x < y:
x += 1
if d < 0:
d += 2 * x + 1
else:
y -= 1
d += 2 * (x - y) + 1
for tx, ty in _circle_generator(cx, cy, x, y):
yield tx, ty
class CircleGenerator(DataGenerator):
def generate(self, amount: int) -> list[Board]:
res = []
generated = set()
for i in range(amount):
success = False
while not success:
success = True
b = Board(self.width, self.height, self.qx, self.qy)
cx, cy, radius = self.random_config()
while (cx, cy, radius) in generated:
cx, cy, radius = self.random_config()
for tx, ty in circle_generator(cx, cy, radius):
b.lives.add((tx, ty))
if len(b.try_evaluate()) == 0:
success = False
continue
res.append(b)
return res
def random_config(self):
cx = random.randint(min(5, self.width), self.width - 1)
cy = random.randint(min(5, self.width), self.height - 1)
radius = random.randint(2, min(self.width, self.height))
return cx, cy, radius

17
DataGenerator/__init__.py Normal file
View File

@@ -0,0 +1,17 @@
from abc import ABC, abstractmethod
from Board import Board
class DataGenerator:
def __init__(self, width, height, qx, qy):
self.width = width
self.height = height
self.qx = qx
self.qy = qy
@abstractmethod
def generate(self, amount: int) -> list[Board]:
pass