[英]Monster chase game on a board: why doesn't X appear if user enters 1 on first go?
Essentially this game allows user to choose a space on a 25 space board. 本质上,该游戏允许用户在25个空格板上选择一个空格。 There's a monster that randomly chooses a space (work in progress with the user's 'scent').
有一个怪物会随机选择一个空间(用户的“气味”正在进行中)。
If you start the program and enter 1 as your first move it doesn't appear on the board. 如果您启动程序并输入1作为第一步,则它不会出现在面板上。 Does anyone know why this may be?
有谁知道为什么会这样吗?
Bonus questions: 奖励问题:
import random
choice = 0
move = 0
user = " X "
monster = "<{:}>"
scent = " i "
space = " "
rabbit = True
mapp = [space for i in range (25)]
print(mapp)
def print_mapp():
row_1 = "|{}|{}|{}|{}|{}|".format(mapp[0],mapp[1],mapp[2],mapp[3],mapp[4])
row_2 = "|{}|{}|{}|{}|{}|".format(mapp[5],mapp[6],mapp[7],mapp[8],mapp[9])
row_3 = "|{}|{}|{}|{}|{}|".format(mapp[10],mapp[11],mapp[12],mapp[13],mapp[14])
row_4 = "|{}|{}|{}|{}|{}|".format(mapp[15],mapp[16],mapp[17],mapp[18],mapp[19])
row_5 = "|{}|{}|{}|{}|{}|".format(mapp[20],mapp[21],mapp[22],mapp[23],mapp[24])
print()
print(row_1)
print(row_2)
print(row_3)
print(row_4)
print(row_5)
print()
print_mapp()
def user_move():
global choice
if user in mapp:
mapp[choice] = scent
choice = (int(input("Where would you like to move? (1-25): "))-1)
check()
mapp[choice] = user
def monster_move():
global move
mapp[move] = space
move = (random.randint(1,25))-1
check()
mapp[move] = monster
def check():
if (mapp[choice] == monster) or (mapp[move] == user):
print("Die sucka\n"*20)
global rabbit
rabbit = False
else:
pass
while rabbit == True:
user_move()
monster_move()
print_mapp()
print("You died a horrible death.")
The problem is in monster_move
, specifically the line: 问题出在
monster_move
,特别是那行:
mapp[move] = space
You initialize move = 0
at the beginning, so it always erases the first square, and since it happens after the user has moved, the user won't show up there. 您在开始时初始化
move = 0
,所以它始终会删除第一个方框,并且由于它是在用户移动之后发生的,因此该用户不会出现在此处。
Instead, you could set it to a different value for the first round and check against that: 相反,您可以在第一轮将其设置为其他值,然后进行检查:
move = -1
and in monster_move
: 并在
monster_move
:
if move > -1:
mapp[move] = space
Bonus questions: 奖励问题:
I'd like for the 'scent' to disappear after one turn..any ideas?
我希望“气味”在一转后消失。有什么想法吗?
Keep track of previous positions and use that to remove the scent. 跟踪以前的位置,并使用它来去除气味。 If you use a queue data structure (in Python, this can be done with a
list
), it is easy to extend to "fancier" algorithms like keeping track for more than one turn. 如果您使用队列数据结构(在Python中,可以使用
list
来完成),则很容易扩展到“更高级”的算法,例如跟踪超过一圈。
At the top, initialize 在顶部,初始化
prev_moves = []
And in user_move
, remove the old scent and add the new: 在
user_move
,删除旧的气味并添加新的气味:
def user_move():
global choice
if user in mapp:
mapp[choice] = scent # replace the user marker
if len(prev_moves) > 0:
mapp[prev_moves.pop(0)] = space # remove earliest move
prev_moves.append(choice) # add most recent move
print(prev_moves)
for move in prev_moves: # place scent markers
mapp[move] = scent
choice = (int(input("Where would you like to move? (1-25): "))-1)
check()
mapp[choice] = user
If you wanted scent to last longer, just check how long the list is and only pop
if it's greater than what you want. 如果您想让香气持续更长的时间,只需检查列表多长时间,然后在列表大于所需大小时才
pop
。
I'd like user to get points for amount of scent left on before dyeing.
我希望用户能在染色前得到剩余气味的分数。 Any ideas?
有任何想法吗?
With the list implementation above, you can just check len(prev_moves)
. 使用上面的列表实现,您只需检查
len(prev_moves)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.