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.
Lets say 1 belongs to Player1, 2 belongs to Player 2 and 0 means empty spaces. Then for each row/column/diagonal in the array you can find out the count of 1 and 2, and if it equals the length of row then it means a Player has already won. Here using
numpy.apply_along_axis
I've applied a function to each row/column/diagonal in the array and it returns the count of 1 or 2 in that row divided by length of that row, so if for any case 1 is returned then it means that row/column/diagonal contains either all 1's or 2's.Demo:
Demo: