mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(ai): add get_holes
This commit is contained in:
parent
0d4ab8aab7
commit
8283742b62
22
src/ai/fitness/holes.py
Normal file
22
src/ai/fitness/holes.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
from .peaks import get_peaks
|
||||||
|
|
||||||
|
|
||||||
|
def get_holes(
|
||||||
|
field: np.ndarray,
|
||||||
|
peaks: Optional[np.array] = None,
|
||||||
|
) -> np.array:
|
||||||
|
if peaks is None:
|
||||||
|
peaks = get_peaks(field)
|
||||||
|
col_count = field.shape[1]
|
||||||
|
holes = np.zeros(col_count, dtype=int)
|
||||||
|
|
||||||
|
for col in range(col_count):
|
||||||
|
start = -peaks[col]
|
||||||
|
if start != 0:
|
||||||
|
holes[col] = np.count_nonzero(field[int(start) :, col] == 0)
|
||||||
|
|
||||||
|
return holes
|
||||||
@ -48,3 +48,12 @@ class TestFitness(unittest.TestCase):
|
|||||||
answers = (5, 0, 4)
|
answers = (5, 0, 4)
|
||||||
for field, answer in zip(self.fields, answers):
|
for field, answer in zip(self.fields, answers):
|
||||||
self.assertEqual(get_bumpiness(None, field), answer)
|
self.assertEqual(get_bumpiness(None, field), answer)
|
||||||
|
|
||||||
|
def test_get_holes(self):
|
||||||
|
answers = (
|
||||||
|
np.array([1, 1, 0, 1, 2]),
|
||||||
|
np.array([0, 0, 0, 0, 0]),
|
||||||
|
np.array([0, 1, 0, 0, 0]),
|
||||||
|
)
|
||||||
|
for field, answer in zip(self.fields, answers):
|
||||||
|
self.assertTrue(np.array_equal(get_holes(field), answer))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user