0

I am trying to simulate a simple game in python. In this game, the player would throw a dice and would move from their current position towards the end line (located in position 100), depending on the dice (which is numbered from 1 to 6).

I'm trying to come up with a function that would do the following: add the current position and the result of the dice. However, if this function gives a number higher than 100, the function would ignore it and throw the dice again, since there's no positions after 100.

Below you can find the 'pseudo-code' I've come up with (half real code, half my ideas/comments):

import random 

def movement(current_position, distance):
        current_position = 0 #a counter should be added here I guess to increment the position
        distance = random.randint(1,6)
        move = current_position + distance
              if move > 100; do:
                  #function telling python to ignore it and throw the dice again
              elif move = 100; do:
                  print("You reached position 100")
              else:
                  return move

Could you help me figure out how to do this?

5
  • Your pseudo-code falls short here. What if it's exactly 100? Or, if you have 99, do you just keep rolling for a 1 even though the final value is inevitable? Commented Oct 2, 2018 at 21:30
  • I guess I should include an 'elif' statement for 100 position (thanks for that! I will include it in my question). But if the position is 99, the dice should definitely be thrown again until its result is 1 and the 100 position is reached. Commented Oct 2, 2018 at 21:33
  • 2
    But you don't seem to be counting the dice rolls, so what difference does it make to use the RNG to get the final 1 or just add it on? Commented Oct 2, 2018 at 21:34
  • Are you storing these numbers anywhere for some type of statistical analysis? A computer could brute force this before we could blink. Commented Oct 2, 2018 at 21:35
  • My idea was to call this function within another function that would count the dice rolls, as you say. But I need to define it before doing that Commented Oct 2, 2018 at 21:40

2 Answers 2

1

You could set up conditions like this where if the dice roll pushes the current value over 100 it is ignored until a dice roll creates a value equal to 100

from random import randint

current = 0
while current != 100:
    r = randint(1, 6)
    if current + r > 100:
        continue
    else:
        current += r
    print(current)
4
8
...
89
93
96
98
99
100
Sign up to request clarification or add additional context in comments.

1 Comment

I would add a print(current) inside of your else statement to track current as it is incremented
0

you could always check if it's over 100 and then revert to the old position. Then you'd call movement from your main function say def playGame():

def playGame():
    position = 0
    while(position != 100):
         position = movement(position)

def movement(current_position):
    distance = random.randint(1,6)
    current_position += distance
          if move > 100:
              current_position -= distance
          else:
              return current_position

1 Comment

Can I make the movement function take two parameters instead of just one? I would use movement(current_position, distance). Would this still be correct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.