洞穴游侠-探索黑暗


9

您的地质学家哥们突然闯进办公室门,激动地睁大了眼睛,请您与他一起去他刚刚发现的地方。在途中,他解释说,他认为自己确实击中了金子。唯一的问题是,它被埋在地下非常不稳定的洞穴中。进行洞穴探险实在太危险了,因此他希望您对他的一个洞穴探险机器人进行编程,以在将其拉回之前收集尽可能多的黄金。他还提到自己已经探查了洞穴,发现了一些可能对机器人有害的野生生物,并且还把一些仍可以使用的设备丢到了那里。每个机器人都配备有两个手臂和一系列传感器。当您到达现场时,他告诉您他正在计划招募更多编码员,

现在,深入到细节。传感器将信息作为ASCII字符传递到您的程序。以下是每个角色的含义以及机器人可能在山洞中遇到的任何事物的描述的列表:

Code    Name/Description

Y       Your bot
        You do things

@       Other bots
        They do other things

-       Ground
        This doesn't do things

C       Centipede
        These will bite you and leave a poison effect
        The bite will cost 1 health
        The poison effect will last for 3 turns, costing 2 health each turn

B       Bats
        If bats end up in the same space you are, your bot runs in a random direction during its turn rather than what you told it to do

L       Lion (because reasons)
        Lions deal heavy damage, 10 health, each time they attack

F       Food
        Eating this will give you 5 health
        Can only be used once

W       Water
        Drinking this will cure poison effects early
        Can only be used once

R       Revealer
        This will increase the range of your visibility to an 11x11 grid
        The extra range will only be 75% correct, but the original range won't be effected

K       Knife
        You do twice as much damage to other bots if you have a knife

G       Gold
        The whole reason you're doing this in the first place

N       Nurse Nina
        She mend you good
        Restores your health by 10 while you occupy the same space as her

}       Boulder
        You can't walk over boulders, and neither can anything else

P       Pit
        If you fall in a pit, you will be stuck for 3 turns

洞穴的大小根据参与的机器人数量而增加。它以30x30的价格开始,每个机器人可获得额外的10x10的价格。因此,两个机器人将探索50x50的洞穴。

机器人的生命值从20开始,但是对生命值没有最大限制。

输入:

您将通过STDIN接收以下格式的输入:

20,5,10,1,0,True,False    <-health, number gold pieces, number of turns your bot has lasted, number of until the poison wears off, number of turns until you are no longer stuck in a pit, if you have a revealer, if you have a knife
-----
-G}--
--Y-L
-C---
---B-

第一行包含有关您的机器人的信息,其余是您的机器人可以看到的网格。如果您的机器人正对着洞穴的四壁之一,您将得到一个看起来更像这样的网格(在一直向西走的情况下):

---
}--
Y--
---
---

洞穴不会环绕,您的视野也不会环绕。洞穴的墙壁未标记,机器人收到的唯一表明它正在靠近墙壁的指示是其视野正在减弱。使用Revealer,您可能会得到以下信息:

--------C--
LW--------B
---K-N-----
--------BR-
-F---------
--B--Y---@N
-W@---F----
------K-F--
----@-}----
R@---G}--}-
--------G-R

输出:

每转一圈您将获得两个移动,并以以下格式输出:

MNNANW    <- Moves are groups of 3 characters representing the action and the direction

可能的操作如下:

M    Move - Move your bot in the specified direction
A    Attack - Attack the square in the specified direction
H    Hold - Do nothing

可能的方向如下:

NN - North (up)
NE - Northeast (up-right)
EE - East (right)
SE - Southeast (down-right)
SS - South
SW - Southwest
WW - West
NW - Northwest

从左到右应用移动。

转弯:

以以下方式转变进度:

  1. 毒气效果适用于任何中毒的玩家

  2. 非机器人移动和攻击

    2a。狮子,C和蝙蝠随机移动

    2b。狮子和enti将攻击与其直接相邻的所有物体(包括对角线)

    2c。蝙蝠效果仅适用于与蝙蝠位于同一空间的机器人

    2d。妮娜护士将在一个位置呆3转,然后跳到随机位置。

  3. 机器人移动

    3a。如果您的机器人给出了无效的输出,它将不会移动

    3b。您的机器人将尝试尽​​可能接近输出指定的空间(有关更多详细信息,请参见底部的注释)

    3c。攻击a,狮子或蝙蝠会杀死它

    3d。不带刀攻击另一个机器人会造成5点伤害,而带刀则造成10点伤害

规则:

  1. 坚持可以在OS X或Linux上运行的通用语言。

  2. 您可以选择将最多不超过1kb的数据写入文件

得分:

机器人只会在山洞中,直到只有一个遗留物,或者直到经过50转为止,以先到者为准。您的机器人将根据收集的金币数量和持续的转数之和来判断。

可以在此处下载控制器代码以进行测试(在下载该代码的目录中创建一个名为“ bots”的文件夹,并将其放入“ bots”中),您将需要NumPy来运行它。随意挖掘它,但您必须原谅...

这是随机机器人的一些代码:

#!/usr/bin/python
import random as r

a = ['M','A','H']
d = ['NN','NE','EE','SE','SS','SW','WW','NW']

print(a[r.randint(0,2)]+d[r.randint(0,7)]+a[r.randint(0,2)]+d[r.randint(0,7)])

****您的机器人将始终沿您的输出指定的大致方向移动,但是如果它被岩石或墙壁遮挡,则确切的方向取决于情况。例如,如果您的机器人像这样靠在墙上:

---
}--
Y--
---
---

而你的输出是

MNWMSW

您的机器人将向下移动一个空间。它无法向北或向西移动,因此该移动无效。它可以向南移动(也可以向南移动),但不能向西移动。但是,如果您的机器人试图向东北移动,它将直接进入该空间(对角线移动是对角线移动,而不是程序上的移动)

排行榜

这些是4场比赛的平均得分。

The bot of Survival:    54.75
Coward:                 52.25
Pufferfish:             50.00
Randombot:              50.00
Indiana Jones:          47.50
TheoremBot:             46.50

每个机器人有多少健康?洞穴的边缘是什么样的?
Conor O'Brien

他们从20开始,可以收集尽可能多的东西。在上面添加了此信息
Beanstalk 2015年

洞穴的边缘未标记,您的程序将仅获取您可能在其上行走的位。
Beanstalk

你真的不知道你的健康吗?
pppery

您能输入机器人视野的长度和宽度吗?
LegionMammal978

Answers:


4

印第安那·琼斯(Python 2)

这个机器人不害怕任何东西。它将设法获得黄金;如果找不到,它将尝试用刀刺伤对手。

#!/usr/bin/env python
import sys
import random
data = sys.stdin.readlines()
health, gold, turns, poison_remaining, pit_remaining, revealer, knife = eval(data[0])
lines = data[1:]

myloc = [-1, -1]

width, height = len(lines[0]), len(lines)

for y, line in enumerate(lines):
    if line.find('Y')>-1:
        myloc = [line.index('Y'), y]
if myloc[0]<width/2:
    padding = int(width/2-myloc[0])
    lines = ['-'*padding+line for line in lines]
    myloc[0]+=padding
elif myloc[0]>width/2+1:
    padding = int(myloc[0]-width/2-1)
    lines = [line+'-'*padding for line in lines]

if myloc[1]<height/2:
    padding = int(height/2-myloc[1])
    lines = ['-'*width]*padding + lines
    myloc[1]+=padding
elif myloc[1]>height/2+1:
    padding = int(myloc[1]-height/2-1)
    lines = lines + ['-'*width]*padding

uddirections = {1:'N',0:'',-1:'S'}
lrdirections = {1:'E',0:'',-1:'W'}

golds = {}
for y, line in enumerate(lines):
    if 'G' in line:
        x = line.index('G')
        direction = ((uddirections[max(min(myloc[1]-y,1),-1)]+lrdirections[max(min(x-myloc[0],1),-1)])*2)[:2]
        distance = max(abs(myloc[0]-x), abs(myloc[1]-y))
        golds[distance] = direction

bots = {}
for y, line in enumerate(lines):
    if '@' in line:
        x = line.index('@')
        direction = ((uddirections[max(min(myloc[1]-y,1),-1)]+lrdirections[max(min(x-myloc[0],1),-1)])*2)[:2]
        distance = max(abs(myloc[0]-x), abs(myloc[1]-y))
        bots[distance] = direction

foods = {}
for y, line in enumerate(lines):
    if 'F' in line:
        x = line.index('F')
        direction = ((uddirections[max(min(myloc[1]-y,1),-1)]+lrdirections[max(min(x-myloc[0],1),-1)])*2)[:2]
        distance = max(abs(myloc[0]-x), abs(myloc[1]-y))
        foods[distance] = direction

knives = {}
for y, line in enumerate(lines):
    if 'K' in line:
        x = line.index('K')
        direction = ((uddirections[max(min(myloc[1]-y,1),-1)]+lrdirections[max(min(x-myloc[0],1),-1)])*2)[:2]
        distance = max(abs(myloc[0]-x), abs(myloc[1]-y))
        knives[distance] = direction

if golds:
    direction = golds[min(golds.keys())]
elif not knife and knives:
    direction = knives[min(knives.keys())]
elif health<20 and foods:
    direction = foods[min(foods.keys())]
elif bots and knife:
    direction = bots[min(bots.keys())]
    if min(bots.keys())==1:
        print ('A'+direction)*2
        sys.exit(0)
    elif min(bots.keys())==2:
        print 'M'+direction+'A'+direction
        sys.exit(0)
else:
    print ('M'+random.choice('NS')+random.choice('NEWS'))*2
    sys.exit(0)
print ('M'+direction)*2

只需更改一件事即可使其正常工作- line.index('Y')如果“ Y”不在行中将抛出错误,但如果“ Y”不在行line.find('Y')中将返回-1。否则,太好了!
Beanstalk

您的机械手有时会输出MSNMSN,这是无效的。
2015年

3

Coward,python3

胆小鬼总是要面对潜在的威胁。

但是,如果他觉得自己很强壮,他会突然跑来跑去,刺伤他身边的一切。

当前实施方式的问题在于,在不知道是第一还是第二移动的情况下发出移动命令。

#!/usr/bin/env python3.4

import sys, random


class Coward():
  """
  A coward always runs from potential threats.

  However, if he feels super strong, he will suddenly run amok 
  and stab everything near him.  
  """
  def __init__(self, hp, gold, turn, poison, pit, revealer, knife):
    self.hp=int(hp)
    self.gold=int(gold)
    if knife=="True": self.knife=True
    else: self.knife=False    
    self.pit=int(pit)
    self.poison=int(poison)

  def readGrid(self, grid):
    self.grid=grid.split("\n")
  @property
  def _confidence(self):
    return self.hp+5*self.knife-2*self.poison
  @property
  def _lineOfY(self):
    for i, line in enumerate(self.grid):
      if "Y" in line:
        return i
  @property
  def _colOfY(self):
    return self.grid[self._lineOfY].index("Y")
  @property
  def _maxX(self):
    return len(self.grid)-1
  @property
  def _maxY(self):
    return len(self.grid[0])-1
  def move(self, step):
    d = {'NN':(0,-1),'NE':(1,-1),'EE':(1,0),'SE':(1,1),'SS':(0,1),'SW':(-1,1),'WW':(-1,0),'NW':(-1,-1)}
    c2d={(0,-1):'NN',(1,-1):'NE',(1,0):"EE",(1,1):"SE",(0,1):"SS",(-1,1):"SW",(-1,0):"WW",(-1,-1):"NW"}
    #Don't move into wall/ boulder/ pit
    #print(d, file=sys.stderr)
    for k,v in list(d.items()):
      x=self._lineOfY+v[0]
      y=self._colOfY+v[1]
      #print (k, v ,x , y, file=sys.stderr)
      if x<0 or y<0 or x>self._maxX or y>self._maxY:
        #print ("Out of bounds: ", k, file=sys.stderr)
        del d[k]
      elif self.grid[x][y]=="}" or self.grid[x][y]=="P":
        del d[k]
    #Always avoid bats, and enemys
    for dx in range(-2,3):
      for dy in range(-2,3):
        x=self._lineOfY+dx
        y=self._colOfY+dy
        if x<0 or y<0 or x>self._maxX or y>self._maxY:
          continue;
        if self.grid[x][y] in ["B", "L", "C", "@"]:
          for k in self._toDirection(dx, dy):
            if k in d: del d[k] #Too many threats from all sides can paralyze the Coward: nowhere to go...
    #print(d, file=sys.stderr)
    tomove=[]
    #Neighboring fields
    for dx in [-1,1]:
      for dy in [-1,1]:
        x=self._lineOfY+dx
        y=self._colOfY+dy
        if x<0 or y<0 or x>self._maxX or y>self._maxY:
          continue
        if self.poison>0 and self.grid[x][y]=="W":
          for k,v in d.items():
            if v==(dx,dy):
              tomove.append(k)
        if self.grid[x][y]=="N": #Always go to nurse, even if dangerous
          tomove.append(c2d[(x,y)])
        if self.grid[x][y] in ["F","K","G"]: #Go to Food, Knife or Gold, if save
          for k,v in d.items():
            if v==(dx,dy):
              tomove.append(k)
    #Further away: Go towards food, knife and gold and Nina if save.
    for target in ["N", "F", "G", "K"]:
      for dx in [-2,2]:
        for dy in [-2,2]:
          x=self._lineOfY+dx
          y=self._colOfY+dy
          if x<0 or y<0 or x>self._maxX or y>self._maxY:
            continue
          if self.grid[x][y]==target:
            l=[ k for k in self._toDirection(dx,dy) if k in d]
            if l: tomove.append(random.choice(l))
    s=list(d.keys())
    random.shuffle(s)
    tomove+=s
    try:
      return "M"+tomove[step-1]
    except IndexError:
      return ""
  def attack(self, step):    
    c2d={(0,-1):'NN',(1,-1):'NE',(1,0):"EE",(1,1):"SE",(0,1):"SS",(-1,1):"SW",(-1,0):"WW",(-1,-1):"NW"}
    #If Bot next to you: always attack
    for k,v in c2d.items():
      x=self._lineOfY+k[0]
      y=self._colOfY+k[1]
      if x<0 or y<0 or x>self._maxX or y>self._maxY:
        continue
      if self.grid[x][y]=="@":
        return "A"+v
    #If Bot or monster could come closer: attack potential new position
    attDir={(-2,-2):["NW"], (-2,-1):["NW","WW"], (-2,0):["WW","NW","SW"], (-2,1):["WW","SW"], (-2,2):["SW"],(-1,-2):["NW","NN"], (-1,2):["SW","SS"], (0,2):["SW","SS","SE"],(0,-2):["NW","NN","NE"],(1,-2):["NN","NE"],(1,2):["SS","SE"],(2,-2):["NE"],(2,-1):["NE","EE"], (2,0):["NE","EE","SE"], (2,1):["EE","SE"], (2,2):["SE"]}
    for k,v in attDir.items():
      x=self._lineOfY+k[0]
      y=self._colOfY+k[1]
      if x<0 or y<0 or x>self._maxX or y>self._maxY:
        continue
      if self.grid[x][y] in ["@","L","C","B"]:
        return "A"+random.choice(v)
    return ""
  def _toDirection(self,dx,dy):
    if dx<0:
      if dy<0:
        return ["WW","NW","NN"]
      elif dy==0:
        return ["WW","NW","SW"]
      elif dy>0:
        return ["WW","SW","SS"]
    elif dx>0:
      if dy<0:
        return ["EE","NE","NN"]
      elif dy==0:
        return ["EE","NE","SE"]
      elif dy>0:
        return ["EE","SE","SS"]
    elif dx==0:
      if dy<0:
        return ["NN","NE","NW"]
      elif dy==0:
        return []
      elif dy>0:
        return ["SS","SE","SW"]
  def _nearBat(self):
    for dx in range(-2,3):
      for dy in range(-2,3):
        x=self._lineOfY+dx
        y=self._colOfY+dy
        if x<0 or y<0:
          continue;
        if self.grid[x][y]=="B":
          return True
    return False
  def makeTurn(self):
    try:
      command=""
      #If stuck, just attack
      if self.pit:
        command+=self.attack(1)+self.attack(2)
      #Always run from bats
      if self._nearBat:
        command+=self.move(1)+self.move(2)
      #If high-confidence: attack
      if self._confidence>30:
        command+=self.attack(1)+self.attack(2)
      #Else: Move somewhere
      command+=self.move(1)+self.move(2)
      #Just in case, two random attacks
      d = ['NN','NE','EE','SE','SS','SW','WW','NW']
      a=random.choice(d)
      b=random.choice([ x for x in d if x!=a])
      command+="A"+a+"A"+b
      return command[:6]
    except Exception as e:
      #print (e, file=sys.stderr)
      #Attacking is better than nothing
      d = ['NN','NE','EE','SE','SS','SW','WW','NW']
      a=random.choice(d)
      b=random.choice([ x for x in d if x!=a])
      return "A"+a+"A"+b


info=sys.stdin.readline()
grid=sys.stdin.read()
info=info.split(",")
bot=Coward(*info)
bot.readGrid(grid)
t=bot.makeTurn()
#print(t, file=sys.stderr)
print(t)

3

生存机器人-Python 2

from __future__ import print_function
health,gold,survived,poison,pit,revealer,knife = input()
if pit:
    exit()
#Yes, this is python 2, despite the use of input()
lines = []
try:
    while True:
        lines.append(raw_input())
except EOFError:
    pass
CMOVES={"NW":(-1,-1),"NN":(-1,+0),"NE":(-1,+1),
        "WW":(+0,-1),             "EE":(-0,+1),
    "SW":(+1,-1),"SS":(+1,+0),"SE":(+1,+1),
}
MOVES={v:k for k,v in CMOVES.iteritems()}
import sys
def get_your_pos():
    for row,line in enumerate(lines):
        for col,square in enumerate(line):
            if square == "Y":
                return row,col
    raise ValueError("Your bot is not present.")
def isnearby(thing,p=None):
    your_pos = p or get_your_pos()
    for move in MOVES:
        yp = your_pos[0]+move[0],your_pos[1]+move[1]
        try:
            if yp[0] >= 0 and yp[1] >= 0 and lines[yp[0]][yp[1]] == thing:
                return move
        except IndexError:
            #Edge of cavern
            pass
for turn in range(2):
    import random
    nprio = .5
    if health > 25:
        nprio -= .2
    elif health < 10:
        nprio += .3
    if poison:
        nprio += .18
    #heal
    motive = how = None
    if random.random() < nprio:
        nurse = isnearby("N")
        if nurse:
            motive = "M"
            how = MOVES[nurse]
        elif random.random() < nprio:
            food = isnearby("F")
            if food:
                motive = "M"
                how = MOVES[food]
    #cure poison
    if poison and not motive:
        water = isnearby("W")
        if water:
            motive = "M"
            how = MOVES[water]
    if not motive:
        #Kill lions, bats, and centipedes
        for animal in ("L","B","C"):
            animal = isnearby(animal)
            if animal:
                motive = "A"
                how = MOVES[animal]
                y = get_your_pos()
                y = y[0]+animal[0],y[1]+animal[1]
                lines = map(list,lines)
                lines[y[0]][y[1]] = "-"
                break
        else:
            #Pick up knives
            if not knife:
                knife = isnearby("K")
                if knife:
                    motive = "M"
                    how = MOVES[knife]
            #Attack other bots
            else:
                prey = isnearby("@")
                if prey:
                    motive = "A"
                    how = MOVES[prey]
    #Get gold
    gold = isnearby("G")
    if gold and not motive:
        motive = "M"
        how = MOVES[gold]
    def isvalidmove(c):
        c = CMOVES[c]
        y = get_your_pos()
        y=(y[0]+c[0],y[1]+c[1])
        if y[0] >= 0 and y[1] >= 0:
            try:
                lines[y[0]][y[1]]
            except LookupError:
                pass
            else:
                return True
    if turn and not motive:
        motive = "M"
        while not (how and how not in (isnearby("}"),isnearby("P"),isnearby("@"))\
              and isvalidmove(how)):
            how = random.choice(CMOVES.keys())
    if not motive:break
    if not turn and motive == "M":
        lines = map(list,lines)
        yp = get_your_pos()
        lines[yp[0]][yp[1]] = "-"
        yp=[yp[0]+CMOVES[how][0],yp[1]+CMOVES[how][1]]
        lines[yp[0]][yp[1]] = "Y"
    print(motive+how,end="")
else:
    exit()
#Nothing found on first move
def isvaguelynearby(thing):
    your_pos = get_your_pos()
    for move in MOVES:
        yp = your_pos[0]+move[0],your_pos[1]+move[1]
        try:
            if yp[0] >= 0 and yp[1] >= 0 and board[yp[0]][yp[1]] != "P":
                dest = isnearby(thing,yp)
                if dest:
                    return move,dest
        except IndexError:
            #Edge of cavern
            pass
if random.random() < nprio:
    dests = isvaguelynearby("N")
    if not dests and random.random() < nprio:
        dests = isvaguelynearby("F")
    if dests:
        m1,m2 = MOVES[dests[0]],MOVES[dests[1]]
        print("M" + m1 + "M" + m2)
        exit()
dests = (poison and isvaguelynearby("W")) or (not knife and isvaguelynearby("K"))\
    or isvaguelynearby("G")
prey = isvaguelynearby("L") or isvaguelynearby("B") or isvaguelynearby("C") or \
       (knife and isvaguelynearby("@"))
if dests:
    m1,m2 = MOVES[dests[0]],MOVES[dests[1]]
    print("M" + m1 + "M" + m2)
elif prey:
    m1,m2 = MOVES[prey[0]],MOVES[prey[1]]
    print("M" + m1 + "A" + m2)
else:
    how = None
    while not (how and how not in     (isnearby("}"),isnearby("P"),isnearby("@"))\
          and isvalidmove(how)):
        how = random.choice(CMOVES.keys())
    print("M"+how,end="")
    lines = map(list,lines)
    yp = get_your_pos()
    lines[yp[0]][yp[1]] = "-"
    yp=[yp[0]+CMOVES[how][0],yp[1]+CMOVES[how][1]]
    lines[yp[0]][yp[1]] = "Y"
    while not (how and how not in (isnearby("}"),isnearby("P"),isnearby("@"))\
          and isvalidmove(how)):
            how = random.choice(CMOVES.keys())
    print("M"+how)

编辑:增加了更好的避坑。


2

河豚,Python 3+

我就是那个人。

#!/usr/bin/env python3.4
import random
def select():
 return "A"+["NN","NE","EE","SE","SS","SW","WW","NW"][random.randint(0,7)]
print(select()+select())

该机器人具有自我维持的目的。如果有时间,我可能会在以后增加打高尔夫球的机会。
Conor O'Brien 2015年

这会使您的代码更长一些,但是如果想要更致命,河豚可以在同一回合中两次攻击
The Beanstalk 2015年

@TheBeanstalk哦,天哪!
Conor O'Brien 2015年

您意识到该机器人将永远不会获得任何金钱,并且并非所有其他机器人都将徘徊于其“杀戮光环”中
pppery 2015年

@ppperry我确实意识到了这一点;这主要是为了使球滚动;它的目的不是要赢,而是要让生活稍微困难些。就像黑帽侠
Conor O'Brien 2015年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.