'Snake and Ladder game stuck after 3 plays
I have been trying to implement the logic , when player rolled dice 3 times continues 6 then reset his score to initial old score before he rolled the dice to 6.
For example :
Player 1 -> Dice : 6 -> Score -> 6 -> First time 6 occurred on Dice
Player 1 -> Dice : 6 -> Score -> 12 -> Second time 6 occurred on Dice
Player 1 -> Dice : 6 -> Score -> 0 -> Third time 6 occurred on Dice [ reset to old score ]
What I tried to implement
old_score.append(ply1)
if dice_rolled_ply_1 == 6:
flag = 0
sum_of_six = sum_of_six + dice_rolled_ply_1
if sum_of_six == 18:
player_1_score = old_score[0]
print(
{
"PLAYER 1 ": lst_of_palyers[i],
"DICE ": dice_rolled_ply_1,
"SCORE ": player_1_score,
"RESET TO OLD SCORE ": "PLAYER 1",
}
)
old_score.clear()
flag = 1
My Main Full code of GAME :
import random
import time
import sys
import copy
lst_of_palyers = ['Vikas','Vamsh']
# LADDER FOR PLAYERS
powerupladder = {4: 56, 12: 50, 14: 55, 22: 58, 41: 79, 54: 88}
powerupladder_ply_1 = copy.deepcopy(powerupladder)
powerupladder_ply_2 = copy.deepcopy(powerupladder)
# SNAKE FOR PLAYERS
snakes = {37: 3, 47: 16, 28: 10, 96: 42, 75: 32, 94: 71}
# PRE-DEFINED VARIABLES
flag = 0
i = 0
ply1 = 0
ply2 = 0
maxCnt = 100
player_1_score = 0
player_2_score = 0
sum_of_six = 0
status = 1
old_score = []
if status == 1:
# Storing the player name in sequence to play
for k, v in who_starts.items():
lst_of_palyers.append(k)
while True:
if flag == 0:
# dice_rolled_ply_1 = random.randint(1, 6)
dice_rolled_ply_1 = 6
player_1_score = ply1 + dice_rolled_ply_1
old_score.append(ply1)
if dice_rolled_ply_1 == 6:
flag = 0
sum_of_six = sum_of_six + dice_rolled_ply_1
if sum_of_six == 18:
player_1_score = old_score[0]
print(
{
"PLAYER 1 ": lst_of_palyers[i],
"DICE ": dice_rolled_ply_1,
"SCORE ": player_1_score,
"RESET TO OLD SCORE ": "PLAYER 1",
}
)
old_score.clear()
flag = 1
else:
# Snake checking
if player_1_score in snakes:
ply1 = snakes[player_1_score]
del snakes[player_1_score]
player_1_score = ply1
print(
{
"PLAYER 1 ": lst_of_palyers[i],
"DICE ": dice_rolled_ply_1,
"SCORE ": player_1_score,
"OOP's YOU GOT A SNAKE ": "PLAYER 1",
}
)
flag = 1
else:
if player_1_score in powerupladder_ply_1:
ply1 = powerupladder_ply_1[player_1_score]
del powerupladder_ply_1[player_1_score]
player_1_score = ply1
print(
{
"PLAYER 1 ": lst_of_palyers[i],
"DICE ": dice_rolled_ply_1,
"SCORE ": player_1_score,
"YOU GOT A LADDER , PLAY AGAIN ": "PLAYER 1",
}
)
flag = 0
else:
ply1 = ply1 + dice_rolled_ply_1
print(
{
"PLAYER 1 ": lst_of_palyers[i],
"DICE ": dice_rolled_ply_1,
"SCORE ": player_1_score,
}
)
flag = 1
else:
dice_rolled_ply_2 = random.randint(1, 6)
player_2_score = ply2 + dice_rolled_ply_2
old_score.append(ply2)
if dice_rolled_ply_2 == 6:
flag=1
sum_of_six = sum_of_six + dice_rolled_ply_2
if sum_of_six == 18:
player_2_score = old_score[0]
print(
{
"PLAYER 2 ": lst_of_palyers[i + 1],
"DICE ": dice_rolled_ply_2,
"SCORE ": player_2_score,
"RESET TO OLD SCORE ": "PLAYER 2",
}
)
old_score.clear()
flag = 0
else:
# Snake checking
if player_2_score in snakes:
ply2 = snakes[player_2_score]
del snakes[player_2_score]
player_2_score = ply2
print(
{
"PLAYER 2 ": lst_of_palyers[i + 1],
"DICE ": dice_rolled_ply_2,
"SCORE ": player_2_score,
"OOP's YOU GOT A SNAKE ": "PLAYER 2",
}
)
flag = 0
else:
if player_2_score in powerupladder_ply_2:
ply2 = powerupladder_ply_2[player_2_score]
del powerupladder_ply_2[player_2_score]
player_2_score = ply2
print(
{
"PLAYER 2 ": lst_of_palyers[i + 1],
"DICE ": dice_rolled_ply_2,
"SCORE ": player_2_score,
"YOU GOT A LADDER , PLAY AGAIN ": "PLAYER 2",
}
)
flag = 1
else:
ply2 = ply2 + dice_rolled_ply_2
print(
{
"PLAYER 2 ": lst_of_palyers[i + 1],
"DICE ": dice_rolled_ply_2,
"SCORE ": player_2_score,
}
)
flag = 0
if ply1 >= maxCnt:
print("")
print(
f"CONGRATULATIONS , {lst_of_palyers[i]} YOU WON THE GAME BY SCORING : {ply1} & YOUR COMPETITOR {lst_of_palyers[i+1]} LOSE GAME BY SCORING : {ply2}"
)
break
elif ply2 >= maxCnt:
print("")
print(
f"CONGRATULATIONS , {lst_of_palyers[i+1]} YOU WON THE GAME BY SCORING : {ply2} & YOUR COMPETITOR {lst_of_palyers[i]} LOSE GAME BY SCORING : {ply1}"
)
break
else:
pass
else:
pass
Currently may game is not moving further after 3 steps
{'PLAYER 1 ': 'Vamsh', 'DICE ': 6, 'SCORE ': 0, 'RESET TO OLD SCORE ': 'PLAYER 1'}
{'PLAYER 2 ': 'Vikas', 'DICE ': 4, 'SCORE ': 56, 'YOU GOT A LADDER , PLAY AGAIN ': 'PLAYER 2'}
{'PLAYER 2 ': 'Vikas', 'DICE ': 4, 'SCORE ': 60}
Any fix for it is much appreciated !!!
Solution 1:[1]
In what you tried to implement, when player 1 rolls a 6 three times, you reset his score and change players, but you do not reset the sum_of_six variable.
Furthermore, in your sample code, player 1 always rolls a six.
As a result, when it is player 1's turn again, sum_of_six is not 18 (it's 24), so you do not print any output and it is player 1's turn over and over again. Since "dice_rolled" is always 6 and "sum_of_six" is never 18, you do not print anything and the program runs in an infinite loop.
At the very least, you need to reset sum_of_six in the case it is 18. Also, if any player rolls a 6, I think they should move along the board and update their score in the same way as if they rolls between 1 and 5, should they not ?
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 | Betebizarre |
