void FindMovableTiles(Piece piece)
        {
            movableTiles.Clear();

            int tileDistance = piece is AttackPiece ? 2 : 1;

            for (int col = (int)piece.Column - tileDistance; col <= piece.Column + tileDistance; col++) {
                for (int row = (int)piece.Row - tileDistance; row <= piece.Row + tileDistance; row++) {
                    movableTiles.Add(new Vector2(col, row));
                }
            }
        }
        void PushBack(Piece piece)
        {
            List<Piece> piecesToMove = new List<Piece>();

            for (uint column = piece.Column; column < Columns; column++) {
                var piecesOnRowInColumn = pieces.Where(
                    p => p.Row == piece.Row &&
                         p.Column == column
                );

                if (piecesOnRowInColumn.Count() > 0) {
                    piecesToMove.Add(piecesOnRowInColumn.ElementAt(0));
                } else {
                    // hit empty tile, get the hell outta here!
                    break;
                }
            }

            foreach (Piece pieceToMove in piecesToMove) {
                pieceToMove.Column++;

                if (pieceToMove.Column >= Columns) {
                    Debug.WriteLine(String.Format("{0} was pushed out of the grid.", pieceToMove));

                    piecesToRemove.Add(pieceToMove);
                }
            }
        }
        void DrawTurnQueueItem(Piece piece, Vector2 position)
        {
            float hoverAmount = 8;

            bool pieceIsAlsoHovered = piece == hoveredPiece && !(piece is EnemyPiece);

            // nullpoint exception ved piece.icon naar alle walls er draebt
            sprite.Draw(piece.Icon, position + new Vector2(0, pieceIsAlsoHovered ? -hoverAmount : 0), Color.White);

            int units = 0;

            if (piece is EnemyPiece) {
                enemyPieces.ForEach(enemy => units += enemy.Units);
            } else {
                units = piece.Units;
            }

            string text = String.Format("{0}", units);
            Vector2 textSize = uiUnitFont.MeasureString(text);

            sprite.DrawString(uiUnitFont, text, position + new Vector2(piece.Icon.Width / 2, piece.Icon.Height) - new Vector2((textSize.X / 2), 0), Color.Black);

            if (piece.UnitsBonus > 0) {
                text = String.Format("(+{0})", piece.UnitsBonus);
                textSize = uiUnitFont.MeasureString(text);

                sprite.DrawString(uiUnitFont, text, position + new Vector2(piece.Icon.Width / 2, piece.Icon.Height + textSize.Y * 0.75f) - new Vector2((textSize.X / 2), 0), Color.Black);
            }
        }
        void Battle(Piece attacker, Piece defender, out int defenderShouldLoseUnits)
        {
            Debug.WriteLine(String.Format("{0} ({1}) attacks {2} ({3})", attacker, attacker.Units + attacker.UnitsBonus, defender, defender.Units));

            defenderShouldLoseUnits = 0;

            int defenderUnitsLeftAfterAttack = defender.Units - (attacker.Units + attacker.UnitsBonus);

            if (defenderUnitsLeftAfterAttack > 0) {
                defenderShouldLoseUnits = defender.Units - defenderUnitsLeftAfterAttack;
            } else {
                defenderShouldLoseUnits = defender.Units;
            }

            Debug.WriteLine(String.Format("{0} loses {1} units", defender, defenderShouldLoseUnits));
        }
        protected override void Update(GameTime gameTime)
        {
            blah += 5 * (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (levelIntroIsFading) {
                levelIntroAlpha -= levelIntroFadeRate * (float)gameTime.ElapsedGameTime.TotalSeconds;

                if (levelIntroAlpha <= 0) {
                    levelIntroIsFading = false;
                }
            }

            MouseState ms = Mouse.GetState();
            KeyboardState ks = Keyboard.GetState();

            if (gameEnded) {
                if (ksLast.IsKeyDown(Keys.Enter) && ks.IsKeyUp(Keys.Enter)) {
                    if (gameWon) {
                        level++;
                    }

                    Reset();

                    switch (level) {
                        case 0: FillPiecesLevelZero(); break;

                        default:
                        case 1: {
                                level = 1;

                                FillPiecesLevelOne();
                            } break;

                        case 2: FillPiecesLevelTwo(); break;
                        case 3: FillPiecesLevelThree(); break;
                        case 4: FillPiecesLevelFour(); break;
                        case 5: FillPiecesLevelFive(); break;
                    }
                }
            }

            grid.Projection = projection;
            grid.View = view;

            grid.Update(gameTime);

            for (int i = unitAnimations.Count - 1; i >= 0; i--) {
                UnitCountAnimation unitAnim = unitAnimations[i];

                unitAnim.Update(gameTime);

                if (unitAnim.Ended) {
                    unitAnimations.Remove(unitAnim);
                }
            }

            hoveredPiece = null;

            if (grid.IsCursorWithinGridBounds) {
                var resultPieces = pieces.Where(
                        p => p.Column == grid.CursorHoveringOnColumn &&
                             p.Row == grid.CursorHoveringOnRow
                    );

                if (resultPieces.Count() > 0) {
                    hoveredPiece = resultPieces.ElementAt(0);
                }

                pieceScreenCoordinate = GetScreenSpaceCoordinate(grid.CursorHoveringOnRow, grid.CursorHoveringOnColumn);
            }

            if (gameStarted) {
                Piece previousPiece = selectedPiece;

                selectedPiece = playerPieces[playerPieceCurrentIndex];

                if (selectedPiece != previousPiece) {
                    FindMovableTiles(selectedPiece);

                    grid.ShowMovableTiles = true;
                }

                if (ksLast.IsKeyDown(Keys.Enter) && ks.IsKeyUp(Keys.Enter)) {
                    if (!isPlacingPiece) {
                        Debug.WriteLine("Skipping turn..");

                        Turn();
                    }
                } else if (selectedPiece is BarracksPiece) {
                    BarracksPiece barracks = selectedPiece as BarracksPiece;

                    if (ksLast.IsKeyDown(Keys.D1) && ks.IsKeyUp(Keys.D1)) {
                        // only toggle if the piece being placed is the same as the production of this action
                        isPlacingPiece = isPlacingPiece && placingPiece is AttackPiece ? !isPlacingPiece : true;

                        placingPiece = !isPlacingPiece ?
                            null :
                            new AttackPiece()
                            {
                                Icon = iconSword,
                                Units = barracks.ProductionUnits
                            };
                    } else if (ksLast.IsKeyDown(Keys.D2) && ks.IsKeyUp(Keys.D2)) {
                        isPlacingPiece = isPlacingPiece && placingPiece is RangedPiece ? !isPlacingPiece : true;

                        placingPiece = !isPlacingPiece ?
                            null :
                            new RangedPiece()
                            {
                                Icon = iconArrow,
                                Units = barracks.ProductionUnits
                            };
                    } else if (ksLast.IsKeyDown(Keys.D3) && ks.IsKeyUp(Keys.D3)) {
                        isPlacingPiece = isPlacingPiece && placingPiece is DefendPiece ? !isPlacingPiece : true;

                        placingPiece = !isPlacingPiece ?
                            null :
                            new DefendPiece()
                            {
                                Icon = iconShield,
                                Units = barracks.ProductionUnits
                            };
                    }

                    grid.ShowMovableTiles = !isPlacingPiece;
                }
            } else {
                if (!gameEnded) {
                    if (ksLast.IsKeyDown(Keys.Space) && ks.IsKeyUp(Keys.Space)) {
                        Debug.WriteLine("Game started..");

                        grid.ShowMovableTiles = true;

                        gameStarted = true;

                        levelIntroIsFading = true;
                    }
                }
            }

            if (msLast.LeftButton == ButtonState.Pressed && ms.LeftButton == ButtonState.Released) {
                if (grid.IsCursorWithinGridBounds) {
                    uint targetRow = grid.CursorHoveringOnRow;
                    uint targetColumn = grid.CursorHoveringOnColumn;

                    if (selectedPiece != null && !(selectedPiece is EnemyPiece)) {
                        bool rowInRange = true;
                        bool columnInRange = true;

                        if (gameStarted) {
                            int tileDistance = selectedPiece is AttackPiece ? (hoveredPiece != null && hoveredPiece is EnemyPiece ? 1 : 2) : 1;

                            if (targetColumn > selectedPiece.Column + tileDistance ||
                                targetColumn < selectedPiece.Column - tileDistance) {
                                columnInRange = false;
                            }

                            if (targetRow > selectedPiece.Row + tileDistance ||
                                targetRow < selectedPiece.Row - tileDistance) {
                                rowInRange = false;
                            }
                        } else {
                            if (targetColumn < 9) {
                                columnInRange = false;
                            }
                        }

                        bool didAct = false;

                        if (hoveredPiece == null) {
                            if (isPlacingPiece) {
                                placingPiece.Row = targetRow;
                                placingPiece.Column = targetColumn;

                                pieces.Add(placingPiece);
                                playerPieces.Add(placingPiece);

                                placingPiece = null;
                                isPlacingPiece = false;

                                didAct = true;
                            } else if (rowInRange && columnInRange) {
                                selectedPiece.Row = targetRow;
                                selectedPiece.Column = targetColumn;

                                didAct = true;
                            }
                        } else {
                            if (gameStarted && !isPlacingPiece) {
                                Piece attacker = selectedPiece;
                                Piece defender = hoveredPiece;

                                if (defender is EnemyPiece) {
                                    if (!(attacker is DefendPiece) && !(attacker is BarracksPiece)) {
                                        bool attackIsAllowed =
                                            (attacker is AttackPiece && (rowInRange && columnInRange)) ||
                                            (attacker is RangedPiece && !(rowInRange && columnInRange));

                                        if (attackIsAllowed) {
                                            int defenderShouldLoseUnits = 0;

                                            Battle(attacker, defender, out defenderShouldLoseUnits);

                                            unitAnimations.Add(new UnitCountAnimation(-defenderShouldLoseUnits, GetScreenSpaceCoordinate(defender.Row, defender.Column))
                                            {
                                                Font = uiUnitFont,
                                                Sprite = sprite
                                            });

                                            if (defenderShouldLoseUnits == defender.Units) {
                                                piecesToRemove.Add(defender);
                                            } else {
                                                defender.Units -= defenderShouldLoseUnits;
                                            }

                                            didAct = true;
                                        }
                                    }
                                }
                            } else if (gameStarted && isPlacingPiece) {
                                if (!(hoveredPiece is EnemyPiece)) {
                                    if (hoveredPiece.GetType().Equals(placingPiece.GetType())) {
                                        hoveredPiece.Units += placingPiece.Units;

                                        unitAnimations.Add(new UnitCountAnimation(placingPiece.Units, GetScreenSpaceCoordinate(hoveredPiece.Row, hoveredPiece.Column))
                                        {
                                            Font = uiUnitFont,
                                            Sprite = sprite
                                        });

                                        isPlacingPiece = false;
                                        placingPiece = null;

                                        didAct = true;
                                    }
                                }
                            }
                        }

                        if (gameStarted && didAct) {
                            Turn();
                        }
                    }

                    if (!gameStarted && !gameEnded && hoveredPiece != null) {
                        Piece previousPiece = selectedPiece;
                        selectedPiece = hoveredPiece;

                        if (selectedPiece != previousPiece) {
                            FindMovableTiles(selectedPiece);
                        }
                    }
                }
            }

            msLast = ms;
            ksLast = ks;

            base.Update(gameTime);
        }
 public Action(Piece owner)
 {
     this.owner = owner;
 }
 public Move(Piece owner)
     : base(owner)
 {
     //Icon = GameContainer.ContentStore.Load<Texture2D>("whatever");
 }