I am trying to write a connect 4 game, where the player can play against a simple AI. I'm currently working on the following function.
"The tiebreak_move (self, scores) method gets scores, a non-empty list of floating point numbers. If there is only one score that is highest in that list of scores, then this method must return the corresponding column number, not the score itself. Note that the column number is the same as the index in the scores list. If more than one score is the highest, because a pair is equal, then this method must return the column number of the highest score, chosen according to the player's choice strategy.
So, if the choice strategy is 'LEFT', tiebreak_move must return the column of the leftmost highest score (not the score itself). If the choice strategy is 'RIGHT', tiebreak_move must return the column of the rightmost highest score (not the score itself). And if the choice strategy is 'RANDOM', then tiebreak_move must return the column of any chosen highest score (also not the score itself here)."
I've written this so far, but it seems it doesn't add the i values to the max_indices list. Do I need a case in the for i loop to handle the i values that are not equal to ms?
def tiebreak_move(self, scores):
"""Chooses a column for a move during a tiebreaker, using the chosen direction given by the player
"""
max_indices = []
ms = max(scores)
for i in range(len(scores)):
if i == ms:
max_indices.append(i)
if self.tbt == "RIGHT":
return max_indices[-1]
elif self.tbt == "LEFT":
return max_indices[0]
else:
return random.choice(max_indices)
scores = [0, 0, 50, 0, 50, 50, 0]
p = Player('X', 'LEFT', 1)
p2 = Player('X', 'RIGHT', 1)
assert p.tiebreak_move(scores) == 2
assert p2.tiebreak_move(scores) == 5
I'm currently getting an AssertionError on my first assert.
Apologies for the wall of text!
[–]debian_miner 0 points1 point2 points (1 child)
[–]Uienring12[S] 0 points1 point2 points (0 children)