mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(ai): add bumpiness calculation
This commit is contained in:
parent
31efe9e265
commit
e9cd973360
16
src/ai/moves/bumpiness.py
Normal file
16
src/ai/moves/bumpiness.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
def bumpiness(
|
||||||
|
field: np.ndarray[int, np.dtype[np.uint8]],
|
||||||
|
) -> int:
|
||||||
|
"""
|
||||||
|
Calculate the bumpiness of a given signal based on peaks.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
field: The game field.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The bumpiness of the field.
|
||||||
|
"""
|
||||||
|
return int(np.sum(np.abs(np.diff(field.shape[0] - np.argmax(field, axis=0)))))
|
||||||
@ -1,5 +1,3 @@
|
|||||||
from typing import Optional
|
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from ai.moves.bumpiness import bumpiness
|
||||||
from ai.moves.height import aggregate_height
|
from ai.moves.height import aggregate_height
|
||||||
from ai.moves.holes import holes
|
from ai.moves.holes import holes
|
||||||
from ai.moves.lines import complete_lines
|
from ai.moves.lines import complete_lines
|
||||||
@ -27,3 +28,6 @@ class TestFitness(unittest.TestCase):
|
|||||||
|
|
||||||
def test_holes(self) -> None:
|
def test_holes(self) -> None:
|
||||||
self.assertEqual(holes(self.field), 2)
|
self.assertEqual(holes(self.field), 2)
|
||||||
|
|
||||||
|
def test_bumpiness(self) -> None:
|
||||||
|
self.assertEqual(bumpiness(self.field), 6)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user