20
4
Introduction
Connect Four is a game where you attempt to get four in a row: horizontally, vertically, or diagonally. In this code golf, we will be trying to find who won, given a game board. There will always be one winner, and only one winner.
Task
Given a Connect Four board, figure out who the winner is: X
or Y
. There will always be one winner, and only one winner. The board size will always be 6 by 7 like how the game board is in the in picture.
Given a board the following board, in this instance, X
is red and Y
is blue:
Your input would be:
OOOOOOO
OOOOOOO
OOOOOOO
OOOOXOO
OOOXXOO
OOXYYYY
You can separate rows of the game by newline character (like above), no dividing character, divide the rows into an array or list, or you can input a matrix of characters.
Correct output for this example:
Y
Y has four in a row; so, Y is the winner. So, we output Y.
Test cases
Input:
OOOOOOO
OOOOOOO
OOOOOOO
OOOOOOO
OOYYOOO
OYXXXXO
Output:
X
Input:
OOOOOOO
OOOOOOO
OOOOOOO
XXXXOOO
YXYYOOO
YXYYXYX
Output:
X
Input:
YXYYXOO
XYXXYOO
XXXYYOO
YYYXXOO
XXYYYYO
XXYYXXO
Output:
Y
Input:
OOOOOOO
OOOOOOO
OYOOOOO
OOYOOOO
OOOYOOO
OOOOYOO
Output:
Y
Input:
OOOOOOO
OOOOOOO
OYOOOOX
OOYOOOX
OOOXOOX
OXOXYOX
Output:
X
Scoring
Least number of bytes wins!
This is the perfect challenge for PMA/Snails https://codegolf.stackexchange.com/questions/47311/language-design-2-d-pattern-matching/47495#47495
– Jerry Jeremiah – 2017-04-21T01:21:36.557@nfnneil Maybe I will make a bounty for an answer in PMA/Snails ... – Jerry Jeremiah – 2017-04-21T01:43:12.630
3Can we assume that the winner will always have one more token than the loser? – math junkie – 2017-04-21T02:38:28.877
1@mathjunkie I was wrong, you can't assume that. – Neil – 2017-04-21T02:44:31.123
3@nfnneil does the output have to be X or Y or can we choose two other consistent outputs to indicate the winner? – Martin Ender – 2017-04-21T05:51:52.310
1Can we choose to use other characters as input? Or to input a numeric matrix? – Luis Mendo – 2017-04-21T07:42:55.203
Can we assume the four pieces of each colour will be in a connected set? – Luis Mendo – 2017-04-21T07:46:29.137
Related. – None – 2017-04-21T11:15:43.563
Almost a duplicate. It looks somewhat different, but the main body of the solution is going to be rather similar in both languages. I'm not confident enough that this is a duplicate to put in a close vote, but think this might be worth extra eyes. – None – 2017-04-21T11:23:22.617