public async Task <ValidateUserInputResponse> ValidateAsync(ValidateUserInputRequest request) { var response = ServiceMessageFactory <ValidateUserInputResponse> .CreateFrom(request); var cleaned = request.Input.Trim().ToUpperInvariant(); if (string.IsNullOrWhiteSpace(cleaned)) { response.ValidationResult = new ValidationResult(NoInputFoundError); return(response); } var tilesRequest = ServiceMessageFactory <FindTilesRequest> .CreateFrom(request); tilesRequest.Filter = tile => tile.GameSessionId == request.GameSessionId; var tileResponse = await tileAccess.FindTilesAsync(tilesRequest); var targetTile = tileResponse.Tiles.FirstOrDefault(i => i.Address == cleaned); if (targetTile == null) { response.ValidationResult = new ValidationResult(AddressNotFoundError); return(response); } response.ValidationResult = targetTile.IsEmpty ? ValidationResult.Success : new ValidationResult(AddressAlreadyPlayedError); return(response); }
public async Task <IsGamePlayableResponse> IsGamePlayableAsync(IsGamePlayableRequest request) { var response = ServiceMessageFactory <IsGamePlayableResponse> .CreateFrom(request); var tilesRequest = ServiceMessageFactory <FindTilesRequest> .CreateFrom(request); tilesRequest.Filter = tile => tile.GameSessionId == request.GameSessionId; var tileResponse = await tileAccess.FindTilesAsync(tilesRequest); var tiles = tileResponse.Tiles; if (tiles.All(i => i.IsEmpty)) // New Game { response.Value = true; } else if (tiles.All(i => !i.IsEmpty)) // No empty spaces. { response.Value = false; } // Check all possible vectors var dictionary = tiles.ToDictionary(tile => tile.Address, tile => tile.Convert()); // A Col if (IsWinningVector(dictionary["A1"], dictionary["A2"], dictionary["A3"])) { response.Value = false; } else // B Col if (IsWinningVector(dictionary["B1"], dictionary["B2"], dictionary["B3"])) { response.Value = false; } else // C Col if (IsWinningVector(dictionary["C1"], dictionary["C2"], dictionary["C3"])) { response.Value = false; } else // 1 Row if (IsWinningVector(dictionary["A1"], dictionary["A2"], dictionary["A3"])) { response.Value = false; } else // 2 Row if (IsWinningVector(dictionary["B1"], dictionary["B2"], dictionary["B3"])) { response.Value = false; } else // 3 Row if (IsWinningVector(dictionary["C1"], dictionary["C2"], dictionary["C3"])) { response.Value = false; } else // Right Diagonal if (IsWinningVector(dictionary["A1"], dictionary["B2"], dictionary["C3"])) { response.Value = false; } else // Left Diagonal if (IsWinningVector(dictionary["A3"], dictionary["B2"], dictionary["C1"])) { response.Value = false; } else { response.Value = true; } return(response); }
public async Task <GetGameBoardResponse> GetBoardAsync(GetGameBoardRequest request) { var response = ServiceMessageFactory <GetGameBoardResponse> .CreateFrom(request); var gameSessionRequest = ServiceMessageFactory <GetGameSessionRequest> .CreateFrom(request); gameSessionRequest.GameSessionId = request.GameSessionId; var gameSessionResponse = await gameSessionAccess.GetGameSessionAsync(gameSessionRequest); if (gameSessionResponse.HasErrors) { response.Errors = "Unable to get the selected game board. Session error."; return(response); } var gameSession = gameSessionResponse.GameSession; // Board for Tic-Tac-Toe var players = new List <Player>(); foreach (var playerId in gameSession.PlayerIds) { var playerRequest = ServiceMessageFactory <GetPlayerRequest> .CreateFrom(request); playerRequest.PlayerId = playerId; var playerResponse = await playerAccess.GetPlayerAsync(playerRequest); players.Add(playerResponse.Player); } response.GameBoard = new DataTable(); response.GameBoard.Columns.Add(new DataColumn(" ")); response.GameBoard.Columns.Add(new DataColumn("A")); response.GameBoard.Columns.Add(new DataColumn("B")); response.GameBoard.Columns.Add(new DataColumn("C")); var tileRequest = ServiceMessageFactory <FindTilesRequest> .CreateFrom(request); tileRequest.Filter = tile => tile.GameSessionId == request.GameSessionId; var tileResponse = await tileAccess.FindTilesAsync(tileRequest); var tiles = tileResponse.Tiles; if (tileResponse.HasErrors) { return(response); } var dataRow = response.GameBoard.NewRow(); dataRow[0] = 1; dataRow[1] = (tiles[0].PlayerId == Guid.Empty ? " " : players.First(i => i.Id == tiles[0].PlayerId).GamePiece); dataRow[2] = (tiles[3].PlayerId == Guid.Empty ? " " : players.First(i => i.Id == tiles[3].PlayerId).GamePiece); dataRow[3] = (tiles[6].PlayerId == Guid.Empty ? " " : players.First(i => i.Id == tiles[6].PlayerId).GamePiece); response.GameBoard.Rows.Add(dataRow); dataRow = response.GameBoard.NewRow(); dataRow[0] = 2; dataRow[1] = (tiles[1].PlayerId == Guid.Empty ? " " : players.First(i => i.Id == tiles[1].PlayerId).GamePiece); dataRow[2] = (tiles[4].PlayerId == Guid.Empty ? " " : players.First(i => i.Id == tiles[4].PlayerId).GamePiece); dataRow[3] = (tiles[7].PlayerId == Guid.Empty ? " " : players.First(i => i.Id == tiles[7].PlayerId).GamePiece); response.GameBoard.Rows.Add(dataRow); dataRow = response.GameBoard.NewRow(); dataRow[0] = 3; dataRow[1] = (tiles[2].PlayerId == Guid.Empty ? " " : players.First(i => i.Id == tiles[2].PlayerId).GamePiece); dataRow[2] = (tiles[5].PlayerId == Guid.Empty ? " " : players.First(i => i.Id == tiles[5].PlayerId).GamePiece); dataRow[3] = (tiles[8].PlayerId == Guid.Empty ? " " : players.First(i => i.Id == tiles[8].PlayerId).GamePiece); response.GameBoard.Rows.Add(dataRow); return(response); }