public static IList <IJigsawPiece> ScramblePieces(IList <IJigsawPiece> pieces, int rows, int cols) { var random = new Random(); var temp = new List <KeyValuePair <int, int> >(); foreach (JigsawPieceBase originPiece in pieces) { int row = random.Next(0, rows); int col = random.Next(0, cols); while (temp.Exists(t => (t.Key == row) && (t.Value == col))) { row = random.Next(0, rows); col = random.Next(0, cols); } temp.Add(new KeyValuePair <int, int>(row, col)); IJigsawPiece targetPiece = pieces.Single(p => p.OriginRow == row && p.OriginColumn == col); //swap positions var tempPoint = new Point(originPiece.Position.X, originPiece.Position.Y); originPiece.Position = new Point(targetPiece.Position.X, targetPiece.Position.Y); targetPiece.Position = tempPoint; //Swap column and row numbers int targetColumn = targetPiece.CurrentColumn; int targetRow = targetPiece.CurrentRow; targetPiece.CurrentColumn = originPiece.CurrentColumn; targetPiece.CurrentRow = originPiece.CurrentRow; originPiece.CurrentColumn = targetColumn; originPiece.CurrentRow = targetRow; } return(pieces); }
private void MovePieces(IJigsawPiece original, IJigsawPiece target) { if ((original != null) && (target != null)) { this.MovePiece(target, original.Position); this.MovePiece(original, target.Position); //swap position var tempOriginPoint = new Point(original.Position.X, original.Position.Y); original.Position = new Point(target.Position.X, target.Position.Y); target.Position = tempOriginPoint; //Swap column and row numbers int targetX = target.CurrentColumn; int targetY = target.CurrentRow; target.CurrentColumn = original.CurrentColumn; target.CurrentRow = original.CurrentRow; original.CurrentColumn = targetX; original.CurrentRow = targetY; //Adjust Z Index int zIndex = (int)original.GetValue(Panel.ZIndexProperty); original.SetValue(Panel.ZIndexProperty, zIndex + 1); if (this.CheckPieces()) { MessageBox.Show("You have successfully finished this puzzel", "Congratulations!"); this.ShowPicture(); } } }
private void MovePiece(IJigsawPiece piece, Point toPoint) { var uiElement = piece as UIElement; if (uiElement != null) { Canvas.SetLeft(uiElement, toPoint.X); Canvas.SetTop(uiElement, toPoint.Y); } }
private void Piece_MouseUp(object sender, MouseButtonEventArgs e) { if (this._isDragging) { this._isDragging = false; var pieceView = (JigsawPieceBase)sender; pieceView.ReleaseMouseCapture(); pieceView.SetValue(Panel.ZIndexProperty, 10); IJigsawPiece originalModel = this.FindPieceByView(pieceView); IJigsawPiece targetModel = this.FindPieceByMousePosition(e.GetPosition(this.Window.Canvas)); this.MovePieces(originalModel, targetModel); } }
public static IList <IJigsawPiece> CreateImagePuzzelPieces(BitmapImage bitmapImage, int totalCols, int totalRows, double pieceSize, PieceType pieceType) { var pieces = new List <IJigsawPiece>(); for (int row = 0; row < totalRows; row++) { for (int col = 0; col < totalCols; col++) { IJigsawPiece jigsawPiece = CreateImagePuzzelPiece(bitmapImage, col, row, pieceSize, pieceType); pieces.Add(jigsawPiece); } } return(pieces); }
public void TestConvertPointWithValidStrings() { const int rows = 4; const int columns = 4; var imaeSource = new BitmapImage(); var pieces = new List <IJigsawPiece>(); for (int row = 0; row < rows; row++) { for (int col = 0; col < columns; col++) { IJigsawPiece jigsawPiece = JigsawPieceFactory.CreateImagePuzzelPiece(imaeSource, col, row, 200, PieceType.SimpleBezier); pieces.Add(jigsawPiece); } } var scrambledPieces = JigsawHelper.ScramblePieces(pieces, rows, columns); foreach (var jigsawPiece in scrambledPieces) { var tempModel = BezierCurveHelper.FindModel(jigsawPiece.CurrentColumn, jigsawPiece.CurrentRow); Assert.AreEqual(jigsawPiece.Position, tempModel.Position); } }
private IJigsawPiece FindPieceByView(IJigsawPiece piece) { return(this.Pieces.SingleOrDefault <IJigsawPiece>(p => (p == piece))); }
private IJigsawPiece FindPieceByView(IJigsawPiece piece) { return this.Pieces.SingleOrDefault<IJigsawPiece>(p => (p == piece)); }