鱼C论坛

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

[原创] 推箱子

[复制链接]
发表于 2020-12-26 03:11:24 | 显示全部楼层 |阅读模式

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

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

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()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-12-26 17:31:25 | 显示全部楼层
能说明一下咋玩吗?
我有点没看懂
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-27 19:58:14 | 显示全部楼层
你这个角色(P)退箱子(B)的时候会卡住,麻烦楼主改一下!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-12-22 11:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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