I am new to python and I was working on program to simulate a game of snake and ladders. I used the random function to roll the dice for both players but the problem was that it came the same. If the player 1 gets a 5 then the player 2 also got a 5. Thus player 1 always won..
I'm not sure of what i missed here.
if turn % 2 == 0:
print(p1n + "'s turn")
showboard()
input('press Enter to roll the dice')
v = random.randint(1, 6)
print('you got a', v)
p1pos += v
temp1 = check_snake(p1pos)
temp2 = check_ladder(p1pos).....
elif turn % 2 == 1:
print(p2n + "'s turn")
#showboard()
r = input('press Enter to roll the dice')
v = random.randint(1,6)
print('you got a', v)
p2pos += v
temp1 = check_snake(p2pos)
temp2 = check_ladder(p2pos)......
I even tried using different random functions. Instead of 'randint' I used 'randrange'.
elif turn % 2 == 1:
print(p2n + "'s turn")
#showboard()
r = input('press Enter to roll the dice')
v = random.randrange(7)
print('you got a', v)
p2pos += v
temp1 = check_snake(p2pos)
temp2 = check_ladder(p2pos)
Even this gave the same problem. I started programming with python quite recently, thus i'm not exactly sure of how the random function works.
This is the whole code:
def play():
end = 100
start = 1
p1n = input('player 1 enter your name')
p2n = input('player 2 enter your name')
p1pos = start
p2pos = start
turn = 0
while 1:
if turn % 2 == 0:
print(p1n + "'s turn")
#showboard()
input('press Enter to roll the dice')
v = random.randint(1, 6)
print('you got a', v)
p1pos += v
temp1 = check_snake(p1pos)
temp2 = check_ladder(p1pos)
if p1pos != temp1:
print('There was a snake at',p1pos,'. Now you are going to', temp1)
p1pos = temp1
elif p1pos != temp2:
print('There was a ladder at', p1pos , '. Now you are going to', temp1)
p1pos == temp2
else:
p1pos = p1pos
if p1pos >= end:
p1pos = end
if check_win(p1pos):
print(p1pos)
print(p1n, "has won")
break
else:
print(p1n + ", now your position is", p1pos)
print(p2n + ", now your position is", p2pos)
turn += 1
elif turn % 2 == 1:
print(p2n + "'s turn")
#showboard()
r = input('press Enter to roll the dice')
v = random.randrange(7)
print('you got a', v)
p2pos += v
temp1 = check_snake(p2pos)
temp2 = check_ladder(p2pos)
if p2pos != temp1:
print('There was a snake at', p2pos , '. Now you are going to ',temp1)
p2pos = temp1
elif p2pos != temp2:
print('There was a snake at', p2pos , '. Now you are going to ',temp2)
p2pos == temp2
else:
p2pos = p2pos
if p2pos >= 100:
p2pos = 100
if check_win(p2pos):
print(p2pos)
print(p2n, "has won")
break
else:
print(p2n + ", now your position is", p1pos)
print(p1n + ", now your position is", p1pos)
turn += 1
Aucun commentaire:
Enregistrer un commentaire