mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
refactor(ai): add Optional
fix fix
This commit is contained in:
parent
4ab9b4bb2a
commit
0d4ab8aab7
@ -13,10 +13,6 @@ def calculate_fitness(game: Game) -> float:
|
|||||||
return fitness
|
return fitness
|
||||||
|
|
||||||
|
|
||||||
def get_holes(field: np.ndarray) -> int:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def get_wells(field: np.ndarray) -> int:
|
def get_wells(field: np.ndarray) -> int:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
from ai.log import log
|
from ai.log import log
|
||||||
@ -8,9 +10,21 @@ def get_peaks(field: np.ndarray) -> np.ndarray:
|
|||||||
return peaks.max(axis=0)
|
return peaks.max(axis=0)
|
||||||
|
|
||||||
|
|
||||||
def get_peaks_max(field: np.ndarray) -> int:
|
def get_peaks_max(
|
||||||
return int(np.max(get_peaks(field)))
|
peaks: Optional[np.ndarray], field: Optional[np.ndarray] = None
|
||||||
|
) -> int:
|
||||||
|
if peaks is None and field is None:
|
||||||
|
raise ValueError("peaks and field cannot both be None")
|
||||||
|
elif peaks is None:
|
||||||
|
peaks = get_peaks(field)
|
||||||
|
return int(np.max(peaks))
|
||||||
|
|
||||||
|
|
||||||
def get_peaks_sum(field: np.ndarray) -> int:
|
def get_peaks_sum(
|
||||||
return np.sum(get_peaks(field))
|
peaks: Optional[np.ndarray], field: Optional[np.ndarray] = None
|
||||||
|
) -> int:
|
||||||
|
if peaks is None and field is None:
|
||||||
|
raise ValueError("peaks and field cannot both be None")
|
||||||
|
elif peaks is None:
|
||||||
|
peaks = get_peaks(field)
|
||||||
|
return np.sum(peaks)
|
||||||
|
|||||||
@ -1,10 +1,13 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
from .peaks import get_peaks, get_peaks_max
|
from .peaks import get_peaks, get_peaks_max
|
||||||
|
|
||||||
|
|
||||||
def get_row_transition(field: np.ndarray) -> int:
|
def get_row_transition(field: np.ndarray, highest_peak: Optional[int] = None) -> int:
|
||||||
highest_peak = get_peaks_max(field)
|
if highest_peak is None:
|
||||||
|
highest_peak = get_peaks_max(None, 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])
|
||||||
@ -12,10 +15,12 @@ def get_row_transition(field: np.ndarray) -> int:
|
|||||||
return int(transitions)
|
return int(transitions)
|
||||||
|
|
||||||
|
|
||||||
def get_col_transition(field: np.ndarray) -> int:
|
def get_col_transition(field: np.ndarray, peaks: Optional[np.ndarray] = None) -> int:
|
||||||
transitions_sum = 0
|
if peaks is None:
|
||||||
peaks = get_peaks(field)
|
peaks = get_peaks(field)
|
||||||
|
|
||||||
|
transitions_sum = 0
|
||||||
|
|
||||||
for col in range(field.shape[1]):
|
for col in range(field.shape[1]):
|
||||||
if peaks[col] <= 1:
|
if peaks[col] <= 1:
|
||||||
continue
|
continue
|
||||||
|
|||||||
@ -32,7 +32,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(field), answer)
|
self.assertEqual(get_peaks_sum(None, field), answer)
|
||||||
|
|
||||||
def test_get_row_transistions(self):
|
def test_get_row_transistions(self):
|
||||||
answers = (8, 0, 2)
|
answers = (8, 0, 2)
|
||||||
@ -45,6 +45,6 @@ class TestFitness(unittest.TestCase):
|
|||||||
self.assertEqual(get_col_transition(field), answer)
|
self.assertEqual(get_col_transition(field), answer)
|
||||||
|
|
||||||
def test_get_bumpiness(self):
|
def test_get_bumpiness(self):
|
||||||
answers = (8, 0, 2)
|
answers = (5, 0, 4)
|
||||||
for field, answer in zip(self.fields, answers):
|
for field, answer in zip(self.fields, answers):
|
||||||
self.assertEqual(get_bumpiness(field), answer)
|
self.assertEqual(get_bumpiness(None, field), answer)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user