Skip to main content

Snakes and Ladders Game!

 Hey! Welcome back to my blog! 

In this blog we will learn to create our one of the favorite childhood game "SNAKES AND LADDERS". I hope everyone is aware about this game. I still remember I used to play this game during my summer vacation and perhaps those were the best days. 

Okay, let's start python coding for the same game!

So for this particular piece of code, I have assumed the following game board and according to that numbers are assigned to the snakes and ladders.



---------------------------------------------------------------------------------------------------------------

from PIL import Image

import random


end=100


def show_board():

    img=Image.open('C:\\Users\\asus01\\Desktop\\slb.jpg')

    img.show()


def check_ladder(points):

        if points==3:

            print("Ladder")

            return 51

        elif points==6:

            print("Ladder")

            return 27

        elif points==20:

            print("Ladder")

            return 70

        elif points==36:

            print("Ladder")

            return 55

        elif points==63:

            print("Ladder")

            return 95

        elif points==68:

            print("Ladder")

            return 100

        else:

            #Not a ladder

            return points

        

def check_snake(points):

    if points==25:

        print("Snake")

        return 5

    elif points==34:

        print("Snake")

        return 1

    elif points==47:

        print("Snake")

        return 19

    elif points==65:

        print("Snake")

        return 52

    elif points==87:

        print("Snake")

        return 57

    elif points==91:

        print("Snake")

        return 61

    elif points==99:

        print("Snake")

        return 69

    else: 

        return points

    

def reached_end(points):

    if points==end:

        return True

    else:

        return False

    

        

        

    

def play():

    #input plYER 1 NAME

    p1_name=input("Player 1, Enter your name: ")

    #input player 2 name

    p2_name=input("Player 2, Enter your name: ")

    #player 1points

    pp1=0

    #player 2 points

    pp2=0

    

    turn=0

    

    while(1):

        if turn%2==0:

            #player 1 turn

            print(p1_name,'Your turn: ')

            #ask player's choice to continue

            c=input("Press 1 to continue or 0 to quit:")

            if c==0:

                print(p1_name,'scored',pp1)

                print(p2_name,'scored',pp2)

                print("Quitting the game thanks!")

                break

            #roll a die 1,2,3,4,5,6,

            dice=random.randint(1,6)

            print('Dice showed: ',dice)

            #add points

            pp1=pp1+dice

            

            pp1=check_ladder(pp1)

            

            pp1=check_snake(pp1)

            #check if the playe goes beyond the board

            if pp1>end:

                pp1=end

            print(p1_name,'Your score',pp1)

            

            if reached_end(pp1):

                print(p1_name,"WON")

                break

        else:

             #player 2 turn

            print(p2_name,'Your turn: ')

            #ask player's choice to continue

            c=input("Press 1 to continue or 0 to quit:")

            if c==0:

                print(p1_name,'scored',pp1)

                print(p2_name,'scored',pp2)

                print("Quitting the game thanks!")

                break

            #roll a die 1,2,3,4,5,6,

            dice=random.randint(1,6)

            print('Dice showed: ',dice)

            #add points

            pp2=pp2+dice

            

            pp2=check_ladder(pp2)

            

            pp2=check_snake(pp2)

            #check if the playe goes beyond the board

            if pp2>end:

                pp2=end

            print(p2_name,'Your score',pp2)

            

            if reached_end(pp2):

                print(p2_name,"WON")

                break

        turn=turn+1

        

        

show_board()

play()

---------------------------------------------------------------------------------------------------------------

I hope this might have helped you learn something new. In case of doubts or suggestion, do comment or email.
STAY TUNED, for more such blogs.




Comments

Popular posts from this blog

Know the current Covid status of your country using Python!

 We all are aware about COVID-19, i.e. coronavirus infectious disease 2019.  Isn't it fascinating, using python for numerous purpose! So, here is the python program that can help you know the status of COVID-19 of your country. -------------------------------------------------------------------------------------------------------------------------- For this you need to install one package using the pip command. pip install covid -------------------------------------------------------------------------------------------------------------------------- from covid import Covid covid=Covid() cases=covid.get_status_by_country_name("India") #enter the name of the country you want to know the status for x in cases:     print(x,":",cases[x]) -------------------------------------------------------------------------------------------------------------------------- Output of the above program, giving current status is: id : 80 country : India confirmed : 9857029 active : 35...

Drawing a star with stamp of turtle in python!

  Here is the code for the same: import   turtle star = turtle . Turtle () star. color ( "pink" ) star. shape ( "turtle" ) wn = turtle . Screen () wn. bgcolor ( "black" ) for  i  in   range ( 100 ):     star. forward ( 200 + 20 * i)     star. stamp ()     star. right ( 144 ) turtle . speed ( 9 ) turtle .done() Output of the above code: