Esempio n. 1
0
def enemyfound():
    # get the vertex and other information about the enemy which is closest to
    # the AI's base
    enemy = findenemy(playerSoldier, RvertexData)
    closest = 100
    for i in enemy.keys():
        distance = len(breadth_first_search(mapgraph, i, 45))
        base_ver = 45
        if distance > len(breadth_first_search(mapgraph, i, 46)):
            distance = len(breadth_first_search(mapgraph, i, 46))
            base_ver = 46
        if distance > len(breadth_first_search(mapgraph, i, 54)):
            distance = len(breadth_first_search(mapgraph, i, 54))
            base_ver = 54
        if distance < closest:
            closest = distance
            enm_ver = i
            build_ver = base_ver

    # train the counter soldier with all money you own
    enm_kind = playerSoldier[enm_ver].kind
    for k in counter.keys():
        if counter[k] == enm_kind:
            buildkind = k
            break
    if enm_kind != 0:
        ComSoldier[build_ver] = \
            soldiers(Com.money, buildkind, build_ver, False)
    else:
        ComSoldier[build_ver] = \
            soldiers(Com.money, 0, build_ver, False)

    # find the soldier that is closest to the enemy's base as attack soldier
    closest = 100
    for i in ComSoldier.keys():
        if ComSoldier[i].getnum() != 0 and ComSoldier[i].move is False:
            distance = len(breadth_first_search(mapgraph, i, 10))
            base_ver = 10
            if distance > len(breadth_first_search(mapgraph, i, 17)):
                distance = len(breadth_first_search(mapgraph, i, 17))
                base_ver = 17
            if distance > len(breadth_first_search(mapgraph, i, 16)):
                distance = len(breadth_first_search(mapgraph, i, 16))
                base_ver = 16
            if distance < closest:
                closest = distance
                com_ver = i
                att_ver = base_ver

    # move all soldier except the attack soldier to defend the enemy and move
    # move the attack soldier toward player's base
    for key in ComSoldier.keys():
        if ComSoldier[key].getnum() != 0 and ComSoldier[key].move is False:
            if key != com_ver:
                def_path = breadth_first_search(mapgraph, key, enm_ver)
                movesoldier(def_path)

            else:
                att_path = breadth_first_search(mapgraph, com_ver, att_ver)
                movesoldier(att_path)
Esempio n. 2
0
def player_moveSoldier(s, d):
    # if there is no soldier initialy in the destination, simply move the
    # soldier by changing the corresponded element in the dictionary
    if playerSoldier[d].getnum() == 0 and ComSoldier[d].getnum() == 0:
        num = playerSoldier[s].getnum()
        playerSoldier[d].setnum(num)
        playerSoldier[d].kind = playerSoldier[s].kind
        playerSoldier[d].verNum = d
        playerSoldier[s] = soldiers(0, None, -1, False)
    # if the start address has no player's army, return (no process)
    elif playerSoldier[s].getnum() == 0:
        return 1
    # if there is player's army in the destination, check if they are the same
    # kind. If so, add up, else, no process
    elif playerSoldier[d].getnum() != 0:
        if playerSoldier[d].kind != playerSoldier[s].kind:
            return 1
        else:
            number_s = playerSoldier[s].getnum()
            number_d = playerSoldier[d].getnum()
            playerSoldier[d].setnum(number_s + number_d)
            playerSoldier[s] = soldiers(0, None, -1, False)
    # if there are AI's army in the destination, attack by calling player_battle
    elif ComSoldier[d].getnum() != 0:
        player_battle(s, d)
    # redraw the map with all process and calculation finished
    refresh()
    pygame.display.flip()
    return 0
Esempio n. 3
0
def farming():
    # upgrade random technology every 5 rounds until not enough money
    if roundnum % 5 == 0:
        up = random.randint(0, 3)
        if up == 1 and Com.tech.soldierAttack <= 2:
            Com.tech.soldierAttack += 0.25
            Com.money -= Com.upgradeNeed['attack']
            Com.upgradeNeed['attack'] += 100
        elif up == 2 and Com.tech.moveSpeed <= 4:
            Com.tech.moveSpeed += 1
            Com.money -= Com.upgradeNeed['speed']
            Com.upgradeNeed['speed'] += 200
        elif up == 3:
            Com.tech.moneyperTurn += 500
            Com.money -= Com.upgradeNeed['money']

    if Com.money == 0:
        return

    # train different soldier in other rounds or use the left money to train
    # archer
    if roundnum % 5 == 1:
        ComSoldier[45] = soldiers(Com.money, kindsOfSoldiers['INFANTRY'], 45, False)
    elif roundnum % 5 == 2:
        ComSoldier[54] = soldiers(Com.money, kindsOfSoldiers['CAVALRY'], 54, False)
    elif roundnum % 5 == 3:
        ComSoldier[46] = soldiers(Com.money, kindsOfSoldiers['PIKEMAN'], 46, False)
    else:
        ComSoldier[55] = soldiers(Com.money, kindsOfSoldiers['ARCHER'], 55, False)

    # move all soldiers toward the player's base use the breadth_first_search to
    # calculate the shortest path and call the movesoldier
    closest = 100
    for i in ComSoldier.keys():
        if ComSoldier[i].getnum() != 0 and ComSoldier[i].move is False:
            distance = len(breadth_first_search(mapgraph, i, 10))
            base_ver = 10
            if distance > len(breadth_first_search(mapgraph, i, 17)):
                distance = len(breadth_first_search(mapgraph, i, 17))
                base_ver = 17
            if distance > len(breadth_first_search(mapgraph, i, 16)):
                distance = len(breadth_first_search(mapgraph, i, 16))
                base_ver = 16
            if distance < closest:
                att_ver = base_ver

            att_path = breadth_first_search(mapgraph, i, att_ver)
            movesoldier(att_path)
Esempio n. 4
0
def com_battle(s, d):
    # get the kind and num for soldier of both side and calculate the left
    # amount by subtract AI's amount by player's amount
    player_kind = playerSoldier[d].kind
    com_kind = ComSoldier[s].kind

    num_p = playerSoldier[d].getnum()*Player.tech.soldierAttack
    num_c = ComSoldier[s].getnum()*Com.tech.soldierAttack
    left = num_c - num_p
    # if player's soldier counter AI's soldier, double the player's amount
    if counter[player_kind] == com_kind:
        left -= num_p
        # if left >= 0, AI win the battle, change the AI's amount to be left and
        # erase player's army
        if left >= 0:
            ComSoldier[d].setnum(int(left/Com.tech.soldierAttack))
            ComSoldier[d].kind = ComSoldier[s].kind
            ComSoldier[d].verNum = d
            ComSoldier[s] = soldiers(0, None, -1, False)
            playerSoldier[d] = soldiers(0, None, -1, False)
        # else, player win, change the amount of player soldier to be left and
        # erase the AI's soldier
        else:
            ComSoldier[s] = soldiers(0, None, -1, False)
            playerSoldier[d].setnum(int(-left/Player.tech.soldierAttack))
    # if AI counters player, double the AI's army and proceed with the same
    # calculation
    elif counter[com_kind] == player_kind:
        left += num_c
        if left >= 0:
            ComSoldier[d].setnum(int(left/2/Com.tech.soldierAttack))
            ComSoldier[d].kind = ComSoldier[s].kind
            ComSoldier[d].verNum = d
            ComSoldier[s] = soldiers(0, None, -1, False)
            playerSoldier[d] = soldiers(0, None, -1, False)
        else:
            ComSoldier[s] = soldiers(0, None, -1, False)
            playerSoldier[d].setnum(int(-left/Player.tech.soldierAttack))
    # if no counter relation, directly proceed with the calculation above
    else:
        if left >= 0:
            ComSoldier[d].setnum(int(left/Com.tech.soldierAttack))
            ComSoldier[d].kind = ComSoldier[s].kind
            ComSoldier[d].verNum = d
            ComSoldier[s] = soldiers(0, None, -1, False)
            playerSoldier[d] = soldiers(0, None, -1, False)
        else:
            ComSoldier[s] = soldiers(0, None, -1, False)
            playerSoldier[d].setnum(int(-left/Player.tech.soldierAttack))
    ComSoldier[d].move = True
Esempio n. 5
0
def justmove(s, d):
    # copy the status to soldier at the destination and change move to True
    # Erase the one at start address.
    num = ComSoldier[s].getnum()
    ComSoldier[d].setnum(num)
    ComSoldier[d].kind = ComSoldier[s].kind
    ComSoldier[d].verNum = d
    ComSoldier[d].move = True
    ComSoldier[s] = soldiers(0, None, -1, False)
Esempio n. 6
0
def player_battle(s, d):
    # get the soldier type for each side and calculate the left army's amount
    player_kind = playerSoldier[s].kind
    com_kind = ComSoldier[d].kind
    num_p = playerSoldier[s].getnum()*Player.tech.soldierAttack
    num_c = ComSoldier[d].getnum()*Com.tech.soldierAttack
    left = num_p - num_c
    # check if there is a counter relation
    # if yes, add the effect of the counter to the result
    # if player's army counter the AI's army, double the player's army
    if counter[player_kind] == com_kind:
        left += num_p
        # if left >= 0, player's army win the battle with the amount of left,
        # and the AI's army has amount 0. Destination move status becomes True
        if left >= 0:
            playerSoldier[d].setnum(int(left/2/Player.tech.soldierAttack))
            playerSoldier[d].kind = playerSoldier[s].kind
            playerSoldier[d].verNum = d
            playerSoldier[s] = soldiers(0, None, -1, False)
            ComSoldier[d] = soldiers(0, None, -1, False)
        # else, AI's army win with the left amount and player's army becomes 0
        else:
            playerSoldier[s] = soldiers(0, None, -1, False)
            ComSoldier[d].setnum(int(-left/Com.tech.soldierAttack))
    # if AI's army counters player's army, double AI's army and repeat the
    # calculation above
    elif counter[com_kind] == player_kind:
        left -= num_c
        if left >= 0:
            playerSoldier[d].setnum(int(left/Player.tech.soldierAttack))
            playerSoldier[d].kind = playerSoldier[s].kind
            playerSoldier[d].verNum = d
            playerSoldier[s] = soldiers(0, None, -1, False)
            ComSoldier[d] = soldiers(0, None, -1, False)
        else:
            playerSoldier[s] = soldiers(0, None, -1, False)
            ComSoldier[d].setnum(int(-left/Com.tech.soldierAttack))
    # if no counter relation, directly do the same calculation above
    else:
        if left >= 0:
            playerSoldier[d].setnum(int(left/Player.tech.soldierAttack))
            playerSoldier[d].kind = playerSoldier[s].kind
            playerSoldier[d].verNum = d
            playerSoldier[s] = soldiers(0, None, -1, False)
            ComSoldier[d] = soldiers(0, None, -1, False)
        else:
            playerSoldier[s] = soldiers(0, None, -1, False)
            ComSoldier[d].setnum(int(-left/Com.tech.soldierAttack))
    playerSoldier[d].move = True
Esempio n. 7
0
def combine(s, d):
    num = ComSoldier[d].getnum() + ComSoldier[s].getnum()
    ComSoldier[d].setnum(num)
    ComSoldier[d].move = True
    ComSoldier[s] = soldiers(0, None, -1, False)
Esempio n. 8
0
# initial all global variable needed
mapgraph, vertexData = Functions.loadmap("map1.txt")
LvertexData = copy.deepcopy(vertexData)
RvertexData = copy.deepcopy(vertexData)
positionData = dict()

initialTech = tech(1, 2, 500)
updateneed = {'attack': 100, 'speed': 300, 'money': 500}

Player = playerData(500, initialTech, updateneed)
Com = playerData(500, initialTech, updateneed)
playerSoldier = dict()
ComSoldier = dict()
for ver in mapgraph.get_vertices():
    playerSoldier[ver] = soldiers(0, None, -1, False)
    ComSoldier[ver] = soldiers(0, None, -1, False)


# Computer AI's army if they are the same kind by adding up the num and erase
# the one in the start address
def combine(s, d):
    num = ComSoldier[d].getnum() + ComSoldier[s].getnum()
    ComSoldier[d].setnum(num)
    ComSoldier[d].move = True
    ComSoldier[s] = soldiers(0, None, -1, False)


# move AI's army if no army in the destination
def justmove(s, d):
    # copy the status to soldier at the destination and change move to True