Add a class diagram

This commit is contained in:
2023-04-09 23:38:29 +02:00
parent 6cf730d056
commit 4b682afab8
4 changed files with 88 additions and 0 deletions

71
README.md Normal file
View 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
```

View File

@@ -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()) {

View File

@@ -56,6 +56,9 @@ public class Game {
}
public MoveResult placeCurrent(Position position) {
if (!started) {
throw new IllegalStateException();
}
if (board.isOccupied(position)) {
return new CantPlace();
}

View File

@@ -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)));
}