public GameSideEnum?GetWinner(Game currentGame)
        {
            var cells      = _gameCellService.GetAll(currentGame.Id).ToArray();
            var cellGroups = CellGroupsHelper.ExecuteStructuredGroups(cells);

            var crossesWinnerGroup = cellGroups.FirstOrDefault(d => d.OccupiedGameCells.Count(s => s.GameSide == GameSideEnum.Crosses) == 3);

            if (crossesWinnerGroup != null)
            {
                return(GameSideEnum.Crosses);
            }
            var zerosWinnerGroup = cellGroups.FirstOrDefault(d => d.OccupiedGameCells.Count(s => s.GameSide == GameSideEnum.Zeros) == 3);

            if (zerosWinnerGroup != null)
            {
                return(GameSideEnum.Zeros);
            }

            return(null);
        }
Exemplo n.º 2
0
        private IList <GameCell> GetList(int gameId)
        {
            var             cells     = _cellRepository.GetAllWithMoves();
            List <GameCell> gameCells = new List <GameCell>();

            foreach (var cell in cells)
            {
                var gameCell = new GameCell
                {
                    GameId = gameId,
                    CellId = cell.Id,
                    Cell   = cell
                };
                var move = cell.Moves.FirstOrDefault(d => d.GameId == gameId && d.CellId == cell.Id);
                if (move != null)
                {
                    gameCell.GameSide = move.Player.GameSideId != null ? (GameSideEnum?)move.Player.GameSideId : null;
                }

                gameCells.Add(gameCell);
            }

            var cellGroups             = CellGroupsHelper.ExecuteStructuredGroups(gameCells.ToArray());
            var highlightedCombination = cellGroups.FirstOrDefault(d =>
                                                                   d.OccupiedGameCells.Count() == 3 &&
                                                                   d.OccupiedGameCells.All(s => s.GameSide == d.OccupiedGameCells.First().GameSide));

            if (highlightedCombination != null)
            {
                foreach (var gameCell in gameCells)
                {
                    if (highlightedCombination.OccupiedGameCells.Any(s => s.CellId == gameCell.CellId))
                    {
                        gameCell.IsHighlighted = true;
                    }
                }
            }
            return(gameCells);
        }