Skip to content

Commit 0e3b6c8

Browse files
authored
Added messages to all asserts in the platform. (#126)
* Added messages to all asserts in the platform. --------- Co-authored-by: Michael Cochez <[email protected]>
1 parent ca5c8c8 commit 0e3b6c8

9 files changed

+23
-23
lines changed

src/schnapsen/bots/alphabeta.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def value(
118118
beta = min(beta, best_value) # alphabeta pruning
119119
if beta <= alpha:
120120
break
121-
assert best_move # We are sure the best_move can no longer be None. We assert to make sure we did not make a logical mistake
121+
assert best_move, "We are sure the best_move can no longer be None" # We assert to make sure we did not make a logical mistake
122122
return best_value, best_move
123123

124124

src/schnapsen/bots/example_bot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def get_move(self, perspective: PlayerPerspective, leader_move: Optional[Move])
4949
elif one_move.is_trump_exchange():
5050
exchange = one_move
5151
jack: Card = exchange.cards[0]
52-
assert jack.rank == Rank.JACK
52+
assert jack.rank == Rank.JACK, f"The rank of the card in a trump exchange must always be a {Rank.JACK}"
5353
else:
5454
normal_move = one_move
5555
card: Card = normal_move.cards[0]

src/schnapsen/bots/gui/guibot.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def _get_move(self, botname: str, perspective: PlayerPerspective, leader_move: O
9393
# we now wait for the browser to make the move
9494
state_exchange.is_move_ready.wait()
9595
move = state_exchange.browser_move
96-
assert move is not None
96+
assert move is not None, "Browser move must not be None if a move has been exchanged"
9797
return move
9898

9999
def __sendmove(self, botname: str) -> str:
@@ -195,11 +195,11 @@ def convert_move(old_move: tuple[Optional[int], Optional[int]]) -> Move:
195195
if not old_move[0]:
196196
assert old_move[1] is not None, "In the old endinge, all moves with the first part not set must be Trump exchanges"
197197
return TrumpExchange(_Old_GUI_Compatibility.old_engine_order[old_move[1]])
198-
assert old_move[0] and old_move[1]
198+
assert old_move[0] and old_move[1], "If it is not a regular move, and not an exchange, it must be a marriage, so expect both cards to be set"
199199
if _Old_GUI_Compatibility.old_engine_order[old_move[0]].rank == Rank.KING:
200200
# swap
201201
old_move = (old_move[1], old_move[0])
202-
assert old_move[0] is not None and old_move[1] is not None
202+
assert old_move[0] is not None and old_move[1] is not None, "After potential swap, if it is not a regular move, and not an exchange, it must be a marriage, so expect both cards to be set"
203203
return Marriage(queen_card=_Old_GUI_Compatibility.old_engine_order[old_move[0]], king_card=_Old_GUI_Compatibility.old_engine_order[old_move[1]])
204204

205205
@staticmethod
@@ -230,7 +230,7 @@ def player_game_state_to_json(perspective: PlayerPerspective, leader_move: Optio
230230
if leader_move.is_regular_move():
231231
partial_move_down = leader_move.as_regular_move().card
232232
else:
233-
assert leader_move.is_marriage()
233+
assert leader_move.is_marriage(), "Expected the move to be a marriage at this point."
234234
partial_move_down = leader_move.as_marriage().underlying_regular_move().card
235235
else:
236236
partial_move_down = None
@@ -271,8 +271,8 @@ def player_game_state_to_json(perspective: PlayerPerspective, leader_move: Optio
271271
else:
272272
this_card_state = 'P2H'
273273

274-
assert this_card_state
275-
assert this_p1_perspective
274+
assert this_card_state, "Whatever happened, the state for this card must be set in one of the branches."
275+
assert this_p1_perspective, "Whatever happened, the perspective information for this card must be set in one of the branches."
276276

277277
card_state[index] = this_card_state
278278
p1_perspective[index] = this_p1_perspective

src/schnapsen/bots/minimax.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def value(
116116
elif not maximizing and value < best_value:
117117
best_move = move
118118
best_value = value
119-
assert best_move # We are sure the best_move can no longer be None. We assert to make sure we did not make a logical mistake
119+
assert best_move, "We are sure the best_move can no longer be None." # We assert to make sure we did not make a logical mistake
120120
return best_value, best_move
121121

122122

src/schnapsen/bots/ml_bot.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ def get_move(self, perspective: PlayerPerspective, leader_move: Optional[Move])
5353
model_output = self.__model.predict_proba(action_state_representations)
5454
winning_probabilities_of_moves = [outcome_prob[1] for outcome_prob in model_output]
5555
highest_value: float = -1
56-
best_move: Move
56+
best_move = None
5757
for index, value in enumerate(winning_probabilities_of_moves):
5858
if value > highest_value:
5959
highest_value = value
6060
best_move = my_valid_moves[index]
61-
assert best_move is not None
61+
assert best_move is not None, "We went over all the moves, selecting the one we expect to lead to the highest average score. Simce there must have been at least one move at the start, this can never be None"
6262
return best_move
6363

6464

src/schnapsen/bots/rdeep.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def get_move(self, perspective: PlayerPerspective, leader_move: Optional[Move])
4444
if average_score > best_score:
4545
best_score = average_score
4646
best_move = move
47-
assert best_move is not None
47+
assert best_move is not None, "We went over all the moves, selecting the one we expect to lead to the highest average score. Simce there must have been at least one move at the start, this can never be None"
4848
return best_move
4949

5050
def __evaluate(self, gamestate: GameState, engine: GamePlayEngine, leader_move: Optional[Move], my_move: Move) -> float:

src/schnapsen/deck.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def filter_suit(self, suit: Suit) -> list[Card]:
316316
:return: (list[Card]): An Iterable of cards with the provided suit.
317317
"""
318318

319-
assert suit in Suit
319+
assert suit in Suit, f"The provided suit {suit} is not a valid {Suit} "
320320
results: list[Card] = [card for card in self._cards if card.suit is suit]
321321
return results
322322

@@ -327,7 +327,7 @@ def filter_rank(self, rank: Rank) -> list[Card]:
327327
:param rank: (Rank): The rank to filter on.
328328
:return: (list[Card]): An Iterable of cards with the provided rank.
329329
"""
330-
assert rank in Rank
330+
assert rank in Rank, f"The provided rank {rank} is not a valid {Rank} "
331331
results: list[Card] = [card for card in self._cards if card.rank is rank]
332332
return results
333333

tests/bots/test_minimax_alphabeta_bot.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ def test_second_phase(self) -> None:
120120
randbot = RandBot(random.Random(j))
121121
if outcome[0] == minimaxA:
122122
outcome2 = engine.play_game_from_state_with_new_bots(state, new_leader=minimaxA, new_follower=randbot, leader_move=None)
123-
assert outcome2[0] == minimaxA
123+
assert outcome2[0] == minimaxA, "expected minimax to win from random in a sitatuon that is winnable"
124124
else:
125125
# minimaxB won
126126
outcome2 = engine.play_game_from_state_with_new_bots(state, new_leader=randbot, new_follower=minimaxB, leader_move=None)
127-
assert outcome2[0] == minimaxB
127+
assert outcome2[0] == minimaxB, "expected minimax to win from random in a sitatuon that is winnable"
128128

129129

130130
class MiniMaxBotAlphaBetaPhaseTwoEasy(TestCase):

tests/test_schnapsen_implementation.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ def test_dealing(self) -> None:
2727
self.assertEqual(len(hand2), 5, "Hand 2 must contain 5 cards")
2828
self.assertEqual(len(rest), 10, "There must be 10 cards left after dealing")
2929
for i in [0, 2, 4, 6, 8]:
30-
assert cards[i] in hand1
31-
assert cards[i + 1] in hand2
32-
assert cards[i] not in rest
33-
assert cards[i + 1] not in rest
30+
assert cards[i] in hand1, f"card {cards[i]} expected to be in hand 1 {hand1} after dealing from {shuffled_deck}"
31+
assert cards[i + 1] in hand2, f"card {cards[i + 1]} expected to be in hand 2 {hand2} after dealing from {shuffled_deck}"
32+
assert cards[i] not in rest, f"card {cards[i]} not expected to be in the rest {rest} after dealing from {shuffled_deck}"
33+
assert cards[i + 1] not in rest, f"card {cards[i]} not expected to be in the rest {rest} after dealing from {shuffled_deck}"
3434
for i in range(10, 20):
35-
assert cards[i] in rest
36-
assert cards[i] not in hand1
37-
assert cards[i] not in hand2
35+
assert cards[i] in rest, f"card {cards[i]} expected to be in the rest {rest} after dealing from {shuffled_deck}"
36+
assert cards[i] not in hand1, f"card {cards[i]} not expected to be in the hand1 {hand1} after dealing from {shuffled_deck}"
37+
assert cards[i] not in hand2, f"card {cards[i]} not expected to be in the hand2 {hand2} after dealing from {shuffled_deck}"

0 commit comments

Comments
 (0)