mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(ai): add complete lines calculation
This commit is contained in:
parent
41cef03f50
commit
1b2158cb25
@ -1,4 +1,4 @@
|
|||||||
from typing import Any, Optional
|
from typing import Any
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|||||||
16
src/ai/moves/lines.py
Normal file
16
src/ai/moves/lines.py
Normal 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))
|
||||||
@ -36,12 +36,12 @@ class TestFitness(unittest.TestCase):
|
|||||||
for field, answer in zip(self.fields, answers):
|
for field, answer in zip(self.fields, answers):
|
||||||
self.assertEqual(get_peaks_sum(field=field), answer)
|
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)
|
answers = (8, 0, 2)
|
||||||
for field, answer in zip(self.fields, answers):
|
for field, answer in zip(self.fields, answers):
|
||||||
self.assertEqual(get_row_transition(field), answer)
|
self.assertEqual(get_row_transition(field), answer)
|
||||||
|
|
||||||
def test_get_col_transistions(self):
|
def test_get_col_transistions(self) -> None:
|
||||||
answers = (5, 0, 1)
|
answers = (5, 0, 1)
|
||||||
for field, answer in zip(self.fields, answers):
|
for field, answer in zip(self.fields, answers):
|
||||||
self.assertEqual(get_col_transition(field), answer)
|
self.assertEqual(get_col_transition(field), answer)
|
||||||
@ -51,7 +51,7 @@ class TestFitness(unittest.TestCase):
|
|||||||
for field, answer in zip(self.fields, answers):
|
for field, answer in zip(self.fields, answers):
|
||||||
self.assertEqual(get_bumpiness(field=field), answer)
|
self.assertEqual(get_bumpiness(field=field), answer)
|
||||||
|
|
||||||
def test_get_holes(self):
|
def test_get_holes(self) -> None:
|
||||||
answers = (
|
answers = (
|
||||||
np.array([1, 1, 0, 1, 2]),
|
np.array([1, 1, 0, 1, 2]),
|
||||||
np.array([0, 0, 0, 0, 0]),
|
np.array([0, 0, 0, 0, 0]),
|
||||||
@ -60,7 +60,7 @@ class TestFitness(unittest.TestCase):
|
|||||||
for field, answer in zip(self.fields, answers):
|
for field, answer in zip(self.fields, answers):
|
||||||
self.assertTrue(np.array_equal(get_holes(field), answer))
|
self.assertTrue(np.array_equal(get_holes(field), answer))
|
||||||
|
|
||||||
def test_get_wells(self):
|
def test_get_wells(self) -> None:
|
||||||
answers = (
|
answers = (
|
||||||
np.array([1, 0, 2, 1, 0]),
|
np.array([1, 0, 2, 1, 0]),
|
||||||
np.array([0, 0, 0, 0, 0]),
|
np.array([0, 0, 0, 0, 0]),
|
||||||
|
|||||||
@ -2,11 +2,12 @@ import unittest
|
|||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from ai.moves.height import aggregate_height
|
from ai.moves.height import aggregate_height
|
||||||
|
from ai.moves.lines import complete_lines
|
||||||
|
|
||||||
|
|
||||||
class TestFitness(unittest.TestCase):
|
class TestFitness(unittest.TestCase):
|
||||||
def test_aggregate_height(self):
|
def setUp(self) -> None:
|
||||||
field = np.array(
|
self.field = np.array(
|
||||||
[
|
[
|
||||||
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
|
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
|
||||||
[0, 1, 1, 1, 1, 1, 1, 0, 0, 1],
|
[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)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user