def print_board(board, robot_a, robot_b):
print('========================')
line += 'A' + robot_a['direction']
line += 'B' + robot_b['direction']
def print_lives(lives_left):
print('Lives left: A - ' + str(lives_left['A']) + ', '
+ 'B' + ' - ' + str(lives_left['B']))
moves = ['>', 'v', '<', '^', '>']
new_move_index = moves.index(robot['direction']) + 1
robot['direction'] = moves[new_move_index]
def move_forward(board, robot):
current_row, current_column = get_position(board, robot)
new_row, new_column = get_new_position(robot, (current_row, current_column))
if is_within_board(board, new_row, new_column) and is_empty(board, new_row, new_column):
board[current_row][current_column] = '-'
board[new_row][new_column] = robot['name']
def laser_shoot(board, robot, lives_left):
if robot['direction'] == '>' or robot['direction'] == '<':
laser_horizontal_shoot(board, robot, lives_left)
laser_vertical_shoot(board, robot, lives_left)
def get_position(board, robot):
for i, row in enumerate(board):
for j, cell in enumerate(row):
if cell == robot['name']:
def get_new_position(robot, current_position):
row, column = current_position
if robot['direction'] == '>':
elif robot['direction'] == 'v':
elif robot['direction'] == '<':
elif robot['direction'] == '^':
def is_empty(board, row, column):
return board[row][column] == '-'
def is_within_board(board, row, column):
board_height = len(board)
board_width = len(board[0])
return 0 <= row < board_height and 0 <= column < board_width
def laser_horizontal_shoot(board, robot, lives_left):
update_range_start, update_range_end = get_update_range_start_end(board, robot)
robot_row = get_position(board, robot)[0]
for i in range(update_range_start, update_range_end):
if board[robot_row][i] == get_opponent_name(robot):
lives_left[get_opponent_name(robot)] -= 1
board[robot_row][i] = '*'
def laser_vertical_shoot(board, robot, lives_left):
update_range_start, update_range_end = get_update_range_start_end(board, robot)
robot_column = get_position(board, robot)[1]
for i in range(update_range_start, update_range_end):
if board[i][robot_column] == get_opponent_name(robot):
lives_left[get_opponent_name(robot)] -= 1
board[i][robot_column] = '*'
def get_update_range_start_end(board, robot):
robot_row, robot_column = get_position(board, robot)
board_height = len(board)
board_width = len(board[0])
if robot['direction'] == '>':
return (robot_column + 1, board_width)
elif robot['direction'] == '<':
elif robot['direction'] == '^':
elif robot['direction'] == 'v':
return (robot_row + 1, board_height)
def get_opponent_name(robot):
for i in range(0, len(board)):
for j in range(0, len(board[i])):
def play_round(board, robot_a, robot_b, lives_left):
act_robot(board, robot_a, lives_left)
act_robot(board, robot_b, lives_left)
print_board(board, robot_a, robot_b)
def act_robot(board, robot, lives_left):
action = random.randint(0, 2)
laser_shoot(board, robot, lives_left)
move_forward(board, robot)
robot_a = {'name': 'A', 'direction': '>'}
robot_b = {'name': 'B', 'direction': '<'}
lives_left = {'A': 2, 'B': 2}
print_board(board, robot_a, robot_b)
while lives_left['A'] > 0 and lives_left['B'] > 0 and rounds_left > 0:
play_round(board, robot_a, robot_b, lives_left)
if (lives_left['A'] == 0 and lives_left['B'] > 0):
print('*** Robot B won! ***')
elif (lives_left['A'] > 0 and lives_left['B'] == 0):
print('*** Robot A won! ***')