带有随机生成的蛇和梯形细胞的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蛇和梯子的主要内容,如果未能解决你的问题,请参考以下文章

#python初学个人笔记01- 用python做一个简易贪吃蛇游戏画蛇和苹果

蛇梯棋(转载)

js基础笔记学习282蛇和食物的碰撞检测2

使用深度 Q 学习的 AI 驱动蛇游戏 源码分享

用C语言编写贪吃蛇小游戏

100行Python代码,轻松完成贪吃蛇小游戏?