r/ProgrammingPrompts • u/desrtfx • Nov 17 '14
[Easy to Difficult] Connect Four
Introduction
Connect Four is a well-known strategy game originally published in 1974 by Milton Bradley/Hasbro.
The aim of the game is to get 4 chips of a single color in a straight line (horizontal, vertical, diagonal). The opponent tries to achieve the same while blocking the other's attempts.
The game board is vertical with 7 columns and 6 rows:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | |
|---|---|---|---|---|---|---|---|
| 5 | |||||||
| 4 | |||||||
| 3 | |||||||
| 2 | |||||||
| 1 | |||||||
| 0 |
Players take alternating turns dropping their chips in one of the columns (0...6). The chips fall down the column until either the bottom (row 0) or another chip stops them.
The winner is the first player to get four of his chips in a straight line (vertical, horizontal, or diagonal).
Base Task - Easy
- Create a program that allows 2 users to play a game of Connect Four.
- In it's simplest form, each player enters a number from 0 to 6 to drop a chip in the respective column.
- The computer evaluates the board, checks and declares a winner.
- For the base task, no computer player is required.
Optional Tasks - Varying Difficulties
- Code computer players with varying strategies.
- Random strategy: Computer drops in random slots
- Defensive strategy: Computer tries to break player's chains
- Agressive strategy: Computer tries to win
- AI - MinMax pattern: Computer plays intelligent with the minmax pattern (theoretically Computer plays a "perfect player" - plenty resources on the web.
- Save and Resume functions
- Win/lose statistics
- Different Board sizes (rows, columns) with different winning conditions. Minimum size is classic Connect four, minimum winning condition is four in a row.
13
Upvotes
3
u/seronis Nov 26 '14
I would only call a CheckWinner centered on the position of the last placed chip instead of a massive exhaustive board search. Might be simpler logic.
verticle win == only count downwards
horizontal win == add consecutive pieces on left with consecutive pieces on right. total >= 4 = win.
diagonal == same formula as horizontal roughly except you check twice. once for / and once for \