Skip to content

Commit 643fa4e

Browse files
authored
Added the bully bot to the set of provided bots. (#124)
* Added the bully bot to the set of provided bots. --------- Co-authored-by: Michael Cochez <[email protected]>
1 parent 0e3b6c8 commit 643fa4e

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/schnapsen/bots/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
from .ml_bot import MLDataBot, MLPlayingBot, train_ML_model
88
from .gui.guibot import SchnapsenServer
99
from .minimax import MiniMaxBot
10+
from .bully_bot import BullyBot
1011

11-
__all__ = ["RandBot", "AlphaBetaBot", "RdeepBot", "MLDataBot", "MLPlayingBot", "train_ML_model", "SchnapsenServer", "MiniMaxBot"]
12+
__all__ = ["RandBot", "AlphaBetaBot", "RdeepBot", "MLDataBot", "MLPlayingBot", "train_ML_model", "SchnapsenServer", "MiniMaxBot", "BullyBot"]

src/schnapsen/bots/bully_bot.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import math
2+
import random
3+
from typing import Optional
4+
from schnapsen.game import Bot, PlayerPerspective, Move, RegularMove
5+
6+
7+
class BullyBot(Bot):
8+
"""This bot plays plays as follows (deterministically using the random number generator provided).
9+
The bully bot only plays valid moves.
10+
If the bot has cards of the trump suit, it plays one of them at random
11+
Else, if the bot is follower and has cards of the same suit as the opponent, play one of these at random.
12+
Else, randomly play one of its cards with the highest score
13+
Args:
14+
rand (random.Random): The random number generator used to make the random choice of cards
15+
name (Optional[str]): The optional name of this bot
16+
"""
17+
18+
def __init__(self, rand: random.Random, name: Optional[str] = None) -> None:
19+
super().__init__(name)
20+
self.rng = rand
21+
22+
def get_move(
23+
self,
24+
perspective: PlayerPerspective,
25+
leader_move: Optional[Move],
26+
) -> Move:
27+
28+
valid_regular_moves: list[RegularMove] = [move.as_regular_move() for move in perspective.valid_moves() if move.is_regular_move()]
29+
30+
# If the bot has cards of the trump suit, it plays one of them at random
31+
trump_suit = perspective.get_trump_suit()
32+
trumps: list[RegularMove] = [move for move in valid_regular_moves if move.card.suit == trump_suit]
33+
if trumps:
34+
return self.rng.choice(trumps)
35+
36+
# Else, if the bot is follower and has cards of the same suit as the opponent, play one of these at random.
37+
if leader_move is not None:
38+
leader_suit = leader_move.cards[0].suit
39+
same_suit_moves = [move for move in valid_regular_moves if move.card.suit == leader_suit]
40+
if same_suit_moves:
41+
return self.rng.choice(same_suit_moves)
42+
43+
# Else, randomly play one of its cards with the highest score
44+
scorer = perspective.get_engine().trick_scorer
45+
46+
highest_score_till_now = -math.inf
47+
highest_moves_till_now = []
48+
for move in valid_regular_moves:
49+
score = scorer.rank_to_points(move.card.rank)
50+
if score > highest_score_till_now:
51+
highest_score_till_now = score
52+
highest_moves_till_now = [move]
53+
elif score == highest_score_till_now:
54+
highest_moves_till_now.append(move)
55+
else:
56+
pass # not scoring higher, so ignored
57+
return self.rng.choice(highest_moves_till_now)

0 commit comments

Comments
 (0)