Inspired by this problem...
Connect Four is a fun two-player game in which the goal is to be the first player to connect four pieces in a row - horizontally, vertically, or diagonally. Players take turns dropping one of their pieces into any of the available columns.
Problem Statement
Given an array (size is arbitrary) in which
- 0 : space is empty
- 1 : player 1 piece
- 2 : player 2 piece
return the column that player 1 should play in to win the game. If there is more than one winning move, return a row vector of appropriate columns. If there are currently no winning moves, return [].
Assumptions
- Pieces fall to the bottommost available row in each column, as in the real game
- The input grid will always represent a valid game state
Example
If
b = [0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
0 0 0 2 0 0 0;
0 0 0 1 0 0 0;
0 0 0 2 2 0 0;
0 0 2 1 1 1 0];
then
y = 7
Solution Stats
Problem Comments
Solution Comments
Show commentsProblem Recent Solvers29
Suggested Problems
-
Remove all the words that end with "ain"
2693 Solvers
-
Find relatively common elements in matrix rows
2157 Solvers
-
Check to see if a Sudoku Puzzle is Solved
341 Solvers
-
Test if a Number is a Palindrome without using any String Operations
256 Solvers
-
Rotate and display numbered tile
382 Solvers
More from this Author43
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!