コード例 #1
0
def king_castling_check_test():
    print("========================================")
    print("Performing the king castling check test")
    print("========================================")
    board = Board()
    piece = King(Colour.WHITE)
    current_location = Location('e', 1)
    board.__add_piece__(piece, current_location)
    board.__add_piece__(Rook(Colour.WHITE), Location('a', 1))
    board.__add_piece__(Rook(Colour.WHITE), Location('h', 1))
    board.__add_piece__(Rook(Colour.BLACK), Location('e', 2))
    print(board)

    allowed_moves = piece.allowed_moves(current_location, board)
    print("For a {} {} starting at position {} the moves are:".format(
        piece.colour, piece.name, current_location))
    print(allowed_moves)

    new_location = allowed_moves[0]
    board.move_piece(piece, current_location, new_location)
    current_location = new_location
    print("Moved the {} to position {}".format(piece.name, new_location))
    print(board)
    print("For a {} {} at position {} the moves are:".format(
        piece.colour, piece.name, current_location))
    print(piece.allowed_moves(current_location, board))
コード例 #2
0
def bishop_take_test():
    print("========================================")
    print("Performing the bishop take test")
    print("========================================")
    board = Board()
    piece = Bishop(Colour.WHITE)
    current_location = Location('c', 3)
    board.__add_piece__(piece, current_location)

    friend_piece = Pawn(Colour.WHITE)
    enemy_piece = Pawn(Colour.BLACK)

    board.__add_piece__(friend_piece, Location('b', 2))
    board.__add_piece__(enemy_piece, Location('d', 4))

    print("Added a Bishop and 2 pawns to the board...")

    print(board)
    allowed_moves = piece.allowed_moves(current_location, board)
    print("For a {} {} starting at position {} the moves are:".format(
        piece.colour, piece.name, current_location))
    print(allowed_moves)

    new_location = allowed_moves[0]
    board.move_piece(piece, current_location, new_location)
    current_location = new_location
    print("Moved the {} to position {}".format(piece.name, new_location))
    print(board)
    print("For a {} {} at position {} the moves are:".format(
        piece.colour, piece.name, current_location))
    print(piece.allowed_moves(current_location, board))
コード例 #3
0
ファイル: boardTEST.py プロジェクト: bverpaalen/ChessBot
def main():
    board = Board(8)
    showFields(board)
    print()
    board.startPos()
    showFields(board)
    print("White:")
    showTeam(board.white)
    print("\nBlack: ")
    showTeam(board.black)
コード例 #4
0
ファイル: game.py プロジェクト: bverpaalen/ChessBot
    def __init__(self, players):
        self.board = Board(8)
        self.players = players

        self.board.startPos()

        for player in players:
            player.pieces = self.board.getPiecesSide(player.side)
            print("Welcome player: " + player.name + " playing " +
                  str(player.side.name))
        #self.move()
        self.board.print()
コード例 #5
0
def knight_take_test():
    print("========================================")
    print("Performing the knight take test")
    print("========================================")
    board = Board()
    white_knight = Knight(Colour.WHITE)
    current_location = Location('b', 1)
    board.__add_piece__(white_knight, current_location)

    white_pawn = Pawn(Colour.WHITE)
    black_pawn = Pawn(Colour.BLACK)

    board.__add_piece__(white_pawn, Location('a', 3))
    board.__add_piece__(black_pawn, Location('c', 3))

    print("Added a knight and 2 pawns to the board...")

    print(board)
    allowed_moves = white_knight.allowed_moves(current_location, board)
    print("For a {} {} starting at position {} the moves are:".format(
        white_knight.colour, white_knight.name, current_location))
    print(allowed_moves)

    new_location = allowed_moves[0]
    board.move_piece(white_knight, current_location, new_location)
    current_location = new_location
    print("Moved the {} to position {}".format(white_knight.name,
                                               new_location))
    print(board)
    print("For a {} {} at position {} the moves are:".format(
        white_knight.colour, white_knight.name, current_location))
    print(white_knight.allowed_moves(current_location, board))
コード例 #6
0
def basic_pawn_test():
    print("========================================")
    print("Performing the basic pawn movement test")
    print("========================================")
    board = Board()
    pawn_colour = Colour.WHITE
    white_pawn_one = Pawn(pawn_colour)
    current_location = Location('a', 2)
    board.__add_piece__(white_pawn_one, current_location)
    print("For a {} pawn starting at position {} the moves are:".format(
        pawn_colour, current_location))
    print(white_pawn_one.allowed_moves(current_location, board))

    new_location = Location('a', 4)
    board.move_piece(white_pawn_one, current_location, new_location)
    current_location = new_location
    print("Moved the pawn to position {}".format(new_location))
    print("For a {} pawn at position {} the moves are:".format(
        pawn_colour, current_location))
    print(white_pawn_one.allowed_moves(current_location, board))
コード例 #7
0
def basic_king_test():
    print("========================================")
    print("Performing the basic king movement test")
    print("========================================")
    board = Board()
    piece = King(Colour.WHITE)
    current_location = Location('d', 5)
    board.__add_piece__(piece, current_location)
    print(board)

    allowed_moves = piece.allowed_moves(current_location, board)
    print("For a {} {} starting at position {} the moves are:".format(
        piece.colour, piece.name, current_location))
    print(allowed_moves)

    new_location = allowed_moves[0]
    board.move_piece(piece, current_location, new_location)
    current_location = new_location
    print("Moved the {} to position {}".format(piece.name, new_location))
    print(board)
    print("For a {} {} at position {} the moves are:".format(
        piece.colour, piece.name, current_location))
    print(piece.allowed_moves(current_location, board))
コード例 #8
0
def basic_knight_test():
    print("========================================")
    print("Performing the basic knight movement test")
    print("========================================")
    board = Board()
    white_knight = Knight(Colour.WHITE)
    current_location = Location('b', 1)
    board.__add_piece__(white_knight, current_location)
    print(board)

    allowed_moves = white_knight.allowed_moves(current_location, board)
    print("For a {} {} starting at position {} the moves are:".format(
        white_knight.colour, white_knight.name, current_location))
    print(allowed_moves)

    new_location = allowed_moves[0]
    board.move_piece(white_knight, current_location, new_location)
    current_location = new_location
    print("Moved the {} to position {}".format(white_knight.name,
                                               new_location))
    print(board)
    print("For a {} {} at position {} the moves are:".format(
        white_knight.colour, white_knight.name, current_location))
    print(white_knight.allowed_moves(current_location, board))
コード例 #9
0
def pawn_en_passant_test():
    print("========================================")
    print("Performing the pawn en passant test")
    print("========================================")
    board = Board()
    white_pawn_one = Pawn(Colour.WHITE)
    black_pawn_one = Pawn(Colour.BLACK)
    black_pawn_two = Pawn(Colour.BLACK)

    wp1_loc = Location('b', 2)
    bp1_loc = Location('a', 7)
    bp2_loc = Location('c', 7)

    board.__add_piece__(white_pawn_one, wp1_loc)
    board.__add_piece__(black_pawn_one, bp1_loc)
    board.__add_piece__(black_pawn_two, bp2_loc)

    print("The board was seeded with the following pieces: ")
    print("{} pawn at position {}".format(white_pawn_one.colour, wp1_loc))
    print("{} pawn at position {}".format(black_pawn_one.colour, bp1_loc))
    print("{} pawn at position {}".format(black_pawn_two.colour, bp2_loc))
    print(board)

    # White pawn moves
    print("For the {} pawn starting at position {} the moves are:".format(
        white_pawn_one.colour, wp1_loc))
    allowed_moves = white_pawn_one.allowed_moves(wp1_loc, board)
    print(allowed_moves)
    new_location = allowed_moves[1]
    board.move_piece(white_pawn_one, wp1_loc, new_location)
    wp1_loc = new_location
    print("Moved the pawn to position {}".format(wp1_loc))
    print(board)

    # Black pawn moves
    print("For the {} pawn starting at position {} the moves are:".format(
        black_pawn_one.colour, bp1_loc))
    allowed_moves = black_pawn_one.allowed_moves(bp1_loc, board)
    print(allowed_moves)
    new_location = allowed_moves[1]
    print("For the {} pawn starting at position {} the moves are:".format(
        black_pawn_two.colour, bp2_loc))
    allowed_moves = black_pawn_two.allowed_moves(bp2_loc, board)
    print(allowed_moves)

    board.move_piece(black_pawn_one, bp1_loc, new_location)
    print("Moved the {} pawn at {} to position {}".format(
        black_pawn_one.colour, bp1_loc, new_location))
    bp1_loc = new_location
    print(board)

    # White pawn moves
    print("For the {} pawn at position {} the moves are:".format(
        white_pawn_one.colour, wp1_loc))
    allowed_moves = white_pawn_one.allowed_moves(wp1_loc, board)
    print(allowed_moves)
    new_location = allowed_moves[0]
    board.move_piece(white_pawn_one, wp1_loc, new_location)
    wp1_loc = new_location
    print("Moved the pawn to position {}".format(wp1_loc))
    print(board)

    # Black pawn moves
    print("For the {} pawn starting at position {} the moves are:".format(
        black_pawn_one.colour, bp1_loc))
    allowed_moves = black_pawn_one.allowed_moves(bp1_loc, board)
    print(allowed_moves)
    print("For the {} pawn starting at position {} the moves are:".format(
        black_pawn_two.colour, bp2_loc))
    allowed_moves = black_pawn_two.allowed_moves(bp2_loc, board)
    print(allowed_moves)
    new_location = allowed_moves[1]

    board.move_piece(black_pawn_two, bp2_loc, new_location)
    print("Moved the {} pawn at {} to position {}".format(
        black_pawn_two.colour, bp2_loc, new_location))
    bp2_loc = new_location
    print(board)

    # White pawn moves
    print("For the {} pawn at position {} the moves are:".format(
        white_pawn_one.colour, wp1_loc))
    allowed_moves = white_pawn_one.allowed_moves(wp1_loc, board)
    print(allowed_moves)
    new_location = allowed_moves[1]
    board.move_piece(white_pawn_one, wp1_loc, new_location)
    wp1_loc = new_location
    print("Moved the pawn to position {}".format(wp1_loc))
    print(board)
    print("The move history for this board is: ")
    [print(item) for item in board.move_history]
コード例 #10
0
ファイル: game.py プロジェクト: bverpaalen/ChessBot
class Game:
    board = None
    players = []
    cur = 0

    def __init__(self, players):
        self.board = Board(8)
        self.players = players

        self.board.startPos()

        for player in players:
            player.pieces = self.board.getPiecesSide(player.side)
            print("Welcome player: " + player.name + " playing " +
                  str(player.side.name))
        #self.move()
        self.board.print()

    def movePiece(self, player, piece, x, y):
        if (isMoveAllowed(player, piece, x, y) == True):
            if (self.board[x][y].occupied == True):
                self.board.movePiece(piece, x, y)

    def isMoveAllowed(self, player, piece, x, y):
        if (piece.side == player.side):
            if (piece.__class__.__name__ == "Pawn"):
                take = self.board.fields[x][y].occupied and self.board.fields[
                    x][y].piece.side != piece.side
                allowed = piece.isMoveAllowed(x, y, take)
            else:
                allowed = piece.isMoveAllowed(x, y)

            if (allowed):
                tempBoard = copy.copy(self.board)
                tempBoard.movePiece(piece, x, y)
                if (not tempBoard.isCheck(player.side)):
                    return True
                else:
                    print("Can't be checked after move!")
                    return False
            else:
                print("Move not Allowed!")
                return False
        else:
            print("Wrong Color!")
            return False
        return False

    def move(self):
        player = self.players[self.cur]
        print("Next to move player: " + player.name + " as " +
              player.side.name)

        piece = player.pieces[0]
        if (player.side == Color.WHITE):
            y = piece.y + 1
        else:
            y = piece.y - 1
        x = 0
        while (not self.isMoveAllowed(player, piece, x, y)):
            print("Wrong")
        self.board.print()
        print(piece.x, piece.y)
コード例 #11
0
ファイル: game_worker.py プロジェクト: 0x10F8/chessbot
def setup_game_board_worker():
    board = Board()
    __add_inital_pieces__(board, Colour.WHITE)
    __add_inital_pieces__(board, Colour.BLACK)
    return board