mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(ai): add get_row_transition
This commit is contained in:
parent
b762fe6774
commit
dec7c4d0e0
@ -13,14 +13,6 @@ def calculate_fitness(game: Game) -> float:
|
||||
return fitness
|
||||
|
||||
|
||||
def get_row_transitions(field: np.ndarray) -> int:
|
||||
pass
|
||||
|
||||
|
||||
def get_col_transitions(field: np.ndarray) -> int:
|
||||
pass
|
||||
|
||||
|
||||
def get_bumpiness(field: np.ndarray) -> int:
|
||||
pass
|
||||
|
||||
|
||||
16
src/ai/fitness/transitions.py
Normal file
16
src/ai/fitness/transitions.py
Normal file
@ -0,0 +1,16 @@
|
||||
import numpy as np
|
||||
|
||||
from .peaks import get_peaks_max
|
||||
|
||||
|
||||
def get_row_transitions(field: np.ndarray) -> int:
|
||||
highest_peak = get_peaks_max(field)
|
||||
|
||||
rows_to_check = slice(int(field.shape[0] - highest_peak), field.shape[0])
|
||||
transitions = np.sum(field[rows_to_check, 1:] != field[rows_to_check, :-1])
|
||||
|
||||
return int(transitions)
|
||||
|
||||
|
||||
def get_col_transitions(field: np.ndarray) -> int:
|
||||
pass
|
||||
@ -6,30 +6,30 @@ from ai.fitness.transitions import get_row_transitions
|
||||
|
||||
|
||||
class TestFitness(unittest.TestCase):
|
||||
def test_get_peaks(self) -> None:
|
||||
field = np.array(
|
||||
[
|
||||
[0, 1, 0, 0, 1],
|
||||
[1, 0, 0, 1, 0],
|
||||
[0, 1, 1, 0, 0],
|
||||
]
|
||||
)
|
||||
self.assertEqual(get_peaks_sum(field), 11)
|
||||
|
||||
def test_get_peaks_zeros(self) -> None:
|
||||
field = np.zeros((3, 5))
|
||||
self.assertEqual(get_peaks_sum(field), 0)
|
||||
|
||||
def test_single_peak(self):
|
||||
field = np.array(
|
||||
[
|
||||
[0, 0, 0, 0, 0],
|
||||
[0, 1, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0],
|
||||
]
|
||||
def setUp(self) -> None:
|
||||
self.fields: tuple[np.ndarray] = (
|
||||
np.array(
|
||||
[
|
||||
[0, 1, 0, 0, 1],
|
||||
[1, 0, 0, 1, 0],
|
||||
[0, 1, 1, 0, 0],
|
||||
]
|
||||
),
|
||||
np.zeros((3, 5)),
|
||||
np.array(
|
||||
[
|
||||
[0, 0, 0, 0, 0],
|
||||
[0, 1, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0],
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
self.assertEqual(get_peaks_sum(field), 2)
|
||||
def test_get_peaks_sum(self) -> None:
|
||||
answers: tuple[int] = (11, 0, 2)
|
||||
for field, answer in zip(self.fields, answers):
|
||||
self.assertEqual(get_peaks_sum(field), answer)
|
||||
|
||||
def test_get_row_transistions(self):
|
||||
answers = (8, 0, 2)
|
||||
for field, answer in zip(self.fields, answers):
|
||||
|
||||
Loading…
Reference in New Issue
Block a user