Esempio n. 1
0
 private static void FillList(ShipArrangement arrangement)
 {
     for (int i = 0; i < 10; i++)
     {
         for (int j = 0; j < 10; j++)
         {
             if (arrangement.GetCellState(i, j) == CellStatе.Ship || arrangement.GetCellState(i, j) == CellStatе.Water)
             {
                 Will.Add(new Vector(i, j));
             }
         }
     }
 }
Esempio n. 2
0
        private static int Count(ShipArrangement arrangement)
        {
            int res = 0;

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if (arrangement.GetCellState(i, j) == CellStatе.Ship || arrangement.GetCellState(i, j) == CellStatе.Water)
                    {
                        res++;
                    }
                }
            }
            return(res);
        }
Esempio n. 3
0
        private static bool FindFourDestroyed(ShipArrangement arrangement)
        {
            int count = 0;

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if (arrangement.GetCellState(i, j) == CellStatе.DestroyedShip)
                    {
                        count++;
                    }
                    else
                    {
                        count = 0;
                    }
                    if (count == 4)
                    {
                        return(true);
                    }
                }
            }
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if (arrangement.GetCellState(j, i) == CellStatе.DestroyedShip)
                    {
                        count++;
                    }
                    else
                    {
                        count = 0;
                    }
                    if (count == 4)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Esempio n. 4
0
        /// <summary>
        /// Сохраняет растановку в указанный файл. Если файл существует, он будет перезаписан.
        /// </summary>
        /// <param name="name">Имя файла с указанным расширением.</param>
        /// <param name="input">Расстановка кораблей.</param>
        public static void SaveArrangement(string name, ShipArrangement input)
        {
            name += ".arr";
            if (!Directory.Exists("arr"))
            {
                Directory.CreateDirectory("arr");
            }
            bool[,] arrangement = new bool[10, 10];
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)

                {
                    arrangement[i, j] = input.GetCellState(i, j) == CellStatе.Ship;
                }
            }
            string     s          = saveArrangement(arrangement);
            FileStream fileStream = new FileStream("arr\\" + name, FileMode.Create);
            Encoding   e          = Encoding.ASCII;

            fileStream.Write(e.GetBytes(s), 0, e.GetBytes(s).Length);
            fileStream.Close();
        }
Esempio n. 5
0
        private static byte[,] ArrangementsToByteArray(ShipArrangement a, ShipArrangement b) // да, я знаю, что этот код не самый очевидный. Рекомендую просто игнорировать его существование
        {
            byte[,] result = new byte[5, 10];
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    switch (a.GetCellState(2 * i, j))
                    {
                    case CellStatе.Water:
                        result[i, j] = 0;
                        break;

                    case CellStatе.WoundedWater:
                        result[i, j] = 0x40;
                        break;

                    case CellStatе.Ship:
                        result[i, j] = 0x80;
                        break;

                    case CellStatе.WoundedShip:
                    case CellStatе.DestroyedShip:
                        result[i, j] = 0xC0;
                        break;
                    }
                    switch (a.GetCellState(2 * i + 1, j))
                    {
                    case CellStatе.Water:
                        break;

                    case CellStatе.WoundedWater:
                        result[i, j] += 0x10;
                        break;

                    case CellStatе.Ship:
                        result[i, j] += 0x20;
                        break;

                    case CellStatе.WoundedShip:
                    case CellStatе.DestroyedShip:
                        result[i, j] += 0x30;
                        break;
                    }
                    switch (b.GetCellState(2 * i, j))
                    {
                    case CellStatе.Water:
                        break;

                    case CellStatе.WoundedWater:
                        result[i, j] += 0x04;
                        break;

                    case CellStatе.Ship:
                        result[i, j] += 0x08;
                        break;

                    case CellStatе.WoundedShip:
                    case CellStatе.DestroyedShip:
                        result[i, j] += 0x0C;
                        break;
                    }
                    switch (b.GetCellState(2 * i + 1, j))
                    {
                    case CellStatе.Water:
                        break;

                    case CellStatе.WoundedWater:
                        result[i, j] += 0x01;
                        break;

                    case CellStatе.Ship:
                        result[i, j] += 0x02;
                        break;

                    case CellStatе.WoundedShip:
                    case CellStatе.DestroyedShip:
                        result[i, j] += 0x03;
                        break;
                    }
                }
            }
            return(result);
        }