Snakes and ladder (Single player) in Python

Python Snakes and stepping stool (Single player): Here, we are going to actualize snakes and stepping stool game for single player utilizing Python program.

Rules of the game:

  • There are 6 face dice which is being moved by the player to their possibility.
  • The player begins at 0 and needs to arrive at the last position (for our situation, it’s 104).
  • There is some stepping stool which ends up being fortunate for the player as they abbreviate the way.
  • There are a few snakes present in the middle of the game which ends up being the foe of the player as they simply protract their approach to 104.

Presently, We are going to execute these guidelines and assemble code for this game.

We will utilize unique information type in this article, is object-arranged programming.

Python code for snakes and stepping stool (singleplayer):

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

zack = snakesandladder('zack',0)    
zack.dice()

Output:

Snakes and ladder (Single player) in Python

Leave a Comment

error: Alert: Content is protected!!