鱼C论坛

 找回密码
 立即注册
查看: 1524|回复: 2

[已解决]小白求助 如何用python写一个迷宫的问题

[复制链接]
发表于 2017-4-14 23:17:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
读取一个txt文档 将里面的数字转化为一个迷宫  且把从入口到出口经过的路线也写出来  (数字转为图片里的路线要另外弄一个函数?)


def create_labyrinth(f):

         a=open(f,'r')
         list1=a.read()
         tab1=list1.split()
         print(tab1)        
         print("迷宫尺寸是",tab1[0],'*',tab1[1])
         print("迷宫入口是(",tab1[2],',',tab1[3],')')
         print("迷宫出口是 (",tab1[4],',',tab1[5],')')
         
f=create_labyrinth("labyrinthe_10x10.txt")
print(f)

最佳答案
2017-4-15 07:38:58
http://bbs.fishc.com/forum.php?mod=viewthread&tid=80943&mobile=2
953BEF4A-2773-4C05-8E42-95B5F32E1F55.png

labyrinthe_10x10.txt

339 Bytes, 下载次数: 5

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-15 07:38:58 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
http://bbs.fishc.com/forum.php?mod=viewthread&tid=80943&mobile=2
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-15 09:40:41 | 显示全部楼层
百度试试
import pygame
from pygame.locals import *

import Mazing

def main():
    pygame.init()
    screen = pygame.display.set_mode((600, 600))
    pygame.display.set_caption('Basic Pygame program')


    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((250, 250, 250))
   

    screen.blit(background, (0, 0))
    pygame.display.flip()

    room = Mazing.Room(background, 30, 30, 10, 10, (10, 10))
    room.generate()
    room.draw()

    while 1:
        for event in pygame.event.get():
            if event.type == QUIT:
                return
               
            screen.blit(background, (0, 0))
            pygame.display.flip()


if __name__ == '__main__':
    main()


#=======Mazing.py========


import MazingUnit
import MazingWall
import MazingGenerator


class Room:
       
    def __init__(self, surface, roomWidthByUnit, roomHighByUnit, unitWidth, unitHigh, startPos):
        self.__surface = surface
        self.__unitWidth = unitWidth
        self.__unitHigh = unitHigh
        self.__roomWidthByUnit = roomWidthByUnit
        self.__roomHighByUnit = roomHighByUnit
        self.__startPos = startPos       
       
        self._unitMap = {}
        self._wallMap_V = {}
        self._wallMap_H = {}
       
        for x in range(0, roomWidthByUnit):
            for y in range(0, roomHighByUnit):
                (startX, startY) = startPos
                self._unitMap[(x,y)] = MazingUnit.Unit(
                    surface,
                    unitWidth,
                    unitHigh,
                    (startX + x * unitWidth, startY + y * unitHigh), 
                    (x,y))
               
        for x in range(0, roomWidthByUnit):
            for y in range(0, roomHighByUnit):              
                if x > 0:
                    self._unitMap[(x-1,y)]._unit_2 = \
                        self._unitMap[(x,y)]
                if y > 0:
                    self._unitMap[(x,y-1)]._unit_3 = \
                        self._unitMap[(x,y)]
                if x != roomWidthByUnit - 1:
                    self._unitMap[(x+1,y)]._unit_0 = \
                        self._unitMap[(x,y)]
                if y != roomHighByUnit - 1:
                    self._unitMap[(x,y+1)]._unit_1 = \
                        self._unitMap[(x,y)]
                
        for x in range(0, roomWidthByUnit + 1):
            for y in range(0, roomHighByUnit + 1):
                (startX, startY) = startPos
                self._wallMap_H[(x,y)] = MazingWall.Wall(surface, (startX + x * unitWidth, startY + y * unitHigh), (startX + (x+1) * unitWidth, startY + y * unitHigh), (x,y))               
                self._wallMap_V[(x,y)] = MazingWall.Wall(surface, (startX + x * unitWidth, startY + y * unitHigh), (startX + x * unitWidth, startY + (y+1) * unitHigh), (x,y))
                if x != roomWidthByUnit and y != roomHighByUnit:
                    self._unitMap[(x,y)]._wall_1 = self._wallMap_H[(x,y)]
                    self._unitMap[(x,y)]._wall_0 = self._wallMap_V[(x,y)]
                    self._wallMap_V[(x,y)]._isBlock = True
                    self._wallMap_H[(x,y)]._isBlock = True

                if x != roomWidthByUnit and y > 0:
                    self._unitMap[(x,y-1)]._wall_3 = self._wallMap_H[(x,y)] 
                    self._wallMap_H[(x,y)]._isBlock = True             
                if x > 0 and y != roomHighByUnit:
                    self._unitMap[(x-1,y)]._wall_2 = self._wallMap_V[(x,y)]                      
                    self._wallMap_V[(x,y)]._isBlock = True
                   
    def getRoomWidthByUnit(self):
        return self.__roomWidthByUnit
    def getRoomHighByUnit(self):
        return self.__roomHighByUnit
               
    def draw(self):
        for x in range(0, self.__roomWidthByUnit + 1):
            for y in range(0, self.__roomHighByUnit + 1):
                self._wallMap_H[(x,y)].draw()
                self._wallMap_V[(x,y)].draw()
               
               
    def generate(self):
        gen = MazingGenerator.Generator()
        gen.randomWallGen(self)
        gen.cutWind(self)
                   
                   
#=======MazingGenerator.py============
import random
import time

class Generator:
    def __init__(self):
        self.__windList = []
        self.__unitMapCopy = {}

    def randomWallGen(self, room):
        threshold = 5
        random.seed(time.gmtime())
        for x in range(0, room.getRoomWidthByUnit()):
            for y in range(1, room.getRoomHighByUnit()):
                i = random.randint(1, 10)
                if i > threshold:
                    room._wallMap_H[(x,y)]._isBlock = False 
        return True
       
    def cutWind(self, room): 
        while(1):
            self.__unitMapCopy = room._unitMap.copy()
            self.__windList = []
            while(0 != len(self.__unitMapCopy)):
                wind = []
                self.__findAndCutOneWind(wind)
                self.__windList.append(wind)
            for wind in self.__windList:
                self.__linkWind(wind)
            room._unitMap.clear()
            for wind in self.__windList:
                for unit in wind:
                    room._unitMap[unit.getPosByUnit()] = unit
            print("wind list len = %d" % len(self.__windList))
            if 1 == len(self.__windList):
                break
               
           
    def __findAndCutOneWind(self, wind):
        print ("Enter __findAndCutOneWind")
        keys = self.__unitMapCopy.keys()
        unit = self.__unitMapCopy.pop(keys[0])       
        wind.append(unit)
        print(unit.getPosByUnit())          
        i = -1 
        while(i < len(wind) - 1):
            i = i + 1
            unit = wind[i]
            if None != unit.move_0() and unit.move_0() not in wind:
                self.__unitMapCopy.pop(unit.move_0().getPosByUnit())
                wind.append(unit.move_0())
                print(unit.getPosByUnit())
            if None != unit.move_1() and unit.move_1() not in wind:
                self.__unitMapCopy.pop(unit.move_1().getPosByUnit())
                wind.append(unit.move_1())
                print(unit.getPosByUnit())
            if None != unit.move_2() and unit.move_2() not in wind:
                self.__unitMapCopy.pop(unit.move_2().getPosByUnit())
                wind.append(unit.move_2())
                print(unit.getPosByUnit())
            if None != unit.move_3() and unit.move_3() not in wind :
                self.__unitMapCopy.pop(unit.move_3().getPosByUnit())
                wind.append(unit.move_3())      
                print(unit.getPosByUnit())
               
    def __linkWind(self, wind):
        cutNum = 0
        for unit in wind:
            if cutNum >= 1:
                break
            i = random.randint(0,4)
            if 0 == i:
                if None == unit.move_0() and None != unit._unit_0 and unit._unit_0 not in wind:
                    unit._wall_0._isBlock = False
                    cutNum = cutNum + 1
                    continue
                if None == unit.move_1() and None != unit._unit_1 and unit._unit_1 not in wind:
                    unit._wall_1._isBlock = False
                    cutNum = cutNum + 1
                    continue
                if None == unit.move_2() and None != unit._unit_2 and unit._unit_2 not in wind:
                    unit._wall_2._isBlock = False
                    cutNum = cutNum + 1
                    continue
                if None == unit.move_3() and None != unit._unit_3 and unit._unit_3 not in wind:
                    unit._wall_3._isBlock = False
                    cutNum = cutNum + 1
                    continue

            if 1 == i:
                if None == unit.move_3() and None != unit._unit_3 and unit._unit_3 not in wind:
                    unit._wall_3._isBlock = False
                    cutNum = cutNum + 1
                    continue
                if None == unit.move_0() and None != unit._unit_0 and unit._unit_0 not in wind:
                    unit._wall_0._isBlock = False
                    cutNum = cutNum + 1
                    continue
                if None == unit.move_1() and None != unit._unit_1 and unit._unit_1 not in wind:
                    unit._wall_1._isBlock = False
                    cutNum = cutNum + 1
                    continue
                if None == unit.move_2() and None != unit._unit_2 and unit._unit_2 not in wind:
                    unit._wall_2._isBlock = False
                    cutNum = cutNum + 1
                    continue

            if 2 == i:
                if None == unit.move_2() and None != unit._unit_2 and unit._unit_2 not in wind:
                    unit._wall_2._isBlock = False
                    cutNum = cutNum + 1
                    continue
                if None == unit.move_3() and None != unit._unit_3 and unit._unit_3 not in wind:
                    unit._wall_3._isBlock = False
                    cutNum = cutNum + 1
                    continue
                if None == unit.move_0() and None != unit._unit_0 and unit._unit_0 not in wind:
                    unit._wall_0._isBlock = False
                    cutNum = cutNum + 1
                    continue
                if None == unit.move_1() and None != unit._unit_1 and unit._unit_1 not in wind:
                    unit._wall_1._isBlock = False
                    cutNum = cutNum + 1
                    continue

            if 3 == i:
                if None == unit.move_1() and None != unit._unit_1 and unit._unit_1 not in wind:
                    unit._wall_1._isBlock = False
                    cutNum = cutNum + 1
                    continue
                if None == unit.move_2() and None != unit._unit_2 and unit._unit_2 not in wind:
                    unit._wall_2._isBlock = False
                    cutNum = cutNum + 1
                    continue
                if None == unit.move_3() and None != unit._unit_3 and unit._unit_3 not in wind:
                    unit._wall_3._isBlock = False
                    cutNum = cutNum + 1
                    continue
                if None == unit.move_0() and None != unit._unit_0 and unit._unit_0 not in wind:
                    unit._wall_0._isBlock = False
                    cutNum = cutNum + 1
                    continue


#=========MazingUnit.py=========



class Unit:
    def __init__(self, surface, width, high, posStart, posByUnit):
        self.__surface = surface
        self.__width = width
        self.__high = high
        self.__posStart = posStart
        self.__posByUnit = posByUnit
       
        self._wall_0 = None
        self._wall_1 = None
        self._wall_2 = None
        self._wall_3 = None

        self._unit_0 = None
        self._unit_1 = None
        self._unit_2 = None
        self._unit_3 = None
       
    def getPosByUnit(self):
        return self.__posByUnit
       
    def getPosStart(self):
        return self.__posStart
       
    def move_0(self):
        if False == self._wall_0._isBlock:
            return self._unit_0
        else:
            return None
           
    def move_1(self):
        if False == self._wall_1._isBlock:
            return self._unit_1
        else:
            return None
           
    def move_2(self):
        if False == self._wall_2._isBlock:
            return self._unit_2
        else:
            return None
           
    def move_3(self):
        if False == self._wall_3._isBlock:
            return self._unit_3
        else:
            return None
                                              

#=======MazingWall.py===============

import pygame
from pygame.locals import *

class Wall:
    def __init__(self, surface, posStartByUnit, posEndByUnit, pos):
        self.__surface = surface
        self.__posStartByUnit = posStartByUnit
        self.__posEndByUnit = posEndByUnit
        self.__pos = pos
        self._isBlock = False
       
    def draw(self):
        if  True == self._isBlock:
            pygame.draw.line(self.__surface, (10, 10, 10), self.__posStartByUnit, self.__posEndByUnit, 1) 
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-11 01:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表