Ejemplo n.º 1
0
def test_generate_graph():
    import field
    f = field.Field((15, 7), walkable='-@0123456789abcdefghijklmnopqrstuvwxyz')
    f.field = [
        '#' * 15, '#             #', '###  ###### ###', '### ####### ###',
        '### ####### ###', '###           #', '#' * 15
    ]
    g1 = generate_graph(f, (1, 1), (8, 1), debug=True)
    g1_expected = Graph()
    g1.verts = {
        0: (1, 1),
        1: (8, 1),
        2: (3, 1),
        3: (3, 2),
        4: (4, 1),
        5: (11, 5),
        6: (11, 1)
    }
    g1.edges = {
        0: Graph.Edge(v1=0, v2=2, dir1='r', dir2='l'),
        1: Graph.Edge(v1=2, v2=3, dir1='d', dir2='u'),
        2: Graph.Edge(v1=2, v2=4, dir1='r', dir2='l'),
        3: Graph.Edge(v1=3, v2=4, dir1='r', dir2='d'),
        4: Graph.Edge(v1=1, v2=4, dir1='l', dir2='r'),
        5: Graph.Edge(v1=3, v2=5, dir1='d', dir2='l'),
        6: Graph.Edge(v1=5, v2=6, dir1='u', dir2='d'),
        7: Graph.Edge(v1=1, v2=6, dir1='r', dir2='l')
    }
    #print(g1.verts == g1_expected.verts and g1.edges == g1_expected.edges)
    #print('press enter'); input()
    f.field = [
        '#' * 15, '#   #         #', '# # # ### ### #', '#   #       # #',
        '### ### ### # #', '###           #', '#' * 15
    ]
    g2 = generate_graph(f, (1, 1), (8, 1), debug=True)
    #print('press enter'); input()
    f.field = [
        '#' * 15, '#   #         #', '# # #   # ### #', '#   #   #   # #',
        '### ### ### # #', '###           #', '#' * 15
    ]
    g3 = generate_graph(f, (1, 1), (8, 1), debug=True)
    #print('press enter'); input()

    try:
        import labyrinth
    except Exception as e:
        print(e)
        return
    f = labyrinth.Labyrinth()
    #f.walkable += '0123456789abcdefghijklmnopqrstuvwxyz@-'
    f.walkable += '@-'
    t = time.time()
    g4 = generate_graph(f, (1, 1),
                        f.exit_coords,
                        ignored=[(f.exit_coords[0] - 2, f.exit_coords[1] - 1)],
                        debug=False)
    tt = time.time()
Ejemplo n.º 2
0
 def check_for_action(self):
     if self.play_button.isMouseOn():
         if not self.playermenu:
             labyrinth.Labyrinth('king')
         else:
             labyrinth.Labyrinth(self.pmo.player)
         self.create_display()
     elif self.character_button.isMouseOn():
         self.pmo = pm.PlayerMenu()
         self.playermenu = True
     elif self.music_button.isMouseOn():
         if self.musicOn:
             self.music = 'Music: OFF'
             self.musicOn = False
             pygame.mixer.music.stop()
         else:
             self.music = 'Music: ON'
             self.musicOn = True
             pygame.mixer.music.load('lab_game/background_song.mp3')
             pygame.mixer.music.set_volume(0.5)
             pygame.mixer.music.play(-1)
         self.create_buttons()
     elif self.exit_button.isMouseOn():
         self.run = False
Ejemplo n.º 3
0
def parse_lab(data):
    l = labyrinth.Labyrinth()
    d = data.strip().split(b'\n')
    print(data.strip().decode())
    print(len(d))
    for i in range(len(d)):
        d[i] = d[i][::2].decode().replace(':','@').replace('<','!').replace('{','+').replace('O','k')
        print(len(d[i]))
        l.field = d
        l.conf = labyrinth.Config(2|4)

    l.doors = []
    l.keys = []
    for i in range(len(d)-1):
        for j in range(len(d[i])-1):
            if d[i][j] == '+':
                l.doors.append( (j,i) )
            if d[i][j] == 'k':
                l.keys.append( (j,i) )
            if d[i][j] == '!':
                l.exit_coords = (j,i)
    print(l.keys, l.doors)
    return solve_labyrinth(l)
Ejemplo n.º 4
0
 def showLabyrinth(self):
     self.clear()
     l = labyrinth.Labyrinth(self)
     l.pack()
Ejemplo n.º 5
0
def handler(conn, addr):
    timer = time.time()

    for nc in range(NEEDED_CORRECT):
        solution = ''
        while len(solution) == 0:
            l = labyrinth.Labyrinth(labyrinth.Config(rooms=True, keys=True))
            try:
                solution = solver.solve_labyrinth(l)
            except:
                pass
        print(l.draw_to_str())
        print('keys:', l.keys, 'doors:', l.doors)

        correct_len = False
        finished = False
        turns = ''
        time_out = False

        while not time_out and not finished:
            question = l.draw_to_str()
            conn.send(question.encode())
            try:
                s = conn.recv(4096)  # getting user input
            except:
                conn.close()
                return

            try:
                ans = s.decode().strip()  # handle user input
            except ValueError:
                try:
                    conn.send(
                        "Don't even try to trick me\nGo away!\n".encode())
                except:
                    pass
                sys.stderr.write(
                    "Value error occured! Input were: {}\nClient: {}\n".format(
                        s, addr))
                conn.close()
                return

            turns += ans
            for i in ans:
                try:
                    l.turn(i)
                except ValueError:
                    try:
                        conn.send(
                            "Don't even try to trick me\nGo away!\n".encode())
                    except:
                        pass
                    sys.stderr.write(
                        "Value error occured! Input were: {}\nClient: {}\n".
                        format(s, addr))
                    conn.close()
                    return
                except GameError as e:
                    try:
                        conn.send("{}\n".format(e).encode())
                    except:
                        pass
                    sys.stderr.write(
                        "Game error occured: {}. Input were: {}\nClient: {}\n".
                        format(e, s, addr))
                    conn.close()
                    return
                except BaseException as e:
                    sys.stderr.write('{}; {}\n'.format(type(e), e))
                    sys.stderr.write(
                        "Unknown error.\nClient: {}\nData: {}\n".format(
                            addr, s))
                    conn.close()
                    return

            if l.reached_exit():
                finished = True
                correct_len = (len(turns) <= len(solution))
            time_out = (time.time() - timer >= MAX_TIME)
            #print(correct_len, time_out)

        if not correct_len:
            conn.send(
                "Too many turns for this map. More optimal way exists!\n".
                encode())
            conn.close()
            return
        if time_out:
            conn.send("Time is up, try again\n".encode())
            conn.close()
            return

    sys.stdout.write("{} successfully solved task\nTime: {}\n".format(
        addr, round(time.time() - timer, 4)))
    conn.send("Gratz! Your flag is: {}\n".format(FLAG).encode())

    conn.close()
    return