mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
refactor(ai)
This commit is contained in:
parent
8283742b62
commit
25e43aeea3
@ -6,7 +6,7 @@ from .peaks import get_peaks
|
|||||||
|
|
||||||
|
|
||||||
def get_bumpiness(
|
def get_bumpiness(
|
||||||
peaks: Optional[np.ndarray], field: Optional[np.ndarray] = None
|
*, peaks: Optional[np.ndarray] = None, field: Optional[np.ndarray] = None
|
||||||
) -> int:
|
) -> int:
|
||||||
if peaks is None and field is None:
|
if peaks is None and field is None:
|
||||||
raise ValueError("peaks and field cannot both be None")
|
raise ValueError("peaks and field cannot both be None")
|
||||||
|
|||||||
@ -11,7 +11,7 @@ def get_peaks(field: np.ndarray) -> np.ndarray:
|
|||||||
|
|
||||||
|
|
||||||
def get_peaks_max(
|
def get_peaks_max(
|
||||||
peaks: Optional[np.ndarray], field: Optional[np.ndarray] = None
|
*, peaks: Optional[np.ndarray] = None, field: Optional[np.ndarray] = None
|
||||||
) -> int:
|
) -> int:
|
||||||
if peaks is None and field is None:
|
if peaks is None and field is None:
|
||||||
raise ValueError("peaks and field cannot both be None")
|
raise ValueError("peaks and field cannot both be None")
|
||||||
@ -21,7 +21,7 @@ def get_peaks_max(
|
|||||||
|
|
||||||
|
|
||||||
def get_peaks_sum(
|
def get_peaks_sum(
|
||||||
peaks: Optional[np.ndarray], field: Optional[np.ndarray] = None
|
*, peaks: Optional[np.ndarray] = None, field: Optional[np.ndarray] = None
|
||||||
) -> int:
|
) -> int:
|
||||||
if peaks is None and field is None:
|
if peaks is None and field is None:
|
||||||
raise ValueError("peaks and field cannot both be None")
|
raise ValueError("peaks and field cannot both be None")
|
||||||
|
|||||||
@ -7,7 +7,7 @@ from .peaks import get_peaks, get_peaks_max
|
|||||||
|
|
||||||
def get_row_transition(field: np.ndarray, highest_peak: Optional[int] = None) -> int:
|
def get_row_transition(field: np.ndarray, highest_peak: Optional[int] = None) -> int:
|
||||||
if highest_peak is None:
|
if highest_peak is None:
|
||||||
highest_peak = get_peaks_max(None, field)
|
highest_peak = get_peaks_max(field=field)
|
||||||
|
|
||||||
rows_to_check = slice(int(field.shape[0] - highest_peak), field.shape[0])
|
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])
|
transitions = np.sum(field[rows_to_check, 1:] != field[rows_to_check, :-1])
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import unittest
|
|||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from ai.fitness.bumpiness import get_bumpiness
|
from ai.fitness.bumpiness import get_bumpiness
|
||||||
|
from ai.fitness.holes import get_holes
|
||||||
from ai.fitness.peaks import get_peaks_sum
|
from ai.fitness.peaks import get_peaks_sum
|
||||||
from ai.fitness.transitions import (
|
from ai.fitness.transitions import (
|
||||||
get_col_transition,
|
get_col_transition,
|
||||||
@ -32,7 +33,7 @@ class TestFitness(unittest.TestCase):
|
|||||||
def test_get_peaks_sum(self) -> None:
|
def test_get_peaks_sum(self) -> None:
|
||||||
answers: tuple[int] = (11, 0, 2)
|
answers: tuple[int] = (11, 0, 2)
|
||||||
for field, answer in zip(self.fields, answers):
|
for field, answer in zip(self.fields, answers):
|
||||||
self.assertEqual(get_peaks_sum(None, field), answer)
|
self.assertEqual(get_peaks_sum(field=field), answer)
|
||||||
|
|
||||||
def test_get_row_transistions(self):
|
def test_get_row_transistions(self):
|
||||||
answers = (8, 0, 2)
|
answers = (8, 0, 2)
|
||||||
@ -47,7 +48,7 @@ class TestFitness(unittest.TestCase):
|
|||||||
def test_get_bumpiness(self):
|
def test_get_bumpiness(self):
|
||||||
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(field=field), answer)
|
||||||
|
|
||||||
def test_get_holes(self):
|
def test_get_holes(self):
|
||||||
answers = (
|
answers = (
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user