Exemple #1
0
        void BuildEnemyField(TableLayoutPanel field)
        {
            Action <Button> onClick = (b) =>
            {
                if (world.GameState == GameStates.Game)
                {
                    if (!world.canShoot)
                    {
                        MessageBox.Show("Больше нельзя стрелять");
                        return;
                    }
                    var shotAttempt = world.TryShoot(field.GetCellPosition(b).Row, field.GetCellPosition(b).Column);
                    if (!shotAttempt)
                    {
                        MessageBox.Show("Сюда нельзя стрелять");
                        return;
                    }
                    var cell = world.enemyField[field.GetCellPosition(b).Row, field.GetCellPosition(b).Column];
                    b.Image            = Converter.GetResource(cell);
                    listBox1.ForeColor = Color.Black;
                    listBox1.Items.Add($"Вы стреляете по позиции X: {field.GetCellPosition(b).Row} Y: {field.GetCellPosition(b).Column}");
                    if (Ship.IsShip(cell))
                    {
                        listBox1.Items.Add("Вы подбили корабль!");
                        if (Ship.IsDead(world.enemyField, new Point(field.GetCellPosition(b).Row, field.GetCellPosition(b).Column)))
                        {
                            listBox1.Items.Add("Вы потопили корабль!");
                        }
                    }
                    else
                    {
                        listBox1.Items.Add("Промах");
                    }
                    UpdateListBox();
                    if (world.canShoot)
                    {
                        endTurn.BackColor = Color.Orange;
                        endTurn.Text      = "Передать ход";
                    }
                    else
                    {
                        endTurn.BackColor = Color.GreenYellow;
                        endTurn.Text      = "Ход завершен";
                    }
                    endTurn.Enabled = true;
                    if (world.enemyShipCount == 0)
                    {
                        MessageBox.Show("ПОБЕДА!!!");
                        listBox1.Items.Add("Победа!");
                        return;
                    }
                }
            };

            FieldConstructor.BuildField(field, onClick);
        }
Exemple #2
0
        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);
        }