Home > Software engineering >  How to move forward and possibly backward a specified number of steps in a python list?
How to move forward and possibly backward a specified number of steps in a python list?

Time:07-23

I'm trying to make a board game in Python. However, I'm stuck with a portion of it.

For example:

If you have a list:

lst = [1, 2, 3, 4, 5, 6, 'E']

A player can roll 1-6 and can start at any position other than 'E' as that's the winning space. If the player is at index 3, or value 4, and rolls a 3 they land on the 'E' space and everything is fine. However, if they roll a 6 for instance, they would move 3 steps forward to the 'E' value and then 'bounce back' 3 spaces back to index 3 or value 4 for a total of 6 moves. If they start at index 5, and roll a 5 they would go back to index 2 for a total of 5 moves etc. I'm not sure how to implement something like this or if there would be a better way to structure this instead? Any help is appreciated.

CodePudding user response:

You could do something like this; I have start assigned as index 3, aka '4' in the list.

lst = [1, 2, 3, 4, 5, 6, 'E']

start = 3
roll = 6

if start   roll >= len(lst):
    finish = 2 * len(lst) - 2 - (start   roll)
else:
    finish = start   roll
print(lst[finish])

Output:

4

CodePudding user response:

I wrote a small logic which might help. I am not sure how are you inputting your first value (randomizing it or inputting) but you can change this up a little to fix what you need.

lst = [1, 2, 3, 4, 5, 6, 'E']


lost = 1 #to loop
count = 0 #to remember the carry
while(lost):
    number = int(input("What do you wanna role? "))
    if number > 0 and number < 7: 
        newVal = number count
        if newVal > 6:
            subtract = 6 - newVal
            newVal = 6 subtract
        elif lst[newVal] == "E":
            print("WIN")
            lost = 0;
        count = newVal;
        print(newVal)
    else:
        print("Number should be in 1-6")

CodePudding user response:

you can replace all print(current_position) variables in while loop with your list: print(list1[current_position-1]), which would basically be the same. You can also change everything with your list, I just don't see any point in doing that unless you really really really need to iter like that through your list.

    import random
    
    Running = True
    current_position = int(input('select a number 1-6: '))
    
    while Running == True:
        option = str(input('type Y to roll, type Q to quit: '))
        if option == 'Y':
            roll = random.randint(1,6)
            print('current position: ',current_position)
            print('rolled number: ',roll)
            if (current_position   roll) == 7:
                print(f'!!!!!!!!Congatulations !!!!!!!!, You have scored {current_position   roll}')
                Running = False
            if (current_position   roll)>7:
                current_position = (current_position-(6-roll))
                print('current_position: ',current_position)
            elif  (current_position   roll) <= 6:
                current_position = current_position   roll
                print('current position: ',current_position)
        else: Running = False

outputs:

    select a number 1-6: 3
    type Y to roll, type Q to quit: Y
    current position:  3
    rolled number:  2
    current position:  5
    type Y to roll, type Q to quit: Y
    current position:  5
    rolled number:  1
    current position:  6
    type Y to roll, type Q to quit: Y
    current position:  6
    rolled number:  4
    current_position:  4
    type Y to roll, type Q to quit: Y
    current position:  4
    rolled number:  3
    !!!!!!!!Congatulations !!!!!!!!, You have scored 7
  • Related