简体   繁体   中英

2D Array Text Based "Game"

I'm learning about nested lists by building a little 2D text-based game of sorts. I already have a procedure, called print_board that accepts a list of lists of characters that represent a board (like a board game). Keeping this simple, the next step is to create a procedure, called move_bot that accepts three arguments:

  1. a board represented by a nested list with characters. Empty spaces are represented by '.'characters. All other characters are things the robot cannot move through.
  2. a robot represented by a list of four values including a string for the robot's name, integer values for the robot's x and y location on the board, and a single character string that is used to display the robot on the board.
  3. a movement direction, represented by a tuple with two values for a move in the x (horizontal) and/or y (vertical) directions, respectively. Movement values are always -1, 0, or 1, (eg, (-1,0) moves left one position in the row).

The move_bot procedure must raise an error if the movement of the robot's current position plus the movement values cause the robot to run into a non empty space or to a position that is not on the board. If the robot is able to move, the procedure updates the board in two ways: The position where the robot is currently at is changed to the '.' character and place on the board where the robot is moving to is changed to the robot's display character (the last item in the robot tuple). Finally, the robot's x and y location is updated in its own list.

Here is print_board :

def print_board (board):
    """
    Receives a list of lists of characters  and displays them
    to the screen such that a rectangular border is drawn around 
    the entire board.  

    Examples:

    >>> print_board([list('..'),list('..')])
    +--+
    |..|
    |..|
    +--+
    >>> print_board([list('..'),list('.....'),list('..')])
    +-----+
    |..   |
    |.....|
    |..   |
    +-----+
    """
    max_len = max([len(r) for r in board])
    bound = "+"+"-"*max_len+"+\n"
    disp = bound
    for r in range(len(board)):
        disp += "|"
        for c in range(len(board[r])):
            disp += board[r][c]
        for e in range(max_len - len(board[r])):
            disp+=" "
        disp+="|\n"
    print(disp+bound,end='')

This works perfectly fine. The issue I'm having is with the move_bot procedure. Namely, I have no idea where to really begin. I really only have this so far:

def move_bot(board, robot, move_dir):
    print_board(board)

Can anyone help me figure out how to get this working properly? Desired output is below:

>>>b = [list('....R'),list('.W.'),list('..W')] #create board
>>>robot = ["Robbie",4,0,'R'] #create the robot
>>>print_board(b)  #Display initial board
+-----+
|....R|
|.W.  |
|..W  |
+-----+
>>>move_bot(b,robot,(-1,0)) #move left
>>> move_bot(b,robot,(-1,0)) #move left
>>> move_bot(b,robot,(0,1))  #move down
>>> print_board(b)           #R is now 2 left and 1 down
+-----+
|.....|
|.WR  |
|..W  |
+-----+
>>> move_bot(b,robot,(1,0))  #Off the end of a row!
Traceback (most recent call last):
...
IndexError: Robbie can't move to (3,1)
>>> move_bot(b, robot,(-1,0)) #Bump into a wall to the left!
Traceback (most recent call last):
...
ValueError: Robbie bumped into something!

You could try something like this:

def move_bot(board, robot, move_dir):

    # Obtain the starting position
    x_start = robot[1]
    y_start = robot[2]
    # Calculate the new robot position.
    x_new = x_start + move_dir[0]
    y_new = y_start + move_dir[1]

    # Now, you have to check if the new location is within range of the board
    # If it is in range, check if it contains a ".". If either of these things
    # is not true, raise an error.

    # If no error was raised, you can change the old position of the robot on
    # the board to ".", and put robot[3] in the new position.
    b[y_start][x_start] = "."
    b[y_new][x_new] = robot[3]

    # Oh, and don't forget to update Robbie!
    robot[1] = x_new
    robot[2] = y_new
    return(b, robot)

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