mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(ai): add get_peaks
fix(ai): `aggregate_height`
This commit is contained in:
parent
b2bea7d03a
commit
e84cacca1c
@ -1,5 +1,7 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
from .peaks import get_peaks
|
||||||
|
|
||||||
|
|
||||||
def aggregate_height(field: np.ndarray[int, np.dtype[np.uint8]]) -> int:
|
def aggregate_height(field: np.ndarray[int, np.dtype[np.uint8]]) -> int:
|
||||||
"""
|
"""
|
||||||
@ -11,10 +13,5 @@ def aggregate_height(field: np.ndarray[int, np.dtype[np.uint8]]) -> int:
|
|||||||
Returns:
|
Returns:
|
||||||
The aggregate height of the field.
|
The aggregate height of the field.
|
||||||
"""
|
"""
|
||||||
heights = np.zeros(field.shape[1], dtype=np.uint8)
|
heights = get_peaks(field)
|
||||||
for col in range(field.shape[1]):
|
|
||||||
for row in range(field.shape[0]):
|
|
||||||
if field[row, col] != 0:
|
|
||||||
heights[col] = field.shape[0] - row
|
|
||||||
break
|
|
||||||
return int(np.sum(heights))
|
return int(np.sum(heights))
|
||||||
|
|||||||
20
src/ai/heuristics/peaks.py
Normal file
20
src/ai/heuristics/peaks.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
def get_peaks(field: np.ndarray[int, np.dtype[np.uint8]]) -> np.ndarray[int, np.dtype[np.uint8]]:
|
||||||
|
"""
|
||||||
|
Calculate the peaks of a given field.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
field: 2D array representing the game field.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
2D array representing the peaks of the field.
|
||||||
|
"""
|
||||||
|
result = np.zeros(field.shape[1], dtype=np.uint8)
|
||||||
|
for col in range(field.shape[1]):
|
||||||
|
for row in range(field.shape[0]):
|
||||||
|
if field[row, col] != 0:
|
||||||
|
result[col] = field.shape[0] - row
|
||||||
|
break
|
||||||
|
return result
|
||||||
Loading…
Reference in New Issue
Block a user