Exemplo n.º 1
0
def placetile(context, x):
    '''Current player places a tile at location x, determines resulting chain effects'''
    '''This function only ends after all resulting actions have finished, and finishes by filling in the placed tile'''

    context['player'][context['cp']]['tilerack'].remove(x)
    #determine result:
    # Tile has no neighbors: nothing happens
    if infos.filled_neighbors(context, x) == []:
        context['grid'][x]['filled'] = 1
        return

    # neighors all of chain x, or some chain x and some unaffiliated: grow chain x
    elif len(infos.neighbor_chains(context, x)) == 1:
        new = infos.neighbor_chains(context, x)[0]
        context['grid'][x]['chain'] = new
        adj_chain(context, x, new)
        context['grid'][x]['filled'] = 1

    # neighbor(s) that are all no chain: new chain
    elif len(infos.neighbor_chains(context, x)) == 0:
        newchain_at(context, x)
        context['grid'][x]['filled'] = 1

    # neighbors of more than one chain: MERGER!
    elif len(infos.neighbor_chains(context, x)) > 1:
        merger_at(context, x)
        context['grid'][x]['filled'] = 1
Exemplo n.º 2
0
def merger_at(context, x):
    """executes chain merger with tile placed at x"""
    print "Placing this tile creates a merger!"
    
    #find sizes of neighboring chains
    n_chain_sizes = {}
    for r in infos.neighbor_chains(context, x):
        n_chain_sizes[r] = infos.chainsize(context, r)

    # If two or more largest neighbor chains are the same size, player must choose merge direction
    maxsize = max(set(n_chain_sizes.values()))
    if n_chain_sizes.values().count(maxsize) > 1:
        contenders = []
        for n in n_chain_sizes.keys():
            if n_chain_sizes[n] == maxsize:
                contenders.append(n)
        
        dom = inputs.merge_dir_input(context, contenders)

    # if one chain is largest, merge direction is set automatically
    else:
        dom = infos.find_key(n_chain_sizes, maxsize)
        print dom + " dominates this merger."

    # Shareholders in chains getting eaten now get bonuses and choose what to do with their stock
    non_doms = infos.neighbor_chains(context, x)
    non_doms.remove(dom)  #this is now a list of the chains getting eaten

    for liquid in non_doms:
        bonus_winners = infos.find_holders(context, liquid)

        if bonus_winners == [[],[]]: #no shareholders at all
            print liquid + " has no shareholders!  Tragic!"

        elif len(bonus_winners[0]) > 1: #tie for Majority
            print "Players " + str(bonus_winners[0]) + " tie for majority shareholder in " + liquid + "."
            split_bonus = infos.sole_bonus(context, liquid)/len(bonus_winners[0])
            print "Each receives an exit bonus of $" + str(split_bonus)

            for tied_maj in bonus_winners[0]:
                context['player'][tied_maj]['cash'] += split_bonus
                sell_stock(context, tied_maj, liquid)
                trade_stock(context, tied_maj, liquid, dom)

        elif len(bonus_winners[0]) == 1 and bonus_winners[1] == []: #Only one shareholder
            print "Player " + str(bonus_winners[0][0]) + " is the only shareholder."
            context['player'][bonus_winners[0][0]]['cash'] += infos.sole_bonus(context, liquid)
            print "Player " + str(bonus_winners[0][0]) + " receives a buyout bonus of $" + str(infos.sole_bonus(context, liquid))
            sell_stock(context, bonus_winners[0][0], liquid)
            trade_stock(context, bonus_winners[0][0], liquid, dom)

        else: # One maj shareholder, one or more mins
            print "Player " + str(bonus_winners[0][0]) + " is the Majority shareholder in " + liquid + '.'
            context['player'][bonus_winners[0][0]]['cash'] += infos.maj_bonus(context, liquid)
            print "Player " + str(bonus_winners[0][0]) + " receives a buyout bonus of $" + str(infos.maj_bonus(context, liquid))
            sell_stock(context, bonus_winners[0][0], liquid)
            trade_stock(context, bonus_winners[0][0], liquid, dom)

            if len(bonus_winners[1]) == 1: # single winning minority holder
                print "Player " + str(bonus_winners[1][0]) + " is the winning minority shareholder in " + liquid + '.'
                context['player'][bonus_winners[1][0]]['cash'] += infos.min_bonus(context, liquid)
                print "Player " + str(bonus_winners[1][0]) + " receives a buyout bonus of $" + str(infos.min_bonus(context, liquid))
                sell_stock(context, bonus_winners[1][0], liquid)
                trade_stock(context, bonus_winners[1][0], liquid, dom)
            
            else: #tie for min bonus
                print "Players " + str(bonus_winners[1]) + " tie for minority shareholder in " + liquid + "."
                split_bonus = infos.min_bonus(context, liquid)/len(bonus_winners[1])
                print "Each receives an exit bonus of $" + str(split_bonus)

                for tied_min in bonus_winners[1]:
                    context['player'][tied_min]['cash'] += split_bonus
                    sell_stock(context, tied_min, liquid)
                    trade_stock(context, tied_min, liquid, dom)

    for chain in non_doms: #add all acquired chains to dominant chain.
        for tile in context['grid'].keys():
            if context['grid'][tile]['chain'] == chain:
                context['grid'][tile]['chain'] = dom
    
    context['grid'][x]['chain'] = dom #add merger tile to dominant chain 
   
    adj_chain(context, x, dom) #add any unafiliated tiles/chains to dominant chain as well