예제 #1
0
    def promote(self, destination, origin=None):
        """
        Executes promote action on piece while it is moving from origin to destination.
        The piece can be promoted if it's origin or destination are in promotion zones.
        No value is passed to origin when forcing a pawn promotion through insert_piece().

        >>> board = [['','','','',''],['','','','',''],['','','','',''],['','','','',''],['','','','','']]
        >>> lower_player = Player('lower', board)
        >>> upper_player = Player('UPPER', board)
        >>> lower_player.create_piece('b', 'e4')
        >>> upper_player.create_piece('K', 'a4')
        >>> lower_player.create_piece('k', 'e1')
        >>> lower_player.move_piece(upper_player, 'e4', 'd5')
        >>> lower_player.promote('d5')
        >>> lower_player.pieces['d5']
        lower +b
        """
        piece = self.pieces[destination]
        if isinstance(piece, King) or isinstance(piece, GoldGeneral):
            raise Exception(piece.id, 'illegal move', 'Promoted unpromotable piece')
        if not piece.promoted:
            coor_dest = parse_location(destination)
            if origin:
                coor_orig = parse_location(origin)
            else:
                coor_orig = coor_dest
            if (piece.id.islower() and (coor_orig[1] == 4 or coor_dest[1] == 4)) \
                or (piece.id.isupper() and (coor_orig[1] == 0 or coor_dest[1] == 0)):
                piece.promoted = True
                piece.id = '+' + piece.id
            self.board[coor_dest[0]][coor_dest[1]] = piece.id
예제 #2
0
 def simulate_drop(self, other, piece, location):
     """
     Copy self.pieces and other.pieces and return a modified version of both
     where the player dropped piece in location.
     """
     temp_pieces = self.pieces.copy()
     if isinstance(piece, Pawn):
         if (self.name.islower() and parse_location(location)[1] == 4) \
             or (self.name.isupper() and parse_location(location)[1] == 0):
             return temp_pieces
     temp_pieces[location] = piece
     return temp_pieces
예제 #3
0
    def insert_piece(self, piece, location):
        """
        Updates the player's pieces dict to map location to piece instance.
        Modifies the Board to reflect the insertion.

        >>> board = [['','','','',''],['','','','',''],['','','','',''],['','','','',''],['','','','','']]
        >>> lower_player = Player('lower', board)
        >>> upper_player = Player('UPPER', board)
        >>> k = King(lower_player)
        >>> K = King(upper_player)
        >>> lower_player.insert_piece(k, 'a1')
        >>> upper_player.insert_piece(K, 'e5')
        >>> lower_player.pieces['a1']
        lower k
        >>> upper_player.pieces['e5']
        UPPER K
        """
        self.pieces[location] = piece
        coor = parse_location(location)
        if isinstance(piece, Pawn):
            """    force Pawn promotion     
                   see details in promote() about the missing argument    """
            if self.name.islower() and coor[1] == 4:
                self.promote(location)
            elif self.name.isupper() and coor[1] == 0:
                self.promote(location)
        self.board[coor[0]][coor[1]] = piece.id
예제 #4
0
    def __init__(self, cell, start_date, end_date, timezone, ramadan):

        starts = [
            "%sT08:30:00", "%sT10:10:00", "%sT11:50:00", "%sT14:20:00",
            "%sT16:00:00"
        ]
        ends = [
            "%sT10:00:00", "%sT11:40:00", "%sT13:20:00", "%sT15:50:00",
            "%sT17:30:00"
        ]
        if ramadan:
            starts = [
                "%sT08:30:00", "%sT09:55:00", "%sT11:20:00", "%sT13:05:00",
                "%sT14:30:00"
            ]
            ends = [
                "%sT09:45:00", "%sT11:10:00", "%sT12:35:00", "%sT14:20:00",
                "%sT15:45:00"
            ]
        start = starts[cell.row] % str(start_date +
                                       datetime.timedelta(cell.col))
        end = ends[cell.row] % str(start_date + datetime.timedelta(cell.col))
        self.start = {"dateTime": start, "timeZone": timezone}
        self.end = {"dateTime": end, "timeZone": timezone}
        self.summary = utils.parse_summary(cell.text)
        self.description = utils.parse_description(cell.text)
        self.location = utils.parse_location(cell.text)
        self.freq = utils.parse_freq(cell.text, end_date)
예제 #5
0
    def get_moves(self, location, other_pieces, pieces=None):
        """
        Return a set of locations a GoldGeneral can move to given a string location
        and a dict of the other player's pieces. Optional arugment pieces for 
        checkmate detection on Pawn drop.

        >>> from player import Player
        >>> lower_player = Player('lower', [['','','','',''],['','','','',''],['','','','',''],['','','','',''],['','','','','']])
        >>> upper_player = Player('UPPER', [['','','','',''],['','','','',''],['','','','',''],['','','','',''],['','','','','']])
        >>> g = GoldGeneral(lower_player)
        >>> G = GoldGeneral(upper_player)
        >>> x = list(g.get_moves('c3', {}))
        >>> x.sort()
        >>> x
        ['b3', 'b4', 'c2', 'c4', 'd3', 'd4']
        >>> x = list(G.get_moves('c3', {}))
        >>> x.sort()
        >>> x
        ['b2', 'b3', 'c2', 'c4', 'd2', 'd3']
        """
        coor = parse_location(location)
        coors = [(coor[0], coor[1] - 1), (coor[0] - 1, coor[1]),
                 (coor[0] + 1, coor[1]), (coor[0], coor[1] + 1)]
        if self.id.islower():
            coors.extend([(coor[0] - 1, coor[1] + 1),
                          (coor[0] + 1, coor[1] + 1)])
        else:
            coors.extend([(coor[0] - 1, coor[1] - 1),
                          (coor[0] + 1, coor[1] - 1)])
        coors = filter(
            lambda x: x[0] > -1 and x[0] < 5 and x[1] > -1 and x[1] < 5, coors)
        return set(map(coor_to_location, coors))
예제 #6
0
    def get_moves(self, location, other_pieces, pieces=None):
        """
        Return a set of locations a King can move to given a string location
        and a dict of the other player's pieces. 
        
        The optional argument pieces is passed when checking if a player has 
        dropped a pawn to see if the drop has resulted in an immediate 
        checkmate, in which case the drop should not be made.

        >>> from player import Player
        >>> lower_player = Player('lower', [['','','','',''],['','','','',''],['','','','',''],['','','','',''],['','','','','']])
        >>> k = King(lower_player)
        >>> x = list(k.get_moves('c3', {}))
        >>> x.sort()
        >>> x
        ['b2', 'b3', 'b4', 'c2', 'c4', 'd2', 'd3', 'd4']
        >>> x = list(k.get_moves('e5', {}))
        >>> x.sort()
        >>> x
        ['d4', 'd5', 'e4']
        """
        coor = parse_location(location)
        coors = [(coor[0] - 1, coor[1] - 1), (coor[0], coor[1] - 1),
                 (coor[0] + 1, coor[1] - 1), (coor[0] - 1, coor[1]),
                 (coor[0] + 1, coor[1]), (coor[0] - 1, coor[1] + 1),
                 (coor[0], coor[1] + 1), (coor[0] + 1, coor[1] + 1)]
        coors = filter(
            lambda x: x[0] > -1 and x[0] < 5 and x[1] > -1 and x[1] < 5, coors)
        return set(map(coor_to_location, coors))
예제 #7
0
 def drop_piece(self, other, piece_id, location):
     """
     Execute drop action of player. Remove piece from list of captured
     pieces and ensure that the drop is legal.
     - Piece can only be dropped on an unoccupied space
     - If piece being dropped is a Pawn:
         (1) The drop cannot result in an immediate checkmate
         (2) The pawn cannot be dropped in the promotion zone
         (3) The pawn cannot be dropped in the same column where
             another one of a player's unpromoted pawns exists
     
     >>> board = [['','','','',''],['','','','',''],['','','','',''],['','','','',''],['','','','','']]
     >>> lower_player = Player('lower', board)
     >>> upper_player = Player('UPPER', board)
     >>> upper_player.create_piece('K', 'a4')
     >>> upper_player.create_piece('+R', 'c4')
     >>> lower_player.create_piece('+b', 'e2')
     >>> lower_player.create_piece('k', 'e1')
     >>> upper_player.create_piece('K', 'a4')
     >>> lower_player.move_piece(upper_player, 'e2', 'c4')
     >>> lower_player.pieces['c4']
     lower +b
     >>> lower_player.captures
     [lower r]
     >>> lower_player.drop_piece(upper_player, 'r', 'b2')
     >>> lower_player.captures
     []
     >>> lower_player.pieces['b2']
     lower r
     """
     if loc_occupied(location, self.board):
         raise Exception(self.name, 'illegal move', 'Dropped piece on an occupied location')
     for i in range(len(self.captures)):
         if self.captures[i].id.lower() == piece_id:
             piece = self.captures[i]
             if isinstance(piece, Pawn):
                 #   handle special cases for pawn drop
                 coor = parse_location(location)
                 temp = self.pieces.copy()
                 self.pieces = self.simulate_drop(other, piece, location)
                 if not other.avoid_checkmate(self):
                     #   case 1: immediate checkmate
                     self.pieces = temp
                     raise Exception(self.name, 'illegal move', 'Dropped pawn in immediate checkmate location')
                 elif (self.name.islower() and coor[1] == 4) \
                     or (self.name.isupper() and coor[1] == 0):
                     #   case 2: drop in promotion zone
                     raise Exception(self.name, 'illegal move', 'Dropped pawn in promotion zone')
                 for loc in self.board[coor[0]]:
                     if (loc == 'p' and self.name.islower()) or (loc == 'P' and self.name.isupper):
                         #   case 3: same column as another unpromoted pawn
                         raise Exception(self.name, 'illegal move', 'Dropped pawn in same column as another unpromoted pawn')
             self.captures.pop(i)
             self.insert_piece(piece, location)
             return
     raise Exception(self.name, 'illegal move', 'No such piece \"{0}\" in captures'.format(piece_id))  
예제 #8
0
    def remove_piece(self, location):
        """ 
        Helper function for move_piece

        Clear board at location and returns the piece which was previously
        at that location.
        """
        if not loc_occupied(location, self.board):
            raise Exception(self.name, 'illegal move', 'Referenced location with no piece')
        coor = parse_location(location)
        self.board[coor[0]][coor[1]] = ''
        return self.pieces.pop(location)
예제 #9
0
파일: saildocs.py 프로젝트: akleeman/slocum
def parse_spot_request(request):
    """
    parses a request for a spot forecast
    """
    model_domain, time_str, variables, image = split_fields(request, 4)
    spot, location_str = model_domain.split(':', 1)
    assert spot.lower() == 'spot'
    if ':' in location_str:
        model, location_str = location_str.split(':', 1)
        model = model.lower()
    else:
        model = 'gefs'
    location = utils.parse_location(location_str)
    # default to 4 days every three hours
    time_str = time_str or '5,6'
    # First try parsing the format `days,period`
    try:
      hours = utils.parse_times(time_str)
    except ValueError, e:
      # if that fails try the gridded format `hour0,hour1,...,hourn`
      hours = utils.parse_hours(time_str)
예제 #10
0
    def get_moves(self, location, other_pieces, pieces=None):
        """
        Return a set of locations a Pawn can move to given a string location
        and a dict of the other player's pieces. Optional arugment pieces for 
        checkmate detection on Pawn drop.

        >>> from player import Player
        >>> lower_player = Player('lower', [['','','','',''],['','','','',''],['','','','',''],['','','','',''],['','','','','']])
        >>> upper_player = Player('UPPER', [['','','','',''],['','','','',''],['','','','',''],['','','','',''],['','','','','']])
        >>> p = Pawn(lower_player)
        >>> P = Pawn(upper_player)
        >>> x = list(p.get_moves('c3', {}))
        >>> x.sort()
        >>> x
        ['c4']
        >>> x = list(p.get_moves('d5', {}))
        >>> x.sort()
        >>> x
        [None]
        >>> p.promoted = True
        >>> x = list(p.get_moves('c3', {}))
        >>> x.sort()
        >>> x
        ['b3', 'b4', 'c2', 'c4', 'd3', 'd4']
        >>> x = list(P.get_moves('c3', {}))
        >>> x.sort()
        >>> x
        ['c2']
        """
        if self.promoted:
            return GoldGeneral.get_moves(self, location, other_pieces)
        coor = parse_location(location)
        coors = list()
        if self.id.islower() and coor[1] < 5:
            coors.append((coor[0], coor[1] + 1))
        elif coor[1] > 0:
            coors.append((coor[0], coor[1] - 1))
        return set(map(coor_to_location, coors))
예제 #11
0
    def get_moves(self, location, other_pieces, pieces=None):
        """
        Return a set of locations a Rook can move to given a string location
        and a dict of the other player's pieces. Optional arugment pieces for 
        checkmate detection on Pawn drop.

        >>> from player import Player
        >>> lower_player = Player('lower', [['','','','',''],['','','','',''],['','','','',''],['','','','',''],['','','','','']])
        >>> r = Rook(lower_player)
        >>> x = list(r.get_moves('c3', {}))
        >>> x.sort()
        >>> x
        ['a3', 'b3', 'c1', 'c2', 'c4', 'c5', 'd3', 'e3']
        >>> r.promoted = True
        >>> x = list(r.get_moves('c3', {}))
        >>> x.sort()
        >>> x
        ['a3', 'b2', 'b3', 'b4', 'c1', 'c2', 'c4', 'c5', 'd2', 'd3', 'd4', 'e3']
        >>> x = list(r.get_moves('c1', {}))
        >>> x.sort()
        >>> x
        ['a1', 'b1', 'b2', 'c2', 'c3', 'c4', 'c5', 'd1', 'd2', 'e1']
        """
        if not pieces:
            pieces = self.player.pieces
        coor = parse_location(location)
        coors = list()
        other_locs = set(parse_location(loc) for loc in other_pieces if loc)
        self_locs = set(parse_location(loc) for loc in pieces if loc)
        i = 1
        # top
        while coor[1] + i < 5:
            if (coor[0], coor[1] + i) in self_locs:
                break
            coors.append((coor[0], coor[1] + i))
            if (coor[0], coor[1] + i) in other_locs:
                break
            i += 1

        # bottom
        i = 1
        while coor[1] - i >= 0:
            if (coor[0], coor[1] - i) in self_locs:
                break
            coors.append((coor[0], coor[1] - i))
            if (coor[0], coor[1] - i) in other_locs:
                break
            i += 1
        # right
        i = 1
        while coor[0] + i < 5:
            if (coor[0] + i, coor[1]) in self_locs:
                break
            coors.append((coor[0] + i, coor[1]))
            if (coor[0] + i, coor[1]) in other_locs:
                break
            i += 1
        # left
        i = 1
        while coor[0] - i >= 0:
            if (coor[0] - i, coor[1]) in self_locs:
                break
            coors.append((coor[0] - i, coor[1]))
            if (coor[0] - i, coor[1]) in other_locs:
                break
            i += 1

        rook_moves = set(map(coor_to_location, coors))
        if self.promoted:
            return rook_moves.union(
                King.get_moves(self, location, other_pieces))
        return rook_moves