首页 > 解决方案 > How to allow user to progress linearly across a 2D game board?

问题描述

I have game board composed of a 2d array. The user's position is marked on the board[0][0], the board is 10 columns and 4 rows. Users movement will be determined by the choice of movement cards which I have placed in a list named movementchits, with values ranging from 0-6.

How can I allow the marker to move across the board, and when at the end of the columns it needs to start on the row below and carry on? I have tried an if statement that takes movementchit value and adds it to the marker_col, and if marker_col >=9 then marker_row+1. I know this is wrong but hopefully gives an insight into what I am trying to achieve.

import random
gems = ["R", "E", "D", "S", "G"]
movementchits = [0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6]
board = [["|".ljust(2) +random.choice(gems).ljust(2)+"|" for a in range(10)]for b in 
range(4)]

marker_row =0
marker_col = 0
marker = "| x |"
board[marker_row][marker_col] = marker

for i in board:
    print("----- "*10)
    print(" ".join(i)) 
    print("----- "*10)

标签: python

解决方案


您可以将移动加在一起并使用整数除法//和模块化%来计算您在列表列表中的位置:

import random

def print_board(b):
    """Prints the current boad, gives as b"""
    for i in b:
        print("----- "*10 + "\n" + " ".join(i) + "\n" + "----- "*10 + "\n")

gems = ["R", "E", "D", "S", "G"]
movementchits = [0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6]

rows = 4
columns = 10

board = [["|".ljust(2) +random.choice(gems).ljust(2)+"|" for a in range(columns)] 
         for b in range(rows)]

empty  = "|   |"
marker = "| x |"

# starting position
player_position = 0  # linear position - row/column computed from it
p_row = 0
p_col = 0
board[p_row][p_col] = marker

for move in movementchits:
    # clear old position
    board[p_row][p_col] = empty

    # advance player
    player_position += move

    # check if you moved off the board 
    if player_position > columns*rows:
        print_board(board)
        print("You fell off the board!")
        break

    # calc new position
    p_row = player_position // columns
    p_col = player_position % columns
    board[p_row][p_col] = marker
    print_board(board)

输出:

----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
| x | | D | | R | | G | | R | | R | | E | | E | | R | | G |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
| G | | G | | D | | G | | S | | D | | D | | R | | R | | E |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
| G | | E | | E | | S | | R | | R | | D | | S | | R | | G |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
| D | | E | | S | | E | | D | | R | | E | | E | | S | | S |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 

----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
|   | | x | | R | | G | | R | | R | | E | | E | | R | | G |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
| G | | G | | D | | G | | S | | D | | D | | R | | R | | E |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
| G | | E | | E | | S | | R | | R | | D | | S | | R | | G |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
| D | | E | | S | | E | | D | | R | | E | | E | | S | | S |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 

----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
|   | |   | | x | | G | | R | | R | | E | | E | | R | | G |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
| G | | G | | D | | G | | S | | D | | D | | R | | R | | E |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
| G | | E | | E | | S | | R | | R | | D | | S | | R | | G |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
| D | | E | | S | | E | | D | | R | | E | | E | | S | | S |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 

# [snipp'ed most of the intermediate outputs for brevity]

----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
|   | |   | |   | | G | |   | | R | |   | | E | | R | |   |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
| G | | G | |   | | G | | S | | D | |   | | R | | R | | E |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
|   | | E | | E | | S | | R | |   | | D | | S | | R | | G |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
|   | | E | | S | | E | | D | | R | | x | | E | | S | | S |
----- ----- ----- ----- ----- ----- ----- ----- ----- -----   

----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
|   | |   | |   | | G | |   | | R | |   | | E | | R | |   |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
| G | | G | |   | | G | | S | | D | |   | | R | | R | | E |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
|   | | E | | E | | S | | R | |   | | D | | S | | R | | G |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
|   | | E | | S | | E | | D | | R | |   | | E | | S | | S |
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 

You fell off the board! # moves sum up to 42 - there are only 40 fields

推荐阅读