# The fastest way to check if a move is a winning move in Tic Tac Toe

I'm creating a tic tac toe game, there will be two players: X and O. X will be a human, and O is an AI which will always choose the best move to play. My board is an 11x11 board and the winning condition is 5 in a row.

How do I know if a board is at end state (where one player has won)? For the 3x3 board, you can do it easily with just a few steps. But for the larger board (11x11) and the winning condition is smaller than the dimension, it's way more complex. The best solution I have found is to check for each n x n (n is the winning condition) square for a winner. But that seems slow.

So here is my question: if you know the last move of a player, can you use that position to check if that player wins without checking every n x n square in the board?

• An important improvement: You don't want to check moves in random order. You want to check the best moves first so the beta strategy can remove other moves quicker. So record the results after four moves for example, and try the ones first for five moves that look best after four moves. – gnasher729 Feb 20 '17 at 14:58
• Having only three outcomes (win, loss, draw) stops beta strategy from working well. Try giving 100 points to four in a row, 20 points to three in a row etc. – gnasher729 Feb 20 '17 at 15:12
• Your question doesn't seem to be about alpha-beta pruning. Including that information isn't relevant. I've deleted all that stuff about alpha-beta search and Moore neighborhood, as it seems to be just a distraction from what you are actually asking. If you have a question about alpha-beta search, I suggest asking that separately. – D.W. Feb 21 '17 at 0:09
• @gnasher729 I don't understand, how can we know if a move is the best move by record the results? I added more outcomes (4 in a row and 3 in a row) and the program runs a little bit faster, but still slow for some moves and still not very intelligent. For example when I have 4 in a row but two sides were blocked then it still returns 800 although I thought it should return 0. – Huy Vo Feb 21 '17 at 12:34
• @HuyVo: Well, that means your evaluation function is daft. Four in a row, both ends open = win unless the opponent has a direct winning move. Four in a row, one end open = forced move by the opponent. Four in a row, no end open = no value at all. If you only value winning and losing, you don't value moves that build up rows and columns at all. – gnasher729 Feb 21 '17 at 23:24

So, if the last move was in square $s$, check whether there's a 5-in-a-row that includes square $s$. There are only 4 directions to check (horizontal, vertical, and both diagonals), and you can check each direction efficiently.