예제 #1
0
        // fills the grid with rectangles
        private void InitBoard(Panel grid, bool isCom)
        {
            var panel = new WrapPanel {
                MaxHeight           = Constants.Window.Height,
                MaxWidth            = Constants.Window.Width,
                HorizontalAlignment = isCom ? HorizontalAlignment.Left : HorizontalAlignment.Right,
                VerticalAlignment   = VerticalAlignment.Top,
                Name = isCom ? "ComField" : "PlayerField"
            };
            var dimension  = Math.Sqrt(Constants.Window.Width * 4) + 1;
            var rectangles = new List <Rectangle>(); // put all of our rectangles into one object to randomly place ships

            for (var i = 0; i < 10; i++)
            {
                for (var j = 0; j < 10; j++)
                {
                    var rect = new Rectangle {
                        Width     = dimension,
                        Height    = dimension,
                        Fill      = new SolidColorBrush(Color.FromRgb(0, 127, 0)),
                        Stroke    = new SolidColorBrush(Colors.White),
                        Uid       = $"x: {j}, y: {i}, {panel.Name}",
                        Name      = "NoShip",
                        IsEnabled = isCom
                    };

                    rect.MouseLeftButtonUp += Calculate;
                    rectangles.Add(rect);
                    panel.Children.Add(rect);
                }
            }

            if (isCom)
            {
                Calculation.AiPlaceShips(rectangles);
            }
            else
            {
                this.InitShipSelectionForPlayer(grid);
                Calculation.SetupAlgorithm();
            }

            grid.Children.Add(panel);
        }