feat(ai): add complete lines calculation

This commit is contained in:
Kristofers Solo 2024-01-06 18:01:07 +02:00
parent 41cef03f50
commit 1b2158cb25
4 changed files with 29 additions and 8 deletions

View File

@ -1,4 +1,4 @@
from typing import Any, Optional
from typing import Any
import numpy as np

16
src/ai/moves/lines.py Normal file
View File

@ -0,0 +1,16 @@
from typing import Any
import numpy as np
def complete_lines(field: np.ndarray[int, Any]) -> int:
"""
Calculates the number of complete lines in the field.
Args:
field: 2D array representing the game field.
Returns:
The number of complete lines in the field.
"""
return np.sum(np.all(field, axis=1))

View File

@ -36,12 +36,12 @@ class TestFitness(unittest.TestCase):
for field, answer in zip(self.fields, answers):
self.assertEqual(get_peaks_sum(field=field), answer)
def test_get_row_transistions(self):
def test_get_row_transistions(self) -> None:
answers = (8, 0, 2)
for field, answer in zip(self.fields, answers):
self.assertEqual(get_row_transition(field), answer)
def test_get_col_transistions(self):
def test_get_col_transistions(self) -> None:
answers = (5, 0, 1)
for field, answer in zip(self.fields, answers):
self.assertEqual(get_col_transition(field), answer)
@ -51,7 +51,7 @@ class TestFitness(unittest.TestCase):
for field, answer in zip(self.fields, answers):
self.assertEqual(get_bumpiness(field=field), answer)
def test_get_holes(self):
def test_get_holes(self) -> None:
answers = (
np.array([1, 1, 0, 1, 2]),
np.array([0, 0, 0, 0, 0]),
@ -60,7 +60,7 @@ class TestFitness(unittest.TestCase):
for field, answer in zip(self.fields, answers):
self.assertTrue(np.array_equal(get_holes(field), answer))
def test_get_wells(self):
def test_get_wells(self) -> None:
answers = (
np.array([1, 0, 2, 1, 0]),
np.array([0, 0, 0, 0, 0]),

View File

@ -2,11 +2,12 @@ import unittest
import numpy as np
from ai.moves.height import aggregate_height
from ai.moves.lines import complete_lines
class TestFitness(unittest.TestCase):
def test_aggregate_height(self):
field = np.array(
def setUp(self) -> None:
self.field = np.array(
[
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0, 0, 1],
@ -17,4 +18,8 @@ class TestFitness(unittest.TestCase):
]
)
self.assertEqual(aggregate_height(field), 48)
def test_aggregate_height(self) -> None:
self.assertEqual(aggregate_height(self.field), 48)
def test_complete_lines(self) -> None:
self.assertEqual(complete_lines(self.field), 2)