public Piece(int i, int j) { this.i = i; this.j = j; ph = new PieceHolder(this); circles = new Ellipse[4]; id = i + 10 * j; img = new Rectangle(); img.Name = "R" + i.ToString() + j.ToString(); img.MouseDown += new MouseButtonEventHandler(img_MouseDown); img.MouseMove += new MouseEventHandler(img_MouseMove); img.MouseUp += new MouseButtonEventHandler(img_MouseUp); img.MouseLeave += new MouseEventHandler(img_MouseLeave); }
private void Snap(PieceHolder pHold) //closest only { bool snapped = false; double xdist; double ydist; foreach (Piece p in pHold.pieces) { if (p.j != 0) { if (!pHold.pieces.Contains(puzzle[p.i][p.j - 1])) { if (Math.Abs((puzzle[p.i][p.j - 1].X + square / pieces) - p.X) < snapRange && Math.Abs(puzzle[p.i][p.j - 1].Y - p.Y) < snapRange) { xdist = (puzzle[p.i][p.j - 1].X + square / pieces - p.X) * -1; ydist = (puzzle[p.i][p.j - 1].Y - p.Y) * -1; pHold.Move(new Piece(), xdist, ydist); p.ph.Snap(puzzle[p.i][p.j - 1]); snapped = true; } } } if (p.j != pieces - 1 && !snapped) { if (!pHold.pieces.Contains(puzzle[p.i][p.j + 1])) { if (Math.Abs((puzzle[p.i][p.j + 1].X - square / pieces) - p.X) < snapRange && Math.Abs(puzzle[p.i][p.j + 1].Y - p.Y) < snapRange) { xdist = (puzzle[p.i][p.j + 1].X - square / pieces - p.X) * -1; ydist = (puzzle[p.i][p.j + 1].Y - p.Y) * -1; pHold.Move(new Piece(), xdist, ydist); p.ph.Snap(puzzle[p.i][p.j + 1]); snapped = true; } } } if (p.i != 0 && !snapped) { if (!pHold.pieces.Contains(puzzle[p.i - 1][p.j])) { if (Math.Abs(puzzle[p.i - 1][p.j].X - p.X) < snapRange && Math.Abs((puzzle[p.i - 1][p.j].Y + square / pieces) - p.Y) < snapRange) { xdist = (puzzle[p.i - 1][p.j].X - p.X) * -1; ydist = (puzzle[p.i - 1][p.j].Y + square / pieces - p.Y) * -1; pHold.Move(new Piece(), xdist, ydist); p.ph.Snap(puzzle[p.i - 1][p.j]); snapped = true; } } } if (p.i != pieces - 1 && !snapped) { if (!pHold.pieces.Contains(puzzle[p.i + 1][p.j])) { if (Math.Abs(puzzle[p.i + 1][p.j].X - p.X) < snapRange && Math.Abs((puzzle[p.i + 1][p.j].Y - square / pieces) - p.Y) < snapRange) { xdist = (puzzle[p.i + 1][p.j].X - p.X) * -1; ydist = (puzzle[p.i + 1][p.j].Y - square / pieces - p.Y) * -1; pHold.Move(new Piece(), xdist, ydist); p.ph.Snap(puzzle[p.i + 1][p.j]); snapped = true; } } } if (snapped) { break; } } if (snapped) { snapCount++; if (snapCount == (pieces * pieces) - 1) { isPlaying = false; snapCount = 0; for (int i = 0; i < pieces; i++) { for (int j = 0; j < pieces; j++) { puzzle[i][j].img.StrokeThickness = 0; } } MessageBox.Show("Puzzle Completed!"); } } }