예제 #1
0
 public bool IsWinkelzugPossible(Field currentAdvokatTokenPosition)
 {
     return this.CheckVertical(currentAdvokatTokenPosition, -1) ||
         this.CheckHorizontal(currentAdvokatTokenPosition, -1) ||
         this.CheckHorizontal(currentAdvokatTokenPosition, 1) ||
         this.CheckVertical(currentAdvokatTokenPosition, 1);
 }
예제 #2
0
        public void MustCreateWinkelzug_WhenFieldContainsAdvocatToken()
        {
            var field = new Field(0, 0) { Token = new AdvocatToken() };

            var move = this.testee.CreateByToken(field.Token, new List<Field> { field });

            move.Should().BeOfType<Winkelzug>();
        }
예제 #3
0
        public void MustCreateParagraphenzug_WhenFieldContainsParagraphToken()
        {
            var field = new Field(1, 0) { Token = new ParagraphToken() };

            var move = this.testee.CreateByToken(field.Token, new List<Field> { field });

            move.Should().BeOfType<Paragraphenzug>();
        }
예제 #4
0
 public void Setup()
 {
     this.field = A.Fake<Field>();
     this.testee = new FieldViewModel(this.field);
 }
예제 #5
0
        public MoveResult PlaceToken(Field field)
        {
            if (this.currentMove == null)
            {
                if (field.Token is NoToken)
                {
                    return MoveResult.CreateInvalidResult("Der erste Zug muss von einem Feld mit einem Spielstein stattfinden.");
                }

                var advokatTokenToTurn = this.Fields.First(t => t.Token is AdvocatToken && t.Token.Color == field.Token.Color);

                if (this.IsWinkelzugPossible(advokatTokenToTurn) == false)
                {
                    this.IsFinished = true;
                    var stringBuilder = new StringBuilder();
                    stringBuilder.Append("Spiel beendet!" + Environment.NewLine + "Gewinner:");
                    foreach (var winner in this.Winners)
                    {
                        stringBuilder.Append(Environment.NewLine + winner.GetColorName());
                    }

                    return MoveResult.CreateInvalidResult(stringBuilder.ToString());
                }

                this.currentMove = this.moveFactory.CreateByToken(field.Token, this.Fields);
            }

            var moveResult = this.currentMove.PerformMove(field);

            if (this.currentMove.IsFinished)
            {
                this.currentMove = null;
            }

            Debug.WriteLine(this.ToString());
            return moveResult;
        }
예제 #6
0
        private Field MoveVertical(Field currentAdvokatTokenPosition, int rowsToMoveDown)
        {
            if ((currentAdvokatTokenPosition.Row > 0 && rowsToMoveDown < 0) || (currentAdvokatTokenPosition.Row < 7 && rowsToMoveDown > 0))
            {
                return this.Fields.SelectByPosition(currentAdvokatTokenPosition.Row + rowsToMoveDown, currentAdvokatTokenPosition.Column);
            }

            return null;
        }
예제 #7
0
        private Field MoveHorizontal(Field currentAdvokatTokenPosition, int columnsToMoveRight)
        {
            if ((currentAdvokatTokenPosition.Column > 0 && columnsToMoveRight < 0) || (currentAdvokatTokenPosition.Column < 7 && columnsToMoveRight > 0))
            {
                return this.Fields.SelectByPosition(currentAdvokatTokenPosition.Row, currentAdvokatTokenPosition.Column + columnsToMoveRight);
            }

            return null;
        }
예제 #8
0
        private bool CheckVertical(Field currentAdvokatTokenPosition, int numberofColumnsToMoveDown)
        {
            var field = this.MoveVertical(currentAdvokatTokenPosition, numberofColumnsToMoveDown);
            var candoWinkelZug = false;
            if (FieldExistsAndIsFree(field))
            {
                var tempField = this.MoveHorizontal(field, -1);
                if (FieldExistsAndIsFree(tempField))
                {
                    return true;
                }

                tempField = this.MoveHorizontal(field, 1);
                if (FieldExistsAndIsFree(tempField))
                {
                    return true;
                }

                candoWinkelZug = this.CheckVertical(field, numberofColumnsToMoveDown);
            }

            if (candoWinkelZug)
            {
                return true;
            }

            return false;
        }
예제 #9
0
        private bool CheckHorizontal(Field currentAdvokatTokenPosition, int numberOfRowsToMoveRight)
        {
            var field = this.MoveHorizontal(currentAdvokatTokenPosition, numberOfRowsToMoveRight);
            bool canDoWinkelZug = false;
            if (FieldExistsAndIsFree(field))
            {
                var tempField = this.MoveVertical(field, -1);
                if (FieldExistsAndIsFree(tempField))
                {
                    return true;
                }

                tempField = this.MoveVertical(field, 1);
                if (FieldExistsAndIsFree(tempField))
                {
                    return true;
                }

                canDoWinkelZug = this.CheckHorizontal(field, 1);
            }

            if (canDoWinkelZug)
            {
                return true;
            }

            return false;
        }
예제 #10
0
 private static bool FieldExistsAndIsFree(Field f)
 {
     return f != null && f.Token is NoToken;
 }
예제 #11
0
        public void PlaceTokenDelegatesMoveWhenTokenIsNullAndCalledTheSecondTime()
        {
            var move = A.Fake<IMove>();
            var startField = new Field(0, 0) { Token = new AdvocatToken() };
            var endField = new Field(1, 0);
            A.CallTo(() => this.moveFactory.CreateByToken(A<Token>._, A<IEnumerable<Field>>._)).Returns(move);

            this.testee.PlaceToken(startField);
            this.testee.PlaceToken(endField);

            A.CallTo(() => move.PerformMove(A<Field>._)).MustHaveHappened(Repeated.Exactly.Twice);
        }
예제 #12
0
        public void PlaceTokenCallsMoveFactoryWhenTokenIsNotNullAndCalledTheFirstTime()
        {
            var field = new Field(0, 0) { Token = new AdvocatToken() };
            this.board.SelectByPosition(4, 4).Token = new AdvocatToken();
            this.testee.PlaceToken(field);

            A.CallTo(() => this.moveFactory.CreateByToken(A<Token>._, A<IEnumerable<Field>>._)).MustHaveHappened();
        }
예제 #13
0
        public void PlaceTokenCallsMoveFactoryAgainWhenPreviousMoveReportedIsFinished()
        {
            var move = A.Fake<IMove>();
            var startField = new Field(0, 0) { Token = new AdvocatToken() };
            var secondField = new Field(1, 0);
            var thirdField = new Field(1, 1) { Token = new AdvocatToken() };
            this.board.SelectByPosition(4, 4).Token = new AdvocatToken();
            A.CallTo(() => this.moveFactory.CreateByToken(A<Token>._, A<IEnumerable<Field>>._)).Returns(move);

            // CreateByToken is called here as no move has been set yet.
            this.testee.PlaceToken(startField);

            A.CallTo(() => move.IsFinished).Returns(true);
            this.testee.PlaceToken(secondField);

            // CreateByToken is called here because move has previously beent set to null.
            this.testee.PlaceToken(thirdField);

            A.CallTo(() => this.moveFactory.CreateByToken(A<Token>._, A<IEnumerable<Field>>._)).MustHaveHappened(Repeated.Exactly.Twice);
        }
예제 #14
0
        private Field GetField(string fieldData)
        {
            if (string.IsNullOrEmpty(fieldData) || (fieldData.Length < 7 && fieldData.Length > 8))
            {
                throw new ArgumentException("The provided field data is not valid.");
            }

            var fieldPositionStartIndex = fieldData.IndexOf('[') + 1;
            var fieldPositionEndIndex = fieldData.IndexOf(']') - 1;

            var row = int.Parse(fieldData.Substring(fieldPositionStartIndex, 1));
            var column = int.Parse(fieldData.Substring(fieldPositionEndIndex, 1));

            var result = new Field(row, column);

            switch (fieldData.Substring(0, 2))
            {
                case "AT":
                    result.Token = new AdvocatToken();
                    break;
                case "PT":
                    result.Token = new ParagraphToken();
                    break;
                case "NO":
                    return result;
                default:
                    throw new ArgumentException("Provided token type is invalid!");
            }

            var containsColor = fieldData.Substring(3, 1) == "[";

            if (containsColor)
            {
                switch (fieldData.Substring(2, 1))
                {
                    case "R":
                        result.Token.Color = Colors.Red;
                        break;
                    case "G":
                        result.Token.Color = Colors.Green;
                        break;
                    case "B":
                        result.Token.Color = Colors.Blue;
                        break;
                    case "Y":
                        result.Token.Color = Colors.Yellow;
                        break;
                }
            }

            return result;
        }
예제 #15
0
 public FieldViewModel(Field field)
 {
     this.field = field;
 }