Exemplo n.º 1
0
def btrialpawn(h, sq):
    assert board.blackToMove()
    assert h == len(board.history)
    assert board.manAt(sq) == -man.pawn
    global mvs
    # pawn may move 2 squares on first move
    if rank(sq) == 7 and board.empty(bak(sq)) and board.empty(bak(bak(sq))):
        mvs.append((h, "-", sq, bak(bak(sq))))
    # pawn moves only one square if beyond original position
    if rank(sq) > 2:
        if board.manAt(bak(sq)) == 0:
            mvs.append((h, "-", sq, bak(sq)))
        if man.isWhite(board.manAt(dbr(sq))):
            mvs.append((h, "x", sq, dbr(sq), board.manAt(dbr(sq))))
        if man.isWhite(board.manAt(dbl(sq))):
            mvs.append((h, "x", sq, dbl(sq), board.manAt(dbl(sq))))
    # pawn promotes on reaching last rank
    if rank(sq) == 2:
        if board.manAt(bak(sq)) == 0:
            for pmn in [-man.queen, -man.rook, -man.bishop, -man.knight]:
                mvs.append((h, "-/", sq, bak(sq), 0, pmn))
        if man.isWhite(board.manAt(dbr(sq))):
            for pmn in [-man.queen, -man.rook, -man.bishop, -man.knight]:
                mvs.append((h, "x/", sq, dbr(sq), board.manAt(dbr(sq)), pmn))
        if man.isWhite(board.manAt(dbl(sq))):
            for pmn in [-man.queen, -man.rook, -man.bishop, -man.knight]:
                mvs.append((h, "x/", sq, dbl(sq), board.manAt(dbl(sq)), pmn))
    # en passant
    if len(board.history) > 0:
        mv = board.lastMove()
        if tag(mv) == "-" and board.manAt(nsq(mv)) == man.pawn and rank(
                osq(mv)) == 2 and rank(nsq(mv)) == 4:
            if nsq(mv) in [rgt(sq), lft(sq)]:
                mvs.append((h, "xep", sq, bak(nsq(mv))))
Exemplo n.º 2
0
def wtrialpawn(h, sq):
    assert board.whiteToMove()
    assert h == len(board.history)
    assert board.manAt(sq) == man.pawn
    global mvs
    # pawn may move 2 squares on first move
    if rank(sq) == 2 and board.empty(fwd(sq)) and board.empty(fwd(fwd(sq))):
        mvs.append((h, "-", sq, fwd(fwd(sq))))
    # pawn moves only one square fwd if beyond original position and square is empty
    if rank(sq) < 7:
        if board.manAt(fwd(sq)) == 0:
            mvs.append((h, "-", sq, fwd(sq)))
        if man.isBlack(board.manAt(dfr(sq))):
            mvs.append((h, "x", sq, dfr(sq), board.manAt(dfr(sq))))
        if man.isBlack(board.manAt(dfl(sq))):
            mvs.append((h, "x", sq, dfl(sq), board.manAt(dfl(sq))))
    # pawn promotes on reaching last rank
    if rank(sq) == 7:
        if board.manAt(fwd(sq)) == 0:
            for pmn in [man.queen, man.rook, man.bishop, man.knight]:
                mvs.append((h, "-/", sq, fwd(sq), 0, pmn))
        if man.isBlack(board.manAt(dfr(sq))):
            for pmn in [man.queen, man.rook, man.bishop, man.knight]:
                mvs.append((h, "x/", sq, dfr(sq), board.manAt(dfr(sq)), pmn))
        if man.isBlack(board.manAt(dfl(sq))):
            for pmn in [man.queen, man.rook, man.bishop, man.knight]:
                mvs.append((h, "x/", sq, dfl(sq), board.manAt(dfl(sq)), pmn))
    # en passant
    if len(board.history) > 0:
        mv = board.lastMove()
        if tag(mv) == "-" and board.manAt(nsq(mv)) == -man.pawn and rank(
                osq(mv)) == 7 and rank(nsq(mv)) == 5:
            if nsq(mv) in [rgt(sq), lft(sq)]:
                mvs.append((h, "xep", sq, fwd(nsq(mv))))
Exemplo n.º 3
0
 def _opposite_corner(self, board, me, you):
     if board[1] is you and empty(board[9]):
         return 9
     if board[9] is you and empty(board[1]):
         return 1
     if board[3] is you and empty(board[7]):
         return 7
     if board[7] is you and empty(board[3]):
         return 3
     return None
Exemplo n.º 4
0
def player(identifier, rewards_state):
    """Non-learning agent that always makes greedy decision"""
    return State(identifier=identifier,
                 epsilon=0,
                 alpha=ALPHA,
                 learning=False,
                 rewards_state=rewards_state,
                 last_board_state=board.empty())
Exemplo n.º 5
0
def play_game(loop, rewards_state):
    """Setup game play state and interface"""

    # Initialize top-level, mutable state with agent X
    state = State(rewards_state, agent.player(X, rewards_state), board.empty())

    # Curry move_selected for referring to correct app state
    move_selected = autocurry(_move_selected)(state)

    # Setup game interface
    interface.setup_game_play(loop, state, move_selected)
Exemplo n.º 6
0
def btrialrook(h, sq):
    assert board.blackToMove()
    assert h == len(board.history)
    assert board.manAt(sq) == -man.rook
    global mvs
    for dir in [fwd, rgt, bak, lft]:
        s = dir(sq)
        done = False
        while not s == None and not done:
            if board.empty(s):
                mvs.append((h, "-", sq, s))
                s = dir(s)
            elif man.isWhite(board.manAt(s)):
                mvs.append((h, "x", sq, s, board.manAt(s)))
                done = True
            elif man.isBlack(board.manAt(s)):
                done = True
Exemplo n.º 7
0
def btrialbishop(h, sq):
    assert board.blackToMove()
    assert h == len(board.history)
    assert board.manAt(sq) == -man.bishop
    global mvs
    for dir in [dfr, dfl, dbr, dbl]:
        s = dir(sq)
        done = False
        while not s == None and not done:
            if board.empty(s):
                mvs.append((h, "-", sq, s))
                s = dir(s)
            elif man.isWhite(board.manAt(s)):
                mvs.append((h, "x", sq, s, board.manAt(s)))
                done = True
            elif man.isBlack(board.manAt(s)):
                done = True
Exemplo n.º 8
0
def _play_training_game(player1, player2):
    """Simulated game between two agents"""

    # Initialize empty board
    board_state = board.empty()

    while board.is_finished(board_state) == False:
        # Player 1 turn
        (board_state, player1) = _play_train_move(player1, board_state)

        # Break if game already finished
        if board.is_finished(board_state): break

        # Player 2 turn
        (board_state, player2) = _play_train_move(player2, board_state)

    # Update rewards for final step
    agent.epoch_finished(player1, board_state)
    agent.epoch_finished(player2, board_state)
Exemplo n.º 9
0
def btrialknight(h, sq):
    assert board.blackToMove()
    assert h == len(board.history)
    assert board.manAt(sq) == -man.knight
    global mvs
    for s in [
            dfr(fwd(sq)),
            dfr(rgt(sq)),
            dbr(rgt(sq)),
            dbr(bak(sq)),
            dbl(bak(sq)),
            dbl(lft(sq)),
            dfl(lft(sq)),
            dfl(fwd(sq))
    ]:
        if board.empty(s):
            mvs.append((h, "-", sq, s))
        elif man.isWhite(board.manAt(s)):
            mvs.append((h, "x", sq, s, board.manAt(s)))
Exemplo n.º 10
0
def btrialqueen(h, sq):
    assert board.blackToMove()
    assert h == len(board.history)
    assert board.manAt(sq) == -man.queen
    global mvs
    for dir in [fwd, rgt, bak, lft, dfr, dfl, dbr, dbl]:
        s = dir(sq)
        done = False
        while not s == None and not done:
            if board.empty(s):
                mvs.append((h, "-", sq, s))
                s = dir(s)
            elif man.isWhite(board.manAt(s)):
                mvs.append((h, "x", sq, s, board.manAt(s)))
                done = True
            elif man.isBlack(board.manAt(s)):
                done = True
            else:
                print "error in btrialqueen"
                sys.exit(1)
Exemplo n.º 11
0
def btrialking(h, sq):
    assert board.blackToMove()
    assert h == len(board.history)
    assert board.manAt(sq) == -man.king
    global mvs
    for dir in [fwd, dfr, rgt, dbr, bak, dbl, lft, dfl]:
        s = dir(sq)
        if board.empty(s):
            mvs.append((h, "-", sq, s))
        elif man.isWhite(board.manAt(s)):
            mvs.append((h, "x", sq, s, board.manAt(s)))
        elif man.isBlack(board.manAt(s)):
            pass
    if board.blackAllowOO and board.empty(f8) and board.empty(g8):
        mvs.append((h, "OO", e8))
    if board.blackAllowOOO and board.empty(d8) and board.empty(
            c8) and board.empty(b8):
        mvs.append((h, "OOO", e8))
Exemplo n.º 12
0
def wtrialking(h, sq):
    assert board.whiteToMove()
    assert h == len(board.history)
    assert board.manAt(sq) == man.king
    global mvs
    for dir in [fwd, dfr, rgt, dbr, bak, dbl, lft, dfl]:
        s = dir(sq)
        if board.empty(s):
            mvs.append((h, "-", sq, s))
        elif man.isBlack(board.manAt(s)):
            mvs.append((h, "x", sq, s, board.manAt(s)))
        elif man.isWhite(board.manAt(s)):
            pass
    if board.whiteAllowOO and board.empty(f1) and board.empty(g1):
        mvs.append((h, "OO", e1))
    if board.whiteAllowOOO and board.empty(d1) and board.empty(
            c1) and board.empty(b1):
        mvs.append((h, "OOO", e1))
Exemplo n.º 13
0
 def select_move(self, board):
     print "Enter a move for %s:" % self.player_symbol
     available = [str(c) for c in board.cells() if empty(c)]
     return int(take_input("Space", available))
Exemplo n.º 14
0
def test_empty():
    brd = board.empty()
    assert len(brd) == 9*9
    for square in brd:
        assert square == 0
Exemplo n.º 15
0
def first_free_space(spaces):
    return (c for c in spaces if empty(c)).next()