Esempio n. 1
0
 private void Restart()
 {
     quantityShipTypeByMass = new int[4];
     IsPlacementOverFlag    = false;
     shot = new int[2];
     hitTheTargetPoints = new int[2];
     didNotHitPoints    = new int[2];
     isBattleFlag       = false;
     myBtField          = new Field(50);
     enemyBtField       = new Field(400);
     TemplateShipField  = new Field(400, Cell.TypeOfCell.onlyShip);
     fieldOfArrangement = new Field(50, Cell.TypeOfCell.onlyShip);
     isConnectingNow    = false;
     IsEnemyReadyFlag   = false;
     amImovingFlag      = false;
     ShipSize           = 1;
     prevCell           = new List <Cell>();
     partsLabels        = new List <Label>();
     fields             = new Stack <Field>();
     orientation        = Ship.Orientation.horizontal;
     init();
     nameTextBox.Text = name;
     regToServer_Click(null, null);
     isVisibleArrangementVisible(false);
     this.Size = (new Size(330, 310));
 }
Esempio n. 2
0
 private void HorizontalradioButton_CheckedChanged(object sender, EventArgs e)
 {
     if (((RadioButton)sender).Checked)
     {
         orientation = Ship.Orientation.horizontal;
     }
 }
Esempio n. 3
0
        private void init()
        {
            Ship.loadingImageShip();
            quantityShipTypeByMass[0] = 4;
            quantityShipTypeByMass[1] = 3;
            quantityShipTypeByMass[2] = 2;
            quantityShipTypeByMass[3] = 1;
            InitializeComponent();
            receivePoint = new IPEndPoint(IPAddress.Any, 0);
            string addres = "127.0.0.1";
            int    port   = 8888;

            client         = new TcpClient(addres, port);
            b              = new Bitmap("fon.jpg");
            DoubleBuffered = true;
            test();
            if (HorisontalradioButton.Checked)
            {
                orientation = Ship.Orientation.vertical;
            }
            if (verticalradioButton.Checked)
            {
                orientation = Ship.Orientation.horizontal;
            }
            thread = new Thread(new ThreadStart(WaitForPackets));
            thread.Start();
            TemplateShip();
            isVisibleMainControls(true);
            isVisibleArrangementVisible(false);
            TimerCallback tm = new TimerCallback(updateForm);

            time = new System.Threading.Timer(tm, null, 0, 100);
            isVisibleArrangementVisible(false);
        }
Esempio n. 4
0
 public void createShipInField(Cell cell, Ship.Orientation orientation, Field field, bool setTypeShip = true)
 {
     this.orientation = orientation;
     cellsOfTheShipList.Add(cell);
     if (setTypeShip)
     {
         cell.type = Cell.TypeOfCell.ship;
     }
     initSpriteShip();
     field.listShips.Add(this);
 }
Esempio n. 5
0
 public void createShipInField(List <Cell> cells, Ship.Orientation orientation, Field field, bool setTypeShip = true)
 {
     this.orientation = orientation;
     cellsOfTheShipList.AddRange(cells);
     if (setTypeShip)
     {
         foreach (Cell c in cells)
         {
             c.type = Cell.TypeOfCell.ship;
         }
     }
     initSpriteShip();
     field.listShips.Add(this);
 }
Esempio n. 6
0
        /* Координаты поля
         *   x
         * y 0 1 2
         *   1
         *   2
         */
        public void CreateShips(bool isPlayer)
        {
            int    size = 4;          // Размер корабля
            int    count = 0;         // Для подсчета количества кораблей нужного размера
            char   orientation = 'h'; // Положение корабля на поле
            Random rnd = new Random(DateTime.Now.Millisecond);
            int    x, y;

            for (int i = 0; i < 10; i++) // 10 кораблей
            {
                try
                { // Можно ввести только координаты от 0 до 9 вручную или автоматически
                    if (isPlayer)
                    {
                        Console.WriteLine("Coord for {0}-x ship", size);
                        x = int.Parse(Console.ReadLine());
                        if (x > 9 || x < 0)
                        {
                            throw new FormatException();
                        }
                        y = int.Parse(Console.ReadLine());
                        if (y > 9 || y < 0)
                        {
                            throw new FormatException();
                        }
                    }
                    else
                    {
                        x = rnd.Next(0, 10);
                        y = rnd.Next(0, 10);
                    }
                }
                catch (FormatException)
                {
                    if (isPlayer)
                    {
                        Console.WriteLine("Only numbers less than 10 or positive");
                    }
                    i--;
                    continue;
                }
                if (size != 1) // Для корабля размера 1 не нужно определять ориентацию
                {
                    if (isPlayer)
                    {
                        Console.WriteLine("'H' or 'h' for Horizontal and 'V' or 'v' for Vertical");
                        orientation = Console.ReadLine().ToCharArray()[0];
                    }
                    else
                    {
                        orientation = rnd.Next(0, 2) == 0 ? 'h' : 'v';
                    }
                }
                if (field[x, y].stateCell == BoardCell.StateCell.Empty) // Если клетка свободна, то ставим корабль
                {
                    ships.Add(new Ship(size, orientation == 'H' || orientation == 'h' ? Ship.Orientation.Horizontal : Ship.Orientation.Vertical));
                }
                else
                {
                    i--;
                    continue;
                }


                try
                {
                    for (int j = 0; j < size; j++) // Размещаем корабль в соответствии с его размером и положением в пространстве
                    {
                        Ship.Orientation ornt = ships.ToArray()[ships.Count - 1].orientation;
                        if (field[x, y].stateCell == BoardCell.StateCell.Ship || field[x, y].stateCell == BoardCell.StateCell.Closed) // Если следующая клетка занята, то пробуем сделать новый корабль
                        {
                            throw new IndexOutOfRangeException();
                        }
                        ships.ToArray()[ships.Count - 1].part[j] = new BoardCell(x, y)
                        {
                            stateCell = BoardCell.StateCell.Ship
                        };                                                                                                       // Присваиваем клетке статус корабля
                        if (ornt == Ship.Orientation.Horizontal)
                        {
                            field[x, y++].stateCell = BoardCell.StateCell.Ship;
                        }
                        else
                        {
                            field[x++, y].stateCell = BoardCell.StateCell.Ship;
                        }
                    }
                }
                catch (IndexOutOfRangeException) // Пробуем еще раз ввести координаты, если вышел за пределы
                {
                    if (isPlayer)
                    {
                        Console.WriteLine("Index Out of Range\n\nEnter again:");
                    }
                    i--;
                    ships.Remove(ships.ToArray()[ships.Count - 1]);
                    continue;
                }

                count++; // Для правильной отработки количества кораблей в соответствии с его размером
                if (size == 4 && count == 1 || size == 3 && count == 2 || size == 2 && count == 3)
                {
                    size--;
                    count = 0;
                }

                CheckRegion(); // Обработка запретной зоны вокруг корабля для других кораблей
                if (isPlayer)
                {
                    ShowBoard();
                }
            }
            CheckCount(isPlayer);

            // После получения кораблей надо убрать закрытые зоны
            for (int q = 0; q < 10; q++)
            {
                for (int p = 0; p < 10; p++)
                {
                    if (field[q, p].stateCell == BoardCell.StateCell.Closed)
                    {
                        field[q, p].stateCell = BoardCell.StateCell.Empty;
                    }
                }
            }
        }