Browse Source

add figure to fields

feat-serverGameRepresentation
Steffen Nitschke 4 years ago
parent
commit
a59cb5a37c
  1. 56
      fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameService.java
  2. 9
      fh.fd.ci.server/src/test/java/de/fd/fh/server/game/GameServiceTest.java

56
fh.fd.ci.server/src/main/java/de/fd/fh/server/game/GameService.java

@ -32,7 +32,7 @@ public class GameService
{ {
for (int y = 0; y < 8; y++) for (int y = 0; y < 8; y++)
{ {
fields.add(new Field(x, y, getColor(x, y) , null));
fields.add(new Field(x, y, getColor(x, y) , getFigure(x, y, whiteUser, blackUser)));
} }
} }
return fields; return fields;
@ -42,4 +42,58 @@ public class GameService
{ {
return (x + y) % 2 != 1 ? Field.Color.WHITE : Field.Color.BLACK; return (x + y) % 2 != 1 ? Field.Color.WHITE : Field.Color.BLACK;
} }
private Figure getFigure(final int x, final int y, final UserId whitePlayer, final UserId blackPlayer)
{
if (x == 0)
{
return getFigureOfGroundLevel(y, whitePlayer);
}
if (x == 1)
{
return new Figure(Figure.Type.PAWN, whitePlayer);
}
if (x == 7)
{
return getFigureOfGroundLevel(y, blackPlayer);
}
if (x == 6)
{
return new Figure(Figure.Type.PAWN, blackPlayer);
}
return null;
}
private Figure getFigureOfGroundLevel(final int y, final UserId player)
{
if (y == 0 || y == 7)
{
return new Figure(Figure.Type.ROOK, player);
}
if (y == 1 || y == 6)
{
return new Figure(Figure.Type.KNIGHT, player);
}
if (y == 2 || y == 5)
{
return new Figure(Figure.Type.BISHOP, player);
}
if (y == 3)
{
return new Figure(Figure.Type.QUEEN, player);
}
if (y == 4)
{
return new Figure(Figure.Type.KING, player);
}
return null;
}
} }

9
fh.fd.ci.server/src/test/java/de/fd/fh/server/game/GameServiceTest.java

@ -62,6 +62,15 @@ class GameServiceTest
final Field.Color expected = (field.getX() + field.getY()) % 2 != 1 ? Field.Color.WHITE : Field.Color.BLACK; final Field.Color expected = (field.getX() + field.getY()) % 2 != 1 ? Field.Color.WHITE : Field.Color.BLACK;
if (field.getX() == 0 || field.getX() == 1 || field.getX() == 6 || field.getX() == 7)
{
assertNotNull(field.getFigure());
}
else
{
assertNull(field.getFigure());
}
assertEquals(expected, field.getColor()); assertEquals(expected, field.getColor());
}); });

Loading…
Cancel
Save