Connect Four is a game where you try to get four pieces in a row. For this problem, you are given a grid of numbers that represents a game between Mr. One and Mr. Two. Your job is to return 1 if Mr. One wins, 2 if Mr. Two wins, and 0 if there is no winner yet. There will never be a two-winner state.
Note that this is a generalized game of Connect Four in which the board might vary in dimension (it is guaranteed to be 4 or greater in each dimension). Also in Connect Four the pieces fall to the bottom of the grid, but in this problem the pieces can appear in any position.
Example
In the board below, the pieces are 1s and 2s. Empty spots are zeros. Mr. Two wins because the third column has four consecutive twos. So the output of your function should be 2.
b = [0 1 1 0 2; 0 1 2 2 1; 1 0 2 2 2; 1 1 2 2 1; 1 1 2 1 0];
Padded with 0's in convolution kernel to avoid collisions
Wow, using a string really cuts down on solution size.
Can someone please tell me whats going on?!
The reshape function creates a matrix with the vector [1 3 9 27] placed vertically, horizontally, and diagonally; the conv2 function does sort of a dot product of these with all sets of four elements in the test matrix; if [1 1 1 1] or [2 2 2 2] appears, the result will be 40 or 80, respectively.
This code is referenced in the following blog post: http://blogs.mathworks.com/community/2015/01/19/robot-game-playing-in-matlab-part-2/
Project Euler: Problem 9, Pythagorean numbers
170 Solvers
515 Solvers
Getting the absolute index from a matrix
181 Solvers
Flag largest magnitude swings as they occur
538 Solvers
Write c^3 as sum of two squares a^2+b^2
254 Solvers