pelita
pelita copied to clipboard
Modifying bot.legal_positions breaks code reusability
(Not sure whether this is a bug or more of a sticky issue to keep in mind.)
Thinking about the suggestion in #789 I found a problem in modifying bot.legal_positions
that subtly breaks when used in code switching scenarios.
# scenario: team was divided in two subgroups working on different strategies
# we try them both and then decide what is best for us
def move(bot, state):
import team_a, team_b
if state == {}:
state[0] = init_state()
state[1] = init_state()
# evaluate strategy of working group a
state_copy = deepcopy(state)
next_pos_a = team_a.move(bot, state_copy)
# evaluate strategy of working group b
state_copy = deepcopy(state)
next_pos_b = team_b.move(bot, state_copy)
# heuristic that decides which of the moves is better
return better_move(bot, next_pos_a, next_pos_b)
Assuming team_a.move
consumes bot.legal_positions
, this will alter the behaviour of the team_b.move
function. This is kind of what you’d expect Python to do, so in itself it is not surprising (and it could be what you want to do), but it does break our ‘promise’ of ‘move function reuse’ that we advertise in the tutorial. Meaning that we really should be careful when using either
bot.legal_positions.pop(xy)
or even
legal_pos = bot.legal_positions
...
legal_pos.pop(xy)
in our demo code. (Any of our example code will make it verbatim to at least one user’s bot.)
I don’t really know what to do about this (making bot.legal_positions
an explicit function would be the most obvious fix) but this also means that #789 could have broken some earlier bots so we should keep this issue in mind.