You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.4 KiB
104 lines
2.4 KiB
package de.fd.fh.server.game;
|
|
|
|
import de.fd.fh.server.user.UserId;
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
|
|
@RequiredArgsConstructor
|
|
public class GameService
|
|
{
|
|
private final GameRepository repository;
|
|
|
|
public GameId createGame(final Game game)
|
|
{
|
|
return (GameId) repository.save(game).getId();
|
|
}
|
|
|
|
public GameId createGame(final UserId blackUser, final UserId whiteUser)
|
|
{
|
|
final Set<Field> fields = initFields(blackUser, whiteUser);
|
|
|
|
final Game game = new Game(null, whiteUser, blackUser, whiteUser, fields, null);
|
|
|
|
return (GameId) repository.save(game).getId();
|
|
}
|
|
|
|
public Game findGameById(final GameId gameId)
|
|
{
|
|
return repository.findByGameId(gameId);
|
|
}
|
|
|
|
private Set<Field> initFields(UserId blackUser, UserId whiteUser)
|
|
{
|
|
final Set<Field> fields = new HashSet<>();
|
|
for (int x = 0; x < 8; x++)
|
|
{
|
|
for (int y = 0; y < 8; y++)
|
|
{
|
|
fields.add(new Field(x, y, getColor(x, y) , getFigure(x, y, whiteUser, blackUser)));
|
|
}
|
|
}
|
|
return fields;
|
|
}
|
|
|
|
private Field.Color getColor(final int x, final int y)
|
|
{
|
|
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;
|
|
}
|
|
}
|