Python Forum

Full Version: snakes and ladders board creation help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have made a simple snakes and ladders game where the dice is rolled automatic and and the end tells you the winner. I would like to display the result of each move on a board / grid from 1 to 49 but have no idea on how to go about doing this below is my code and i am open to all suggestions that are not to complicated. Think

import time
from random import randint
print("Welcome to my game please input the players names")
player1=input("Please enter player 1's name followed by the return key : ")
player2=input("Please enter player 2's name followed by the return key : ")
print("right then ", player1," and ",player2, " the game is simple i'll explain the rules as we go along")
player1position=1
player2position=1
while player1position<48 or player2position<48:
    print("Its" , player1 , "' go ")
    dice1=randint(0,6)
    dice2=randint(0,6)
    if dice1==dice2:
        print("both dices rolled a ", dice1, " so you move back ", dice1+dice2 ," spaces")
        player1position=player1position-dice1+dice2
    else:
        print("Your first dice was a ", dice1, " and your second was a ", dice2)
        print("your total is ", dice1+dice2)
        player1position=player1position+dice1+dice2
        print("player one is now on square ", player1position)
        time.sleep(0.1)
    print("Its" , player2 , "' go ")
    dice1=randint(0,6)
    dice2=randint(0,6)
    if dice1==dice2:
        print("both dices rolled a ", dice1, " so you move back " ,dice1+dice2 ," spaces")
        player2position=player2position-dice1+dice2
    print("Your first dice was a ", dice1, " and your second was a ", dice2)
    print("your total is ", dice1+dice2)
    player2position=player2position+dice1+dice2
    print("player two is now on square ", player2position)
    time.sleep(0.1)
else:
    if player1position >49:
        print(player1 , "has won well done")
    else:
        print(player2 , "has won well done")
Two ways to do this come to mind. You can store the board as a list of lists, or you can build the board as you display it. Here's a couple examples on a 5 x 5 board:

board = [['.'] * 5 for row in range(5)]
board[1][2] = '1' # player 1 position
board[3][4] = '2' # player 2 position
print('\n'.join([''.join(row) for row in board]))
Or building as you go along:

text = ''
for row in range(5):
    for column in range(5):
        if row * 5 + column == player1position:
            text += '1'
        elif row * 5 + column == player2position:
            text += '2'
        else:
            text += '.'
    text += '\n'
I'm using a calculation in the building version, but you can do sort of the reverse calculation in the list of lists: board[player1position // 5][player1position % 5].

Keep in mind that if you are using the list version, you have to remove the old positions when you put the players in a new position.

Note that both of these will have the player move down the board, visually. If you want them to move up the board, you would have to reverse the rows in the list when printing them, or run the row loop from 4 to 0 when building the board.

This is not the limit of the ways you can build a board, but just some quick ideas.