/* * Initiate the drop event and if successful, move image from menu to board * @author Stanislav Yurchenko * @version 03/12/2017 */ private void DragAndDrop(object sender, DragEventArgs e) { Label lbl = sender as Label; // Find where the event was fired int row = Grid.GetRow(lbl); int column = Grid.GetColumn(lbl); Image img = new Image(); // Get name of ship from the dragged content Ship s = GetShipReference(e.Data.GetData(typeof(String)) as String); // Get the image reference BitmapImage bitImage = new BitmapImage(new Uri(s.GetPath(), UriKind.Relative)); img.Source = bitImage; if (s.Vertical) { img.SetValue(Grid.RowSpanProperty, s.GetLength()); } else { img.SetValue(Grid.ColumnSpanProperty, s.GetLength()); } try { user.GameGrid.AddShip(s, row, column); } catch (IndexOutOfRangeException) { return; } // Bind the image to the specific row and column Grid.SetRow(img, row); Grid.SetColumn(img, column); // Add the image to the board PlayerGrid.Children.Add(img); // Render the label inactive lbl.AllowDrop = false; // Remove ship image from menu, check all images in the grid foreach (Image menuShip in SeparationGrd.Children.OfType <Image>()) { if (menuShip.Name.Equals(s.GetName())) { SeparationGrd.Children.Remove(menuShip); // Will complain about changing list otherwise break; } } if (user.GameGrid.ShipsPlaced()) { Mittelspiel(); } }
/* * Draw the ship sent to this method * @author Stanislav Yurchenko * @version 06/12/2017 */ private void DrawShip(Ship s) { bool neverDrawn = true; for (int i = 0; i < drawnElements.Length; i++) { if (drawnElements[i] == null) { drawnElements[i] = s.GetName(); } else if (s.GetName().Equals(drawnElements[i])) { neverDrawn = false; } } if (neverDrawn) { int row; int column; BitmapImage img; column = s.Coordinates[0] % 10; row = s.Coordinates[0] / 10; img = new BitmapImage(new Uri(s.GetPath(), UriKind.Relative)); Image image = new Image() { Source = img }; Grid.SetRow(image, row); Grid.SetColumn(image, column); Grid.SetZIndex(image, 98); EnemyGrd.Children.Add(image); if (s.Vertical) { image.SetValue(Grid.RowSpanProperty, s.GetLength()); } else { image.SetValue(Grid.ColumnSpanProperty, s.GetLength()); } } }
/* * Method tries to add ship to board * @author Stanislav Yurchenko * @version 03/12/2017 */ public void AddShip(Ship s, int row, int column) { int[] coordinates = new int[s.GetLength()]; // check if vertical ship is too close to bottom if (s.Vertical && row + s.GetLength() > 10) { throw new IndexOutOfRangeException("Too close to border"); // check if horizontal ship is too close to right side } else if (!s.Vertical && column + s.GetLength() > 10) { throw new IndexOutOfRangeException("Too close to border"); } // Find every coordinate for ship and assign them to the property as long as they don't overlap for (int i = 0; i < coordinates.Length; i++) { coordinates[i] = row * 10 + column; foreach (Ship boat in ships) { if (boat.Coordinates != null && !s.GetName().Equals(boat.GetName()) && boat.Overlap(coordinates[i], true)) { s.Coordinates = null; throw new IndexOutOfRangeException("Overlaps with other ship"); } } if (s.Vertical) { row++; } else { column++; } } s.Coordinates = coordinates; s.GenerateFrame(); }