/// <summary> /// Places a child to it position. /// </summary> /// <param name="movement"></param> private void RefreshChild(PositionMovement movement) { PivotItem child; if (this.children.TryGetValue(movement.Old, out child) == false) { // if the child has not been generated yet, it cannot be placed anywhere return; } // change the position of the child if (movement.IsMoved) { this.children.Remove(movement.Old); this.children.Add(movement.New, child); } if (movement.New < 0) { // hide child with negative position child.Visibility = Visibility.Collapsed; } else { if (movement.IsMoved) { // refresh and show moved child this.Generator.Reuse(movement.New, child); child.Visibility = Visibility.Visible; } // arrange visible child, even it has not been moved this.ArrangeItemAtPosition(child, movement.New); } }
/// <summary> /// Calculates position movements and calls RefreshChild for each child. /// </summary> private void RefreshChildren() { if (this.currentPage < 0) { return; } var movements = new PositionMovement[ChildrenCount]; for (int i = 0; i < ChildrenCount; i++) { // define new positions as [currentPage - 1, currentPage, currentPage + 1] var newPosition = this.currentPage + i - 1; if (newPosition == this.Count) { // if Current Page is last, last child will have position = -2 (invisible) newPosition = -2; } movements[i] = new PositionMovement(newPosition); } // mark not moved children for (int i = 0; i < ChildrenCount; i++) { if (this.children.ContainsKey(movements[i].New)) { movements[i].MarkAsNotMoved(); } } // set old position for moved child (usually it only one) foreach (var oldPosition in this.children.Keys) { // ensure there are no movement for this child if (movements.Any(m => m.Old == oldPosition) == false) { for (int i = 0; i < ChildrenCount; i++) { // search "free" movement for the child if (movements[i].Old < 0 && movements[i].IsMoved) { movements[i].Old = oldPosition; break; } } } } // apply movements foreach (var movement in movements) { this.RefreshChild(movement); } }
/// <summary> /// Check for any four in a row. /// </summary> /// <param name="playerPieceToCheck">Player's game piece to check</param> /// <returns>true if a player has won</returns> private bool FourInARow(PlayerPiece playerPieceToCheck, GameboardPosition gameboardPosition) { //Define linear checks for a win PositionMovement[] down = new PositionMovement[] { PositionMovement.Down }; PositionMovement[] leftRight = new PositionMovement[] { PositionMovement.Left, PositionMovement.Right }; PositionMovement[] UrightDleft = new PositionMovement[] { PositionMovement.UpRight, PositionMovement.DownLeft }; PositionMovement[] UleftDright = new PositionMovement[] { PositionMovement.UpLeft, PositionMovement.DownRight }; int counter = 0; //Check down counter = ConsecutivePieces(playerPieceToCheck, gameboardPosition, down); if (CheckForFourPieces(counter)) { return(true); } //Check left and right counter = ConsecutivePieces(playerPieceToCheck, gameboardPosition, leftRight); if (CheckForFourPieces(counter)) { return(true); } //Check up right and down left counter = ConsecutivePieces(playerPieceToCheck, gameboardPosition, UrightDleft); if (CheckForFourPieces(counter)) { return(true); } //Check up left and down right counter = ConsecutivePieces(playerPieceToCheck, gameboardPosition, UleftDright); if (CheckForFourPieces(counter)) { return(true); } return(false); }
/// <summary> /// Check if the next piece in line is a valid consecutive piece /// </summary> /// <param name="piece"></param> /// <param name="gameboardPosition"></param> /// <param name="movement"></param> /// <param name="numberOfMoves"></param> /// <returns></returns> private bool CheckNextPiece(PlayerPiece piece, GameboardPosition gameboardPosition, PositionMovement movement, int numberOfMoves) { //Define constraints const int top = -1; const int bottom = 6; const int right = 7; const int left = -1; //The next position that we are checking GameboardPosition newPosition = new GameboardPosition(-1, -1); //Based on the given Movement, set newPosition to the next position in line switch (movement) { case PositionMovement.UpRight: newPosition = MovePositionUpRight(gameboardPosition, numberOfMoves); break; case PositionMovement.Right: newPosition = MovePositionRight(gameboardPosition, numberOfMoves); break; case PositionMovement.DownRight: newPosition = MovePositionDownRight(gameboardPosition, numberOfMoves); break; case PositionMovement.Down: newPosition = MovePositionDown(gameboardPosition, numberOfMoves); break; case PositionMovement.DownLeft: newPosition = MovePositionDownLeft(gameboardPosition, numberOfMoves); break; case PositionMovement.Left: newPosition = MovePositionLeft(gameboardPosition, numberOfMoves); break; case PositionMovement.UpLeft: newPosition = MovePositionUpLeft(gameboardPosition, numberOfMoves); break; default: break; } //Check if new position is within the borders if (newPosition.Row < bottom && newPosition.Row > top && newPosition.Column < right && newPosition.Column > left) { //Check if the new position is the right piece if (GetPlayerPieceByGameBoardPosition(newPosition) == piece) { return(true); } else { return(false); } } else { return(false); } }