Connect Four Kata

Connect Four Kata

Connect Four is a two player game in which the players take turns dropping coloured tokens into a 7x6 grid of slots. The aim of the game is to connect four of your tokens together either horizontally, verically or diagonally before your opponent can connect four of theirs.

Unsurprisingly for a game that was published in 1974 and released all over the world, there are several variations of the classic Connect Four game, from changing the size of the grid to allowing players to remove their opponents tokens. This will make the computer controller player a lot more interesting- classic Connect Four has been solved and as such there is a perfect play order- but this does not apply to the variations.


Feature 1 - Playing Connect Four

Let's start with the basics. We want to be able to actually play the game. No fancy stuff, so we'll just be looking at classic Connect Four for now. So what we're going to need is a grid, 7 columns with 6 rows. We're going to need some way to place tokens in the grid referenced by column, and they'll need to fall down to the correct position. We're also going to need a way to see if the game is over, so checking whether the game has been won- remember, it can be horizontal, vertical, or diagonal.

Placing Tokens

As a VP of Gaming
I want players to be able to place tokens
So that they can play

  • Given a grid with $number tokens in column $column
    When I place a token in that column
    Then that token should be in row $row
Number Column Row
0 B 1
2 D 3
5 F 6

Checking Four in a Row

As a VP of Gaming
I want my game to check for winners
So that a player can win the game

  • Given I have a grid like $grid
    When I check for winners
    Then the winner should be $winner
Grid Winner
OOOOOOO OOOOOOO OOOOOOO OOOOOOO OOOOOOO YYYYOOO Yellow
OOOOOOO OOOOOOO ROOOOOO ROOOOOO ROOOOOO ROOOOOO Red
OOOOOOO OOOOOOO OOOOOOO OOOOOOO OOOOOOO OOOOOOO None
OOOOOOO OOOOOOO RYRROOO YYRYOOO RRYYOOO RYRYOOO Red
OOOOOOO OOOOOOO YYRYOOO YYRYOOO RRYROOO RYRYOOO Yellow