A13 发表于 2020-12-26 03:11:24

推箱子


from typing import List
import os

def SearchElement(Map):
    Row = 0;Column = 0
    Wall = [];Box = [];Player = [];Endplace = []
    for i in Map:
      for j in i:
            if (j.upper() == "W"):
                Wall.append((Row,Column))
            elif (j.upper() == "B"):
                Box.append((Row,Column))
            elif (j.upper() == "P"):
                Player.append(Row)
                Player.append(Column)
            elif (j.upper() == "E"):
                Endplace.append((Row,Column))
            Column += 1
      Row += 1;Column = 0
    return Wall,Box,Player,Endplace

def PrintMap(Map):
    for i in Map:
      for j in i:
            print(j,end='')
      print()

def PushBox(Player,Oprate,Element):
    Box = Element;Wall = Element
    if (Oprate == "W"):
      if (Player-1,Player) not in Box and (Player-1,Player) not in Wall:
            Box = (Player-1,Player)
            return True,Box
      else:
            return False,Box
    elif (Oprate == "S"):
      if (Player+1,Player) not in Box and (Player+1,Player) not in Wall:
            Box = (Player+1,Player)
            return True,Box
      else:
            return False,Box
    elif (Oprate == "A"):
      if (Player,Player-1) not in Box and (Player,Player-1) not in Wall:
            Box = (Player,Player-1)
            return True,Box
      else:
            return False,Box
    elif (Oprate == "D"):
      if (Player,Player+1) not in Box and (Player,Player+1) not in Wall:
            Box = (Player,Player+1)
            return True,Box
      else:
            return False,Box

def PlayerMove(Player,Oprate,Element):
    Wall = Element;Box = Element
    if (Player in Wall):
      return False,Box
    elif (Player in Box):
      PushBoxResult = PushBox(Player,Oprate,Element)
      if PushBoxResult:
            Box = PushBoxResult
            return True,Box
      else:
            return False,Box
    else:
      return True,Box

def main():
    # W is Wall/B is Box/P is Player/E is Endplace
    Map = [["W","W","W","W","W","W","W","W","W","W","W"],
         ["W"," "," "," "," ","W"," "," "," "," ","W"],
         ["W"," ","W"," "," ","B"," ","B"," "," ","W"],
         ["W","E"," "," "," ","W"," ","W"," "," ","W"],
         ["W","E"," "," "," "," "," ","W"," ","P","W"],
         ["W","W","W","W","W","W","W","W","W","W","W"]]
    Element = list(SearchElement(Map))#Wall is Element/Box is Element/Endplace is Element
    Player = Element
    PrintMap(Map)
    Oprate = input("How do you want to move:").upper()
    while(True):
      Map]] = " "
      if (Oprate == "W"):
            MoveAndBox = PlayerMove(Player=(Player-1,Player),Oprate=Oprate,Element=Element)
            if MoveAndBox:
                Player -= 1
                Element = MoveAndBox
      elif (Oprate == "S"):
            MoveAndBox = PlayerMove(Player=(Player+1,Player),Oprate=Oprate,Element=Element)
            if MoveAndBox:
                Player += 1
                Element = MoveAndBox
      elif (Oprate == "A"):
            MoveAndBox = PlayerMove(Player=(Player,Player-1),Oprate=Oprate,Element=Element)
            if MoveAndBox:
                Player -= 1
                Element = MoveAndBox
      elif (Oprate == "D"):
            MoveAndBox = PlayerMove(Player=(Player,Player+1),Oprate=Oprate,Element=Element)
            if MoveAndBox:
                Player += 1
                Element = MoveAndBox
      elif (Oprate == "B"):
            main()
      else:
            print("Error Input.")
      for i in Element:
            Map]] = "E"
      for i in Element:
            Map]] = "B"
      Map]] = "P"
      os.system('cls')
      PrintMap(Map)
      if sorted(Element) == sorted(Element):
            if (input("Do you want to play it again? Y/n").upper() == "Y"):
                main()
            else:
                break
      else:
            Oprate = input("How do you want to move:").upper()

if __name__ == "__main__":
    main()

小伤口 发表于 2020-12-26 17:31:25

能说明一下咋玩吗?
我有点没看懂{:10_250:}

GIAOchen 发表于 2020-12-27 19:58:14

你这个角色(P)退箱子(B)的时候会卡住,麻烦楼主改一下!!!
页: [1]
查看完整版本: 推箱子