Add a class diagram
This commit is contained in:
71
README.md
Normal file
71
README.md
Normal file
@@ -0,0 +1,71 @@
|
||||
Morpion
|
||||
=======
|
||||
|
||||
Diagramme de classes
|
||||
====================
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class Position {
|
||||
-x : int
|
||||
-y : int
|
||||
+x() int
|
||||
+y() int
|
||||
}
|
||||
|
||||
class Board {
|
||||
-width : int
|
||||
-height : int
|
||||
+place(p : Placed)
|
||||
+isOccupied() bool
|
||||
+get(p : Position) Tile
|
||||
+isBound(p : Position) bool
|
||||
+isFull() bool
|
||||
}
|
||||
class Tile {
|
||||
}
|
||||
<<interface>> Tile
|
||||
Empty <|-- Tile
|
||||
Placed <|-- Tile
|
||||
class Placed {
|
||||
-value : char
|
||||
+value() char
|
||||
}
|
||||
Tile "*" <-- Board
|
||||
|
||||
class Game {
|
||||
-board : Board
|
||||
-players : List~Player~
|
||||
-currentPlayer : int
|
||||
+board() Board
|
||||
+currentPlayer() Player
|
||||
+nextPlayer()
|
||||
+placeCurrent(p : Position) MoveResult
|
||||
+players() List~Player~
|
||||
}
|
||||
Board <-- Game
|
||||
|
||||
class MoveResult {
|
||||
}
|
||||
<<interface>> MoveResult
|
||||
class Result {
|
||||
-wins : List~Win~
|
||||
}
|
||||
class CantPlace {
|
||||
}
|
||||
Result <|-- MoveResult
|
||||
CantPlace <|-- MoveResult
|
||||
class Win {
|
||||
-positions : List~Position~
|
||||
+positions() List~Position~
|
||||
}
|
||||
|
||||
class WinChecker {
|
||||
+detectFrom(b : Board, p : Position) List~Win~
|
||||
}
|
||||
<<interface>> WinChecker
|
||||
Win <.. WinChecker
|
||||
Position <.. Win
|
||||
Position <.. Board
|
||||
MoveResult <.. Game
|
||||
```
|
@@ -23,6 +23,7 @@ public class CliPlay {
|
||||
.mapToObj(i -> new Player("Player" + i))
|
||||
.toList();
|
||||
game = new Game(players.toArray(Player[]::new));
|
||||
game.start();
|
||||
|
||||
final AsciiBoardFormatter formatter = new AsciiBoardFormatter();
|
||||
while (!isGameOver() && !game.board().isFull()) {
|
||||
|
@@ -56,6 +56,9 @@ public class Game {
|
||||
}
|
||||
|
||||
public MoveResult placeCurrent(Position position) {
|
||||
if (!started) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
if (board.isOccupied(position)) {
|
||||
return new CantPlace();
|
||||
}
|
||||
|
@@ -25,6 +25,12 @@ public class GameTest {
|
||||
assertThrows(IllegalStateException.class, () -> game.start());
|
||||
}
|
||||
|
||||
@Test
|
||||
void cantPlayerIfNotStarted() {
|
||||
Game game = new Game(new Player("Alice"));
|
||||
assertThrows(IllegalStateException.class, () -> game.placeCurrent(new Position(1, 2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCurrentPlayer() {
|
||||
final Player alice = new Player("Alice");
|
||||
@@ -67,6 +73,7 @@ public class GameTest {
|
||||
@Test
|
||||
void playerPlace() {
|
||||
final Game game = new Game(new Player("Alice"));
|
||||
game.start();
|
||||
game.placeCurrent(new Position(1, 2));
|
||||
assertEquals(new Placed('x'), game.board().get(new Position(1, 2)));
|
||||
}
|
||||
@@ -74,6 +81,7 @@ public class GameTest {
|
||||
@Test
|
||||
void playerPlaceDifferentPlayer() {
|
||||
final Game game = new Game(new Player("Alice"), new Player("Bob"));
|
||||
game.start();
|
||||
game.placeCurrent(new Position(0, 0));
|
||||
game.placeCurrent(new Position(2, 2));
|
||||
assertEquals(new Placed('o'), game.board().get(new Position(2, 2)));
|
||||
@@ -82,6 +90,7 @@ public class GameTest {
|
||||
@Test
|
||||
void workWithThreePlayers() {
|
||||
final Game game = new Game(new Player("Albert"), new Player("Blob"), new Player("Cyril"));
|
||||
game.start();
|
||||
game.placeCurrent(new Position(1, 2));
|
||||
game.placeCurrent(new Position(2, 1));
|
||||
game.placeCurrent(new Position(2, 2));
|
||||
@@ -94,6 +103,7 @@ public class GameTest {
|
||||
@ValueSource(ints = {0, 1, 2})
|
||||
void scoreVertically(int x) {
|
||||
final Game game = new Game(new Player("Ema"));
|
||||
game.start();
|
||||
assertEquals(new Result(), game.placeCurrent(new Position(x, 0)));
|
||||
assertEquals(new Result(), game.placeCurrent(new Position(x, 1)));
|
||||
assertEquals(new Result(new Win(
|
||||
@@ -106,6 +116,7 @@ public class GameTest {
|
||||
@Test
|
||||
void scoreDiagonally() {
|
||||
final Game game = new Game(new Player("Victor"));
|
||||
game.start();
|
||||
assertEquals(new Result(), game.placeCurrent(new Position(0, 0)));
|
||||
assertEquals(new Result(), game.placeCurrent(new Position(2, 2)));
|
||||
assertEquals(new Result(new Win(
|
||||
@@ -121,6 +132,7 @@ public class GameTest {
|
||||
final Player alice = new Player("Alice");
|
||||
final Player bob = new Player("Bob");
|
||||
final Game game = new Game(alice, bob);
|
||||
game.start();
|
||||
if (secondPlayerWin) {
|
||||
game.placeCurrent(new Position(2, 1));
|
||||
}
|
||||
@@ -149,6 +161,7 @@ public class GameTest {
|
||||
@Test
|
||||
void cantPlaceTwoTimes() {
|
||||
final Game game = new Game(new Player("Alice"), new Player("Bob"));
|
||||
game.start();
|
||||
game.placeCurrent(new Position(0, 0));
|
||||
assertEquals(new CantPlace(), game.placeCurrent(new Position(0, 0)));
|
||||
}
|
||||
|
Reference in New Issue
Block a user