简体   繁体   中英

Movement of a player around an oddly shaped grid in python

I am currently trying to move a player's position around a grid, for a small game. The task is to move him the number of places that he rolls the 2 dice. I've made the 2 dimensional array for the grid, and it works perfectly fine, as well as the rolling of the dice, but when the dice are doubles, he moves backwards, however I have no idea how to make him move around the grid as its shaped really oddly. Any assistance would be greatly appreciated.

what i have so far:

grid = [[43,44,45,46,47,48,49],
        [42,41,40,39,38,37,36],
        [29,30,31,32,33,34,35],
        [28,27,26,25,24,23,22],
        [15,16,17,18,19,20,21],
        [14,13,12,11,10,9,8],
        [1,2,3,4,5,6,7]]

dice1 = randint(1,6)
dice2 = randint(1,6)
if dice1 == dice2:
    doubles = True

I would approach it by having a row and col position and function for moving forward and backwards. Here is how I would do forward movement, I will leave backwards to you:

row = 6
col = 0
def forward(num):
    if row % 2 == 0: # move right to go forward for even row
        if num > 6-col:
            row -= 1
            col = 6
            return forward(num-(6-col))
        col += num
    else:
        if num > col:
            row -= 1
            col = 0
            return forward(num-(col+1))
        col -= num

You'll need to add checks for when they get to the final position.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM