python snakes and ladders

Solutions on MaxInterview for python snakes and ladders by the best coders in the world

showing results for - "python snakes and ladders"
Alma
08 May 2018
1#Heres a snakes and ladders game I made, felt the need to share it so others could play it too
2
3import tkinter as tk
4from time import sleep
5from random import randint
6
7root = tk.Tk()
8
9Grid = []
10for i in range(8):
11  GridRow = []
12  for j in range(8):
13    GridRow.append("Empty")
14  Grid.append(GridRow)
15Grid[0][0] = "End"
16Grid[7][0] = "Start"
17
18#Snakes
19Grid[0][3] = [2,1,"S"]
20Grid[0][6] = [2,7,"S"]
21Grid[2][6] = [5,2,"S"]
22Grid[3][1] = [4,0,"S"]
23Grid[3][5] = [5,6,"S"]
24Grid[6][2] = [6,5,"S"]
25
26#Ladders
27Grid[2][0] = [0,0,"L"]
28Grid[1][7] = [0,7,"L"]
29Grid[2][4] = [1,5,"L"]
30Grid[5][5] = [1,2,"L"]
31Grid[7][2] = [5,0,"L"]
32Grid[7][5] = [5,7,"L"]
33
34#[Row,Collumn]
35player1 = [7,0]
36player2 = [7,0]
37
38LabelGrid = []
39
40def updateGrid():
41  global player1
42  global player2
43  global LabelGrid
44  global Grid
45  for i in LabelGrid:
46    i.grid_forget()
47  for i in range(8):
48    for j in range(8):
49      root.grid_rowconfigure(i,weight=1,minsize=64)
50      root.grid_columnconfigure(j,weight=1,minsize=64)
51      Label = tk.Label(root)
52      Label.grid(column=j,row=i,sticky="nsew")
53      LabelGrid.append(Label)
54      if (i+j)%2 == 0:
55        Label.configure(bg="Black")
56      if Grid[i][j] == "Empty":
57        Label.configure(text="")
58      elif Grid[i][j] == "Start":
59        Label.configure(text="Start",bg="Sky Blue")
60      elif Grid[i][j] == "End":
61        Label.configure(text="End",bg="Gold")
62      else:
63        LabelText = "Leads to\nCollumn "+str(Grid[i][j][1])+"\nRow "+str(Grid[i][j][0])
64        Label.configure(text=LabelText,bg="Red" if Grid[i][j][2] == "S" else "Green")
65  
66  p1 = tk.Label(root,text="Player 1",bg="Yellow")
67  p1.grid(column=player1[1],row=player1[0],sticky="n")
68  LabelGrid.append(p1)  
69  p2 = tk.Label(root,text="Player 2",bg="Blue")
70  p2.grid(column=player2[1],row=player2[0],sticky="s")
71  LabelGrid.append(p2)
72  root.update()
73
74def movePlayer(player,spaces):
75  global Grid
76  endSpace = player
77  for i in range(spaces):
78    if endSpace == [0,0]:
79      return endSpace
80    if endSpace[0]%2 == 1:
81      if endSpace[1] == 7:
82        endSpace[0] -= 1
83      else:
84        endSpace[1] += 1
85    else:
86      if endSpace[1] == 0:
87        endSpace[0] -= 1
88      else:
89        endSpace[1] -= 1
90  if type(Grid[endSpace[0]][endSpace[1]]) == list :
91    return [Grid[endSpace[0]][endSpace[1]][0],Grid[endSpace[0]][endSpace[1]][1]]
92  return endSpace
93
94Turn = 1
95Winner = ""
96
97Text = tk.Label(root,text="Loading")
98WaitVariable = tk.IntVar()
99Button = tk.Button(root,text="Roll",command=lambda: WaitVariable.set(1))
100Text.grid(column=0,row=8,columnspan=8,sticky="nsew")
101Button.grid(column=0,row=9,columnspan=8,sticky="nsew")
102
103root.grid_rowconfigure(8,weight=1,minsize=32)
104root.grid_rowconfigure(9,weight=1,minsize=32)
105
106updateGrid()
107while True:
108  Text.configure(text="Player "+("1" if Turn%2 ==1 else "2")+"'s turn")
109  Button.wait_variable(WaitVariable)
110  roll = randint(1,6)
111  Text.configure(text="Rolled a "+str(roll))
112  if Turn%2 == 1:
113    player1 = movePlayer(player1,roll)
114    if player1 == [0,0]:
115      Winner = "Player 1"
116      break
117  else:
118    player2 = movePlayer(player2,roll)
119    if player2 == [0,0]:
120      Winner = "Player 1"
121      break
122  Turn += 1
123  updateGrid()
124  sleep(1)
125
126Text.configure(text=Winner+" wins!")
127updateGrid()
queries leading to this page
snakes and ladders game pythonsnakes and ladder game in pythonsnakes and ladders game code with py listsnake and ladder game with auto and manual pythonsnakes and ladders in python using oopssnakes and ladders game code with py dictionarytext based snakes and ladders in pythonhow to make snake and ladderwith pythonsnakes and ladder pythonsnake and ladder pythonsnakes and ladders pythonsimple code for snakes and ladders python codesnakes and ladders pythonsnakes and ladders python codesnakes and ladders game in pythonpython code snakes and ladderssnakes and ladders python listpython snake and ladder only textsnakes and ladders in python follows what algorithmtext based snake and ladder game project in pythontext based snakes and ladders in python with auto and manual modeladders and snakes code pyhow to make a snakes and ladders board in pythonsnakes and ladders turtle pythonsnakes and ladders python ooppython average snakes and ladder gamehow to make a snakes and ladders board in python pygamesnakes and ladders game python code with dictionary how to make snakes and ladders in pythonsnake and ladder game project in pythonsnake and ladder game pythonwhat classes do i need for snakes and ladders pythonsnakes and ladders game python object oriantedpython snakes and ladderssnakes and ladders board pythonsimple snakes and ladders python codesnakes and ladders games with python classessnake and ladder game in pythonsnakes and ladders game in python cheggpython snakes and ladders