public CreateMovementResponse Run(CreateMovementRequest request) { GameEntity game = gameRepository.Retrieve(request.id); if (game == null) { throw new BadRequestException("Partida não encontrada"); } if (game.currentPlayer != TypePlayerExtension.GetByPlay(request.player)) { throw new BadRequestException("Não é turno do jogador"); } if (!PositionIsValid(request.position, game)) { throw new BadRequestException("Espaço inválido"); } MovementEntity movement = new MovementEntity() { gameId = request.id, player = TypePlayerExtension.GetByPlay(request.player), turn = game.turn, x = request.position.x, y = request.position.y }; game = TransactionHandler.HandleTransaction(() => { movementRepository.Create(movement); return(gameRepository.RegisterMovement(request.id, movement)); }); string winner = CheckForWinner(game.board); string msg = null; if (game.turn >= 9 && winner == null) { winner = "Draw"; } if (winner != null) { msg = "Partida finalizada"; } else { msg = "Partida em andamento"; } return(new CreateMovementResponse() { msg = msg, winner = winner }); }
public CreateGameResponse Run(CreateGameRequest request) { GameEntity gameEntity = new GameEntity() { currentPlayer = new Random().Next() % 2 == 0 ? TypePlayer.X : TypePlayer.O }; string id = gameRepository.Create(gameEntity); return(new CreateGameResponse() { id = id, firstPlayer = TypePlayerExtension.ToString(gameEntity.currentPlayer) }); }
public GameEntity RegisterMovement(string id, MovementEntity movement) { GameEntity game = list_Games_In_Memory.Where(g => String.Equals(g.id, id)) .FirstOrDefault(); if (game == null) { return(null); } int index = list_Games_In_Memory.IndexOf(game); game.board[movement.x, movement.y] = TypePlayerExtension.ToString(movement.player); game.SwitchPlayer(); game.turn += 1; list_Games_In_Memory[index] = game; return(game); }