集智社区
资源加载中,请稍后...
集智社区

集智社区

Sic Parvis Magna

[2018.03.01]直播Python课程有奖作业

发表至编程讨论

直播地址:负基础学Python - 景略集智的Bilibili Live

经典的《街头霸王》有12个人物可以选择,在没有鼠标和触摸板的年代,选择靠的是方向键,这里我们就用Python来模拟一下选人的过程。

这12个待选人物以2x6矩阵的形式排列:

fighters = [["Ryu", "E.Honda", "Blanka", "Guile", "Balrog", "Vega"],
        ["Ken", "Chun Li", "Zangief", "Dhalsim", "Sagat", "M.Bison"]]

可以施加的行动就是“上下左右”四种:

moves = ["up","down","right","left"]

最左上角的Ryu(耗油跟)的位置是0,0, 右下角M.Bison的位置是2,5。值得注意的是边界规则:

  • 对于左右方向的移动,列表是“首尾相接”的。比如指针在Vega的时候再执行right,就会跳回Ryu
  • 对于上下方向的移动,列表是“两端封闭”的。比如指针在Ryu的时候再执行up,光标的位置不变。

请完成函数street_fighter_selection()的定义,输入参数有之前的fighters, initial_position,以及由"up","down","right","left"四种元素组成的移动序列。

要求输出的是,在moves的作用下,光标都扫过了哪些英雄,按顺序组成一个列表。

程序说明
街头霸王
示例代码
fighters = [["Ryu", "E.Honda", "Blanka", "Guile", "Balrog", "Vega"], ["Ken", "Chun Li", "Zangief", "Dhalsim", "Sagat", "M.Bison"]] def street_fighter_selection(fighters, initial_position, moves): # >>>> show me the code <<<< # >>>> show me the code <<<<
正确答案
def street_fighter_selection(fighters, initial_position, moves): vert = initial_position[0] hori = initial_position[1] characters = [] for i in moves: if i == "right": hori += 1 elif i == "left": hori -= 1 if i == "up" and vert == 1: vert = 0 if i == "down" and vert == 0: vert = 1 characters.append(fighters[vert][hori%6]) return characters
程序验证过程
moves = ["up"]+["left"]*6+["down"]+["right"]*6 solution = ['Ryu', 'Vega', 'Balrog', 'Guile', 'Blanka', 'E.Honda', 'Ryu', 'Ken', 'Chun Li', 'Zangief', 'Dhalsim', 'Sagat', 'M.Bison', 'Ken'] street_fighter_selection(fighters, (0,0), moves) == solution
本话题更新于
回复
1
#1
y = initial_position[0]
x = initial_position[1]
fighter = []
for move in moves:
    if move == 'up':
        if y != 0:
            y -= 1
    elif move == 'down':
        if y != 1:
            y += 1
    elif move == 'right':
        x += 1
    elif move == 'left':
        x -= 1
    fighter.append( fighters[y][x%6])
return fighter
回复
0
#2

fighters = [["Ryu", "E.Honda", "Blanka", "Guile", "Balrog", "Vega"], ["Ken", "Chun Li", "Zangief", "Dhalsim", "Sagat", "M.Bison"]]

moves = ["up","down","right","left"]

position=[0,0]

def street_fighter_selection(fighters, position, moves): GaoHuanDeYeWang=[] for i in moves: if i=="right": position[1]+=1 elif i=="left": position[1]-=1 elif i=="up" and position[0]==1: position[0]=0 elif i=="down" and position[0]==0:

回复
0
#3

GaoHuanDeYeWang=[] for i in moves: if i=="right": initial_position[1]+=1 elif i=="left": initial_position[1]-=1 elif i=="up" and initial_position[0]==1: initial_position[0]=0 elif i=="down" and initial_position[0]==0: initial_position[0]=1

    GaoHuanDeYeWang.append(fighters[initial_position[0]][initial_position[1]%6])

return GaoHuanDeYeWang
回复
0
#4
result=[]
x = initial_position[0]
y = initial_position[1]
for i in moves:
    if i=="right":
        y+=1
    elif i=="left":
        y-=1
    elif i=="up" and x==1:
        x=0
    elif i=="down" and x==0:
        x=1


    result.append(fighters[x][y%6])

return result
回复
0
#5
KYOOO9月前

def street_fighter_selection(fighters, initial_position, moves):

# >>>> show me the code <<<<
x = initial_position[0]
y = initial_position[1]
res = []

for i in moves:
    if i == 'left':
        x -= 1
    elif i == 'right':
        x += 1
    elif i == 'up' and y > 0:
        y -= 1
    elif i == 'down' and y < (len(fighters)-1):
        y += 1

    res.append(fighters[y][x%6])

return res

# >>>> show me the code <<<<
回复
1
#6
叶藏9月前

>>>> show u the code <<<<

selected_fighters = []
x = initial_position[0]
y = initial_position[1]

for i in moves:
    if i == "up":
        if y !=0:
            y -= 1
    if i == "down":
        if y != 1:
            y += 1
    if i == "right":
        x += 1
    if i == "left":
        x -= 1

    selected_fighters.append(fighters[y][x%6])

return selected_fighters
# >>>> show u the code <<<<
回复
0
#7

>>>> show me the code <<<<

x = initial_position[0] y = initial_position[1] res = [] for i in moves: if i == 'left': x -= 1 elif i == 'right': x += 1 elif i == 'up' and y > 0: y -= 1 elif i == 'down' and y < (len(fighters)-1): y += 1 res.append(fighters[y][x%6]) return res

>>>> show me the code <<<<

回复
0
#9
@叶藏

>>>> show u the code <<<<

selected_fighters = []
x = initial_position[0]
y = initial_position[1]

for i in moves:
    if i == "up":
        if y !=0:
            y -= 1
    if i == "down":
        if y != 1:
            y += 1
    if i == "right":
        x += 1
    if i == "left":
        x -= 1

    selected_fighters.append(fighters[y][x%6])

return selected_fighters
# >>>> show u the code <<<<

你的code我届到了,很赛艇,上下方向的条件判断其实可以再精简些

回复
0
#10
@黑衣白骨徐子扬

GaoHuanDeYeWang=[] for i in moves: if i=="right": initial_position[1]+=1 elif i=="left": initial_position[1]-=1 elif i=="up" and initial_position[0]==1: initial_position[0]=0 elif i=="down" and initial_position[0]==0: initial_position[0]=1

    GaoHuanDeYeWang.append(fighters[initial_position[0]][initial_position[1]%6])

return GaoHuanDeYeWang

GaoHuanDeYeWang这个列表没用上啊

回复
0
#11
@Kaiser

GaoHuanDeYeWang这个列表没用上啊

打了那个的话 这个编译器会报错 所以改成了最后一个

回复
0
#12
@KYOOO

def street_fighter_selection(fighters, initial_position, moves):

# >>>> show me the code <<<<
x = initial_position[0]
y = initial_position[1]
res = []

for i in moves:
    if i == 'left':
        x -= 1
    elif i == 'right':
        x += 1
    elif i == 'up' and y > 0:
        y -= 1
    elif i == 'down' and y < (len(fighters)-1):
        y += 1

    res.append(fighters[y][x%6])

return res

# >>>> show me the code <<<<
elif i == 'down' and y < (len(fighters)-1):

这里挺好,多于2行也适用了

回复
0
#13

考虑到只有实际意义上的前5楼提交了答案,那么我直接摇骰子好啦

回复
0
#14

2

回复
0
#15
@黑衣白骨徐子扬
result=[]
x = initial_position[0]
y = initial_position[1]
for i in moves:
    if i=="right":
        y+=1
    elif i=="left":
        y-=1
    elif i=="up" and x==1:
        x=0
    elif i=="down" and x==0:
        x=1


    result.append(fighters[x][y%6])

return result

你好,公众号里发我一下收货信息

回复
0
#16
@船长

你好,公众号里发我一下收货信息

总觉得败了人品,你们三月十号的大奖抽不到了呢

回复
0
#17
@黑衣白骨徐子扬

总觉得败了人品,你们三月十号的大奖抽不到了呢

能抽到三月十号大奖的蜜汁自信是怎么炼成的

回复
1
#18
游弋9月前
y = initial_position[0]
x = initial_position[1]
ex = []
for i in moves:
    if i=="right":
        x+=1
        if x>(len(fighters[0])-1):
            x=0

    elif i=="left":
        x-=1
        if x<0:
            x=(len(fighters[0])-1)

    elif i=="up":
        y-=1
        if y<0:
            y=0


    elif i=="down" and y==0:
        y=1           
    #这条向下的列个数不知道该怎么打
回复
0
#19
游弋9月前
@游弋
y = initial_position[0]
x = initial_position[1]
ex = []
for i in moves:
    if i=="right":
        x+=1
        if x>(len(fighters[0])-1):
            x=0

    elif i=="left":
        x-=1
        if x<0:
            x=(len(fighters[0])-1)

    elif i=="up":
        y-=1
        if y<0:
            y=0


    elif i=="down" and y==0:
        y=1           
    #这条向下的列个数不知道该怎么打
    ex.append( fighters[y][x])
return ex
回复
0
#20
游弋9月前
@游弋
    ex.append( fighters[y][x])
return ex

我主要想万一以后加新的任务也能继续用

回复
0
#21
游弋9月前
@游弋

我主要想万一以后加新的任务也能继续用

人物

最新回复:收起讨论