コード例 #1
0
ファイル: MainForm.cs プロジェクト: Naklen/NavalBattle
        void BuildPlayerField(TableLayoutPanel field)
        {
            Action <Button> onClick = (b) =>
            {
                if (world.GameState == GameStates.Preparing)
                {
                    if (selectedShip == null)
                    {
                        MessageBox.Show("Выберите корабль");
                    }
                    else if (selectedShip.Item3 == 0)
                    {
                        MessageBox.Show("Все корабли этого типа уже расставлены");
                    }
                    else
                    {
                        var shipPlaced = world.TryPlaceShip(field.GetCellPosition(b).Row, field.GetCellPosition(b).Column, selectedShip.Item4, direction);
                        if (!shipPlaced)
                        {
                            if (world.GetPlacedShipCount() == world.playerShipCount)
                            {
                                MessageBox.Show("Все корабли расставлены");
                            }
                            else
                            {
                                MessageBox.Show("Здесь нельзя разместить корабль");
                            }
                        }
                        else
                        {
                            if (selectedShip.Item4 == 1)
                            {
                                b.Image = Converter.GetResource(world.playerField[field.GetCellPosition(b).Row, field.GetCellPosition(b).Column]);
                            }
                            else
                            {
                                var x = field.GetCellPosition(b).Row;
                                var y = field.GetCellPosition(b).Column;
                                b.Image = Converter.GetResource(world.playerField[x, y]);
                                for (var i = 1; i < selectedShip.Item4; i++)
                                {
                                    if (direction == ShipDirection.Right)
                                    {
                                        var nextButton = PlayerField.Controls[(y + i) * 10 + x] as Button;
                                        nextButton.Image = Converter.GetResource(world.playerField[x, y + i]);
                                    }
                                    else
                                    {
                                        var nextButton = PlayerField.Controls[y * 10 + x - i] as Button;
                                        nextButton.Image = Converter.GetResource(world.playerField[x - i, y]);
                                    }
                                }
                            }
                            UpdateSelectedTuple(selectedShip, selectedShip.Item3 - 1);
                            selectedShip.Item2.Text = selectedShip.Item3.ToString();
                        }
                    }
                }
                //UpdateCell(field.GetCellPosition(b).Row, field.GetCellPosition(b).Column, field, world.playerField);
            };

            FieldConstructor.BuildField(field, onClick);
        }