带有随机生成的蛇和梯形细胞的Python蛇和梯子
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带有随机生成的蛇和梯形细胞的Python蛇和梯子相关的知识,希望对你有一定的参考价值。
程序规格:https://www.dropbox.com/s/durgz0t0w3vewsn/Programming%20Project%20(Snakes%20%20Ladders).docx?dl=0
我到目前为止编写的代码:
import random
#define players list
player1=input("Player 1, please enter your name: ")
player2=input("Player 2, please enter your name: ")
position1=0
position2=0
location=0
#dice rolls
while(location<=100):
dice1=random.randint(1,6)
position1+=dice1
print(player1, ", you are player 1 and your starting number is: ", dice1, "and your position is: ", position1)
dice2=random.randint(1,6)
position2+=dice2
print(player2, ", you are player 2 and your starting number is: ", dice2, "and your position is: ", position2)
print("
")
location=max(position1,position2)
if position1>position2:
print(player1, "is the winner!")
else:
print(player2, "is the winner!")
#Ladder Positions
def generate_ladders_position():
ladderspotion=[]
laddersposition=random.sample(range(5, 86), 15)
return laddersposition
print("Ladder cells: " , generate_ladders_position())
#Snakes Positions
def generate_snakes_position():
snakesposition=[]
snakesposition=random.sample(range(20, 96), 10)
return snakesposition
print("Snake cells: " , generate_snakes_position())
#Check for ladders
def check_for_ladder():
if position1==generate_ladders_position():
position1=position1+15
print(player1, "landed on a ladder")
return check_for_ladder()
if position2==generate_ladders_position():
position2=position2+15
print(player, "landed on a ladder")
return check_for_ladder
答案
您需要保存蛇和梯子
ladders = generate_ladders()
snake = generate_snakes()
#Check for ladders
def check_for_ladder():
# check if it is in ladders
if position1 in ladders:
position1=position1+15
print(player1, "landed on a ladder")
return check_for_ladder()
if position2 in ladders:
position2=position2+15
print(player, "landed on a ladder")
return check_for_ladder
以上是关于带有随机生成的蛇和梯形细胞的Python蛇和梯子的主要内容,如果未能解决你的问题,请参考以下文章