Python Forum
Shortest paths to win snake and ladder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Shortest paths to win snake and ladder
#1
Can anyone please help me with python code that remembers all possible paths that a player can take in a snake and ladder game. At last, print all the shortest paths.

Ex: In a board of 100 steps (dice - 1 to 6),
SNAKES = {16: 6, 48: 26, 49: 11, 56: 53, 62: 19,
64: 60, 87: 24, 93: 73, 95: 75, 98: 78}
LADDERS = {2: 38, 4: 14, 9: 31, 21: 42,
28: 84, 36: 44, 51: 67, 71: 91, 80: 100}
Shortest paths to win are?
Reply
#2
What have you tried? We're not big on writing code for people here, but we would be happy to help you fix your code when you run into problems. When you do run into problems, please post your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.

Edit: I will say this: searching every path is a waste of time. Note that the shortest path is going to use ladders and avoid snakes. Find the shortest path from ladder to ladder that avoids snakes, and combine those paths.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Do you know that there is something called Google search engine which is free to use?
Reply
#4
Thanks, ThomasL for thinking fellow people as that low.
FYI, I did do my search (research) before posting here to discuss with my fellow python users/teachers/developers their point of views and most of the answer where either implementation of snake and ladder game itself or no: of least steps to win (some example with 1 shortest path)
But, I wanted all shortest paths if you read my first post (Question)

After looking into Administrator post, I sat down and came with below code. Also, I understood we should not ignore snakes as suggested above since a snake can be between 2 big ladders which will shorten the steps needed. Ofcourse, we can still optimize it.

top = 100
snakes = {16: 6, 48: 26, 49: 11, 56: 53, 62: 19,
64: 60, 87: 24, 93: 73, 95: 75, 98: 78}
ladders = {2: 38, 4: 14, 9: 31, 21: 42,
28: 84, 36: 44, 51: 67, 71: 91, 80: 100}
jump = {**snakes, **ladders}

steps = 0
paths = dict()
reached_top = False
all_short_paths = list()
reached_bottom = False

while not reached_top:
    steps += 1
    if steps == 1:
        paths[1] = [[0,1,2,3,4,5,6]]
        for index,val in enumerate(paths[1][0]):
            if val in jump.keys():
                paths[1][0][index] = jump[val]
    else:
        paths[steps] = list()
        for lists in paths[steps - 1]:
            for val in lists[1:]:
                paths[steps].append([val, val+1, val+2, val+3, val+4, val+5, val+6])

        for index1,lists in enumerate(paths[steps]):
            for index2,val in enumerate(lists):
                if val in jump.keys():
                    paths[steps][index1][index2] = jump[val]
                    val = jump[val]
                if val == top:
                    reached_top = True
                    if paths[steps][index1][0]:
                        all_short_paths.append([100,paths[steps][index1][0]])

all_short_paths = [list(x) for x in set(tuple(x) for x in all_short_paths)]
steps_down = 1
while not reached_bottom:
    new_all_short_paths = list()
    for lists1 in all_short_paths:
        for lists2 in paths[steps - steps_down]:
            if lists1[-1] in lists2:
                new_all_short_paths.append(lists1 + [lists2[0]])
    all_short_paths = [list(x) for x in set(tuple(x) for x in new_all_short_paths)]
    steps_down += 1
    if steps - steps_down == 1:
        reached_bottom = True

print(f"Shortest steps to win is {steps}.\nNo: of Shortest paths are,\n {len(all_short_paths)}")
print(all_short_paths)
Reply
#5
(Jun-29-2019, 05:09 PM)sandaab Wrote: Thanks, ThomasL for thinking fellow people as that low.
My post was intended as a help and not as an insult.
You did not show any code and your question was
Quote:Shortest paths to win are?
That imho is not a question about my thoughts about your problem.
As a generous person i provide you with another link with interesting information about your problem.
Have fun and carpe diem.
Reply
#6
My question was "Can anyone please help me with python code that remembers all possible paths that a player can take in a snake and ladder game. At last, print all the shortest paths."

And also, at last, I said "Shortest Paths" not "Shortest Path"

But, thanks for clarifying the dispute and helping. Apologies.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Good class design - with a Snake game as an example bear 1 1,713 Jan-24-2024, 08:36 AM
Last Post: annakenna
  pdf2image, poppler and paths jehoshua 18 13,949 Jun-14-2022, 06:38 AM
Last Post: jehoshua
  Windows paths issue otalado 3 1,417 May-29-2022, 09:11 AM
Last Post: snippsat
  Help for the shortest way to install a suitable version of Python, Numpy, and OpenCV. Ezzat 2 2,226 Dec-23-2021, 12:34 PM
Last Post: snippsat
  automatically get absolute paths oclmedyb 1 2,062 Mar-11-2021, 04:31 PM
Last Post: deanhystad
  Mouvement Snake (The game) Catif 0 1,267 Nov-27-2020, 08:28 PM
Last Post: Catif
  chkFile with absolute paths JarredAwesome 7 2,913 Sep-21-2020, 03:51 AM
Last Post: bowlofred
  Paths millpond 12 5,081 Jul-30-2020, 01:16 PM
Last Post: snippsat
  Problems with windows paths delphinis 6 5,050 Jul-21-2020, 06:11 PM
Last Post: Gribouillis
  How to handle paths with spaces in the name? zBernie 1 6,667 Nov-22-2018, 04:04 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020