Exemplo n.º 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);
        }
Exemplo n.º 2
0
        void EnemyTurn()
        {
            var   isFirstAttempt = true;
            Point target         = Point.Empty;

            while (enemy.canShootAgain)
            {
                if (isFirstAttempt || Ship.IsDead(world.playerField, target))
                {
                    target = enemy.Shoot(world);
                    LogEnemyShot(target);
                    if (Ship.IsMiddlePart(world.playerField[target.X, target.Y]))
                    {
                        var nearestEnd       = enemy.ShootToNearestEnd(target, world);
                        var nearestEndButton = PlayerField.Controls[nearestEnd.Y * 10 + nearestEnd.X] as Button;
                        nearestEndButton.Image = Converter.GetResource(world.playerField[nearestEnd.X, nearestEnd.Y]);
                        LogEnemyShot(nearestEnd);
                    }
                    isFirstAttempt = false;
                }
                else
                {
                    target = enemy.ShootAround(target, world);
                    LogEnemyShot(target);
                }
                var playerButton = PlayerField.Controls[target.Y * 10 + target.X] as Button;
                playerButton.Image = Converter.GetResource(world.playerField[target.X, target.Y]);
            }
            world.canShoot = true;
            if (world.playerShipCount == 0)
            {
                MessageBox.Show("Поражение!");
                listBox1.Items.Add("Поражение!");
            }
            UpdateListBox();
        }
Exemplo n.º 3
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);
        }