Problem 43574. Connect 4 (the boardgame)
You are playing the popular game "Connect 4" ( https://en.wikipedia.org/wiki/Connect_Four) against Matlab. Luckily, Matlab isn't trying to hard and just places its chips randomly.
Write a function which plays the game for you. As starting point you are given the same function which you opponent is using (random placement). But you'll have to do better, because you will have to crush Matlab by winning 10 games in a row in order to pass this test!
Each time your function as called it is your turn to make a move. The input of the function is a 5x7 matrix, representing the game board, containing either "1" (a chip of your opponent), "2" (one of your chips) or "0" (an empty space). Calculate your best move and output the column index of your next chip (make sure this column isn't full yet or your turn is lost).
Good luck!
Solution Stats
Problem Comments
Solution Comments
Show commentsProblem Recent Solvers15
Suggested Problems
-
Replace NaNs with the number that appears to its left in the row.
2925 Solvers
-
831 Solvers
-
We love vectorized solutions. Problem 1 : remove the row average.
802 Solvers
-
Numbers spiral diagonals (Part 1)
188 Solvers
-
Number of Even Elements in Fibonacci Sequence
1250 Solvers
More from this Author25
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!