Creating an Agent
Creating an agent is as simple as subclassing the BaseAgent class. The most important part is to override the
get_action()
function, which is where you obtain the game state object. The return value of the function will be the
next action that gets executed by the dcss-ai-wrapper API in the current game of DCSS that the agent is playing.
1from dcss.agent.base import BaseAgent
2from dcss.state.game import GameState
3from dcss.actions.action import Action
4
5import random
6
7
8import time
9
10
11
12class MyAgent(BaseAgent):
13
14 def __init__(self):
15 super().__init__()
16 self.gamestate = None
17 self.start_time = time.time()
18 self.num_actions_sent = 0
And then to run the agent, you need to create a WebSockGame object with the agent class, like this:
1from dcss.websockgame import WebSockGame
2from dcss.connection.config import WebserverConfig
3
4 self.gamestate = gamestate
5 # get all possible actions
6 actions = Action.get_all_move_commands()
7 # pick an action at random
8 self.num_actions_sent +=1
9 print("Actions per second = {}".format(self.num_actions_sent / (time.time() - self.start_time)))
10 return random.choice(actions)
11
12
13if __name__ == "__main__":
The full example can be found in src/dcss/agent/randomagent.py:
1from dcss.agent.base import BaseAgent
2from dcss.state.game import GameState
3from dcss.actions.action import Action
4
5from dcss.websockgame import WebSockGame
6from dcss.connection.config import WebserverConfig
7
8import random
9
10from loguru import logger
11
12import time
13
14
15
16class MyAgent(BaseAgent):
17
18 def __init__(self):
19 super().__init__()
20 self.gamestate = None
21 self.start_time = time.time()
22 self.num_actions_sent = 0
23
24 def get_action(self, gamestate: GameState):
25 self.gamestate = gamestate
26 # get all possible actions
27 actions = Action.get_all_move_commands()
28 # pick an action at random
29 self.num_actions_sent +=1
30 print("Actions per second = {}".format(self.num_actions_sent / (time.time() - self.start_time)))
31 return random.choice(actions)
32
33
34if __name__ == "__main__":
Moving On
Next we look at the state representations available from the GameState
object.