예제 #1
0
def umkdash(mv):
    assert tag(mv) in ["-", "x"]
    assert not tag(mv) == "-" or man.isEmpty(cmn(mv))
    assert not tag(mv) == "x" or man.isMan(cmn(mv))
    assert man.isMan(board.theBoard[nsq(mv)])
    assert board.valid()

    # maintain KingAt and Men sets
    if color(mv) == 'white':
        board.whiteMen.remove(nsq(mv))
        board.whiteMen.add(osq(mv))
        if board.theBoard[nsq(mv)] == man.king:
            board.whiteKingAt = osq(mv)
    else:
        board.blackMen.remove(nsq(mv))
        board.blackMen.add(osq(mv))
        if board.theBoard[nsq(mv)] == -man.king:
            board.blackKingAt = osq(mv)

    # update the board
    zobrist.PlaceMan(osq(mv), board.theBoard[nsq(mv)])
    #board.theBoard[osq(mv)] = board.theBoard[nsq(mv)]
    assert not tag(mv) == "-" or man.isEmpty(cmn(mv))
    assert not tag(mv) == "x" or man.isMan(cmn(mv))
    zobrist.PlaceMan(nsq(mv), cmn(mv))
    #board.theBoard[nsq(mv)] = cmn(mv)

    # maintain history
    del board.history[hmc(mv)]
    assert not tag(mv) == "-" or board.valid()
예제 #2
0
def mkdashpromote(mv):
    assert tag(mv) in ["-/", "x/"]
    assert man.isPawn(board.manAt(osq(mv)))
    assert board.manAt(nsq(mv)) == cmn(mv)
    assert not color(mv) == 'white' or man.whiteQRBN(pmn(mv))
    assert not color(mv) == 'black' or man.blackQRBN(pmn(mv))
    assert not color(mv) == 'white' or board.manAt(osq(mv)) == man.pawn
    assert not color(mv) == 'black' or board.manAt(osq(mv)) == -man.pawn
    assert not tag(mv) == "-/" or cmn(mv) == 0
    assert not tag(mv) == "x/" or cmn(mv) != 0
    assert board.valid()
    # update board
    zobrist.PlaceMan(osq(mv), 0)
    #board.theBoard[osq(mv)] = 0
    zobrist.PlaceMan(nsq(mv), pmn(mv))
    #board.theBoard[nsq(mv)] = pmn(mv)
    # maintain Men
    if color(mv) == 'white':
        board.whiteMen.remove(osq(mv))
        board.whiteMen.add(nsq(mv))
    else:
        board.blackMen.remove(osq(mv))
        board.blackMen.add(nsq(mv))
    board.history[hmc(mv)] = mv
    assert not tag(mv) == "-" or board.valid()
예제 #3
0
def make(mv):
    assert board.valid()
    # Save flags first since move may affect flags - note inverse symmetry with umake
    # fixes an earlier bug where I did it last.  Lesson: follow the math patterns!  -(AB)=(-B)(-A)

    board.pushFlags()
    ty = tag(mv)
    if ty == "-":
        mkdash(mv)
    elif ty == "x":
        mkcapture(mv)
    elif ty == "OO":
        mkOO(mv)
    elif ty == "OOO":
        mkOOO(mv)
    elif ty == "-/":
        mkdashpromote(mv)
    elif ty == "x/":
        mkcapturepromote(mv)
    elif ty == "xep":
        mkxep(mv)
    elif ty == "null":
        mknull(mv)
    else:
        print "makemv: bad move type: %s" % mv[1]
    assert board.valid()
예제 #4
0
    def _handle_moving(self, event: pygame.event):
        if ModeFeature.AI in self.mode.features:
            if self.board.current_player == self.board.player_B:
                return

        mouse_click_position = event.pos
        pos_x, pos_y = [
            get_pos(pos, self.elem_size) for pos in mouse_click_position
        ]

        if self.active_pos is None:
            if valid(pos_x, pos_y,
                     self.board.size_map) and self.board[pos_x,
                                                         pos_y] is not None:
                if self.board[pos_x,
                              pos_y].player is self.board.current_player:
                    self.active_pos = (pos_x, pos_y)
        else:
            if self.board.do_move(*self.active_pos, pos_x,
                                  pos_y) or self.active_pos == (pos_x, pos_y):
                self.active_pos = None
            elif valid(pos_x, pos_y, self.board.size_map):
                if self.board[pos_x, pos_y] is not None and \
                        self.board[pos_x, pos_y].player is self.board.current_player:
                    self.active_pos = (pos_x, pos_y)
예제 #5
0
def umkcapturepromote(mv):
    assert tag(mv) == "x/"
    assert board.valid()
    umkdashpromote(mv)
    if color(mv) == 'white':
        board.blackMen.add(nsq(mv))
    else:
        board.whiteMen.add(nsq(mv))
    assert board.valid()
예제 #6
0
def main():
    global autoshow, verbose
    alphabeta.silent = False
    board.reset()
    show()
    line = raw_input(prompt)
    toks = line.split(" ")
    cmd = toks[0]
    while cmd != "quit":
        board.valid()  # agressive validation
        # find and execute command
        if cmd in ["h", "help"]:
            help()
        elif cmd in ["s", "show"]:
            show()
        elif cmd in ["l", "legals"]:
            legals()
        elif cmd in ["a", "allow"]:
            allow()
        elif cmd in ["n", "new"]:
            new()
        elif cmd in ["", "b", "best"]:
            mybest()
        elif cmd in ["r", "random"]:
            rand()
        elif cmd in ["hammer"]:
            hammer()
        elif cmd in ["showoff"]:
            autoshow = False
        elif cmd in ["showon"]:
            autoshow = True
        elif cmd in ["verboseon"]:
            verbose = True
        elif cmd in ["verboseoff"]:
            verbose = False
        elif cmd in ["find"]:
            if len(toks) == 1:
                s = raw_input("enter move> ")
            else:
                s = toks[1]
            find(s)
        elif cmd in ["u", "undo"]:
            undo()
        elif cmd in ["d", "dump"]:
            board.dump()
        else:
            trymv(cmd)
        # get next command
        line = raw_input(prompt)
        toks = line.split(" ")
        cmd = toks[0]
예제 #7
0
def test_bishop():
    mode = GameMode()
    mode.set_size(8)
    mode.set_arrangement([(Unit(BishopMover()), 2, 2),
                          (Unit(BishopMover()), 3, 3)])
    board = Board(mode)
    x = 2
    y = 2
    assert board.do_pofig_move(2, 2, 4, 4) is False
    for i in range(8):
        for j in range(8):
            if i != x and j != y:
                board[i, j] = None

    for dx in range(-10, 10):
        for dy in range(-10, 10):
            if dx == 0 and dy == 0:
                continue
            to_x = x + dx
            to_y = y + dy
            if valid(to_x, to_y, 8):
                if abs(dx) == abs(dy):
                    assert board.do_pofig_move(x, y, to_x, to_y) is True
                    assert board.do_pofig_move(to_x, to_y, x, y) is True
                else:
                    assert board.do_pofig_move(x, y, to_x, to_y) is False
예제 #8
0
def mybest_static(silent=False):
    mvlist = legal.moves()
    if len(mvlist) > 0:
        if board.whiteToMove():
            mv, score = best.forWhite(mvlist)
        else:
            mv, score = best.forBlack(mvlist)
        if not silent:
            if move.color(mv) == 'white':
                msg = "After %s %s [%s]" % (move.num(mv), san.pr(
                    mv, mvlist), score)
            else:
                msg = "After %s ...%s [%s]" % (move.num(mv), san.pr(
                    mv, mvlist), score)
        move.make(mv)
        if not silent:
            print msg
            auto()
        if verbose:
            board.dump()
            assert board.valid()  # aggressive validation
        return True
    else:
        print "no moves"
        return False
예제 #9
0
def test_swapper():
    mode = GameMode()
    arrangement = [(Unit(SwapMover()), 2, 2)]
    mode.set_arrangement(arrangement)
    mode.set_size(8)
    board = Board(mode)

    board[5, 5] = None
    x = 2
    y = 2
    for dx in range(-10, 10):
        for dy in range(-10, 10):
            if dx == 0 and dy == 0:
                continue
            to_x = x + dx
            to_y = y + dy
            if valid(to_x, to_y, 8):
                assert board.do_pofig_move(x, y, to_x, to_y) is False

    x = 1
    y = 1
    arrangement.clear()
    arrangement.append((Unit(SwapMover()), 1, 1))
    for dx in range(-1, 2):
        for dy in range(-1, 2):
            if dx == 0 and dy == 0:
                continue
            arrangement.append((Unit(KingMover()), x + dx, y + dy))

    mode.set_arrangement(arrangement)
    board = Board(mode)

    for i, j in product(range(5, 8), range(5, 8)):
        assert board.do_pofig_move(x, y, i, j) is True
        assert board.do_pofig_move(i, j, x, y) is True
예제 #10
0
def mkdash(mv):
    assert tag(mv) in ["-", "x"]
    assert not tag(mv) == "-" or man.isEmpty(cmn(mv))
    assert not tag(mv) == "x" or man.isMan(cmn(mv))
    assert man.isMan(board.manAt(osq(mv)))
    assert board.valid()

    # maintain allow flags
    if color(mv) == 'white':
        if osq(mv) == e1 and board.manAt(e1) == man.king:
            board.whiteAllowOO = False
            board.whiteAllowOOO = False
        elif osq(mv) == h1 and board.manAt(h1) == man.rook:
            board.whiteAllowOO = False
        elif osq(mv) == a1 and board.manAt(a1) == man.rook:
            board.whiteAllowOOO = False
    else:
        if osq(mv) == e8 and board.manAt(e8) == -man.king:
            board.blackAllowOO = False
            board.blackAllowOOO = False
        elif osq(mv) == h8 and board.manAt(h8) == -man.rook:
            board.blackAllowOO = False
        elif osq(mv) == a8 and board.manAt(a8) == -man.rook:
            board.blackAllowOOO = False

    # maintain KingAt and Men sets
    if color(mv) == 'white':
        board.whiteMen.remove(osq(mv))
        board.whiteMen.add(nsq(mv))
        if board.manAt(osq(mv)) == man.king:
            board.whiteKingAt = nsq(mv)
    else:
        board.blackMen.remove(osq(mv))
        board.blackMen.add(nsq(mv))
        if board.manAt(osq(mv)) == -man.king:
            board.blackKingAt = nsq(mv)

    # update board
    zobrist.PlaceMan(nsq(mv), board.theBoard[osq(mv)])
    zobrist.PlaceMan(osq(mv), 0)

    #board.theBoard[nsq(mv)] = board.theBoard[osq(mv)]
    #board.theBoard[osq(mv)] = 0

    # update history
    board.history[hmc(mv)] = mv
    assert not tag(mv) == "-" or board.valid()
예제 #11
0
def umkcapture(mv):
    assert tag(mv) == "x"
    assert man.isMan(mn(mv))
    assert not color(mv) == 'white' or man.isBlack(mn(mv))
    assert not color(mv) == 'black' or man.isWhite(mn(mv))
    assert board.valid()
    umkdash(mv)
    # board.theBoard[nsq(mv)] = cmn(mv)  # WHAT ???
    # note the theBoard will be invalid at this point
    if color(mv) == 'white':
        assert man.isBlack(board.manAt(nsq(mv)))
        board.blackMen.add(nsq(mv))
    else:
        assert man.isWhite(board.manAt(nsq(mv)))
        board.whiteMen.add(nsq(mv))
    # after the above board maintennance it will be valid again
    assert board.valid()
예제 #12
0
def mkOO(mv):
    assert tag(mv) == "OO"
    assert board.valid()
    if color(mv) == 'white':
        assert osq(mv) == e1
        assert board.whiteAllowOO
        zobrist.PlaceMan(e1, 0)
        #board.theBoard[e1] = 0
        zobrist.PlaceMan(f1, man.rook)
        #board.theBoard[f1] = man.rook
        zobrist.PlaceMan(g1, man.king)
        #board.theBoard[g1] = man.king
        zobrist.PlaceMan(h1, 0)
        #board.theBoard[h1] = 0
        board.whiteMen.remove(e1)
        board.whiteMen.add(f1)
        board.whiteMen.add(g1)
        board.whiteMen.remove(h1)
        board.whiteKingAt = g1
        board.whiteAllowOO = False  # not need since board.popFlags()  will fix
        board.whiteAllowOOO = False  # not need since board.popFlags()  will fix
    else:
        assert color(mv) == 'black'
        assert osq(mv) == e8
        assert board.blackAllowOO
        zobrist.PlaceMan(e8, 0)
        #board.theBoard[e8] = 0
        zobrist.PlaceMan(f8, -man.rook)
        #board.theBoard[f8] = -man.rook
        zobrist.PlaceMan(g8, -man.king)
        #board.theBoard[g8] = -man.king
        zobrist.PlaceMan(h8, 0)
        #board.theBoard[h8] = 0
        board.blackMen.remove(e8)
        board.blackMen.add(f8)
        board.blackMen.add(g8)
        if not h8 in board.blackMen:
            print "move.271:"
            board.dump()
        board.blackMen.remove(h8)
        board.blackKingAt = g8
        board.blackAllowOO = False  # not need since board.popFlags()  will fix
        board.blackAllowOOO = False  # not need since board.popFlags()  will fix
    board.history[hmc(mv)] = mv
    assert board.valid()
예제 #13
0
def mkcapturepromote(mv):
    assert tag(mv) == "x/"
    assert board.valid()
    mkdashpromote(mv)
    if color(mv) == 'white':
        board.blackMen.remove(nsq(mv))
        # capturing rooks on original squares affects castling
        if nsq(mv) == h8:
            board.blackAllowOO = False
        elif nsq(mv) == a8:
            board.blackAllowOOO = False
    else:
        board.whiteMen.remove(nsq(mv))
        # capturing rooks on original squares affects castling
        if nsq(mv) == h1:
            board.whiteAllowOO = False
        elif nsq(mv) == a1:
            board.whiteAllowOOO = False
    assert board.valid()
예제 #14
0
def umkOOO(mv):
    assert tag(mv) == "OOO"
    assert board.valid()
    if color(mv) == 'white':
        assert osq(mv) == e1
        assert not board.whiteAllowOO
        assert not board.whiteAllowOOO
        zobrist.PlaceMan(e1, man.king)
        #board.theBoard[e1] = man.king
        zobrist.PlaceMan(d1, 0)
        #board.theBoard[d1] = 0
        zobrist.PlaceMan(c1, 0)
        #board.theBoard[c1] =  0
        zobrist.PlaceMan(a1, man.rook)
        #board.theBoard[a1] = man.rook
        board.whiteMen.add(e1)
        board.whiteMen.remove(d1)
        board.whiteMen.remove(c1)
        board.whiteMen.add(a1)
        board.whiteKingAt = e1
        board.whiteAllowOOO = True  # not need since board.popFlags()  will fix
    else:
        assert osq(mv) == e8
        assert not board.blackAllowOO
        assert not board.blackAllowOOO
        zobrist.PlaceMan(e8, -man.king)
        #board.theBoard[e8] = -man.king
        zobrist.PlaceMan(d8, 0)
        #board.theBoard[d8] = 0
        zobrist.PlaceMan(c8, 0)
        #board.theBoard[c8] = 0
        zobrist.PlaceMan(a8, -man.rook)
        #board.theBoard[a8] = -man.rook
        board.blackMen.add(e8)
        board.blackMen.remove(d8)
        board.blackMen.remove(c8)
        board.blackMen.add(a8)
        board.blackKingAt = e8
        board.blackAllowOOO = True  # not need since board.popFlags()  will fix
    del board.history[hmc(mv)]
    assert board.valid()
예제 #15
0
def umkOO(mv):
    assert tag(mv) == "OO"
    assert board.valid()
    if color(mv) == 'white':
        assert osq(mv) == e1
        assert not board.whiteAllowOO
        assert not board.whiteAllowOOO
        zobrist.PlaceMan(e1, man.king)
        #board.theBoard[e1] = man.king
        zobrist.PlaceMan(f1, 0)
        #board.theBoard[f1] = 0
        zobrist.PlaceMan(g1, 0)
        #board.theBoard[g1] = 0
        zobrist.PlaceMan(h1, man.rook)
        #board.theBoard[h1] = man.rook
        board.whiteMen.add(e1)
        board.whiteMen.remove(f1)
        board.whiteMen.remove(g1)
        board.whiteMen.add(h1)
        board.whiteKingAt = e1
        board.whiteAllowOO = True  # not need since board.popFlags()  will fix
    else:
        assert osq(mv) == e8
        assert not board.blackAllowOO
        assert not board.blackAllowOOO
        zobrist.PlaceMan(e8, -man.king)
        #board.theBoard[e8] = -man.king
        zobrist.PlaceMan(f8, 0)
        #board.theBoard[f8] = 0
        zobrist.PlaceMan(g8, 0)
        #board.theBoard[g8] = 0
        zobrist.PlaceMan(h8, -man.rook)
        #board.theBoard[h8] = -man.rook
        board.blackMen.add(e8)
        board.blackMen.remove(f8)
        board.blackMen.remove(g8)
        board.blackMen.add(h8)
        board.blackKingAt = e8
        board.blackAllowOO = True  # not need since board.popFlags()  will fix
    del board.history[hmc(mv)]
    assert board.valid()
예제 #16
0
def mkOOO(mv):
    assert tag(mv) == "OOO"
    assert board.valid()
    if color(mv) == 'white':
        assert osq(mv) == e1
        assert board.whiteAllowOOO
        zobrist.PlaceMan(e1, 0)
        #board.theBoard[e1] = 0
        zobrist.PlaceMan(d1, man.rook)
        #board.theBoard[d1] = man.rook
        zobrist.PlaceMan(c1, man.king)
        #board.theBoard[c1] = man.king
        zobrist.PlaceMan(a1, 0)
        #board.theBoard[a1] = 0
        board.whiteMen.remove(e1)
        board.whiteMen.add(d1)
        board.whiteMen.add(c1)
        board.whiteMen.remove(a1)
        board.whiteKingAt = c1
        board.whiteAllowOO = False
        board.whiteAllowOOO = False
    else:
        assert osq(mv) == e8
        assert board.blackAllowOOO
        zobrist.PlaceMan(e8, 0)
        #board.theBoard[e8] = 0
        zobrist.PlaceMan(d8, -man.rook)
        #board.theBoard[d8] = -man.rook
        zobrist.PlaceMan(c8, -man.king)
        #board.theBoard[c8] = -man.king
        zobrist.PlaceMan(a8, 0)
        #board.theBoard[a8] = 0
        board.blackMen.remove(e8)
        board.blackMen.add(d8)
        board.blackMen.add(c8)
        board.blackMen.remove(a8)
        board.blackKingAt = c8
        board.blackAllowOO = False
        board.blackAllowOOO = False
    board.history[hmc(mv)] = mv
    assert board.valid()
예제 #17
0
def mkcapture(mv):
    assert tag(mv) == "x"
    assert not color(mv) == 'white' or man.isBlack(board.manAt(nsq(mv)))
    assert not color(mv) == 'black' or man.isWhite(board.manAt(nsq(mv)))
    assert board.valid()
    assert not man.isKing(board.manAt(nsq(mv)))
    mkdash(mv)
    if color(mv) == 'white':
        board.blackMen.remove(nsq(mv))  # <<--------
        # capturing rooks on original squares affects castling
        if nsq(mv) == h8:
            board.blackAllowOO = False
        elif nsq(mv) == a8:
            board.blackAllowOOO = False
    else:
        board.whiteMen.remove(nsq(mv))
        # capturing rooks on original squares affects castling
        if nsq(mv) == h1:
            board.whiteAllowOO = False
        elif nsq(mv) == a1:
            board.whiteAllowOOO = False
    assert board.valid()
예제 #18
0
def umkdashpromote(mv):
    assert tag(mv) in ["-/", "x/"]
    assert board.manAt(osq(mv)) == 0
    assert board.manAt(nsq(mv)) == pmn(mv)
    assert not color(mv) == 'white' or man.whiteQRBN(pmn(mv))
    assert not color(mv) == 'black' or man.blackQRBN(pmn(mv))
    assert not tag(mv) == "-/" or cmn(mv) == 0
    assert not tag(mv) == "x/" or cmn(mv) != 0
    assert board.valid()
    zobrist.PlaceMan(nsq(mv), cmn(mv))
    #board.theBoard[nsq(mv)] = cmn(mv)
    if color(mv) == 'white':
        zobrist.PlaceMan(osq(mv), man.pawn)
        #board.theBoard[osq(mv)] = man.pawn
        board.whiteMen.remove(nsq(mv))
        board.whiteMen.add(osq(mv))
    else:
        zobrist.PlaceMan(osq(mv), -man.pawn)
        #board.theBoard[osq(mv)] = -man.pawn
        board.blackMen.remove(nsq(mv))
        board.blackMen.add(osq(mv))
    del board.history[hmc(mv)]
    assert not tag(mv) == "-" or board.valid()
예제 #19
0
def umake(mv):
    assert board.valid()
    ty = tag(mv)
    if ty == "-":
        umkdash(mv)
    elif ty == "x":
        umkcapture(mv)
    elif ty == "-/":
        umkdashpromote(mv)
    elif ty == "x/":
        umkcapturepromote(mv)
    elif ty == "xep":
        umkxep(mv)
    elif ty == "OO":
        umkOO(mv)
    elif ty == "OOO":
        umkOOO(mv)
    elif ty == "null":
        umknull(mv)
    else:
        print "makemv: bad move type: %s" % mv[1]
    # pop flags last -- in keeping with reverse order of inverting functions
    board.popFlags()
    assert board.valid()
예제 #20
0
def test_usual():
    mode = GameMode()
    mode.set_size(8)
    mode.set_arrangement([(Unit(UsualMover()), 2, 2)])
    x = 2
    y = 2
    board = Board(mode)
    for dx in range(-10, 10):
        for dy in range(-10, 10):
            if dx == 0 and dy == 0:
                continue
            to_x = x + dx
            to_y = y + dy
            if valid(to_x, to_y, 8):
                if abs(dx) <= 1 and abs(dy) <= 1 and dx * dy == 0:
                    assert board.do_pofig_move(x, y, to_x, to_y) is True
                    assert board.do_pofig_move(to_x, to_y, x, y) is True
                else:
                    assert board.do_pofig_move(x, y, to_x, to_y) is False

    arrangement = [(Unit(UsualMover()), 4, 4)]
    x = 4
    y = 4
    for dx in range(-1, 2):
        for dy in range(-1, 2):
            if (dx == 0 and dy == 0) or dx * dy != 0:
                continue
            arrangement.append((Unit(PawnMover()), x + dx, y + dy))

    mode.set_size(20)
    mode.set_arrangement(arrangement)

    board = Board(mode)

    for dx in range(-2, 3, 2):
        for dy in range(-2, 3, 2):
            if (dx == 0 and dy == 0) or dx * dy != 0:
                continue
            to_x = x + dx
            to_y = y + dy
            assert board.do_pofig_move(x, y, to_x, to_y) is True
            assert board.do_pofig_move(to_x, to_y, x, y) is True
예제 #21
0
def rand(silent=False):
    mvlist = legal.moves()
    if len(mvlist) > 0:
        #n = random.randint(0,len(mvlist)-1)
        mv = random.choice(mvlist)
        if not silent:
            if move.color(mv) == 'white':
                msg = "After %s %s" % (move.num(mv), san.pr(mv, mvlist))
            else:
                msg = "After %s ...%s" % (move.num(mv), san.pr(mv, mvlist))
        move.make(mv)
        if not silent:
            print msg
            auto()
        if verbose:
            board.dump()
            assert board.valid()  # aggressive validation
        return True
    else:
        print "no moves"
        return False
예제 #22
0
def mybest():
    mvlist = legal.moves()
    if len(mvlist) > 0:
        #mv,score,cnt,sec = alphabeta.best(mvlist)
        mv, score, cnt, sec = searchfunction(mvlist)
        if not silent:
            if move.color(mv) == 'white':
                fmt = "After %s %s [%s] searched %s positions in %s sec"
            else:
                fmt = "After %s ...%s [%s] searched %s positions in %s sec"
            print fmt % (move.num(mv), san.pr(mv, mvlist), score, cnt, sec)
            print "ttHits=%s   |WTT|=%s  |BTT|=%s" % (
                alphabeta.ttHits, len(alphabeta.WTT), len(alphabeta.BTT))
        move.make(mv)
        auto()
        if verbose:
            board.dump()
            assert board.valid()  # aggressive validation
        return True
    else:
        print "no moves"
        return False
예제 #23
0
def test_pawn():
    mode = GameMode()
    mode.set_size(8)
    arrangement = [(Unit(PawnMover()), 2, 2)]
    mode.set_arrangement(arrangement)
    board = Board(mode)

    x = 2
    y = 2

    for dx in range(-10, 10):
        for dy in range(-10, 10):
            if dx == 0 and dy == 0:
                continue
            to_x = x + dx
            to_y = y + dy
            if valid(to_x, to_y, 8):
                if abs(dx) <= 1 and abs(dy) <= 1 and dx * dy == 0:
                    assert board.do_pofig_move(x, y, to_x, to_y) is True
                    assert board.do_pofig_move(to_x, to_y, x, y) is True
                else:
                    assert board.do_pofig_move(x, y, to_x, to_y) is False
예제 #24
0
def test_rook():
    mode = GameMode()
    mode.set_size(8)
    mode.set_arrangement([(Unit(RookMover()), 2, 2),
                          (Unit(RookMover()), 2, 3)])
    board = Board(mode)
    x = 2
    y = 2
    assert board.do_pofig_move(2, 2, 2, 6) is False
    board[2, 3] = None

    for dx in range(-10, 10):
        for dy in range(-10, 10):
            if dx == dy == 0:
                continue
            to_x = x + dx
            to_y = y + dy
            if valid(to_x, to_y, 8):
                if dx == 0 or dy == 0:
                    assert board.do_pofig_move(x, y, to_x, to_y) is True
                    assert board.do_pofig_move(to_x, to_y, x, y) is True
                else:
                    assert board.do_pofig_move(x, y, to_x, to_y) is False
예제 #25
0
파일: hill.py 프로젝트: tomdora/cs440Ass1
	def climb(self, iterations):
		start = time.time()
		
		n_max = self.puzzle.boardSize - 1
		newPuzzle = copy.deepcopy(self.puzzle)
		count = 1
		
		while iterations > 0:
			# print('Iteration: ' + str(count))
			eval1 = evaluate.evaluate(self.puzzle.boardBuilt, self.puzzle.boardSize).value
			i_r = random.randint(0, n_max)
			
			if i_r == n_max:
				j_r = random.randint(0, (n_max - 1))
			else:
				j_r = random.randint(0, n_max)
				
			newPuzzle.boardBuilt[i_r][j_r] = board.valid(i_r, j_r, self.puzzle.boardSize)
			
			eval2 = evaluate.evaluate(newPuzzle.boardBuilt, self.puzzle.boardSize).value
			
			if eval2 >= eval1:
				# print('Hill Climbing mutation better')
				self.puzzle = copy.deepcopy(newPuzzle)
				eval = evaluate.evaluate(self.puzzle.boardBuilt, self.puzzle.boardSize).value
			else:
				# print('Original puzzle better or as good')
				eval = evaluate.evaluate(self.puzzle.boardBuilt, self.puzzle.boardSize).value
				
			count += 1
			iterations -= 1
			
		self.score = evaluate.evaluate(self.puzzle.boardBuilt, self.puzzle.boardSize).value
		
		end = time.time()
		
		self.evalTime = (end - start) * 1000
예제 #26
0
def test_king():
    mode = GameMode()
    mode.set_size(8)
    mode.set_arrangement([(Unit(KingMover()), 2, 2),
                          (Unit(KingMover()), 1, 1)])
    board = Board(mode)
    x = 2
    y = 2

    assert board.do_pofig_move(2, 2, 1, 1) is False
    board[1, 1] = None

    for dx in range(-10, 10):
        for dy in range(-10, 10):
            if dx == dy == 0:
                continue
            to_x = x + dx
            to_y = y + dy
            if valid(to_x, to_y, 8):
                if abs(dx) <= 1 and abs(dy) <= 1:
                    assert board.do_pofig_move(x, y, to_x, to_y) is True
                    assert board.do_pofig_move(to_x, to_y, x, y) is True
                else:
                    assert board.do_pofig_move(x, y, to_x, to_y) is False
예제 #27
0
def test_police():
    mode = GameMode()
    mode.set_size(15)
    arrangement = [(Unit(PoliceManMover()), 4, 4)]
    x = 4
    y = 4
    for dx in range(-1, 2):
        for dy in range(-1, 2):
            if dx != 0 or dy != 0 or dx != dy:
                arrangement.append((Unit(KingMover()), x + dx, y + dy))
    mode.set_arrangement(arrangement)
    board = Board(mode)

    for i in range(15):
        for j in range(15):
            if board[i, j] is not None and board[i,
                                                 j].player == board.player_B:
                board[i, j] = None

    for dx in range(-1, 2):
        for dy in range(-1, 2):
            p_x = x + dx
            p_y = y + dy
            for d2x in range(-1, 2):
                for d2y in range(-1, 2):
                    if board[p_x + dx, p_y + dy] is None:
                        assert board.do_pofig_move(p_x, p_y, p_x + dx,
                                                   p_y + dy) is True
                        assert board.do_pofig_move(p_x + dx, p_y + dy, p_x,
                                                   p_y) is True

    for i in range(15):
        for j in range(15):
            if board[i, j] is not None and (i != 4 or j != 4):
                board[i, j].player = board.player_B

    for dx in range(-1, 2):
        for dy in range(-1, 2):
            if dx == 0 and dy == 0:
                continue
            p_x = x + dx
            p_y = y + dy
            for d2x in range(-1, 2):
                for d2y in range(-1, 2):
                    assert board.do_pofig_move(p_x, p_y, p_x + dx,
                                               p_y + dy) is False

    x = 2
    y = 2
    mode.set_size(5)
    mode.set_arrangement([(Unit(PoliceManMover()), 2, 2)])
    board = Board(mode)
    for dx in range(-6, 6):
        for dy in range(-6, 6):
            if dx == dy and dx == 0:
                continue
            to_x = x + dx
            to_y = y + dy
            if valid(to_x, to_y, 6):
                if abs(dx) <= 1 and abs(dy) <= 1:
                    assert board.do_pofig_move(x, y, to_x, to_y) is True
                    assert board.do_pofig_move(to_x, to_y, x, y) is True
                else:
                    assert board.do_pofig_move(x, y, to_x, to_y) is False