python snakes and ladders

#Heres a snakes and ladders game I made, felt the need to share it so others could play it too

import tkinter as tk
from time import sleep
from random import randint

root = tk.Tk()

Grid = []
for i in range(8):
  GridRow = []
  for j in range(8):
    GridRow.append("Empty")
  Grid.append(GridRow)
Grid[0][0] = "End"
Grid[7][0] = "Start"

#Snakes
Grid[0][3] = [2,1,"S"]
Grid[0][6] = [2,7,"S"]
Grid[2][6] = [5,2,"S"]
Grid[3][1] = [4,0,"S"]
Grid[3][5] = [5,6,"S"]
Grid[6][2] = [6,5,"S"]

#Ladders
Grid[2][0] = [0,0,"L"]
Grid[1][7] = [0,7,"L"]
Grid[2][4] = [1,5,"L"]
Grid[5][5] = [1,2,"L"]
Grid[7][2] = [5,0,"L"]
Grid[7][5] = [5,7,"L"]

#[Row,Collumn]
player1 = [7,0]
player2 = [7,0]

LabelGrid = []

def updateGrid():
  global player1
  global player2
  global LabelGrid
  global Grid
  for i in LabelGrid:
    i.grid_forget()
  for i in range(8):
    for j in range(8):
      root.grid_rowconfigure(i,weight=1,minsize=64)
      root.grid_columnconfigure(j,weight=1,minsize=64)
      Label = tk.Label(root)
      Label.grid(column=j,row=i,sticky="nsew")
      LabelGrid.append(Label)
      if (i+j)%2 == 0:
        Label.configure(bg="Black")
      if Grid[i][j] == "Empty":
        Label.configure(text="")
      elif Grid[i][j] == "Start":
        Label.configure(text="Start",bg="Sky Blue")
      elif Grid[i][j] == "End":
        Label.configure(text="End",bg="Gold")
      else:
        LabelText = "Leads to\nCollumn "+str(Grid[i][j][1])+"\nRow "+str(Grid[i][j][0])
        Label.configure(text=LabelText,bg="Red" if Grid[i][j][2] == "S" else "Green")
  
  p1 = tk.Label(root,text="Player 1",bg="Yellow")
  p1.grid(column=player1[1],row=player1[0],sticky="n")
  LabelGrid.append(p1)  
  p2 = tk.Label(root,text="Player 2",bg="Blue")
  p2.grid(column=player2[1],row=player2[0],sticky="s")
  LabelGrid.append(p2)
  root.update()

def movePlayer(player,spaces):
  global Grid
  endSpace = player
  for i in range(spaces):
    if endSpace == [0,0]:
      return endSpace
    if endSpace[0]%2 == 1:
      if endSpace[1] == 7:
        endSpace[0] -= 1
      else:
        endSpace[1] += 1
    else:
      if endSpace[1] == 0:
        endSpace[0] -= 1
      else:
        endSpace[1] -= 1
  if type(Grid[endSpace[0]][endSpace[1]]) == list :
    return [Grid[endSpace[0]][endSpace[1]][0],Grid[endSpace[0]][endSpace[1]][1]]
  return endSpace

Turn = 1
Winner = ""

Text = tk.Label(root,text="Loading")
WaitVariable = tk.IntVar()
Button = tk.Button(root,text="Roll",command=lambda: WaitVariable.set(1))
Text.grid(column=0,row=8,columnspan=8,sticky="nsew")
Button.grid(column=0,row=9,columnspan=8,sticky="nsew")

root.grid_rowconfigure(8,weight=1,minsize=32)
root.grid_rowconfigure(9,weight=1,minsize=32)

updateGrid()
while True:
  Text.configure(text="Player "+("1" if Turn%2 ==1 else "2")+"'s turn")
  Button.wait_variable(WaitVariable)
  roll = randint(1,6)
  Text.configure(text="Rolled a "+str(roll))
  if Turn%2 == 1:
    player1 = movePlayer(player1,roll)
    if player1 == [0,0]:
      Winner = "Player 1"
      break
  else:
    player2 = movePlayer(player2,roll)
    if player2 == [0,0]:
      Winner = "Player 1"
      break
  Turn += 1
  updateGrid()
  sleep(1)

Text.configure(text=Winner+" wins!")
updateGrid()

0
0

                                    class snakesandladder(object):<br/>    def __init__(self, name, position):<br/>        self.name = name<br/>        self.position = position<br/>        self.ladd = [4,24,48,67,86]<br/>        self.lengthladd = [13,23,5,12,13]<br/>        self.snake = [6,26,47,23,55,97]   <br/>        self.lengthsnake = [4,6,7,5,8,9]<br/>        <br/>        <br/>    def dice(self):<br/>        chances = 0<br/>        print("----------------LeTs StArT ThE GaMe----------------\n")<br/>        while self.position  104:<br/>                self.position = self.position - roll<br/>            if self.position == 104:<br/>                print('completed the game')<br/>                break<br/>            <br/>            if self.position in self.ladd:<br/>                for n in range(len(self.ladd)):<br/>                    if self.position == self.ladd[n]:<br/>                        self.position = self.position + self.lengthladd[n]<br/>            if self.position in self.snake:<br/>                for n in range(len(self.snake)):<br/>                    if self.position == self.snake[n]:<br/>                        self.position = self.position - self.lengthsnake[n]<br/>                        <br/>            print('Current position of the player : ', self.position, '\n')<br/>            chances += 4/4<br/>        print('ToTal number oF chances : ', chances)<br/>    <br/><br/>zack = snakesandladder('zack',0)    <br/>zack.dice()

0
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source