/// <summary> /// Set iscompleted on true, call targetfieldistrap method, call targetfieldcontainsitem method, move the player /// and decrement the playerremainingmoves when the sourceplayer has more moves left, the targetfield is valid and the targetfield is in range /// </summary> public override void Execute() { if (SourcePlayer.PlayerRemainingMoves < 1) { throw new CantMoveException("No more moves left"); } else if (TargetFieldIsInvalid() == true) { throw new CantMoveException("Targetfield is blocked by another player or is a wall"); } else if (VerifyMoveRange() == false) { throw new CantMoveException("Targetfield is out of range"); } else { TargetFieldIsTrap(); TargetFieldContainsItem(); if (!(Level.PlayingField[XPosition, YPosition].FieldType is Trapdoor)) { SourcePlayer.MovePlayer(XPosition, YPosition); } SourcePlayer.PlayerRemainingMoves--; IsCompleted = true; } }
/// <summary> /// Checks if the targetfield is a trap /// If the target field is a trap it will be triggered and if it is a trapdoor the player will be additionally set to a random free and accessable field /// </summary> public void TargetFieldIsTrap() { if (Level.PlayingField[_xPosition, _yPosition].FieldType is Trap) { Level.PlayingField[_xPosition, _yPosition].FieldType.Effects(SourcePlayer); if (Level.PlayingField[_xPosition, _yPosition].FieldType is Trapdoor) { Boolean successfulMoving = false; int randomX; int randomY; while (successfulMoving == false) { randomX = new Random().Next(0, 19); randomY = new Random().Next(0, 19); if (Level.PlayingField[randomX, randomY].FieldType is Floor && Level.FieldBlockedByPlayer(randomX, randomY) == false) { successfulMoving = true; SourcePlayer.MovePlayer(randomX, randomY); } else { //Player can not move to a wall or another trap } } } } else { //Field is not a trap } }