Question

Part VI: Move Starman by Movement Commands (20 points) Write a function move () that takes the following arguments, in this order I. board: A game board. 2. movement: A string that represents what kind of movement that Starman needs to perform. Four move ment commands are listed below: . U : go up one row ·D: go down one row : go left one column R: go right one column Nole: You may assume that the movement is always one of the four valid ones in this part This function shouild call the four functions you wrote for Parts I hrough V Your move () function should check the value of movement and then perform the corresponding movement returned from the function that it cals, As an example, if the value of movement is D, then move() w Hint: Dont over-think this problem - it is lierally four conditional statements hased on movement s value is ill by calling the appropriate function you write in Part II through V. move 0 must also return the value that return whatever value is returned by move up () Under each conditional s tatement you call one of the move X () functions and return the result from the function call. Examples: I0W. W01 board3 [[, F, W, ,F, , ,.,, ,W), = CSE 101-Fall 2017 Homework #3 Page 9

media%2Fd46%2Fd46038d9-3283-4924-9e31-90

Python programming

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def find_star_in_board(board):
    x, y = [-1, -1]
    for i in range(len(board)):
        for j in range(len(board[i])):
            if board[i][j] == '*':  # find * positions
                x = i
                y = j
                break
    return [x, y]


def replace_in_board(board, x, y, x_new, y_new):
    if 0 <= x_new < len(board) and 0 <= y_new < len(board):
        if board[x_new][y_new] == 'F' or board[x_new][y_new] == '.':
            board[x_new][y_new] = '*'
            board[x][y] = '.'
            return [x_new + 1, y_new + 1]
        elif board[x_new][y_new] == 'W':
            return [-1, -1]
        elif board[x_new][y_new] == 'O':
            board[x_new][y_new] = 'X'
            board[x][y] = '.'
            return [x_new + 1, y_new + 1]
    else:
        return [-1, -1]


def move_up(board):
    [x, y] = find_star_in_board(board)
    return replace_in_board(board, x, y, x - 1, y)


def move_down(board):
    [x, y] = find_star_in_board(board)
    return replace_in_board(board, x, y, x + 1, y)


def move_left(board):
    [x, y] = find_star_in_board(board)
    return replace_in_board(board, x, y, x, y - 1)


def move_right(board):
    [x, y] = find_star_in_board(board)
    return replace_in_board(board, x, y, x, y + 1)


def move(board, direction):
    if direction == 'U':
        return move_up(board)
    elif direction == 'L':
        return move_left(board)
    elif direction == 'R':
        return move_right(board)
    elif direction == 'D':
        return move_down(board)


def main():
    board1 = [['.', '.', '.', '.', 'W'],
              ['.', '.', '.', '.', '.'],
              ['.', 'F', '.', 'F', '.'],
              ['.', '*', 'O', '.', 'W'],
              ['.', '.', 'O', '.', '.']]
    print(board1)
    print(move(board1, 'R'))
    print(board1)


if __name__ == '__main__':
    main()

\color{red}\underline{Output:}

Add a comment
Know the answer?
Add Answer to:
Python programming Part VI: Move Starman by Movement Commands (20 points) Write a function move ()...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT