दोनों: टीएनटी रन चैलेंज


25

यह एक Minecraft मिनी-गेम से प्रेरित था। नियम बहुत सरल हैं: आप दौड़ते हैं और चारों ओर कूदते हैं, और आपके द्वारा इस पर कदम रखते ही आप गायब होने वाले हर ब्लॉक को हटा देते हैं। लक्ष्य आखिरी बचा है।

आपका बॉट पूरा कार्यक्रम होना चाहिए। इसे कमांड लाइन तर्क के रूप में इनपुट को स्वीकार करना चाहिए। इनपुट "दुनिया" का एक नक्शा होगा; यहाँ एक उदाहरण है:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxx xxxxxxxxxxxxxxxxxxxxxxxxxxx
xxx xxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxx x xxxxxxxxxxxxx@xxxxxxxxxxx
xxxxxx1xxxxxxxxxxxxx xxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx xxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx xxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx xxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx xxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx xxxxxxxxxxx
xxxxxxxxxx           xxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx xxxxxxxxxxxx
xxxxxxxxxxxxxxxxx x x xxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx xxxxxxxxxxx
xxxxxxxxxxxxxx xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxx xxx xx3xxxxxxxxxx
xxxxxxxxxxxxxxxxxxx xxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx  x
xxxxxxxxxxxxxxxxxxxxxxxxxxx   xx
xxxxxxxxxxxxxxxxxxxxxxxxx      2
xxxxxxxxxxxxxxxxxxxxxxx         

किंवदंती इस प्रकार है:

x: solid block

 : empty air

@: your bot

1,2,3,4,5,6,7,8,9,0: other bots

आपका बॉट पूर्णांक की एक जोड़ी के रूप में आपके कदम का उत्पादन करना चाहिए। उदाहरण: -1, 21 ब्लॉक को बाईं ओर ले जाएगा और 2 ब्लॉक डाउन (निर्देशांक मूल शीर्ष बाएं कोने में है)।

आप अपने वर्तमान स्थान से चार ब्लॉक, मैनहट्टन की दूरी तक बढ़ सकते हैं। यदि आप इससे आगे बढ़ने की कोशिश करते हैं, तो यह कदम अमान्य है। कोई भी चाल जो आपको पिछले छोर पर ले जाएगी, वह आपको किनारे पर रख देगी। अमान्य चालों को अनदेखा किया जाएगा।

एक बार जब आप एक ब्लॉक पर उतरते हैं तो इसे हटा दिया जाता है; यदि आप अगले ब्लॉक पर बने रहेंगे तो आप गिर जाएंगे। दो बॉट एक ही मोड़ पर एक ही ब्लॉक पर उतर सकते हैं और दोनों बच सकते हैं; यदि ऐसा होता है, तो दोनों बॉट केवल खुद को देखेंगे और दूसरे बॉट को नहीं।

यदि आपको दृढ़ता के लिए फ़ाइलों को संग्रहीत करने की आवश्यकता है, तो कृपया अपने बॉट के नाम वाले फ़ोल्डर में ऐसा करें। यदि कोई मौजूद है तो आप अन्य बॉट के लगातार डेटा को नहीं पढ़ सकते हैं।

मैच नियंत्रक https://paste.ee/p/Xf65d पर उपलब्ध है ।

कृपया उन भाषाओं का उपयोग करें जिन्हें एक मानक लिनक्स या OSX इंस्टॉल पर चलाया जा सकता है।

वर्तमान परिणाम (100 राउंड):

JumpBot                   31
LookBot                   27
ShyBot                    26
Slow Bot                  15
KnightBot                 2
Moat Builder              0
UpBot                     0
Random Bot                0

इसी तरह, हालांकि यहां एक महत्वपूर्ण अंतर यह है कि आप कई ब्लॉकों को "कूद" सकते हैं - इस प्रकार आप किसी को ब्लॉक नहीं कर सकते यदि वे देखते हैं कि आप क्या कर रहे हैं।
स्काइलर

आप सैंडबॉक्स में एक डुबकी के रूप में बंद नहीं कर सकते हैं और मुझे नहीं लगता कि यह काफी एक है
नीला

1
क्या चाल एक साथ या अनुक्रमिक हैं? क्या इनपुट वास्तव में कमांड लाइन तर्क के रूप में एक न्यूलाइन-युक्त स्ट्रिंग है?
23

1
मैं बोट को कॉल करने के लिए एक बार बिना किसी दुनिया को शुरू करने का सुझाव
दूंगा

@ फैर्सम चाल एक साथ हैं; इनपुट वास्तव में एक newline- युक्त कमांड लाइन तर्क है। अगर आपको इसके बजाय स्टडिन की आवश्यकता है, तो मुझे बताएं और मैं शायद या तो अनुमति देने के लिए कंटेस्टेंट को संशोधित कर सकता हूं।
स्काइलर

जवाबों:


9

स्लो बॉट (पायथन)

वह एक लाइन पैटर्न में चलता है और उन्हें बनाने से पहले उनकी चाल की जाँच करता है (जब वह लंबे रनटाइम्स को रोकने के लिए जीवित रहता है तो आत्महत्या करता है) उसने मेरे टेस्ट टूर्नामेंट में 195/200 बैटल जीता।

import sys
import re


class vec2(object):
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

    def __add__(self, other):
        return vec2(self.x + other.x, self.y + other.y)

    def __sub__(self, other):
        return vec2(self.x - other.x, self.y - other.y)

    def __iadd__(self, other):
        return self + other

    def __isub__(self, other):
        return self - other

    def __neg__(self):
        return vec2(-self.x, -self.y)


def xy_to_i(vec=vec2(0, 0)):
    vec -= vec2(1, 1)
    vec.y += (vec.x - vec.x % 32) / 32
    return vec.x + vec.y * 33


def i_to_xy(i=0):
    vec = vec2(0, 0)
    vec.x = i % 33
    vec.y = (i - vec.x) / 32 + 1
    vec.x += 1
    return vec


class World(object):
    def __init__(self, map=''):
        self.map = map

    def getPlayerPosition(self):
        return i_to_xy(re.search('@', self.map).start())

    def getNumOtherBots(self):
        return len(re.findall('([0123456789])', ' ' + self.map + ' '))

    def get_tile(self, vec=vec2(0, 0)):
        i = xy_to_i(vec)
        return self.map[i:i + 1]


world = World(sys.argv[1])
pos = world.getPlayerPosition()


def check_moveV(vecd=vec2(0, 0)):
    try:
        vecn = pos + vecd

        if vecn.x > 32 or vecn.x < 1 or vecn.y > 32 or vecn.y < 1 \
            or abs(vecd.x) + abs(vecd.y) > 4:
            return False

        # Note: this will also avoid positions other bots are on (will disappear in the next step).

        return world.get_tile(vecn) == 'x'
    except:
        raise
        return False


def check_move(x=0, y=0):
    return check_moveV(vec2(x, y))


def run():
    if world.getNumOtherBots() == 0:
        return '0 0'  # Suicide if we are the only one left.

    # this creates the "line" pattern

    if check_move(0, -1):
        return '0 -1'

    if check_move(0, 1):
        return '0 1'

    if check_move(1, 0):
        return '1 0'

    if check_move(1, -1):
        return '1 -1'

    # If we get here, we are desperate and need to find a safe place to jump.

    for dx in range(-2, 2):
        for dy in range(-2, 2):
            if check_move(dx, dy):
                return '%i %i' % (dx, dy)

    # If we can't find a place to jump in close range, try long range.

    for dx in range(-4, 4):
        for dy in range(-4, 4):
            if check_move(dx, dy):
                return '%i %i' % (dx, dy)

    # If we get here, we are dead no matter what; accept our fate.

    return '0 0'


print(run())

मैं अजगर में विशेषज्ञ नहीं हूं और इसे कम / बेहतर करने के 100 तरीके हैं


1
बस एक बात, अगर आप दूसरे बॉट के साथ एक ही जगह पर हैं और आप अंतिम दो हैं, तो आप सोचेंगे कि यह आखिरी और आत्महत्या है।
तिमटेक

जब मैं दृढ़ता को लागू करता हूं, तो वह आत्महत्या तक 5 राउंड इंतजार करेगा
बॉउन

महान, यही मैं सुझाव देने वाला था। बहुत बढ़िया जवाब, वैसे।
टिमटेक

6

जंपबोट (C)

अगले दौर में सबसे अधिक संभव चालों के साथ मैदान में कूदने की कोशिश करें।

#include <stdio.h>
#include <stdlib.h>

typedef struct map {
     char *raw_map;
     int size;
     int lines;
     char *pos;
} *MAP;

typedef struct cdata {
     int result;
     MAP m;
     int x;
     int y;
} *CDATA;

typedef struct mdata {
     int x;
     int y;
     int moves;
     int bx;
     int by;
     MAP m;
} *MDATA;

int numberOfMoves(MAP, int, int);
char getAt(MAP, int, int);

int abs(int x)
{
    return x < 0 ? x*-1 : x;
}

void count(void *data, int x, int y)
{
    CDATA d = (CDATA)data;
    char c = getAt(d->m, d->x + x, d->y + y);
    if(c != 'x') return;
    d->result++;
}

void choose(void *data, int x, int y)
{
    MDATA m = (MDATA)data;
    char c = getAt(m->m, m->x + x, m->y + y);
    if(c != 'x') return;
    int moves = numberOfMoves(m->m, m->x+x, m->y+y);
    if(moves > m->moves || (!m->bx && !m->by)) {
        m->moves = moves;
        m->bx = x;
        m->by = y;
    }
}

MAP parse_input(char *input)
{
    MAP m = malloc(sizeof *m);
    if(!m) {
        fprintf(stderr, "failed to alloc map\n");
        return NULL;
    }

    m->size=0;
    m->lines=1;
    m->pos=0;

    char *temp;
    for(temp = input;*temp;temp++) {
        switch(*temp) {
            case '\n': m->lines++; break;
            default: break;
        }
    }
    m->size = (temp + 1) - (input + m->lines);
    m->raw_map = malloc(m->size);
    if(!m->raw_map) {
        fprintf(stderr, "failed to alloc raw_map\n");
        return NULL;
    }

    int index = 0;
    for(temp = input; *temp; temp++) {
        if(*temp == '@') m->pos = m->raw_map + index;
        if(*temp != '\n') m->raw_map[index++] = *temp;
    }

    return m;
}

char getAt(MAP m, int x, int y)
{
    return m->raw_map[x + y*(m->size / m->lines)];
}

void posToXY(MAP m, int *x, int *y)
{
    int index = m->pos - m->raw_map;
    int length = m->size / m->lines;
    *x = index % length;
    *y = index / length;
}

typedef void (*DOFUNC)(void *, int, int);
void processMoves(MAP m, int x, int y, DOFUNC proc, void *data)
{
    int length = m->size / m->lines;    
    int left = x>=4 ? 4 : x;
    int right = x + 4 <= length ? 4 : length - (x + 1);
    int up = y >= 4 ? 4 : y;
    int down = y + 4 <= m->lines ? 4 : m->lines - (y + 1);

    for(int i=-left; i<=right; i++) {
        for(int j=-up; j<=down; j++) {
            if((abs(i) + abs(j) <= 4) && (i || j)) (*proc)(data, i, j);
        }
    }
}

int numberOfMoves(MAP m, int x, int y)
{
    struct cdata d;
    d.result = 0;
    d.x = x;
    d.y = y;
    d.m = m;
    processMoves(m, x, y, &count, &d);
    return d.result;
}

void getMove(MAP m, int *x, int *y)
{
    struct mdata d;
    posToXY(m, &d.x, &d.y);
    d.moves = 0;
    d.bx = 0;
    d.by = 0;
    d.m = m;
    processMoves(m, d.x, d.y, &choose, &d);
    *x = d.bx;
    *y = d.by;
}

int main(int argc, char *argv[])
{
    if(argc != 2) {
        fprintf(stderr, "bad number of arguments %d\n", argc);
        return -1;
    }

    MAP m = parse_input(argv[1]);
    int x=0, y=0;
    getMove(m, &x, &y);
    printf("%d %d\n", x, y);
    return 0;
}

5

लुकबोट (C)

सरल बॉट जो कि स्लो बॉट के प्रदर्शन के समान है, सिवाय इसके कि यह एक यादृच्छिक संभव चाल बनाता है। इसे प्रिडिक्टबॉट में सुधारने की योजना बनाएं।

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <sys/time.h>

#define WORLDSZ (32)
#define WORLDSZ_2 (WORLDSZ*WORLDSZ)

int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a<b?a:b;}

struct Position{
    int x,y;
};
typedef struct Position Position;

struct World{
    Position me;
    double enemymap[WORLDSZ][WORLDSZ]; //chance of enemy present
    bool open[WORLDSZ][WORLDSZ];
};
typedef struct World World;

void world_read(World *world,const char *arg){
    int x,y,i=0;
    for(y=0;y<WORLDSZ;y++,i++){
        for(x=0;x<WORLDSZ;x++,i++){
            if(arg[i]=='@'){world->me.x=x; world->me.y=y;}
            world->enemymap[y][x]=arg[i]>='0'&&arg[i]<='9';
            world->open[y][x]=arg[i]=='x';
        }
    }
}

//returns relative position
Position world_calcmove(World *world){
    const int mex=world->me.x,mey=world->me.y;
    int dx,dy;
    Position poss[40];
    int nposs=0;
    for(dy=max(-mey,-4);dy<=min(WORLDSZ-1-mey,4);dy++){
        const int absdy=abs(dy);
        for(dx=max(-mex,absdy-4);dx<=min(WORLDSZ-1-mex,4-absdy);dx++){
            if(!world->open[mey+dy][mex+dx])continue;
            poss[nposs].x=dx;
            poss[nposs++].y=dy;
        }
    }
    if(nposs==0){
        poss[0].x=poss[0].y=0;
        return poss[0];
    }
    return poss[rand()%nposs];
}

int main(int argc,char **argv){
    if(argc!=2){
        fprintf(stderr,"Call with world!\n");
        return 1;
    }
    struct timeval tv;
    gettimeofday(&tv,NULL);
    srand(tv.tv_sec*1000000ULL+tv.tv_usec);

    World world;
    world_read(&world,argv[1]);
    Position move=world_calcmove(&world);
    printf("%d %d\n",move.x,move.y);
}

5

मॉट बिल्डर (अजगर)

अगर मैं खुद के आसपास एक खाई खोदता हूं, तो कोई भी इसे बाहर नहीं कर सकता है।

... "एक कोने के सिम्युलेटर 2016 में खुद को पेंट करें" के रूप में भी जाना जाता है।

import numpy
import sys
import math
import os

if not os.path.exists('./moatbuilder'):
    os.mkdir('./moatbuilder')

raw_field = sys.argv[1]
field = numpy.array([numpy.array(list(i)) for i in raw_field.splitlines()])
field_size = len(field)
x, y = raw_field.replace('\n','').index('@')%field_size, int(raw_field.replace('\n','').index('@')/field_size)
# If there are no holes, it's the first round - reset persistence
if raw_field.count(' ')==0:
    open('./moatbuilder/persistent','w').write('')

def bigmove(target):
    if x < target[0]:
        return min(4, target[0] - x), 0
    elif x > target[0]:
        return max(-4, target[0] - x), 0
    elif y < target[1]:
        return 0, min(4, target[1] - y)
    else:
        return 0, max(-4, target[1] - y)

def smallmove(target):
        if x < target[0]:
        try:
            return min(max(1, list(field[y][x:x+4]).index('x')), target[0] - x), 0
        except:
            return 0, 0
        elif x > target[0]:
        try:
            return max(min(-1, 0-list(reversed(field[y][x-4:x])).index('x')), target[0] - x), 0
        except:
            return 0, 0
        elif y < target[1]:  
        try:
                    return 0, min(max(1, list(field[:,x][y:y+4]).index('x')), target[1] - y)
        except:
            return 0, 0
        else:
        try:
            return 0, max(min(-1, 0-list(reversed(field[:,x][y-4:y])).index('x')), target[1] - y)
        except:
            return 0, 0


try:
    mode = int(open('./moatbuilder/persistent').read())
except:
    mode = 1

# Modes:
# 1 - go to the center
# 2 - go to an outside edge
# 3 - dig moat
if mode==1:
    dx, dy = bigmove((int(field_size/2), int(field_size/2)))
    if dx==0 and dy==0:
        open('./moatbuilder/persistent', 'w').write('2')
        mode = 2
if mode==2:
    dx, dy = bigmove((int(field_size-1), int(field_size/2)))
    if dx==0 and dy==0:
        dy = 1
        open('./moatbuilder/persistent', 'w').write('3')
        mode = 3
elif mode==3:
    direction = max(field_size-x, field_size-y)%2
    if direction == 1:
        if x > y:
            dx, dy = smallmove((y, y))
        else:
            dx, dy = smallmove((x, field_size - 1))
        if dx==0 and dy==0:
            dx = 1
    else:
        if y > x:
            dx, dy = smallmove((x, x))
        else:
            dx, dy = smallmove((field_size - 1, y))
        if dx==0 and dy==0:
            dy = 1

print "%i %i" % (dx, dy)

यह वास्तव में काफी अच्छी तरह से काम करता है, लेकिन यह बॉट के खिलाफ खो जाएगा जो लंबे समय तक रहते हैं (
यानी

आप smallmove () में खरोज ठीक करने के लिए चाहते हो सकता है ... मेरी अजगर इस :) भोजन नहीं करता
tomsmeding

5

मोंटे (पायथन)

क्षमा करें, वह सजा बस बननी थी।

वैसे भी, यह बॉट सभी संभावित चाल सेट पर मोंटे कार्लो ट्री सर्च करके काम करता है । जंपबोट के बारे में सोचें, केवल अधिक गहराई में।

चलाने के लिए, इसे एक अतिरिक्त कमांड लाइन तर्क की आवश्यकता है (नियंत्रक में निर्दिष्ट किया जा सकता है)। यह नियंत्रित करता है कि बॉट को कितने समय तक (एमएस में) खोज करनी चाहिए; मैंने परीक्षण में 750-1500 का उपयोग किया।

कोड:

import sys
import math
import copy
#from profilestats import profile
pmap = sys.argv[2].split("\n")
pmap = [list(r) for r in pmap]

#find a player
#@profile
def find(tmap,bot):
   r,c=-1,-1
   for row in range(len(tmap)):
      for col in range(len(tmap[row])):
         if tmap[row][col]==bot:
            r,c=row,col
   return r,c

mer,mec=find(pmap,'@')
bots=[(mer,mec)]

#find all the other players
for b in range(10):
   r,c=find(pmap,str(b))
   if r != -1:
      bots.append((r,c))

#getter function, treats oob as spaces
def get(tmap,r,c):
   if r<0 or r>=len(tmap) or c<0 or c>=len(tmap[r]):
      return ' '
   return tmap[r][c]

#returns manhattan distance between 2 positions  
def dist(r1,c1,r2,c2):
   return abs(r1-r2)+abs(c1-c2)

#gets all possible moves from a map
#@profile 
def moves(tmap,ther=-1,thec=-1):
   if ther==-1: ther,thec = find(tmap,'@')
   pos=[]
   for r in range(-4,5):
      for c in range(-4,5):
         if abs(r)+abs(c)<=4 and get(tmap,ther+r,thec+c)=='x':
            pos.append((r,c))
   return pos


ttlmoves = 40
#monte-carlo tree node
class MCNode:
   def __init__(self):
      self.wins=0
      self.simu=0
      self.chld=[]
      self.cmap=[[]]
      self.prnt=None
      self.r=-1
      self.c=-1
   def add(self, cnode):
      self.chld.append(cnode)
      cnode.prnt = self
   #used to balance exploitation and exploration
   #@profile
   def param(self,cin):
      return self.chld[cin].wins/self.chld[cin].simu\
             + 1.414 * math.sqrt( math.log(self.simu) / \
             self.chld[cin].simu )
   #finds the child with the highest param
   #@profile
   def best(self):
      vals = [self.param(x) for x in range(len(self.chld))]
      binx = 0
      bval = vals[0]
      for x in range(len(vals)):
         if vals[x]>bval:
            binx=x
            bval=vals[x]
      return self.chld[binx]


#update all the parents 
#@profile   
def backprog(leaf):
   par = leaf.prnt
   if not (par is None):
      par.wins+=leaf.wins
      par.simu+=leaf.simu
      backprog(par)

#expand all the moves from a position
#@profile
def expand(rootn):
   ther,thec = rootn.r,rootn.c
   for r,c in moves(rootn.cmap,rootn.r,rootn.c):
      nmap = copy.deepcopy(rootn.cmap)
      nmap[ther+r][thec+c] = '@'
      nmap[ther][thec]=' '
      nnode = MCNode()
      nm = moves(nmap,ther+r,ther+c)
      nnode.wins = len(nm)
      nnode.simu = ttlmoves
      nnode.r=ther+r
      nnode.c=thec+c
      nnode.cmap = nmap
      rootn.add(nnode)
      backprog(nnode)

root = MCNode()
m = moves(pmap,mer,mec)
root.wins = len(m)
root.simu = ttlmoves
root.cmap=copy.deepcopy(pmap)
root.r=mer
root.c=mec
expand(root)

#simulate a bunch of outcomes
import time
curt  = lambda: int(round(time.time() * 1000))
strt = curt()
ttme = int(sys.argv[1])
while curt()-strt < ttme:
   tnode=root
   while tnode.chld:
      tnode=tnode.best()
   expand(tnode)

#choose the most explored one
bnode = max(root.chld,key=lambda n:n.simu)

#output
print("{} {}".format((bnode.c-mec),(bnode.r-mer)))

परीक्षण

25 राउंड:

MonteBot            14
JumpBot             6
ShyBot              5
LookBot             1
KnightBot           0
SlowBot             0

100 राउंड:

JumpBot             38
MonteBot            36
ShyBot              15
LookBot             14
SlowBot             2
KnightBot           0

200 राउंड:

MonteBot            87
JumpBot             64
LookBot             33
ShyBot              21
SlowBot             5
KnightBot           0

उपर्युक्त सभी सिमुलेशन का उपयोग खोज समय 750 है। यह बॉट शायद अधिक लंबे समय तक खोज समय के साथ बेहतर होगा (मुझे नहीं पता कि अधिकतम अनुमति क्या है)।

सुधार

इस बॉट में अभी भी सुधार की आवश्यकता है:

  1. प्रदर्शन: इसे खोज करने के लिए पूरे समय की आवश्यकता है।
  2. भविष्यवाणी: यह अन्य बॉट की चालों का हिसाब नहीं देगा।
  3. शेष राशि: मुझे यकीन नहीं है कि अगर मैं जिस नोड का पता लगाऊं, उसका गणना करने के लिए यूसीटी फॉर्मूला का उपयोग कर रहा हूं, तो यह इष्टतम है।

4

ShyBot (पायथन)

यह बॉट वास्तव में अन्य बॉट्स को पसंद नहीं करता है और यदि संभव हो तो खुद को दूर रखने की कोशिश करेगा। ShyBot भी वास्तव में इस बारे में सावधान है कि वह कहाँ कदम रखता है; यह अन्य बॉट्स पर भी कदम नहीं रखेगा। हालांकि, ShyBot अभी भी अक्सर हारता है जो असुरक्षित बनाता है।

import sys
map = sys.argv[1]
map = map.split("\n")
map = [list(r) for r in map]

def find(map,bot):
   r,c=-1,-1
   for row in range(len(map)):
      for col in range(len(map[row])):
         if map[row][col]==bot:
            r,c=row,col
   return r,c


mer,mec=find(map,'@')
bots=[(mer,mec)]

for b in range(10):
   r,c=find(map,str(b))
   if r != -1:
      bots.append((r,c))

avg=[0,0]

for b in bots:
   avg[0]+=b[0]
   avg[1]+=b[1]

avg[0] = avg[0]/len(bots)
avg[1] = avg[1]/len(bots)

def get(map,r,c):
   if r<0 or r>=len(map) or c<0 or c>=len(map[r]):
      return ' '
   return map[r][c]

def dist(r1,c1,r2,c2):
   return abs(r1-r2)+abs(c1-c2)

pos=[]
for r in range(-4,5):
   for c in range(-4,5):
      if abs(r)+abs(c)<=4 and get(map,mer+r,mec+c)=='x':
         pos.append((r,c))

if len(pos)==0:
   bestr,bestc=0,0
else:
   bestr,bestc=pos[0]

for r,c in pos:
   if dist(mer+r,mec+c,avg[0],avg[1])>dist(mer+bestr,mec+bestc,avg[0],avg[1]):
      bestr,bestc=r,c

print(str(bestc)+" "+str(bestr))

4

नाइटबॉट (जावा)

यह शतरंज की तरह काम करता है, और इसका नाम ट्विच ...

...

.........

............................माफ़ कीजिये...

public class KnightBot{
   private static String[] map;
   private static int myx;
   private static int myy;
   public static void main(String[] args){
      map=args[0].split("\n");
      for(int y=0;y<map.length;y++){
         if(map[y].indexOf("@")!=-1){
            myy = y;
            myx = map[y].indexOf("@");
            break;
         }
      }
      System.out.println(move((int)(Math.random()*4),4));
   }
   public static String move(int dir,int tries){
      if(tries==0)return "0 0";
      int x=dir<2?1:-1;
      int y=dir%2==0?2:-2;
      if((myx+x<0||myx+x>=map[0].length()||myy+y<0||myy+y>=map.length)||map[y+myy].charAt(myx+x)!='x'){
         x=dir<2?2:-2;
         y=dir%2==0?1:-1;
      }
      if((myx+x<0||myx+x>=map[0].length()||myy+y<0||myy+y>=map.length)||map[y+myy].charAt(myx+x)!='x')
         return move(++dir>3?0:dir,tries-1);
      return x+" "+y;
   }
}

SwirlyBot (Java)

ये स्पष्ट रूप से इष्टतम समाधान नहीं हैं, लेकिन मुझे आशा है कि यह मिडलेवल परीक्षण के लिए उपयोगी होगा।

public class SwirlyBot{
   private static String[] map;
   private static int myx;
   private static int myy;
   public static void main(String[] args){
      map=args[0].split("\n");
      for(int y=0;y<map.length;y++){
         if(map[y].indexOf("@")!=-1){
            myy = y;
            myx = map[y].indexOf("@");
            break;
         }
      }
      System.out.println(move(0));
   }
   public static String move(int dir){
      switch(dir){
         case 0:
            if(!safe(0,1)){
               if(safe(1,1)){
                  return "1 1";//Down-Right
               }else{
                  if(safe(1,0)){
                     return "1 0";//Right
                  }
               }
            }
            break;
         case 1:
            if(!safe(1,0)){
               if(safe(1,-1)){
                  return "1 -1";//Up-Right
               }else{
                  if(safe(0,-1)){
                     return "0 -1";//Up
                  }
               }
            }
            break;
         case 2:
            if(!safe(0,-1)){
               if(safe(-1,-1)){
                  return "-1 -1";//Up-Left
               }else{
                  if(safe(-1,0)){
                     return "-1 0";//Left
                  }
               }
            }
            break;
         case 3:
            if(!safe(-1,0)){
               if(safe(-1,1)){
                  return "-1 1";//Down-Left
               }else{
                  if(safe(0,1)){
                     return "0 1";//Down
                  }
               }
            }
            break;
         case 4:
            if(safe(0,-1))return "0 -1";
            break;
         case 5:
            if(!safe(0,2)){
               if(safe(1,2)){
                  return "1 2";//Down-Right
               }else{
                  if(safe(2,2)){
                     return "2 2";
                  }else{
                     if(safe(2,1)){
                        return "2 1";
                     }else{
                        if(safe(2,0)){
                           return "2 0";//Right
                        }
                     }
                  }
               }
            }
            break;
         case 6:
            if(!safe(2,0)){
               if(safe(2,-1)){
                  return "2 -1";//Up-Right
               }else{
                  if(safe(2,-2)){
                     return "2 -2";
                  }else{
                     if(safe(1,-2)){
                        return "1 -2";
                     }else{
                        if(safe(0,-2)){
                           return "0 -2";//Up
                        }
                     }
                  }
               }
            }
            break;
         case 7:
            if(!safe(0,-2)){
               if(safe(-1,-2)){
                  return "-1 -2";//Up-Left
               }else{
                  if(safe(-2,-2)){
                     return "-2 -2";
                  }else{
                     if(safe(-2,-1)){
                        return "-2 -1";
                     }else{
                        if(safe(-2,0)){
                           return "-2 0";//Left
                        }
                     }
                  }
               }
            }
            break;
         case 8:
            if(!safe(-2,0)){
               if(safe(-2,1)){
                  return "-2 1";//Down-Left
               }else{
                  if(safe(-2,2)){
                     return "-2 2";
                  }else{
                     if(safe(-1,2)){
                        return "-1 2";
                     }else{
                        if(safe(0,2)){
                           return "0 2";//Down
                        }
                     }
                  }
               }
            }
            break;
      }
      if(dir<8)return move(dir+1);
      return "0 -1";
   }
   public static boolean safe(int x, int y){
      return !((myx+x<0||myx+x>=map[0].length()||myy+y<0||myy+y>=map.length)||map[y+myy].charAt(myx+x)!='x');
   }
}

नमस्कार, और PPCG में आपका स्वागत है! बहुत बढ़िया जवाब!
NoOneIsHere

2

रैंडम बॉट, अपबॉट

मुकाबला करने के लिए दो शुरुआती बॉट:

रैंडम बॉट: एक उदाहरण बॉट जो बेतरतीब ढंग से चलता है।

import random

x = random.randint(-4, 4)
y = random.randint(max(-4, -4 + abs(x)), min(4, 4 - abs(x)))
print x, y

अपबॉट: एक उदाहरण बॉट जो आगे बढ़ता है।

print '0 -1'

मैंने अपने (अब हटाए गए) रैंडम वॉकर उत्तर के लिए 10 परीक्षण राउंड चलाए, और उल्लासपूर्वक, UpBot बहुत अच्छा कर रहा है। उन्होंने 10 में से 7 फेरे लिए।
user48538


यहां पूर्ण परीक्षा परिणाम दिए गए हैं , ज़िप फ़ाइल के रूप में प्रदान किए गए हैं।
user48538

अपबॉट अच्छी तरह से करता है क्योंकि वह केवल एक समय में एक ब्लॉक को स्थानांतरित करता है, इसलिए यह आमतौर पर उसे एक दीवार में चलाने के लिए अधिक समय लेता है जितना कि एक छेद में चलने के लिए रैंडम बॉट करता है।
स्काइलर

1
@ zyabin101: आप जानते हैं, आप इसे पूरा चला सकते हैं, एक पूर्ण टूर्नामेंट खेलने के लिए 'y' को हिट कर सकते हैं, और राउंड के लिए 10 दर्ज कर सकते हैं।
स्काइलर

1

StalkerBot (पायथन)

निकटतम बॉट को जितना संभव हो उतना करीब से देखेगा। टार्गेट्स स्लो बॉट (व्यर्थ) स्वचालित आत्महत्या। (यदि मैं इसके समान वर्ग पर हूं और कोई अन्य खिलाड़ी नहीं हैं, तो यह मुझे नहीं देखेगा और आत्महत्या कर लेगा।)

#!/usr/bin/python3
from math import inf
from sys import argv

class Vector:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

    def __add__(self, other):
        return Vector(self.x + other.x, self.y + other.y)

    def __sub__(self, other):
        return Vector(self.x - other.x, self.y - other.y)

    def __neg__(self):
        return Vector(-self.x, -self.y)

    def __abs__(self):
        return self.x ** 2 + self.y ** 2  # Technically the square of the magnitude, but we only need it for comparison.

    def __iter__(self):
        yield self.x
        yield self.y

def get_location(grid, target='@'):
    for i, line in enumerate(grid):
        for j, char in enumerate(line):
            if char == target:
                return Vector(i, j)

def main(grid):
    my_location = get_location()

    min_distance = inf
    min_distance_direction = None

    for i in range(10):
        enemy_location = get_location(str(i))

        if enemy_location is not None:
            direction = enemy_location - my_location
            distance = abs(direction)

            if distance < current_min:
                min_distance = distance
                min_distance_direction = direction

            if distance == 1:
                break

    if min_distance_direction is not None:
        return min_distance_direction

    for d in range(1, 5):
        for x in range(-d, d):
            for y in (d - abs(x), abs(x) - d):
                if grid[x][y] == ' ':
                    return x, y

    return 0, 0

if __name__ == '__main__':
    print(*main(argv[1].splitlines()))

1
केवल FYI करें, सामान्य तौर पर हम ऐसे एडिट को मंजूरी नहीं देते हैं जो कोड को बदल देते हैं (जैसे कि आपने इस प्रश्न के दूसरे उत्तर पर किया था)। मुझे यह मंजूर था कि चूंकि ऐसा लग रहा था कि यह तर्क या किसी भी चीज को नहीं छू रहा था, बल्कि इसे साफ कर दिया था, लेकिन यह अधिकांश उत्तरों पर उड़ान भरने वाला नहीं था। हालांकि वह निश्चित रूप से इसका इस्तेमाल कर सकता है।
Rɪᴋᴇʀ

@ रिकर अंडरस्टूड। यह तर्क बदलने के लिए नहीं समझ में आता है, लेकिन मुझे उस कोड को पढ़ने में परेशानी हो रही थी, इसलिए मैंने फॉर्मेटिंग को साफ करने का फैसला किया।
सोलोमन उको

1
कोई समस्या नहीं है, लेकिन ध्यान रखें कि गोल्फिंग संपादन और इसी तरह आगे बढ़ने वाले अन्य प्रश्नों पर अस्वीकार किए जाने की संभावना है। मैं मानता हूं कि आपने जो कोड संपादित किया था, वह थोड़े विनस्की था।
आरɪᴋᴇʀ

1
@ रिकर मूल रूप से, कोई भी संपादन नहीं करते हैं जो स्कोर को प्रभावित कर सकते हैं।
सोलोमन उको
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.