示例#1
0
文件: play.py 项目: joppepelzer/Chess
def play(board, fr, to, colour, move_count):

    M = moves.Moves(board, fr, to, colour, move_count)
    piece = board[fr[1]][fr[0]]  # reversed indexing because of matrix

    if check_validity(M, piece.lower()):
        board[to[1]][to[0]] = board[fr[1]][fr[0]]
        board[fr[1]][fr[0]] = '.'
        kicked_off = board[to[1]][to[0]]
    else:
        return False

    checkmate()
    print_board(board)

    return board
示例#2
0
def getRateTable(activity,
                 options,
                 operating_mode_ids=None,
                 source_type_ids=None):
    #to do: add option to select pollutants (for faster runs)
    '''Get an emissions rate lookup table.

    Parameters

    
    - activity: a dictionary structured exactly like the activity parameter
      to the moves.Moves initializer, except that it does not have the `links` key.

    - options: a dictionary structured like the options parameter to the moves.Moves
      initializer, except that only the 'breakdown' key is allowed, and the only
      value allowed in the list is 'process'

    - operating_mode_ids: a sequence of operating mode IDs to be included in the lookup
      table. Optional; if it is not included all operating modes
      will be included. Limiting the number of operating modes will make
      this function go faster

    - source_type_ids: as above, but for source type IDs. 


    Return Value

    Returns a list of dictionaries.
    Each dictionary has keys for opmode, pollutant, source, and quantity;
    and optionally for process.
    All quantity values are for one vehicle hour, and are either in grams or kJ. 

    Example::

        >>> activity =  {'age_distr': {21: {5: 1.0},
        ...                        11: {5: 1.0}},
        ...          'county': 50027,
        ...          'day_type': 5,
        ...          'hour': 16,
        ...          'month': 6,
        ...          'year': 2015}

        >>> options = {}

        >>> table =  getRateTable(activity,
        ...                   options,
        ...                   operating_mode_ids = [0, 1],
        ...                   source_type_ids = [11, 21])
        running MOVES ...
        
        >>> print sorted(table)[0]
        {'source': 21, 'opmode': 0, 'pollutant': 1, 'quantity': 0.147307}


    '''

    if operating_mode_ids is None:
        operating_mode_ids = (0, 1, 11, 12, 13, 14, 15, 16, 21, 22, 23, 24, 25,
                              27, 28, 29, 30, 33, 35, 37, 38, 39, 40, 501)

    if source_type_ids is None:
        source_type_ids = st_ids

    assert all([st in activity['age_distr'] for st in source_type_ids])

    num_source_types = len(source_type_ids)

    sourcetypeshare = 1.0 / len(source_type_ids)

    activity['links'] = None

    linkid = 5
    link_lookup = {}
    links = {}

    for opmode in operating_mode_ids:

        link_lookup[linkid] = opmode

        link = {
            'grade': 0.,
            'length': 15.,
            'road_type': 5,
            'speed': 30.,
            'volume': 2. * num_source_types
        }  #one vehicle hour for each source type

        link['source_distr'] = dict.fromkeys(source_type_ids, sourcetypeshare)

        link['opmode_distr'] = dict.fromkeys(source_type_ids, {opmode: 1.0})

        links[linkid] = link

        linkid += 1

    activity['links'] = links

    options2 = {'detail': 'opmode', 'breakdown': ['source']}

    if 'process' in options.get('breakdown', []):
        options2['breakdown'].append('process')

    m = moves.Moves(activity, options2)

    moves_out = m.run()

    ratetable = list(moves_out)

    #what could go wrong with the db table?
    #in the perfect world, there would be one row for every
    #combination  opmode, pollutant, source, and process.
    #in the real world, it could be that if a certain combination
    #has a quantity of zero, the quanity could be None (null),
    #or maybe there is no row at all

    #so how to make it all agree?
    #for now, just set nulls to 0, and ignore the
    #possibility of missing rows

    for row in ratetable:
        linkid = row.pop('link')

        opmode = link_lookup[linkid]

        row['opmode'] = opmode

        if row['quantity'] is None:
            row['quantity'] = 0.0

    return ratetable
示例#3
0
def getAverageSpeedRateTable(activity,
                             roadtype_grade_speed,
                             source_type_ids=None):
    '''Get an emissions rate lookup table.

    Parameters

    
    - activity: a dictionary structured exactly like the activity paramter
      to the moves.Moves initializer, except that it does not have the `links` key.

    - roadtype_grade_speed: a sequence of tuples. Each is tuple of road type id,
      grade, and speed. Road type IDs can be found in the MOVES database. Grade
      is given in percent, and speed in miles per hour. 

    - source_type_ids: a sequence of source type IDs to be included in the lookup
      table. Optional; if it is not given all source types
      will be included. Limiting the number of source types will make
      this function go faster


    Return Value

    Returns a nested dict keyed by pollutant, source type, roadtype, grade, speed
    All values are for one vehicle hour, and are either in grams or kJ.


    Example::

        activity =  {'age_distr': dict.fromkeys((11,21,31,32,41,42,43,51,52,53,54,61,62),
                                            {5: 1.0}),
                     'county': 50027,
                     'day_type': 5,
                     'hour': 16,
                     'month': 6,
                     'year': 2015}


        table =  getAverageSpeedRateTable(activity,
                                      [(5, 1., 20.),(4, 1., 50.)])

    

    '''

    if source_type_ids is None:
        source_type_ids = st_ids

    assert all([st in activity['age_distr'] for st in source_type_ids])

    sourcetypeshare = 1.0 / len(source_type_ids)

    activity['links'] = None

    linkid = 5
    link_lookup = {}
    links = {}

    for roadtype, grade, speed in roadtype_grade_speed:

        link_lookup[linkid] = dict(roadtype=roadtype, grade=grade, speed=speed)

        link = {
            'grade': float(grade),
            'length': float(speed) / 2.,
            'road_type': roadtype,
            'speed': float(speed),  #was 30
            'volume': 2.
        }  #one vehicle hour

        link['source_distr'] = dict.fromkeys(source_type_ids, sourcetypeshare)

        links[linkid] = link

        linkid += 1

    activity['links'] = links

    options = {'detail': 'average', 'breakdown': ['source']}

    m = moves.Moves(activity, options)

    moves_out = m.run()

    ratetable = {}

    for row in moves_out:
        linkid = row['link']

        roadtype = link_lookup[linkid]['roadtype']
        grade = link_lookup[linkid]['grade']
        speed = link_lookup[linkid]['speed']

        sourcetype = row['source']
        pollutant = row['pollutant']

        if row['quantity'] is None:
            quantity = 0.0
        else:
            quantity = row['quantity'] / sourcetypeshare

        a = ratetable.setdefault(pollutant, {})

        b = a.setdefault(sourcetype, {})

        c = b.setdefault(roadtype, {})

        d = c.setdefault(grade, {})

        d[speed] = quantity

    return ratetable
示例#4
0
def initialize_blue_party():
    global blue_party
    blue_party = {}
    blue_movesets = {}

    # format: name, type, category, moveType, PP, power, accuracy, effect
    blue_movesets["Pidgeot"] = {
        1:
        moves.Moves("Aerial Ace", "flying", "physical", "damaging", 20, 60,
                    None, "bypass accuracy"),
        2:
        moves.Moves("Sand-Attack", "ground", "status", "opponent", 15, -1, 1,
                    "accuracy"),
        3:
        moves.Moves("Feather Dance", "flying", "status", "opponent", 15, -2, 1,
                    "attack"),
        4:
        moves.Moves("Whirlwind", "normal", "status", "opponent switch", 20, 1,
                    1, "switchOut")
    }

    blue_movesets["Rhydon"] = {
        1:
        moves.Moves("Stone Edge", "rock", "physical", "damaging", 5, 80, 0.8,
                    None),
        2:
        moves.Moves("Take Down", "normal", "physical", "damaging", 20, 90,
                    0.85, "recoil", None, 1 / 4),
        3:
        moves.Moves("Scary Face", "normal", "status", "opponent", 10, -2, 1,
                    "speed"),
        4:
        moves.Moves("Earthquake", "ground", "physical", "damaging", 10, 100, 1,
                    None)
    }

    blue_movesets["Arcanine"] = {
        1:
        moves.Moves("Flamethrower", "fire", "special", "damaging", 15, 95, 1,
                    None),
        2:
        moves.Moves("ExtremeSpeed", "normal", "physical", "damaging", 5, 80, 1,
                    "attack first"),
        3:
        moves.Moves("Bite", "dark", "physical", "damaging", 25, 60, 1, None),
        4:
        moves.Moves("Roar", "normal", "status", "opponent switch", 20, 1, 1,
                    "switchOut")
    }

    blue_movesets["Blastoise"] = {
        1:
        moves.Moves("Ice Beam", "ice", "special", "damaging", 10, 95, 1, None),
        2:
        moves.Moves("Hydro Pump", "water", "special", "damaging", 5, 120, 0.8,
                    None),
        3:
        moves.Moves("Bite", "dark", "physical", "damaging", 25, 60, 1, None),
        4:
        moves.Moves("Surf", "water", "special", "damaging", 15, 90, 1, None)
    }

    blue_movesets["Exeggutor"] = {
        1:
        moves.Moves("Giga Drain", "grass", "special", "damaging", 10, 60, 1,
                    "lifesteal", None, 1 / 2),
        2:
        moves.Moves("Egg Bomb", "normal", "physical", "damaging", 10, 100,
                    0.75, None),
        3:
        moves.Moves("Psychic", "psychic", "special", "damaging", 10, 90, 1,
                    None),
        4:
        moves.Moves("Double Team", "normal", "status", "self", 15, 1, None,
                    "evasion")
    }

    blue_movesets["Alakazam"] = {
        1:
        moves.Moves("Psychic", "psychic", "special", "damaging", 10, 90, 1,
                    None),
        2:
        moves.Moves("Calm Mind", "psychic", "status", "self", 20, 2, 1,
                    "sp_atk"),
        3:
        moves.Moves("Shadow Ball", "ghost", "special", "damaging", 15, 80, 1,
                    None),
        4:
        moves.Moves("Recover", "normal", "status", "self", 20, 0.5, 1, "heal")
    }

    # Format: name, hp, atk, defense, sp_atk, sp_def, speed, accuracy, moveSet, type1, type2
    blue_party[1] = pkmn.CreatePokemon("Pidgeot", 190, 145, 139, 134, 134, 157,
                                       blue_movesets["Pidgeot"], "normal",
                                       "flying")
    blue_party[2] = pkmn.CreatePokemon("Rhydon", 212, 200, 189, 106, 106, 101,
                                       blue_movesets["Rhydon"], "ground",
                                       "rock")
    blue_party[3] = pkmn.CreatePokemon("Arcanince", 197, 178, 145, 167, 145,
                                       161, blue_movesets["Arcanine"], "fire",
                                       "fire")
    blue_party[4] = pkmn.CreatePokemon("Blastoise", 186, 148, 167, 150, 172,
                                       130, blue_movesets["Blastoise"],
                                       "water", "water")
    blue_party[5] = pkmn.CreatePokemon("Exeggutor", 202, 161, 150, 194, 128,
                                       117, blue_movesets["Exeggutor"],
                                       "grass", "psychic")
    blue_party[6] = pkmn.CreatePokemon("Alakazam", 162, 112, 106, 205, 150,
                                       189, blue_movesets["Alakazam"],
                                       "psychic", "psychic")
示例#5
0
def initialize_red_party():
    global red_party
    red_party = {}
    red_movesets = {}

    # format: name, type, category, moveType, PP, power, accuracy, effect
    red_movesets["Charizard"] = {
        1:
        moves.Moves("Flamethrower", "fire", "special", "damaging", 15, 95, 1,
                    None),
        2:
        moves.Moves("Aerial Ace", "flying", "physical", "damaging", 20, 60,
                    None, "bypass accuracy"),
        3:
        moves.Moves("Dragon Claw", "dragon", "physical", "damaging", 15, 80, 1,
                    None),
        4:
        moves.Moves("Earthquake", "ground", "physical", "damaging", 10, 100, 1,
                    None)
    }

    red_movesets["Lapras"] = {
        1:
        moves.Moves("Ice Beam", "ice", "special", "damaging", 10, 95, 1, None),
        2:
        moves.Moves("Surf", "water", "special", "damaging", 15, 90, 1, None),
        3:
        moves.Moves("Thunderbolt", "electric", "special", "damaging", 15, 95,
                    1, None),
        4:
        moves.Moves("Psychic", "psychic", "special", "damaging", 10, 90, 1,
                    None)
    }

    red_movesets["Jolteon"] = {
        1:
        moves.Moves("Thunderbolt", "electric", "special", "damaging", 15, 95,
                    1, None),
        2:
        moves.Moves("Thunder", "electric", "special", "damaging", 5, 120, 0.7,
                    None),
        3:
        moves.Moves("Flash", "normal", "status", "opponent", 20, -1, 1,
                    "accuracy"),
        4:
        moves.Moves("Shadow Ball", "ghost", "special", "damaging", 15, 80, 1,
                    None)
    }

    red_movesets["Primape"] = {
        1:
        moves.Moves("Cross Chop", "fighting", "physical", "damaging", 5, 100,
                    0.8, None),
        2:
        moves.Moves("Aerial Ace", "flying", "physical", "damaging", 20, 60,
                    None, "bypass accuracy"),
        3:
        moves.Moves("Bulk Up", "fighting", "status", "self", 15, 1, None,
                    "attack"),
        4:
        moves.Moves("Thunder Punch", "electric", "physical", "damaging", 15,
                    75, 1, None)
    }

    red_movesets["Nidoking"] = {
        1:
        moves.Moves("Megahorn", "bug", "physical", "damaging", 10, 120, 0.85,
                    None),
        2:
        moves.Moves("Sludge Bomb", "poison", "special", "damaging", 10, 90, 1,
                    None),
        3:
        moves.Moves("Earthquake", "ground", "physical", "damaging", 10, 100, 1,
                    None),
        4:
        moves.Moves("Shadow Ball", "ghost", "special", "damaging", 15, 80, 1,
                    None)
    }

    red_movesets["Dragonite"] = {
        1:
        moves.Moves("Dragon Claw", "dragon", "physical", "damaging", 15, 80, 1,
                    None),
        2:
        moves.Moves("Double Team", "normal", "status", "self", 15, 1, None,
                    "evasion"),
        3:
        moves.Moves("Fly", "flying", "physical", "damaging", 15, 90, 0.95,
                    None),
        4:
        moves.Moves("Dragon Rush", "dragon", "physical", "damaging", 5, 110,
                    0.75, None)
    }

    # Format: name, hp, atk, defense, sp_atk, sp_def, speed, accuracy, moveSet, type1, type2
    red_party[3] = pkmn.CreatePokemon("Charizard", 162, 115, 109, 140, 116,
                                      131, red_movesets["Charizard"], "fire",
                                      "flying")
    red_party[2] = pkmn.CreatePokemon("Lapras", 214, 116, 111, 116, 126, 91,
                                      red_movesets["Lapras"], "water", "ice")
    red_party[1] = pkmn.CreatePokemon("Jolteon", 200, 96, 91, 141, 126, 162,
                                      red_movesets["Jolteon"], "electric",
                                      "electric")
    red_party[4] = pkmn.CreatePokemon("Primape", 149, 136, 91, 91, 101, 126,
                                      red_movesets["Primape"], "fighting",
                                      "fighting")
    red_party[5] = pkmn.CreatePokemon("Nidoking", 165, 123, 107, 116, 106, 116,
                                      red_movesets["Nidoking"], "poison",
                                      "ground")
    red_party[6] = pkmn.CreatePokemon("Dragonite", 175, 165, 126, 131, 131,
                                      111, red_movesets["Dragonite"], "dragon",
                                      "flying")