gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
用python实现角色向四周移动,并且不走重复路
- User
用python实现角色向四周移动,并且不走重复路
- Assistant
以下是一个简单的示例代码,实现了角色向四周移动,并且不重复走同一路。
pythondef move(board, direction, row, col): new_row, new_col = row, col if direction == 'up': new_row -= 1 elif direction == 'down': new_row += 1 elif direction == 'left': new_col -= 1 elif direction == 'right': new_col += 1 if 0 <= new_row < len(board) and 0 <= new_col < len(board[0]) and board[new_row][new_col] == 0: row, col = new_row, new_col return row, col def main(): board = [[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0]] row, col = 2, 2 while True: print("当前位置:({}, {})".format(col, row)) direction = input("请输入移动方向(up/down/left/right):") row, col = move(board, direction, row, col) if row == 0 and col == 0: print("成功到达目标位置!") break