鱼C论坛

 找回密码
 立即注册
查看: 1519|回复: 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 | 显示全部楼层
百度试试
  1. import pygame
  2. from pygame.locals import *

  3. import Mazing

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


  8.     background = pygame.Surface(screen.get_size())
  9.     background = background.convert()
  10.     background.fill((250, 250, 250))
  11.    

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

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

  17.     while 1:
  18.         for event in pygame.event.get():
  19.             if event.type == QUIT:
  20.                 return
  21.                
  22.             screen.blit(background, (0, 0))
  23.             pygame.display.flip()


  24. if __name__ == '__main__':
  25.     main()


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


  27. import MazingUnit
  28. import MazingWall
  29. import MazingGenerator


  30. class Room:
  31.       
  32.     def __init__(self, surface, roomWidthByUnit, roomHighByUnit, unitWidth, unitHigh, startPos):
  33.         self.__surface = surface
  34.         self.__unitWidth = unitWidth
  35.         self.__unitHigh = unitHigh
  36.         self.__roomWidthByUnit = roomWidthByUnit
  37.         self.__roomHighByUnit = roomHighByUnit
  38.         self.__startPos = startPos      
  39.       
  40.         self._unitMap = {}
  41.         self._wallMap_V = {}
  42.         self._wallMap_H = {}
  43.       
  44.         for x in range(0, roomWidthByUnit):
  45.             for y in range(0, roomHighByUnit):
  46.                 (startX, startY) = startPos
  47.                 self._unitMap[(x,y)] = MazingUnit.Unit(
  48.                     surface,
  49.                     unitWidth,
  50.                     unitHigh,
  51.                     (startX + x * unitWidth, startY + y * unitHigh),
  52.                     (x,y))
  53.                
  54.         for x in range(0, roomWidthByUnit):
  55.             for y in range(0, roomHighByUnit):              
  56.                 if x > 0:
  57.                     self._unitMap[(x-1,y)]._unit_2 = \
  58.                         self._unitMap[(x,y)]
  59.                 if y > 0:
  60.                     self._unitMap[(x,y-1)]._unit_3 = \
  61.                         self._unitMap[(x,y)]
  62.                 if x != roomWidthByUnit - 1:
  63.                     self._unitMap[(x+1,y)]._unit_0 = \
  64.                         self._unitMap[(x,y)]
  65.                 if y != roomHighByUnit - 1:
  66.                     self._unitMap[(x,y+1)]._unit_1 = \
  67.                         self._unitMap[(x,y)]
  68.                
  69.         for x in range(0, roomWidthByUnit + 1):
  70.             for y in range(0, roomHighByUnit + 1):
  71.                 (startX, startY) = startPos
  72.                 self._wallMap_H[(x,y)] = MazingWall.Wall(surface, (startX + x * unitWidth, startY + y * unitHigh), (startX + (x+1) * unitWidth, startY + y * unitHigh), (x,y))               
  73.                 self._wallMap_V[(x,y)] = MazingWall.Wall(surface, (startX + x * unitWidth, startY + y * unitHigh), (startX + x * unitWidth, startY + (y+1) * unitHigh), (x,y))
  74.                 if x != roomWidthByUnit and y != roomHighByUnit:
  75.                     self._unitMap[(x,y)]._wall_1 = self._wallMap_H[(x,y)]
  76.                     self._unitMap[(x,y)]._wall_0 = self._wallMap_V[(x,y)]
  77.                     self._wallMap_V[(x,y)]._isBlock = True
  78.                     self._wallMap_H[(x,y)]._isBlock = True

  79.                 if x != roomWidthByUnit and y > 0:
  80.                     self._unitMap[(x,y-1)]._wall_3 = self._wallMap_H[(x,y)]
  81.                     self._wallMap_H[(x,y)]._isBlock = True            
  82.                 if x > 0 and y != roomHighByUnit:
  83.                     self._unitMap[(x-1,y)]._wall_2 = self._wallMap_V[(x,y)]                     
  84.                     self._wallMap_V[(x,y)]._isBlock = True
  85.                   
  86.     def getRoomWidthByUnit(self):
  87.         return self.__roomWidthByUnit
  88.     def getRoomHighByUnit(self):
  89.         return self.__roomHighByUnit
  90.                
  91.     def draw(self):
  92.         for x in range(0, self.__roomWidthByUnit + 1):
  93.             for y in range(0, self.__roomHighByUnit + 1):
  94.                 self._wallMap_H[(x,y)].draw()
  95.                 self._wallMap_V[(x,y)].draw()
  96.                
  97.                
  98.     def generate(self):
  99.         gen = MazingGenerator.Generator()
  100.         gen.randomWallGen(self)
  101.         gen.cutWind(self)
  102.                   
  103.                   
  104. #=======MazingGenerator.py============
  105. import random
  106. import time

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

  111.     def randomWallGen(self, room):
  112.         threshold = 5
  113.         random.seed(time.gmtime())
  114.         for x in range(0, room.getRoomWidthByUnit()):
  115.             for y in range(1, room.getRoomHighByUnit()):
  116.                 i = random.randint(1, 10)
  117.                 if i > threshold:
  118.                     room._wallMap_H[(x,y)]._isBlock = False
  119.         return True
  120.       
  121.     def cutWind(self, room):
  122.         while(1):
  123.             self.__unitMapCopy = room._unitMap.copy()
  124.             self.__windList = []
  125.             while(0 != len(self.__unitMapCopy)):
  126.                 wind = []
  127.                 self.__findAndCutOneWind(wind)
  128.                 self.__windList.append(wind)
  129.             for wind in self.__windList:
  130.                 self.__linkWind(wind)
  131.             room._unitMap.clear()
  132.             for wind in self.__windList:
  133.                 for unit in wind:
  134.                     room._unitMap[unit.getPosByUnit()] = unit
  135.             print("wind list len = %d" % len(self.__windList))
  136.             if 1 == len(self.__windList):
  137.                 break
  138.                
  139.            
  140.     def __findAndCutOneWind(self, wind):
  141.         print ("Enter __findAndCutOneWind")
  142.         keys = self.__unitMapCopy.keys()
  143.         unit = self.__unitMapCopy.pop(keys[0])      
  144.         wind.append(unit)
  145.         print(unit.getPosByUnit())         
  146.         i = -1
  147.         while(i < len(wind) - 1):
  148.             i = i + 1
  149.             unit = wind[i]
  150.             if None != unit.move_0() and unit.move_0() not in wind:
  151.                 self.__unitMapCopy.pop(unit.move_0().getPosByUnit())
  152.                 wind.append(unit.move_0())
  153.                 print(unit.getPosByUnit())
  154.             if None != unit.move_1() and unit.move_1() not in wind:
  155.                 self.__unitMapCopy.pop(unit.move_1().getPosByUnit())
  156.                 wind.append(unit.move_1())
  157.                 print(unit.getPosByUnit())
  158.             if None != unit.move_2() and unit.move_2() not in wind:
  159.                 self.__unitMapCopy.pop(unit.move_2().getPosByUnit())
  160.                 wind.append(unit.move_2())
  161.                 print(unit.getPosByUnit())
  162.             if None != unit.move_3() and unit.move_3() not in wind :
  163.                 self.__unitMapCopy.pop(unit.move_3().getPosByUnit())
  164.                 wind.append(unit.move_3())      
  165.                 print(unit.getPosByUnit())
  166.                
  167.     def __linkWind(self, wind):
  168.         cutNum = 0
  169.         for unit in wind:
  170.             if cutNum >= 1:
  171.                 break
  172.             i = random.randint(0,4)
  173.             if 0 == i:
  174.                 if None == unit.move_0() and None != unit._unit_0 and unit._unit_0 not in wind:
  175.                     unit._wall_0._isBlock = False
  176.                     cutNum = cutNum + 1
  177.                     continue
  178.                 if None == unit.move_1() and None != unit._unit_1 and unit._unit_1 not in wind:
  179.                     unit._wall_1._isBlock = False
  180.                     cutNum = cutNum + 1
  181.                     continue
  182.                 if None == unit.move_2() and None != unit._unit_2 and unit._unit_2 not in wind:
  183.                     unit._wall_2._isBlock = False
  184.                     cutNum = cutNum + 1
  185.                     continue
  186.                 if None == unit.move_3() and None != unit._unit_3 and unit._unit_3 not in wind:
  187.                     unit._wall_3._isBlock = False
  188.                     cutNum = cutNum + 1
  189.                     continue

  190.             if 1 == i:
  191.                 if None == unit.move_3() and None != unit._unit_3 and unit._unit_3 not in wind:
  192.                     unit._wall_3._isBlock = False
  193.                     cutNum = cutNum + 1
  194.                     continue
  195.                 if None == unit.move_0() and None != unit._unit_0 and unit._unit_0 not in wind:
  196.                     unit._wall_0._isBlock = False
  197.                     cutNum = cutNum + 1
  198.                     continue
  199.                 if None == unit.move_1() and None != unit._unit_1 and unit._unit_1 not in wind:
  200.                     unit._wall_1._isBlock = False
  201.                     cutNum = cutNum + 1
  202.                     continue
  203.                 if None == unit.move_2() and None != unit._unit_2 and unit._unit_2 not in wind:
  204.                     unit._wall_2._isBlock = False
  205.                     cutNum = cutNum + 1
  206.                     continue

  207.             if 2 == i:
  208.                 if None == unit.move_2() and None != unit._unit_2 and unit._unit_2 not in wind:
  209.                     unit._wall_2._isBlock = False
  210.                     cutNum = cutNum + 1
  211.                     continue
  212.                 if None == unit.move_3() and None != unit._unit_3 and unit._unit_3 not in wind:
  213.                     unit._wall_3._isBlock = False
  214.                     cutNum = cutNum + 1
  215.                     continue
  216.                 if None == unit.move_0() and None != unit._unit_0 and unit._unit_0 not in wind:
  217.                     unit._wall_0._isBlock = False
  218.                     cutNum = cutNum + 1
  219.                     continue
  220.                 if None == unit.move_1() and None != unit._unit_1 and unit._unit_1 not in wind:
  221.                     unit._wall_1._isBlock = False
  222.                     cutNum = cutNum + 1
  223.                     continue

  224.             if 3 == i:
  225.                 if None == unit.move_1() and None != unit._unit_1 and unit._unit_1 not in wind:
  226.                     unit._wall_1._isBlock = False
  227.                     cutNum = cutNum + 1
  228.                     continue
  229.                 if None == unit.move_2() and None != unit._unit_2 and unit._unit_2 not in wind:
  230.                     unit._wall_2._isBlock = False
  231.                     cutNum = cutNum + 1
  232.                     continue
  233.                 if None == unit.move_3() and None != unit._unit_3 and unit._unit_3 not in wind:
  234.                     unit._wall_3._isBlock = False
  235.                     cutNum = cutNum + 1
  236.                     continue
  237.                 if None == unit.move_0() and None != unit._unit_0 and unit._unit_0 not in wind:
  238.                     unit._wall_0._isBlock = False
  239.                     cutNum = cutNum + 1
  240.                     continue


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



  242. class Unit:
  243.     def __init__(self, surface, width, high, posStart, posByUnit):
  244.         self.__surface = surface
  245.         self.__width = width
  246.         self.__high = high
  247.         self.__posStart = posStart
  248.         self.__posByUnit = posByUnit
  249.       
  250.         self._wall_0 = None
  251.         self._wall_1 = None
  252.         self._wall_2 = None
  253.         self._wall_3 = None

  254.         self._unit_0 = None
  255.         self._unit_1 = None
  256.         self._unit_2 = None
  257.         self._unit_3 = None
  258.       
  259.     def getPosByUnit(self):
  260.         return self.__posByUnit
  261.       
  262.     def getPosStart(self):
  263.         return self.__posStart
  264.       
  265.     def move_0(self):
  266.         if False == self._wall_0._isBlock:
  267.             return self._unit_0
  268.         else:
  269.             return None
  270.            
  271.     def move_1(self):
  272.         if False == self._wall_1._isBlock:
  273.             return self._unit_1
  274.         else:
  275.             return None
  276.            
  277.     def move_2(self):
  278.         if False == self._wall_2._isBlock:
  279.             return self._unit_2
  280.         else:
  281.             return None
  282.            
  283.     def move_3(self):
  284.         if False == self._wall_3._isBlock:
  285.             return self._unit_3
  286.         else:
  287.             return None
  288.                                              

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

  290. import pygame
  291. from pygame.locals import *

  292. class Wall:
  293.     def __init__(self, surface, posStartByUnit, posEndByUnit, pos):
  294.         self.__surface = surface
  295.         self.__posStartByUnit = posStartByUnit
  296.         self.__posEndByUnit = posEndByUnit
  297.         self.__pos = pos
  298.         self._isBlock = False
  299.       
  300.     def draw(self):
  301.         if  True == self._isBlock:
  302.             pygame.draw.line(self.__surface, (10, 10, 10), self.__posStartByUnit, self.__posEndByUnit, 1)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-19 17:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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