From 6c2cc134abf3f32d1d6ec172c6201f8d990c88ab Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Sun, 24 Aug 2014 12:52:10 +0200 Subject: Initial commit --- qml/python/transformations.py | 125 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 qml/python/transformations.py (limited to 'qml/python/transformations.py') diff --git a/qml/python/transformations.py b/qml/python/transformations.py new file mode 100644 index 0000000..380c30c --- /dev/null +++ b/qml/python/transformations.py @@ -0,0 +1,125 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import random +random.seed() + +class Translation(object): + """ A translation transformation. + """ + + def __init__(self, board): + """ Create a new translation from the board. + """ + self.board = board + + def is_valid(self): + """ Apply if a translation if the goban is not maximized. + """ + return self.board.min_x != 0 or self.board.min_y != 0 + + def apply_points(self, coord): + """ Move the points to the lower position. + """ + x, y = coord + return (x - self.board.min_x, y - self.board.min_y) + + def get_new_size(self): + """ The goban size does not change. + """ + return 0, 0, self.board.max_x - self.board.min_x, self.board.max_y - self.board.min_y + + def get_new_side(self): + """ There is no changes on the sides. + """ + return self.board.side + +class Rotation(object): + """ A rotation transformation. + """ + + def __init__(self, board): + """ Create a new roation from the board. + """ + self.board = board + + def is_valid(self): + """ Apply the rotation in following cases : + * if the board is widther than heigther + * randomly is width == heigth + * never if the board is heigther than widther + """ + + width, heigth = self.board.get_size() + should = heigth - width + if should == 0: + return random.randint(0, 1) == 1 + return should < 0 + + def apply_points(self, coord): + """ Apply the transformation on a point. + """ + x, y = coord + return (self.board.max_y - y, x) + + def get_new_size(self): + """ Apply the transformation on the goban size. + """ + max_x = self.board.max_y - self.board.min_y + return 0, self.board.min_x, max_x, self.board.max_x + + def get_new_side(self): + """ Apply the transformations on the sides. + """ + return { + "TOP": self.board.side["RIGHT"], + "LEFT": self.board.side["TOP"], + "RIGHT": self.board.side["BOTTOM"], + "BOTTOM":self.board.side["LEFT"] + } + +class Symmetry(object): + """ A translation transformation. + """ + + def __init__(self, board): + self.board = board + + self.x_flip = random.randint(0, 1) == 1 + self.y_flip = random.randint(0, 1) == 1 + + def is_valid(self): + """ The transformation is valid if one flip is required in one + direction. """ + return self.x_flip or self.y_flip + + def apply_points(self, coord): + """ Flip in both directions. + """ + x, y = coord + if self.x_flip: + new_x = self.board.max_x - x + self.board.min_x + else: + new_x = x + + if self.y_flip: + new_y = self.board.max_y - y + self.board.min_y + else: + new_y = y + + return (new_x, new_y) + + 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 { + "TOP": self.board.side["BOTTOM" if self.y_flip else "TOP"], + "LEFT": self.board.side["RIGHT" if self.x_flip else "LEFT"], + "RIGHT": self.board.side["LEFT" if self.x_flip else "RIGHT"], + "BOTTOM":self.board.side["TOP" if self.y_flip else "BOTTOM"] + } -- cgit v1.2.3