Skip to content

How to create a new bot and play with it?

christyesmee edited this page Jan 2, 2024 · 1 revision

Creating a new bot and playing a game with that bot, requires some adjustments in the code that you cloned from the GitHub repo 'schnapsen'.

1. Create the new bot

First, you have to create a new python file with the rules you want your bot to follow.

Let's say you created a new python file for your new bot called RandBot2. See the code below of this code (it is a copied version of the original Random bot). I have changed the name of the class of the bot into RandBot2 (initially it was RandBot). Also, I changed the name of the bot from rand to rand2.

import random
from typing import Optional
from schnapsen.game import Bot, PlayerPerspective, Move


class RandBot2(Bot):
    def __init__(self, rand2: random.Random, name: Optional[str] = None) -> None:
        super().__init__(name)
        self.rng = rand2

    def get_move(
        self,
        perspective: PlayerPerspective,
        leader_move: Optional[Move],
    ) -> Move:
        moves: list[Move] = perspective.valid_moves()
        move = self.rng.choice(moves)
        return move

2. Import your new class

Great! Now to use your newly created bot, it needs to be imported into the __init.py__ and either the cli.py or server.py files. You can think of this like connecting a device to a power source; it's how your bot gets integrated and becomes operational within the larger application. Follow these steps to ensure a successful import:

2.1 __init__.py

The __init__.py file in the schnapsen\bots directory acts as an initializer for the package. It should correctly reference the file where RandBot2 is defined.

from .rand import RandBot
from .random2 import RandBot2
from .alphabeta import AlphaBetaBot
from .rdeep import RdeepBot
from .ml_bot import MLDataBot, MLPlayingBot, train_ML_model
from .gui.guibot import SchnapsenServer
from .minimax import MiniMaxBot

__all__ = ["RandBot", "RandBot2", "AlphaBetaBot", "RdeepBot", "MLDataBot", "MLPlayingBot", "train_ML_model", "SchnapsenServer", "MiniMaxBot"]

2.2 cli.py

At the top of the file, add an import statement for your new class

  • For example, if your class is named RandBot2 and is located in a file named random2.py, the import statement would be:
from schnapsen.bots import MLDataBot, train_ML_model, MLPlayingBot, RandBot, RandBot2

2.3 server.py

In the server.py file you have to import your newly created bot as well. So, add RandBot2 to the line of from schnapsen.bots import.

If you don't want to use the GUI bot for bot1, ensure the line # bot1 = s.make_gui_bot(name="mybot1") remains commented out. If you wish to use a GUI bot for bot1, uncomment this line and comment out the bot1 = RandBot2(random.Random(12)) line.

import random
from schnapsen.bots import SchnapsenServer
from schnapsen.bots import RandBot, RandBot2
from schnapsen.game import SchnapsenGamePlayEngine


if __name__ == "__main__":
    engine = SchnapsenGamePlayEngine()
    with SchnapsenServer() as s:
        bot1 = RandBot2(random.Random(12))
        # bot1 = s.make_gui_bot(name="mybot1")
        bot2 = s.make_gui_bot(name="mybot2")
        engine.play_game(bot1, bot2, random.Random(100))