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

What is turtle.stamp() in python?

turtle.stamp() turtle.stamp() is used to stamp a copy of the turtle shape, it prints the shape at that point and moves forward with the instructions. Here shape of the turtle does not matter. It does the same for every turtle shape.  Example:  Here is the example of stamp() used: import   turtle def   tri ():      for   i   in   range ( 10 ):          turtle . degrees ( 60 )          turtle . forward ( 200 + 10 * i )          turtle . stamp ()          turtle . right ( 200 ) turtle . speed ( 7 ) turtle . color ( "orange" )         tri () turtle . done () For more such posts, stay tuned!

Introduction to Artificial Intelligence

WHAT  IS  ARTIFICIAL  INTELLIGENCE? Artificial Intelligence                  Concept of AI goes back to the classical ages. Under greek   mythology , the concept of machines and mechanical men were well thought off. In 1950, “ALAS TURING”, published landmark paper “Computing Machinery and Intelligence”, proposes “Imitation Game”, known as the famous “TURING TEST” He speculated about the possibility of creating machines that think. Unfortunately, until this date we haven’t found a machine that has fully cleared the Turin Test. Followed by this was the era of 1951, in which MARVIN MINSKY and DEAN EDMUNDS built the first   artificial neural network using 3000 vacuum tubes stimulating 40 neurons. Next in 1952,   ARTHUR SAMUEL, wrote the first machine learning program(Game AI) for checkers. In 1956, there was the birth of AI. JOHN Mc CARTHY, first coined the term “AI” at the Dartmouth Conference. Coming to 1959, first AI laboratory, where   the research on AI has begu

Use Python to know about Artificial Intelligence

 Hey, welcome back!   You'll be happy to know that you can use python to know about any topic related to your research from wikipedia. Let's check the code.     #import module import requests import pandas as pd from bs4 import BeautifulSoup def getdata(url):     r=requests.get(url)     return r.text htmldata=getdata("https://en.wikipedia.org/wiki/Artificial_Intelligence") soup= BeautifulSoup(htmldata, 'html.parser') data='' for data in soup.find_all("p"):     print(data.get_text()) Here is the output of the code. You can use the same code for acquiring various kind of information just by doing a slight change.  htmldata=getdata("https://en.wikipedia.org/wiki/(#Entering the topic name") Assume, you want to know about solar eclipse, here is the code for it. Observe the slight change in it. #import module import requests import pandas as pd from bs4 import BeautifulSoup def getdata(url):     r=requests.get(url)     return r.text htmld