mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2026-03-22 00:36:20 +00:00
feat(ai): add aggregate height calculation
This commit is contained in:
16
src/ai/moves/height.py
Normal file
16
src/ai/moves/height.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from typing import Any, Optional
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
def aggregate_height(field: np.ndarray[int, Any]) -> int:
|
||||
"""
|
||||
Calculates the aggregate height of the field.
|
||||
|
||||
Args:
|
||||
field: 2D array representing the game field.
|
||||
|
||||
Returns:
|
||||
The aggregate height of the field.
|
||||
"""
|
||||
return np.sum(field.shape[0] - np.argmax(field, axis=0))
|
||||
20
tests/ai/test_moves.py
Normal file
20
tests/ai/test_moves.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import unittest
|
||||
|
||||
import numpy as np
|
||||
from ai.moves.height import aggregate_height
|
||||
|
||||
|
||||
class TestFitness(unittest.TestCase):
|
||||
def test_aggregate_height(self):
|
||||
field = np.array(
|
||||
[
|
||||
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
|
||||
[0, 1, 1, 1, 1, 1, 1, 0, 0, 1],
|
||||
[0, 1, 1, 0, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 0, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
]
|
||||
)
|
||||
|
||||
self.assertEqual(aggregate_height(field), 48)
|
||||
Reference in New Issue
Block a user