def test_initial_special(self): g = tak.Position.from_config(tak.Config(size=5)) with pytest.raises(tak.IllegalMove): g.move(tak.Move(0, 0, tak.MoveType.PLACE_CAPSTONE)) with pytest.raises(tak.IllegalMove): g.move(tak.Move(0, 0, tak.MoveType.PLACE_STANDING))
def test_place_flat(self): g = tak.Position.from_config(tak.Config(size=5)) g1 = g.move(tak.Move( x=0, y=0, )) assert g.ply == 0 assert g[0, 0] == [] assert g1[0, 0] == [tak.Piece(tak.Color.BLACK, tak.Kind.FLAT)] assert g1.ply == 1 assert g1.stones[1].caps == 1 assert g1.stones[1].stones == 20 g2 = g1.move(tak.Move( x=4, y=4, )) assert g2[0, 0] == [tak.Piece(tak.Color.BLACK, tak.Kind.FLAT)] assert g2[4, 4] == [tak.Piece(tak.Color.WHITE, tak.Kind.FLAT)] assert g2.stones[0].stones == 20 assert g2.stones[1].stones == 20 g3 = g2.move(tak.Move( x=2, y=2, )) assert g3[2, 2] == [tak.Piece(tak.Color.WHITE, tak.Kind.FLAT)]
def test_initial_slide(self): g = tak.Position.from_config(tak.Config(size=5)) with pytest.raises(tak.IllegalMove): g.move(tak.Move(0, 0, tak.MoveType.SLIDE_RIGHT, (1, ))) g = g.move(tak.Move(0, 0)) with pytest.raises(tak.IllegalMove): g.move(tak.Move(0, 0, tak.MoveType.SLIDE_RIGHT, (1, )))
def test_slide_multiple(self): g = tak.Position.from_squares(tak.Config(size=5), [ [W, B, W, B], [W], [B], [B], [W], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [B], ], 2) g1 = g.move(tak.Move(0, 0, tak.MoveType.SLIDE_RIGHT, (1, 1, 1, 1))) assert g1[0, 0] == [] assert g1[1, 0] == [B, W] assert g1[2, 0] == [W, B] assert g1[3, 0] == [B, B] assert g1[4, 0] == [W, W]
def test_basic_slide(self): g = tak.Position.from_squares(tak.Config(size=5), [ [W], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [B], ], 2) g1 = g.move(tak.Move(0, 0, tak.MoveType.SLIDE_RIGHT, (1, ))) assert g1[0, 0] == [] assert g1[1, 0] == [W]
def transform_move(sym, move, size): ox, oy, _ = np.matmul(sym, [move.x, move.y, size - 1]) type = move.type if type.is_slide(): dx, dy, _ = np.matmul(sym, move.type.direction() + (0, )) type = tak.MoveType.from_direction(dx, dy) return tak.Move(int(ox), int(oy), type, move.slides)
def test_illegal_slide(self): g = tak.Position.from_squares(tak.Config(size=5), [ [W, B, W, B, W, W, W], [], [], [], [], [], [], [], [], [], [W], [], [], [], [], [], [], [], [], [], [], [], [], [], [B], ], 2) with pytest.raises(tak.IllegalMove): g.move(tak.Move(1, 1, tak.MoveType.SLIDE_RIGHT, (1, ))) with pytest.raises(tak.IllegalMove): g.move(tak.Move(0, 0, tak.MoveType.SLIDE_UP, (6, ))) with pytest.raises(tak.IllegalMove): g.move(tak.Move(0, 0, tak.MoveType.SLIDE_UP, ())) with pytest.raises(tak.IllegalMove): g.move(tak.Move(0, 0, tak.MoveType.SLIDE_LEFT, (1, ))) with pytest.raises(tak.IllegalMove): g.move(tak.Move(4, 4, tak.MoveType.SLIDE_LEFT, (1, )))
def test_place_special(self): g = tak.Position.from_squares(tak.Config(size=5), [ [W], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [B], ], 2) g1 = g.move(tak.Move(2, 2, tak.MoveType.PLACE_CAPSTONE)) assert g1[2, 2] == [WC] assert g1.stones[tak.Color.WHITE.value].caps == 0 g2 = g1.move(tak.Move(1, 2, tak.MoveType.PLACE_STANDING)) assert g2[1, 2] == [BS] with pytest.raises(tak.IllegalMove): g2.move(tak.Move(2, 3, tak.MoveType.PLACE_CAPSTONE)) with pytest.raises(tak.IllegalMove): g2.move(tak.Move(2, 2, tak.MoveType.PLACE_FLAT))
def test_smash(self): g = tak.Position.from_squares(tak.Config(size=5), [ [WC, W], [BS, W], [], [], [], [], [W], [], [], [], [W], [], [], [], [], [], [], [], [], [], [], [], [], [], [B], ], 2) for m in [ tak.Move(0, 0, tak.MoveType.SLIDE_RIGHT, (2, )), tak.Move(0, 0, tak.MoveType.SLIDE_RIGHT, (1, 1)), tak.Move(1, 1, tak.MoveType.SLIDE_DOWN, (1, )) ]: with pytest.raises(tak.IllegalMove) as exc: g.move(m) assert 'standing stone' in str(exc.value) g1 = g.move(tak.Move(0, 0, tak.MoveType.SLIDE_RIGHT, (1, ))) assert g1[1, 0] == [WC, B, W]
def test_cap_slide(self): g = tak.Position.from_squares(tak.Config(size=5), [ [WC, W], [BS, W], [], [], [], [BC], [W], [], [], [], [W], [], [], [], [], [], [], [], [], [], [], [], [], [], [B], ], 2) for m in [ tak.Move(0, 0, tak.MoveType.SLIDE_UP, (2, )), tak.Move(0, 0, tak.MoveType.SLIDE_UP, (1, 1)), tak.Move(0, 0, tak.MoveType.SLIDE_UP, (1, )), tak.Move(1, 1, tak.MoveType.SLIDE_LEFT, (1, )) ]: with pytest.raises(tak.IllegalMove) as exc: g.move(m) assert 'capstone' in str(exc.value)
def test_all_moves(): moves = set(tak.enumerate_moves(5)) assert tak.Move(0, 0) in moves assert tak.Move(0, 4) in moves assert tak.Move(4, 4) in moves assert tak.Move(0, 0, tak.MoveType.PLACE_CAPSTONE) in moves assert tak.Move(0, 0, tak.MoveType.PLACE_STANDING) in moves assert tak.Move(5, 5) not in moves assert tak.Move(0, 0, tak.MoveType.SLIDE_RIGHT, (1, 1, 1, 1)) in moves assert tak.Move(1, 0, tak.MoveType.SLIDE_RIGHT, (1, 1, 1)) in moves assert tak.Move(2, 0, tak.MoveType.SLIDE_RIGHT, (1, 1, 1)) not in moves assert all(m.type != tak.MoveType.SLIDE_LEFT for m in moves if m.x == 0) assert all(m.type != tak.MoveType.SLIDE_RIGHT for m in moves if m.x == 4) assert all(m.type != tak.MoveType.SLIDE_UP for m in moves if m.y == 4) assert all(m.type != tak.MoveType.SLIDE_DOWN for m in moves if m.y == 0)
def test_valid(self): cases = [ ( "a1", tak.Move(0, 0, tak.MoveType.PLACE_FLAT), "a1", ), ( "Sa4", tak.Move(0, 3, tak.MoveType.PLACE_STANDING), "Sa4", ), ( "Ch7", tak.Move(7, 6, tak.MoveType.PLACE_CAPSTONE), "Ch7", ), ( "Fh7", tak.Move(7, 6, tak.MoveType.PLACE_FLAT), "h7", ), ( "a1>", tak.Move(0, 0, tak.MoveType.SLIDE_RIGHT, (1,)), "a1>", ), ( "2a2<", tak.Move(0, 1, tak.MoveType.SLIDE_LEFT, (2,)), "2a2<", ), ( "3a1+111", tak.Move(0, 0, tak.MoveType.SLIDE_UP, (1, 1, 1)), "3a1+111", ), ] for case in cases: ptn, move, out = case parsed = tak.ptn.parse_move(ptn) assert parsed == move, "parse_ptn('{0}') = {1} != {2}".format( ptn, parsed, move) rt = tak.ptn.format_move(parsed) assert rt == out
def parse_move(move): m = re.search(r'\A([CFS]?)([1-8]?)([a-h])([1-8])([<>+-]?)([1-8]*)[CFS]?\Z', move) if not m: raise BadMove(move, "malformed move") stone, pickup, file, rank, dir, drops = m.groups() x = ord(file) - ord('a') y = ord(rank) - ord('1') if pickup and not dir: raise BadMove(move, "pick up but no direction") typ = None if dir: typ = slide_map[dir] else: typ = place_map[stone] slides = None if drops: slides = tuple(ord(c) - ord('0') for c in drops) if (drops or pickup) and not dir: raise BadMove(move, "pickup/drop without a direction") if dir and not pickup and not slides: pickup = '1' if pickup and not slides: slides = (int(pickup), ) if pickup and int(pickup) != sum(slides): raise BadMove( move, "inconsistent pickup and drop: {0} v {1}".format(pickup, drops)) return tak.Move(x, y, typ, slides)