public bool IsWinkelzugPossible(Field currentAdvokatTokenPosition) { return this.CheckVertical(currentAdvokatTokenPosition, -1) || this.CheckHorizontal(currentAdvokatTokenPosition, -1) || this.CheckHorizontal(currentAdvokatTokenPosition, 1) || this.CheckVertical(currentAdvokatTokenPosition, 1); }
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>(); }
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>(); }
public void Setup() { this.field = A.Fake<Field>(); this.testee = new FieldViewModel(this.field); }
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; }
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; }
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; }
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; }
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; }
private static bool FieldExistsAndIsFree(Field f) { return f != null && f.Token is NoToken; }
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); }
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(); }
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); }
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; }
public FieldViewModel(Field field) { this.field = field; }