maxpumperla / deep_learning_and_the_game_of_go

Code and other material for the book "Deep Learning and the Game of Go"
https://www.manning.com/books/deep-learning-and-the-game-of-go
987 stars 390 forks source link

How to pick which player goes first? (in human vs. bot) #103

Open phdsmhong opened 2 years ago

phdsmhong commented 2 years ago

I launched my deep learning model with the frontend play_predict_19.html (on page 184 in chapter 8) by updating the codes in chapter 7. This book is truly awesome. Thank you! :)

The next task is to allow the code to choose which player goes first in human vs. bot setting. Currently, human always goes first (chooses black). At first, I thought it was an easy task, but I realize it is more difficult than I thought. I changed the Player class in gotypes.py as follows, but it does not work. Still, human always goes first. Can anyone please advise how to fix it? Thank you in advance!

class Player(enum.Enum):

    if random.randint(0, 1) == 0:
        black = 1
        white = 2
    else:
        white = 1
        black = 2

    @property
    def other(self):
        return Player.black if self == Player.white else Player.white
phdsmhong commented 2 years ago

I realize that in the "play_predict_19.html" file, it is coded such that human always goes first (presumably with javascript and html). But, I am puzzled that it is difficult to implement "which player goes first" feature even in python. Any advice would be greatly appreciated.

macfergus commented 2 years ago

Hi @phdsmhong, if you want the bot to play as black and make the first move, I believe you can do this entirely in the javascript side. The server assumes black goes first, but will happily choose a move for whoever has the next turn, black or white.

Take a look here: https://github.com/maxpumperla/deep_learning_and_the_game_of_go/blob/chapter_7/code/dlgo/httpfrontend/static/play_predict_19.html#L188

In the current implementation, the JS waits for the human player to click, then applies the human move, then asks the server for the bot move

The fetch to /select-move/predict is where we ask the bot for its move: https://github.com/maxpumperla/deep_learning_and_the_game_of_go/blob/chapter_7/code/dlgo/httpfrontend/static/play_predict_19.html#L214

If you don't mind a little code duplication, you can request the first bot move immediately after creating the board -- copy that fetch block to above line 188, so it makes the first request before waiting for the human to move

You will also need to update a few other places in the JS where black and white are hardcoded

Hope this helps!

phdsmhong commented 2 years ago

@macfergus, I cannot thank you enough! This is extremely helpful. It seems that I need to study Javascript from now on. Do you think I do not need to change any python codes?

macfergus commented 2 years ago

Yes, I am pretty sure you won't need to change the Python code to make this work. You can test this by directly making a request to your server. While your server is running, try this in another Python repl:

>>> import requests
>>> response = requests.post(
  'http://localhost:5000/select-move/predict',
  json={"board_size": 19, "moves": []}
)
>>> response.json()
{'bot_move': 'Q16', 'diagnostics': {}}

You can see there that the server will select the first move (in this case, it opens with Q16). So, I'm pretty sure all the needed changes will be on the web side