From 5049d4c8a05d3f9b72f4c7e048e94b753713beda Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Sat, 27 Sep 2014 12:13:38 +0200 Subject: Added navigation tree management --- qml/python/game.py | 2 +- qml/python/tests/test2.sgf | 6 ++++++ qml/python/tests/test_transformations.py | 14 +++++++++++++- qml/python/transformations.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 qml/python/tests/test2.sgf (limited to 'qml/python') diff --git a/qml/python/game.py b/qml/python/game.py index de7842a..428fa02 100644 --- a/qml/python/game.py +++ b/qml/python/game.py @@ -133,7 +133,7 @@ class Game(object): """ Create a normalized board, translated on lower coord. """ - for transformation in [Translation(self), Rotation(self), Symmetry(self)]: + for transformation in [Translation(self), Rotation(self), Symmetry(self), ToIndex(self)]: if not transformation.is_valid(): continue diff --git a/qml/python/tests/test2.sgf b/qml/python/tests/test2.sgf new file mode 100644 index 0000000..292a61f --- /dev/null +++ b/qml/python/tests/test2.sgf @@ -0,0 +1,6 @@ +(;GM[1]FF[3] +;AW[ok][qk][rl][mm][ln][pn][op][pp][rp][qq][rq][pr][qr][sr] +AB[qn][qo][ro][np][qp][mq][oq][pq];B[on] +(;W[po];B[pm];W[no];B[oo]) +(;W[oo];B[no];W[pm];B[po]) +) diff --git a/qml/python/tests/test_transformations.py b/qml/python/tests/test_transformations.py index b802b9f..d244e48 100644 --- a/qml/python/tests/test_transformations.py +++ b/qml/python/tests/test_transformations.py @@ -3,7 +3,7 @@ import unittest -from python.transformations import Rotation, Translation, Symmetry +from python.transformations import Rotation, Translation, Symmetry, ToIndex class FakeBoard(): @@ -132,3 +132,15 @@ class TestRotation(unittest.TestCase): symmetry.y_flip = False self.assertFalse(symmetry.is_valid()) + +class TestToIndex(unittest.TestCase): + """ Test the toIndex transformation. + """ + + def test_apply_points(self): + """ Test the points index. + """ + toIndex = ToIndex(FakeBoard(2, 1, 8, 5)) + self.assertEqual(0, toIndex.apply_points((2, 1))) + self.assertEqual(7, toIndex.apply_points((2, 2))) + self.assertEqual(8, toIndex.apply_points((3, 2))) diff --git a/qml/python/transformations.py b/qml/python/transformations.py index 3bb9383..40aafc0 100644 --- a/qml/python/transformations.py +++ b/qml/python/transformations.py @@ -123,3 +123,32 @@ class Symmetry(object): "RIGHT": self.board.side["LEFT" if self.x_flip else "RIGHT"], "BOTTOM":self.board.side["TOP" if self.y_flip else "BOTTOM"] } + +class ToIndex(object): + """" Transform each point position in point index. + """ + + def __init__(self, board): + self.board = board + + def is_valid(self): + """ This transformation is always valid. + """ + return True; + + def apply_points(self, coord, name = None): + """ + """ + x_size = min(19, self.board.max_x - self.board.min_x + 1) + x, y = coord + return (x - self.board.min_x) + (y - self.board.min_y) * x_size + + def get_new_size(self): + """ The size is not changed. + """ + return self.board.min_x, self.board.min_y, self.board.max_x, self.board.max_y + + def get_new_side(self): + """ There is no changes on the sides. + """ + return self.board.side -- cgit v1.2.3