'Not able to do powerup of score - snake and ladder game | python |

I am currently trying to write code for snake and ladder , but seems got stuck not getting appropriate result

My code :

# Importing random module 
import random

# Snake and Ladder final destinaton  
maxCnt = 100

# Two player playing the Snake and Ladder game 
players = ['Alice','Bob']

# Snake as an Obstacle not to allow player to reach their final destination
snakes   = { 12:2 , 33:4 , 38:5 , 54:3 }

# Powerup - bonus ladder for player to reach final destination Quick  
powerupladder = { 3:16 , 18:25 , 26:10 , 28:2 , 34:2, 43:12, 54:2 , 98:1 }

# Counter - initially set to 0
counter = 0

# Initially set flag and player score to Zero 
flag = 0
player1 = 0
p1ayer2 = 0

# Logic - infinite loop to get user a chance to play everytime 
while True:

  if flag == 0:
    
    if player1 in powerupladder:
        
        player1_score = player1 + powerupladder[p1ayer1_dice]  # 3 -> 0 + 16  : 16
        player1 = player1 + powerupladder[p1ayer1_dice]  # 16

    else: 

      # Player throwing dice 
      p1ayer1_dice = random.randint(1, 6)
      
      if p1ayer1_dice in powerupladder:
        player1_score = player1 + powerupladder[p1ayer1_dice]  # 3 -> 0 + 16  : 16
        player1 = player1 + powerupladder[p1ayer1_dice]  # 16 
      else:
        player1_score = player1 + p1ayer1_dice   
        if player1_score in powerupladder:
          player1 = player1 + powerupladder[player1_score]
        else:
          player1 = player1 + p1ayer1_dice
    
    # Score 
    print('The player 1 rolling dice : ',p1ayer1_dice)

    # Score 
    print('The player 1 score : ',player1_score) 

    # Exiting with status as success if player reaches the final destination  
    if player1_score >= maxCnt:
      print(f'Congratulations {players[0]} you won the snake and ladder... you have finally reached destination')
      player1_score=0
      break

Output of above code:

The player 1 rolling dice :  1
The player 1 score :  1
The player 1 rolling dice :  1
The player 1 score :  2
The player 1 rolling dice :  3
The player 1 score :  18
The player 1 rolling dice :  3
The player 1 score :  34
The player 1 rolling dice :  3
The player 1 score :  50
The player 1 rolling dice :  1
The player 1 score :  51
The player 1 rolling dice :  2
The player 1 score :  53
The player 1 rolling dice :  3
The player 1 score :  69
The player 1 rolling dice :  5
The player 1 score :  74
The player 1 rolling dice :  4
The player 1 score :  78
The player 1 rolling dice :  3
The player 1 score :  94
The player 1 rolling dice :  2
The player 1 score :  96
The player 1 rolling dice :  4
The player 1 score :  100
Congratulations vikas you won the snake and ladder... you have finally reached destination

Issue : I have powerupladder which double the score like

If my dice throw : 3 then corresponding to key: 3 the value is 16

So my score is 16 and next time dice is thrown it gives me 2

So my score will be 18 -> correspond to 18 value is 25 -> final score should be 18 + 25 : 43

But my issue is when sum available in pwoerupladder it does not perform again powerup simply goes to run again dice

Please see the output shared where when score becomes 18 it runs again dice instead of powerupladder



Solution 1:[1]

Thank you @Tim Roberts

I was able to get desired output

Code :

# Importing random module 
import random

# Snake and Ladder final destinaton  
maxCnt = 100

# Two player playing the Snake and Ladder game 
players = ['vikas','vamshi']

# Snake as an Obstacle not to allow player to reach their final destination
snakes   = { 12:2 , 33:4 , 38:5 , 54:3 }

# Powerup - bonus ladder for player to reach final destination Quick  
powerupladder = { 3:16 , 18:25 , 26:10 , 28:2 , 34:2, 43:12,  53:1,  54:2 , 98:1 }

# Counter - initially set to 0
counter = 0

# Initially set flag and player score to Zero 
flag = 0
player1 = 0
p1ayer2 = 0

# Logic - infinite loop to get user a chance to play everytime 
while True:

  if flag == 0:
    
    # Player throwing dice 
    p1ayer1_dice = random.randint(1, 6)
    
    if p1ayer1_dice in powerupladder:
      player1_score = player1 + powerupladder[p1ayer1_dice]  # 3 -> 0 + 16  : 16
      player1 = player1 + powerupladder[p1ayer1_dice]  # 16 
    else:
      player1_score = player1 + p1ayer1_dice   
      if player1_score in powerupladder:
        player1 = player1 + powerupladder[player1_score]
      else:
        player1 = player1 + p1ayer1_dice
    
    if player1 in powerupladder:
      player1_score = player1 + powerupladder[player1]
      player1 = player1 + powerupladder[player1] 

    # Score 
    print('The player 1 Rollout dice : ',p1ayer1_dice,'Score : ',player1_score)


    # Exiting with status as success if player reaches the final destination  
    if player1_score >= maxCnt:
      print(f'Congratulations {players[0]} you won the snake and ladder... you have finally reached destination')
      player1_score=0
      break

Output :

The player 1 Rollout dice :  2 Score :  2
The player 1 Rollout dice :  1 Score :  43
The player 1 Rollout dice :  6 Score :  49
The player 1 Rollout dice :  6 Score :  55
The player 1 Rollout dice :  5 Score :  60
The player 1 Rollout dice :  4 Score :  64
The player 1 Rollout dice :  5 Score :  69
The player 1 Rollout dice :  3 Score :  85
The player 1 Rollout dice :  2 Score :  87
The player 1 Rollout dice :  3 Score :  103
Congratulations Alice you won the snake and ladder... you have finally reached destination

If more gaps are their will be filling it up.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 codeholic24