|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- 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[1];Wall = Element[0]
- if (Oprate == "W"):
- if (Player[0]-1,Player[1]) not in Box and (Player[0]-1,Player[1]) not in Wall:
- Box[Box.index(Player)] = (Player[0]-1,Player[1])
- return True,Box
- else:
- return False,Box
- elif (Oprate == "S"):
- if (Player[0]+1,Player[1]) not in Box and (Player[0]+1,Player[1]) not in Wall:
- Box[Box.index(Player)] = (Player[0]+1,Player[1])
- return True,Box
- else:
- return False,Box
- elif (Oprate == "A"):
- if (Player[0],Player[1]-1) not in Box and (Player[0],Player[1]-1) not in Wall:
- Box[Box.index(Player)] = (Player[0],Player[1]-1)
- return True,Box
- else:
- return False,Box
- elif (Oprate == "D"):
- if (Player[0],Player[1]+1) not in Box and (Player[0],Player[1]+1) not in Wall:
- Box[Box.index(Player)] = (Player[0],Player[1]+1)
- return True,Box
- else:
- return False,Box
- def PlayerMove(Player,Oprate,Element):
- Wall = Element[0];Box = Element[1]
- if (Player in Wall):
- return False,Box
- elif (Player in Box):
- PushBoxResult = PushBox(Player,Oprate,Element)
- if PushBoxResult[0]:
- Box = PushBoxResult[1]
- 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[0]/Box is Element[1]/Endplace is Element[3]
- Player = Element[2]
- PrintMap(Map)
- Oprate = input("How do you want to move:").upper()
- while(True):
- Map[Player[0]][Player[1]] = " "
- if (Oprate == "W"):
- MoveAndBox = PlayerMove(Player=(Player[0]-1,Player[1]),Oprate=Oprate,Element=Element)
- if MoveAndBox[0]:
- Player[0] -= 1
- Element[1] = MoveAndBox[1]
- elif (Oprate == "S"):
- MoveAndBox = PlayerMove(Player=(Player[0]+1,Player[1]),Oprate=Oprate,Element=Element)
- if MoveAndBox[0]:
- Player[0] += 1
- Element[1] = MoveAndBox[1]
- elif (Oprate == "A"):
- MoveAndBox = PlayerMove(Player=(Player[0],Player[1]-1),Oprate=Oprate,Element=Element)
- if MoveAndBox[0]:
- Player[1] -= 1
- Element[1] = MoveAndBox[1]
- elif (Oprate == "D"):
- MoveAndBox = PlayerMove(Player=(Player[0],Player[1]+1),Oprate=Oprate,Element=Element)
- if MoveAndBox[0]:
- Player[1] += 1
- Element[1] = MoveAndBox[1]
- elif (Oprate == "B"):
- main()
- else:
- print("Error Input.")
- for i in Element[3]:
- Map[i[0]][i[1]] = "E"
- for i in Element[1]:
- Map[i[0]][i[1]] = "B"
- Map[Player[0]][Player[1]] = "P"
- os.system('cls')
- PrintMap(Map)
- if sorted(Element[1]) == sorted(Element[3]):
- 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()
复制代码 |
|