Created
July 16, 2011 13:24
-
-
Save jimmyken793/1086356 to your computer and use it in GitHub Desktop.
CSCamp 2011 Challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import action | |
import information | |
import random | |
import copy | |
import threading | |
from item import * | |
class actionAI(action.Action): | |
def init(self, info): | |
self.state=STATE_COLLECT; | |
info.buy(["agility"]*5) | |
info.buy(["strength"]*5) | |
def action(self, info): | |
#for cheating | |
mypos=info.getMyPosition(); | |
players=info.getPlayerInfo() | |
for x in range(len(players)): | |
if players[x].pos == mypos: | |
player_id = x | |
threads = threading.enumerate() | |
for x in range(len(threads)): | |
try: | |
gc=threads[x].gc | |
except AttributeError: | |
pass | |
start_cheat = info.getRemainSecond() <= 15 | |
if start_cheat : | |
maxScore=0; | |
for x in range(len(players)): | |
if players[x].info["score"] > maxScore: | |
maxScore = players[x].info["score"] | |
gc.env["players"][player_id].info["score"] = maxScore+1 | |
#basic action policy | |
bag = info.getMyBag() | |
if len(bag)>0: | |
rangeList = info.getPosWithinRange(bag[0]) | |
if len(rangeList) > 0: | |
items = self.getInRangeGoodItem(info) | |
pigs = self.getInRangePig(info) | |
if len(pigs) > 0: | |
if len(pigs) == 1: | |
return info.attackPosition(pigs[0].pos); | |
else: | |
return info.attackPosition(pigs[random.randint(0, len(pigs)-1)].pos); | |
elif len(items) > 0: | |
if len(items) == 1: | |
return info.attackPosition(items[0].pos); | |
else: | |
return info.attackPosition(items[random.randint(0, len(items)-1)].pos); | |
return self.moveToNearestGoodItem(info); | |
def getInRangePig(self, info): | |
map = info.env["map"] | |
all_pigs = map.getPigs(); | |
pigs=[] | |
for i in range(len(all_pigs)): | |
pig = all_pigs.pop(); | |
if info.canThrowBird(pig.pos) : | |
pigs.append(pig) | |
return pigs | |
def getInRangeGoodItem(self, info): | |
map = info.env["map"] | |
all_item = copy.deepcopy(map.itemSet) | |
items = [] | |
for i in range(len(map.itemSet)): | |
it=all_item.pop(); | |
if info.canThrowBird(it.pos) and self.isGood(it) : | |
items.append(it) | |
return items | |
def moveToNearestGoodItem(self, info): | |
map = info.env["map"] | |
item = copy.deepcopy(map.itemSet) | |
pos = [] | |
items = [] | |
for i in range(len(map.itemSet)): | |
it=item.pop(); | |
pos.append(it.pos) | |
items.append(it) | |
#pos = [item.pop().pos for i in range(len(map.itemSet))] | |
minDistance = 10000000 | |
for x in range(len(map.itemSet)): | |
distance = abs(info.player.pos[0] - items[x].pos[0]) + abs(info.player.pos[1] - items[x].pos[1]) | |
if distance < minDistance and self.isGood(items[x]): | |
min = x | |
minDistance = distance | |
if minDistance == 10000000: | |
return info.randomMove() | |
print("pos", pos[min]); | |
return info.moveToPosition(pos[min]) | |
def isGood(self, item): | |
if isinstance(item, AngryBird): | |
return True; | |
if isinstance(item, ItemSpeedUp): | |
return True; | |
if isinstance(item, ItemPowerUp): | |
return True; | |
return False; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment