I am trying to build a m*n Tic-Tac-Toe using numpy to maintain my game-state. To evaluate a win, we need atleast 'q' continuous blocks of an element in either row, columns or along the diagonals. (3 in case of the original tic-tac-toe).
One crude approach I have currently implemented currently is to generate all the row, column and vectors along the diagonal path and then iterate over each of them to find a continious block of 'q' using iteration.
I would want to optimize it further. Since, I am relatively new to working with numpy and computation using matrices, I want to know if we can do it in any better fashion, more specifically, if I can better leverage numpy functionalities to do some more of these computations.
You can use slices instead of generation. A numpy array can be indexed like so (see https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html):
You can check all elements of a numpy array with
==, which generates an array of answers.Better optimization: You only need to check lines where the last piece was placed, assuming you check after each move. And you only need to check for the last player's piece (X or O).