The bank has been broken into, and all the local mafia thugs have an unusual alibi: they were at home playing Connect 4! In order to assist with the investigation, you are asked to write a program to validate all the Connect 4 boards that have been seized in order to check that the positions are indeed positions from a valid Connect 4 game, and have not been hastily put together as soon as the police knocked on the door.
The rules for connect 4: players R
and Y
take it in turns to drop tiles of their colour into columns of a 7x6 grid. When a player drops a tile into the column, it falls down to occupy the lowest unfilled position in that column. If a player manages to get a horizontal, vertical or diagonal run of four tiles of their colour on the board, then they win and the game ends immediately.
For example (with R
starting), the following is an impossible Connect 4 position.
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |R| | | | |
| | |Y| | | | |
|R| |Y| | | | |
Your program or function must take in a Connect 4 board and return either
- A falsy value, indicating that the position is impossible or
- A string of numbers from 1 to 7, indicating one possible sequence of moves leading to that position (the columns are numbered
1
to7
from left to right, and so the sequence112
, for example, indicates a red move in column1
, followed by a yellow move in column1
, followed by a red move in column2
). You may choose a column-numbering other than 1234567 if you like, as long as you specify in your solution. If you want to return the list in some other format; for example as an array[2, 4, 3, 1, 1, 3]
then that is fine too, as long as it is easy to see what the moves are.
You can choose to read the board in in any sensible format including using letters other than R
and Y
for the players, but you must specify which player goes first. You can assume that the board will always be 6x7, with two players.
You may assume that the positions you receive are at least physically possible to create on a standard Connect 4 board; i.e., that there will be no 'floating' pieces. You can assume that the board will be non-empty.
This is code golf, so shortest answer wins. Standard loopholes apply.
Examples
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | | --> 1234567 (one possible answer)
| | | | | | | |
|R|Y|R|Y|R|Y|R|
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |R| | | | | --> false
| | |Y| | | | |
|R| |Y| | | | |
| | | | | | | |
| | |Y| | | | |
| | |R| | | | |
| | |Y| | | | | --> 323333 (only possible answer)
| | |R| | | | |
| |Y|R| | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | | --> false (this is the position arising after
| |Y|Y|Y|Y| | | the moves 11223344, but using those moves
| |R|R|R|R| | | the game would have ended once R made a 4)
| | | | | | | |
| | | | | | | |
|Y| | | | | | |
|R|Y| | | | | | --> 2134231211 (among other possibilities)
|R|R|Y| | | | |
|Y|R|R|Y| | | |
| | | | | | | |
| | | | | | | |
|Y| | | | | | |
|R|Y| | | | | | --> false (for example, 21342312117 does not
|R|R|Y| | | | | work, because Y has already made a diagonal 4)
|Y|R|R|Y| | |R|
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | | --> 112244553 or similar
|Y|Y| |Y|Y| | |
|R|R|R|R|R| | |