我需要帮助才能使我的python蛇和梯子游戏转变

时间:2017-04-05 08:39:42

标签: python python-2.7

我在python上制作了一个蛇和梯子游戏,但是只有一个问题。我的代码一次性播放整个游戏,但我需要在两个玩家之间轮流玩游戏。我不知道现在该做什么。请帮助。

P.S。我很抱歉,如果我的演讲非常糟糕,我只是第一次使用它并且不知道我应该做什么/说什么。

这是我的代码:

player1 = 0
player2 = 0

a = range(1,8);b = range(8,15)[::-1];c = range(15,22);d = range(22,29)[::-1];e = range(29,36);f = range(36,43);g = range(43,50)
print "Snakes and Ladders game"
print g,'\n',f,'\n',e,'\n',d,'\n',c,'\n',b,'\n',a

def check_for_snakes1_and_ladders1_and_snakes2_and_ladders2(n):
    """This method checks for the presence of snakes or ladders in the board"""
    ladders1 = {4:7,41:44}
    ladders2 = {26:33}
    snakes1 = {9:6,17:14}
    snakes2 = {48:41}
    if ladders1.has_key(n):
        print "You landed on a Bridge, Move Forward 3 spaces"
        n = ladders1[n]
    elif ladders2.has_key(n):
      print "You Landed on a Bridge, Move Forward 7 spaces"
      n = ladders2[n]
    elif snakes2.has_key(n):
      print "Oh No! You landed on an Obstacle, Move Backwards 7 spaces"
      n = snakes2[n]
    elif snakes1.has_key(n):
        print "Oh No! You landed on an Obstacle, Move Backwards 3 spaces"
        n = snakes1[n]
    return n


def roll_dice(r):
  from random import randint
  numbers=(randint(1,6))
  print "You Rolled a :",numbers
  return numbers

while player1 < 49 or player2 < 49:
    print "Its turn of player1\n"
    player1 = roll_dice(player1) + player1
    player1 = check_for_snakes1_and_ladders1_and_snakes2_and_ladders2(player1)
    print "Current status of Player1:",player1,"and Player2:",player2

    if player1 > 49:
        print "Winner of the game is player1"
        break

    print "Its turn of player2\n"
    player2 = roll_dice(player2) + player2
    player2 = check_for_snakes1_and_ladders1_and_snakes2_and_ladders2(player2)
    print "Current status of Player1:",player1," and Player2:",player2

    if player2 > 49:
        print "Winner of the game is player2"
        break

0 个答案:

没有答案