fcarsten / tic-tac-toe

Teaching a machine to play tic-tac-toe
Apache License 2.0
55 stars 37 forks source link

Board.py -> WIN_CHECK_DIRS #1

Closed PaulBunkerWhich closed 5 years ago

PaulBunkerWhich commented 5 years ago

Hi,

WIN_CHECK_DIRS is confusing to me could you provide some explanation to what is going on with this constant please?

Thanks

fcarsten commented 5 years ago

Hi Paul,

this constant is used to speed up the check of whether one of the player has won the game.

Instead of iterating through all positions on the board and testing if there are three in a row from that position, we only use a subset of positions and directions that cover all possible cases.

Ie from the top left position we search to the right, down, and diagonally down-right, from the top middle we only search down, from the top right we only search down and diagonally down-left, etc. Some positions we can skip completely, like the the middle of the board as they re covered by the other cases.

The constant encodes for each board-positions the directions we search for a winning three-in-a-row.

PaulBunkerWhich commented 5 years ago

Thanks for the clarification.

I see that in apply_direction you move across one column and along the 1d vector to the next row if the direction is (1,1) from the starting square. Took a minute for me to get my head around it, at first I was thinking 1,1 would be up and right but because the board is printed top to bottom I see now it's down but a positive number because we move along the board in 1d.

Thanks for the articles too I'm working my way through them.