Esempio n. 1
0
    def escape_el_stats(
        cls, pos
    ):  #[OCCUPIED from w, OCCUPIED from b, 1 move w to occupied, 1 move b to occupied, free muerte escape line, # muerte line with just B, # muerte line with just W]
        stats = {
            PLAYER1: np.zeros(2, dtype=int),
            PLAYER2: np.zeros(2, dtype=int),
            KING_VALUE: np.zeros(2, dtype=int)
        }
        block_stats = {
            PLAYER1: np.zeros(3, dtype=int),
            PLAYER2: np.zeros(3, dtype=int),
            KING_VALUE: np.zeros(3, dtype=int)
        }  # b:[blocking black, blocking white, blocking king] w:same k:same
        free_esc_seg = np.zeros(3, dtype=int)
        possible_free_line = [
            _indices[2], _indices[-3],
            _indices.transpose()[2],
            _indices.transpose()[-3]
        ]

        pos = pos.flatten()
        for w in winning_el:
            if pos[w]:  #if w pos is full it will be impossible get it on
                stats[pos[w]][0] += 1
                continue
            for pms in possible_move_segments[w]:
                segment = pos[pms]
                if not segment.sum():  #avoid board segment filtered
                    continue
                c = np.bincount(segment, minlength=4)
                if c[1:].sum() == 1 and segment[-1] != 0:  #1 move check
                    if cls.pos_update(pos, pms[-1])[pms[1:-1]].sum() == 0:
                        stats[segment[-1]][1] += 1
                elif c[1:].sum() == 2 and segment[
                        -1] != 0:  #Direct Blocking check [win_el, 0, ..., ->#<-, 0, ..., #]
                    cutted_seg = segment[1:-1]
                    if (cutted_seg == cls.pos_update(
                            pos, pms[-1])[pms[1:-1]]).all():
                        cutted_seg = cutted_seg[cutted_seg > 0]
                        if len(cutted_seg) == 1:
                            block_stats[cutted_seg[0]][segment[-1] - 1] += 1

        for pfl in possible_free_line:
            ln_seg = pos[pfl]
            if ln_seg.sum() == 0:
                free_esc_seg[0] += 1
            else:
                ln_seg[ln_seg == KING_VALUE] = PLAYER2
                c = np.bincount(ln_seg, minlength=3)
                if c[PLAYER1] == 0:
                    free_esc_seg[2] += 1
                elif c[PLAYER2] == 0:
                    free_esc_seg[1] += 1

        return np.asarray([np.asarray(stats[x], dtype=int)
                           for x in stats]), np.asarray([
                               np.asarray(block_stats[x], dtype=int)
                               for x in block_stats
                           ]), free_esc_seg
Esempio n. 2
0
def test_tables_possiblemovesegments():
    #Horizontal random check
    r = randint(0, row - 1)
    lung = randint(2, col)
    start_el = randint(0, col - lung)
    check_move = _indices[r][start_el:start_el + lung]
    assert len(
        check_move) <= col, "Horizontal move can not be longer than cols"
    i = check_move[0]
    ret = False
    for move in possible_move_segments[i]:
        if (len(check_move) == move.size and (check_move == move).all()):
            ret = True
    assert ret, "horizontal move %s is not in possible_move_segments[%s]" % (
        check_move, i)

    #Vertical random check
    r = randint(0, col - 1)
    lung = randint(2, row)
    start_el = randint(0, row - lung)
    check_move = _indices.transpose()[r][start_el:start_el + lung]
    assert len(check_move) <= row, "Vertical move can not be longer than rows"
    i = check_move[0]
    ret = False
    for move in possible_move_segments[i]:
        if (len(check_move) == move.size and (check_move == move).all()):
            ret = True
    assert ret, "vertical move %s is not in possible_move_segments[%s]" % (
        check_move, i)
Esempio n. 3
0
def test_tables_capturesegments():
    assert capture_segments.size != 0, "capture_segments.size is equal to 0, so why are you using this module/package with a table board without captures?"
    r = randint(0, row - 1)
    if _indices[:][r].size > 3:
        c = randint(0, row - 3)
        assert _indices[:][r][
            c:c +
            3] in capture_segments, "%s not in capture_segments" % _indices[:][
                r][c:c + 3]

    c = randint(0, col - 1)
    if _indices.transpose()[:][c].size > 3:
        r = randint(0, row - 3)
        assert _indices.transpose()[:][c][
            r:r +
            3] in capture_segments, "%s not in capture_segments" % _indices[:][
                c][r:r + 3]

    assert len(capture_segments) == (
        (col - 2) * row + (row - 2) *
        col), "col:%s row:%s capture_segments does not have %s elements" % (
            col, row, (col - 2) * row + (row - 2) * col)
Esempio n. 4
0
def test_tables_crosscentersegments():
    count = np.zeros(6, dtype=int)
    assert len(
        cross_center_segments
    ) == col * row, "cross_center_segments must be an index square with %s elements index" % (
        col * row)
    for cross in cross_center_segments:
        assert cross.size > 1, "Every cross must contain at least 2 elements"
        assert cross.size <= 5, "Every cross must contain less or equal 5 elements"
        count[cross.size] += 1

    corners = 4
    per = ((col - 2) * 2) + ((row - 2) * 2)
    oths = col * row - per - corners
    assert count[3] == corners, "the 4 corners must contains 3 cross elements"
    assert count[4] == per, "the perimeters cross elements must be %s" % per
    assert count[5] == oths, "the inside elements must be %s" % oths

    left_corners = {_indices[0][0]}
    right_corners = {_indices[0][-1]}
    left_corners.add(_indices[-1][0])
    right_corners.add(_indices[-1][-1])

    uppper_per = set([])
    bottom_per = set([])
    left_per = set([])
    right_per = set([])
    uppper_per.update(_indices[0][1:-1])
    bottom_per.update(_indices[-1][1:-1])
    left_per.update(_indices.transpose()[0][1:-1])
    right_per.update(_indices.transpose()[-1][1:-1])

    oths = set(_indices.flatten())
    oths -= uppper_per
    oths -= bottom_per
    oths -= left_per
    oths -= right_per
    oths -= left_corners
    oths -= right_corners

    for lc in left_corners:
        assert lc == cross_center_segments[lc][
            0], "index %s must be also in the array contained in cross_center_segments[%s]" % (
                lc, lc)
        assert len(cross_center_segments[lc]) == 3
        assert (lc + 1 in cross_center_segments[lc])

    for rc in right_corners:
        assert rc == cross_center_segments[rc][
            0], "index %s must be also in the array contained in cross_center_segments[%s]" % (
                rc, rc)
        assert len(cross_center_segments[rc]) == 3
        assert (rc - 1 in cross_center_segments[rc])

    for up in uppper_per:
        assert up == cross_center_segments[up][
            0], "index %s must be also in the array contained in cross_center_segments[%s]" % (
                up, up)
        assert len(cross_center_segments[up]) == 4
        assert (up - 1 in cross_center_segments[up])
        assert (up + 1 in cross_center_segments[up])
        assert (up + col in cross_center_segments[up])

    for bp in bottom_per:
        assert bp == cross_center_segments[bp][
            0], "index %s must be also in the array contained in cross_center_segments[%s]" % (
                up, up)
        assert len(cross_center_segments[bp]) == 4
        assert (bp - 1 in cross_center_segments[bp])
        assert (bp + 1 in cross_center_segments[bp])
        assert (bp - col in cross_center_segments[bp])

    for lp in left_per:
        assert lp == cross_center_segments[lp][
            0], "index %s must be also in the array contained in cross_center_segments[%s]" % (
                up, up)
        assert len(cross_center_segments[lp]) == 4
        assert (lp - col in cross_center_segments[lp])
        assert (lp + 1 in cross_center_segments[lp])
        assert (lp + col in cross_center_segments[lp])

    for rp in right_per:
        assert rp == cross_center_segments[rp][
            0], "index %s must be also in the array contained in cross_center_segments[%s]" % (
                up, up)
        assert len(cross_center_segments[rp]) == 4
        assert (rp - col in cross_center_segments[rp])
        assert (rp - 1 in cross_center_segments[rp])
        assert (rp + col in cross_center_segments[rp])

    for ot in oths:
        assert ot == cross_center_segments[ot][
            0], "index %s must be also in the array contained in cross_center_segments[%s]" % (
                ot, ot)
        assert len(cross_center_segments[ot]) == 5
        assert (ot + 1 in cross_center_segments[ot])
        assert (ot - 1 in cross_center_segments[ot])
        assert (ot + col in cross_center_segments[ot])
        assert (ot - col in cross_center_segments[ot])
Esempio n. 5
0
                 [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0]])

throne_el = 40  #king.flatten().dot(_indices.flatten())


def add_camp(line1, line2):
    seg = line1[int(len(line1) / 2) - 1:int(len(line1) / 2) + 2]
    seg = np.append(seg, line2[int(len(line2) / 2)])
    camps.append(seg)
    for c in seg:
        camp_segments[c] = seg


add_camp(_indices[0], _indices[1])
add_camp(_indices.transpose()[-1], _indices.transpose()[-2])
add_camp(_indices[-1], _indices[-2])
add_camp(_indices.transpose()[0], _indices.transpose()[1])


# --- WINNING WHITE ELEMENTS ---
def add_winning_el(line):
    for x in line:
        if x not in winning_el and len(camp_segments[x]) == 0:
            winning_el.append(x)


#every border elements
add_winning_el(_indices[0][1:-1])
add_winning_el(_indices[-1][1:-1])
add_winning_el(_indices.transpose()[0][1:-1])
Esempio n. 6
0
# -*- coding: utf-8 -*-
import numpy as np
from vablut.modules.tables import _indices, move_segments, capture_segments, rev_segments, possible_move_segments, near_center_segments, cross_center_segments
from vablut.modules.ashton import PLAYER1, PLAYER2, DRAW, COMPUTE, KING_VALUE, throne_el, blacks, whites, king, king_capture_segments, winning_el, prohibited_segments, capturing_dic

COL = int(len(_indices[0]))
ROW = int(len(_indices.transpose()[0]))


class WrongMoveError(Exception):
    pass


class Board(object):
    def __init__(self,
                 pos=None,
                 stm=PLAYER2,
                 end=COMPUTE,
                 last_move=None,
                 draw_dic=None):
        if pos is None:
            pos = {PLAYER1: blacks, PLAYER2: (whites, king)}

        self._pos = pos
        self._stm = stm
        self._draw_dic = draw_dic
        if end == COMPUTE:
            self._end = self._check_end(self.pos, last_move, draw_dic)
        else:
            self._end = end