public async Task <DataTable> GetBoard(Guid gameSessionId) { // Board for Tic-Tac-Toe var gameSession = await gameSessionAccess.GetGameSession(gameSessionId); var players = new List <Player>(); foreach (var playerId in gameSession.PlayerIds) { var player = await playerAccess.GetPlayer(playerId); players.Add(player); } var tiles = await tileAccess.FindTiles(gameSessionId); var dataTable = new DataTable(); dataTable.Columns.Add(new DataColumn(" ")); dataTable.Columns.Add(new DataColumn("A")); dataTable.Columns.Add(new DataColumn("B")); dataTable.Columns.Add(new DataColumn("C")); var dataRow = dataTable.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); dataTable.Rows.Add(dataRow); dataRow = dataTable.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); dataTable.Rows.Add(dataRow); dataRow = dataTable.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); dataTable.Rows.Add(dataRow); return(dataTable); }
public async Task <DataTable> GetBoard(Guid gameSessionId) { // Board for Tic-Tac-Toe var gameSessions = await gameSessionAccess.FindGameSessions(i => i.Id == gameSessionId); var gameSession = gameSessions.First(); var players = await playerAccess.FindPlayers(i => gameSession.PlayerIds.Contains(i.Id)); var tiles = await tileAccess.FindTiles(i => i.Id == gameSessionId); var dataTable = new DataTable(); dataTable.Columns.Add(new DataColumn(" ")); dataTable.Columns.Add(new DataColumn("A")); dataTable.Columns.Add(new DataColumn("B")); dataTable.Columns.Add(new DataColumn("C")); var dataRow = dataTable.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); dataTable.Rows.Add(dataRow); dataRow = dataTable.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); dataTable.Rows.Add(dataRow); dataRow = dataTable.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); dataTable.Rows.Add(dataRow); return(dataTable); }
public async Task <ValidationResult> ValidateUserInput(Guid gameSessionId, string input) { var cleaned = input.Trim().ToUpperInvariant(); if (string.IsNullOrWhiteSpace(cleaned)) { return(await Task.FromResult(new ValidationResult(NoInputFoundError))); } var tiles = await tileAccess.FindTiles(gameSessionId); var targetTile = tiles.FirstOrDefault(i => i.Address == cleaned); if (targetTile == null) { return(new ValidationResult(AddressNotFoundError)); } var validationResult = targetTile.IsEmpty ? ValidationResult.Success : new ValidationResult(AddressAlreadyPlayedError); return(validationResult); }
public async Task <bool> IsPlayable(Guid gameSessionId) { var tiles = await tileAccess.FindTiles(i => i.Id == gameSessionId); if (tiles.All(i => i.IsEmpty)) { return(true); // New Game } if (tiles.All(i => !i.IsEmpty)) { return(false); // No empty spaces. } // Check all possible vectors var dictionary = tiles.ToDictionary(tile => tile.Address, tile => tile); // A Col if (IsWinningVector(dictionary["A1"], dictionary["A2"], dictionary["A3"])) { return(false); } // B Col if (IsWinningVector(dictionary["B1"], dictionary["B2"], dictionary["B3"])) { return(false); } // C Col if (IsWinningVector(dictionary["C1"], dictionary["C2"], dictionary["C3"])) { return(false); } // 1 Row if (IsWinningVector(dictionary["A1"], dictionary["A2"], dictionary["A3"])) { return(false); } // 2 Row if (IsWinningVector(dictionary["B1"], dictionary["B2"], dictionary["B3"])) { return(false); } // 3 Row if (IsWinningVector(dictionary["C1"], dictionary["C2"], dictionary["C3"])) { return(false); } // Right Diagonal if (IsWinningVector(dictionary["A1"], dictionary["B2"], dictionary["C3"])) { return(false); } // Left Diagonal if (IsWinningVector(dictionary["A3"], dictionary["B2"], dictionary["C1"])) { return(false); } return(true); }