示例#1
0
        private bool ValidateTilePlacement(TilePlacement tilePlacement, DirectionEnum direction)
        {
            //check to see if they are trying to place a tile on top of a tile that already exists
            if (_tilePlacements.Any(tp => tp.XCoord == tilePlacement.XCoord && tp.YCoord == tilePlacement.YCoord))
            {
                //There is already a tile at this location
                return(false);
            }

            var tiles = GetTilesInDirection(tilePlacement, direction);

            if (tiles.Count == 0)
            {
                //This direction is valid because there are no tiles
                return(true);
            }

            if (tiles.Any(t => t.Shape == tilePlacement.Tile.Shape && t.Color == tilePlacement.Tile.Color))
            {
                //If we find any tiles with the same shape and same color the turn is invalid
                return(false);
            }

            if (tiles.Count == 1)
            {
                return(tiles[0].Color == tilePlacement.Tile.Color || tiles[0].Shape == tilePlacement.Tile.Shape);
            }

            //Check if we are validating against Shape or Color
            if (tiles.Any(t => t.Color != tiles[0].Color))
            {
                //Validate same shape different color
                //If we find any tiles with a different shape the turn is invalid
                return(tiles.All(t => t.Shape == tilePlacement.Tile.Shape));
            }
            else
            {
                //Or validate same color different shape
                //If we find any tiles with a different color the turn is invalid
                return(tiles.All(t => t.Color == tilePlacement.Tile.Color));
            }
        }
示例#2
0
 public TilePlacement(TilePlacement tilePlacement)
 {
     Tile   = tilePlacement.Tile;
     XCoord = tilePlacement.XCoord;
     YCoord = tilePlacement.YCoord;
 }
示例#3
0
        private List <Tile> GetTilesInDirection(TilePlacement tilePlacement, DirectionEnum direction)
        {
            var tiles = new List <Tile>();
            var hitEmptyTilePlacement = false;

            switch (direction)
            {
            case DirectionEnum.Horizontal:
                var xCoordIndex = tilePlacement.XCoord;
                //Get tiles to the left
                do
                {
                    --xCoordIndex;
                    var lPlacement = _tilePlacements.SingleOrDefault(tp => tp.XCoord == xCoordIndex && tp.YCoord == tilePlacement.YCoord);
                    if (lPlacement == null)
                    {
                        hitEmptyTilePlacement = true;
                    }
                    else
                    {
                        tiles.Add(lPlacement.Tile);
                    }
                } while (hitEmptyTilePlacement == false);

                //Get tiles to the right
                xCoordIndex           = tilePlacement.XCoord;
                hitEmptyTilePlacement = false;
                do
                {
                    ++xCoordIndex;
                    var rPlacement = _tilePlacements.SingleOrDefault(tp => tp.XCoord == xCoordIndex && tp.YCoord == tilePlacement.YCoord);
                    if (rPlacement == null)
                    {
                        hitEmptyTilePlacement = true;
                    }
                    else
                    {
                        tiles.Add(rPlacement.Tile);
                    }
                } while (hitEmptyTilePlacement == false);
                break;

            case DirectionEnum.Vertical:
                var yCoordIndex = tilePlacement.YCoord;
                //Get tiles above
                do
                {
                    ++yCoordIndex;
                    var uPlacement = _tilePlacements.SingleOrDefault(tp => tp.XCoord == tilePlacement.XCoord && tp.YCoord == yCoordIndex);
                    if (uPlacement == null)
                    {
                        hitEmptyTilePlacement = true;
                    }
                    else
                    {
                        tiles.Add(uPlacement.Tile);
                    }
                } while (hitEmptyTilePlacement == false);

                //Get tiles below
                yCoordIndex           = tilePlacement.YCoord;
                hitEmptyTilePlacement = false;
                do
                {
                    --yCoordIndex;
                    var dPlacement = _tilePlacements.SingleOrDefault(tp => tp.XCoord == tilePlacement.XCoord && tp.YCoord == yCoordIndex);
                    if (dPlacement == null)
                    {
                        hitEmptyTilePlacement = true;
                    }
                    else
                    {
                        tiles.Add(dPlacement.Tile);
                    }
                } while (hitEmptyTilePlacement == false);
                break;
            }

            return(tiles);
        }