Skip to content

Conversation

marco
Copy link
Member

@marco marco commented Dec 7, 2017

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.

(Done for the "Create your own Chess bot." Google Code-in task.)


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>```
Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Member

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. 😃

Copy link
Member Author

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.

@showell
Copy link
Contributor

showell commented Dec 7, 2017

@skunkmb I'm excited about this! I made a couple initial comments.

@marco marco force-pushed the create-chess-bot branch 3 times, most recently from c49cb7e to 91d44e2 Compare December 7, 2017 05:25
@aero31aero
Copy link
Member

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. :)

@marco
Copy link
Member Author

marco commented Dec 7, 2017

@aero31aero OK, will do in the future. Thanks!

Copy link
Member

@tommyip tommyip left a 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 (
Copy link
Member

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.

Copy link
Member Author

@marco marco Dec 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tommyip OK, I simplified it to an if statement in d91b679.

bot_handler.storage.contains('is_with_computer')
) else False

last_fen = bot_handler.storage.get('last_fen') if (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save as above

Copy link
Member Author

@marco marco Dec 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tommyip OK, I simplified it to an if statement in d91b679.

bot_handler,
True if start_computer_regex_match.group(
'user_color'
) == 'white' else False
Copy link
Member

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?

Copy link
Member Author

@marco marco Dec 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tommyip OK, I changed that in d91b679. Thanks!

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'
Copy link
Member

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)

Copy link
Member Author

@marco marco Dec 7, 2017

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!

Copy link
Member

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.

Copy link
Member

@tommyip tommyip Dec 7, 2017

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tommyip Oh, I understand now! 😃 Thanks! I fixed that in commit 4a31674.

Replies to the bot handler.

Parameters:
- message (object): The Zulip Bots message object.
Copy link
Member

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)

Copy link
Member Author

@marco marco Dec 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tommyip OK, I added the type annotations in 95437b0 and 0e159f7. Thanks!

@aero31aero
Copy link
Member

Hey, not a complete review here, just a minor comment.

You need to add the files to tools/run-mypy's force_include list for mypy to validate the type annotations. When you do that, you can run tools/test-static-analysis to run mypy.

@marco
Copy link
Member Author

marco commented Dec 8, 2017

@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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, test_chess.py?

Copy link
Member Author

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:
Copy link
Member

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?

Copy link
Member

@aero31aero aero31aero left a 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.

@zulipbot
Copy link
Member

zulipbot commented Dec 8, 2017

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 upstream/master branch and resolve your pull request's merge conflicts accordingly.

@showell
Copy link
Contributor

showell commented Dec 9, 2017

@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 😄

@marco marco force-pushed the create-chess-bot branch 2 times, most recently from 8da4c59 to 307a1ac Compare December 9, 2017 03:26
@marco
Copy link
Member Author

marco commented Dec 9, 2017

@showell Squashed! 😃 It's running now on chat.zulip.org as @chess-bot, for example in this topic. Thank you!

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.
@marco
Copy link
Member Author

marco commented Dec 9, 2017

There was a merge conflict, so I rebased the commit and pushed it. Other than that it's the same as before the rebase.

@tommyip
Copy link
Member

tommyip commented Dec 9, 2017

FYI I approved the GCI task. @skunkmb can you follow with this until it is merged.

@marco
Copy link
Member Author

marco commented Dec 9, 2017

@tommyip Thank you! Yes, I'll definitely follow this. 😃

@showell
Copy link
Contributor

showell commented Dec 10, 2017

I am working on getting this merged. I am converting the tests to use StubBotTestCase, and we need to handle an empty message properly.

@showell
Copy link
Contributor

showell commented Dec 10, 2017

Ok, merged as 700ce6a

Thanks @skunkmb!

@showell showell closed this Dec 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants