-
-
Notifications
You must be signed in to change notification settings - Fork 392
bots: Create Chess Bot. #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
312dc7d
to
ffa54b3
Compare
|
||
To make your next move, respond to Chess Bot with | ||
|
||
```rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1: do <your move>``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like it would be easier to communicate this using examples. Chess players are more likely to know chess notation than being able to parse out what you're saying here, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'll add an example. Thanks! The reason the beginning part (rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
) is needed is because the bot can't carry over data from earlier messages, so instead the board information has to be sent along with the move.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @skunkmb! We do support saving the data. I am in a bit of a hurry so cannot link to exact code samples but take a look at the virtual_fs
bot and try running it once as well.
We do not need to send the data with each move and can use our bot handler storage. 😃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@showell @aero31aero I added to my commit, and now the match data is saved between responses. I didn't know you could do that 😃 ! Thanks!
I think this makes it a lot more intuitive for the user to understand what they're supposed to do. Now the user doesn't have to have all of the match data at the front of their response.
@skunkmb I'm excited about this! I made a couple initial comments. |
c49cb7e
to
91d44e2
Compare
Looks great at a glance @skunkmb! Just a minor request. I'm sure this PR would be going through some further changes before being merged, so can you please add new commits to this instead of amending the previous commit? That would make it easier for you and the reviewers to see at a glance what was changed from a past version. We'll just squash the commits finally before merging. :) |
@aero31aero OK, will do in the future. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome work! Just some syntactical changes needed.
move_regex_match = re.match(MOVE_REGEX, content) | ||
resign_regex_match = re.match(RESIGN_REGEX, content) | ||
|
||
is_with_computer = bot_handler.storage.get('is_with_computer') if ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually prefer to use ternary operators only for single line assignments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bot_handler.storage.contains('is_with_computer') | ||
) else False | ||
|
||
last_fen = bot_handler.storage.get('last_fen') if ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Save as above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bot_handler, | ||
True if start_computer_regex_match.group( | ||
'user_color' | ||
) == 'white' else False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just start_computer_regex_match.group('user_color') == white
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def handle_message(self, message, bot_handler): | ||
START_REGEX = 'start with other user$' | ||
START_COMPUTER_REGEX = ( | ||
'start as (?P<user_color>white|black) with computer' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that it is that important, but we should avoid compiling regex in loops. (Might become more important later on when we decide to run bots directly on the chat server)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tommyip OK. How should I go about compiling the RegEx outside of the loop? Or is it not something I should worry about for now? Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just re.compile it in the global namespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the loop I just mean the handle_message function, since that is called on every message it receives.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replies to the bot handler. | ||
|
||
Parameters: | ||
- message (object): The Zulip Bots message object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We uses Python 3 inline type annotation. (Check out http://mypy.readthedocs.io/en/stable/type_inference_and_annotations.html)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, not a complete review here, just a minor comment. You need to add the files to |
b85aa04
to
02747f3
Compare
@aero31aero OK, I added it. I also had to make a few changes in the annotations in order for it to pass. Thanks! |
tools/run-mypy
Outdated
@@ -46,6 +46,7 @@ force_include = [ | |||
"zulip_bots/zulip_bots/bots/googlesearch/test_googlesearch.py", | |||
"zulip_bots/zulip_bots/bots/help/help.py", | |||
"zulip_bots/zulip_bots/bots/help/test_help.py", | |||
"zulip_bots/zulip_bots/bots/chess/chess.py", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, test_chess.py
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, added in in e0ffeef. Thanks!
@@ -22,7 +23,7 @@ def usage(self): | |||
'Stockfish program on this computer.' | |||
) | |||
|
|||
def initialize(self, bot_handler): | |||
def initialize(self, bot_handler: ExternalBotHandler) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should further apply this style to other bots as well. This is certainly loads better than : Any
. @neiljp is this what you were recommending?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM after a code read. Albeit 700 LoC is a lot to go through so I might have missed something. @neiljp can you take a look as well?
i believe if we find bugs we can do incremental and small fixes later.
Heads up @skunkmb, we just merged some commits that conflict with the changes your made in this pull request! You can review this repository's recent commits to see where the conflicts occur. Please rebase your feature branch against the |
02747f3
to
e0ffeef
Compare
@skunkmb Go ahead and squash this into a single commit, and we'll merge it. We'll want to showcase it on chat.zulip.org by having you run it off of your laptop pointing at chat.zulip.org. I agree with @aero31aero that we may have some fix-forward work to do, but that's fine. WE HAVE A CHESS BOT! hahah 😄 |
8da4c59
to
307a1ac
Compare
@showell Squashed! 😃 It's running now on chat.zulip.org as |
Chess Bot is a bot that allows you to play chess against either another user or the computer. Use `start with other user` or `start as <color> with computer` to start a game. In order to play against a computer, `chess.conf` must be set with the key `stockfish_location` set to the location of the Stockfish program on this computer. Use `bot_handler.storage` to preserve game state across messages.
307a1ac
to
9811c1a
Compare
There was a merge conflict, so I rebased the commit and pushed it. Other than that it's the same as before the rebase. |
FYI I approved the GCI task. @skunkmb can you follow with this until it is merged. |
@tommyip Thank you! Yes, I'll definitely follow this. 😃 |
I am working on getting this merged. I am converting the tests to use StubBotTestCase, and we need to handle an empty message properly. |
Chess Bot is a bot that allows you to play chess against either another user or the computer. Use
start with other user
orstart as <color> with computer
to start a game.In order to play against a computer,
chess.conf
must be set with the keystockfish_location
set to the location of the Stockfish program on this computer.(Done for the "Create your own Chess bot." Google Code-in task.)