Morpion, really?!

At first sight, this example can seem boring and useless. Trust me, it is not. It is easy enough to be quickly understood (really, who doesn't know morpion, tictactoe, whatever).

The goal is to build a bridge from the understanding of a problem to its Java implementation, step-by-step: what are the entities, the data and the behaviors?

Morpion is a game that takes place on a board. The board is made of 3x3 cells. Two players play in turns by placing a tile marked X or O on a specific cell. If three tiles of a player are horizontally, vertically or diagonally aligned, the given player automatically wins.

  • Who is involved? Board, Cell, Player, Tile
  • What do they do?
    1. Start a game,
    2. Place a tile, in turns
    3. Check for alignments,
    4. Win or lose
  • How do they do that?
    1. Create an empty board and two players,
    2. Modify the content of a cell with a given tile, if the cell is empty
    3. Recursively look for 3 consecutives tiles of the same type, vertically, horizontally, diagonally
    4. Yield a player
  • But, for real, HOW do they do that?
    1. x = new Player("X"), o = new Player("O"), new Board(3, 3, x, o)
    2. x.play(board, 1, 1), board.place("X", 1, 1), error? ...
    3. Try to analyse only ONE direction.
    4. if step3 OK => return...

An example of solution can be downloaded here. You can directly import it with IntelliJ (Import project > on the new window: select 'pom.xml' file > on the new window: tick 'Import maven projects automatically' > Next > Next (again) > Select 1.8 JdK > Next > Choose where you want to store this project > Finish)