import random
def playSnakesLadders(playerCount):
"""
Function to play Snakes and Ladders game
:param playerCount: Number of players
"""
# Create the board
board = [[0 for i in range(11)] for j in range(11)]
# Add snakes and ladders
for i in range(1, playerCount + 1):
# Randomly choose a position for the snake or ladder
ladderStart, ladderEnd = 1 + random.randint(0, 9), 1 + random.randint(0, 9)
# Check if the snake or ladder already exists on the board
while board[ladderStart][ladderEnd] != 0:
ladderStart, ladderEnd = 1 + random.randint(0, 9), 1 + random.randint(0, 9)
board[ladderStart][ladderEnd] = i
# Initialize player positions
playerPositions = [0 for i in range(playerCount)]
# Players take turns rolling the dice and moving their position
while True:
# Roll the dice
dice = random.randint(1, 6)
# Move the player
playerIndex = 0
while playerPositions[playerIndex] != (dice - 1):
playerIndex = (playerIndex + 1) % playerCount
newPosition = playerPositions[playerIndex] + dice
# Check if the new position is within the board limits
while newPosition >= 10 or newPosition < 0:
newPosition = playerPositions[playerIndex] + dice
# Update player position on the board
board[newPosition][playerIndex] = i
# Check if the player has reached the end of the game
if board[9][playerIndex] == playerCount:
print("Player", playerIndex + 1, "wins!")
break
# Update player positions
playerPositions = [board[i][j] for i in range(1, playerCount + 1) for j in range(