예제 #1
0
        /// <summary>
        /// Загружает расстановкy из указанного файла.
        /// </summary>
        /// <param name="name">Имя файла.</param>
        /// <returns></returns>
        public static ShipArrangement LoadArrangement(string name)
        {
            ShipArrangement res = null;

            try
            {
                name += ".arr";
                if (!File.Exists("arr\\" + name))
                {
                    throw new LoadingArrangementException();
                }
                FileStream fileStream = new FileStream("arr\\" + name, FileMode.Open);
                byte[]     bytes      = new byte[68];
                fileStream.Read(bytes, 0, bytes.Length);
                Encoding e = Encoding.ASCII;
                string   s = e.GetString(bytes);
                fileStream.Close();
                res = new ShipArrangement();
                bool[,] arrangement = loadArrangement(s);
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        res.SetCellState((arrangement[i, j]) ? CellStatе.Ship : CellStatе.Water, i, j);
                    }
                }
            }
            catch (LoadingArrangementException e)
            {
                Common.LogService.Trace($"Неверный формат файла: {name}");
                throw new LoadingArrangementException("Неверный формат файла");
            }
            catch (Exception e)
            {
                Common.LogService.Trace(e.Message);
                throw new LoadingArrangementException(e.Message);
            }
            return(res);
        }
예제 #2
0
        public static ShipArrangement ByteToArrangement(string str)
        {
            byte[] input = new byte[25];
            for (int i = 0; i < 25; i++)
            {
                input[i] = HexToByte(str[2 * i].ToString() + str[2 * i + 1]);
            }
            ShipArrangement result = new ShipArrangement();

            CellStatе[] map = new CellStatе[100];
            for (int i = 0; i < 25; i++)
            {
                bool[] tmp = CryptSystem.ByteToBool(input[i]);
                if (tmp[0])
                {
                    if (tmp[1])
                    {
                        map[4 * i] = CellStatе.WoundedShip;
                    }
                    else
                    {
                        map[4 * i] = CellStatе.Ship;
                    }
                }
                else
                {
                    if (tmp[1])
                    {
                        map[4 * i] = CellStatе.WoundedWater;
                    }
                    else
                    {
                        map[4 * i] = CellStatе.Water;
                    }
                }
                if (tmp[2])
                {
                    if (tmp[3])
                    {
                        map[4 * i + 1] = CellStatе.WoundedShip;
                    }
                    else
                    {
                        map[4 * i + 1] = CellStatе.Ship;
                    }
                }
                else
                {
                    if (tmp[3])
                    {
                        map[4 * i + 1] = CellStatе.WoundedWater;
                    }
                    else
                    {
                        map[4 * i + 1] = CellStatе.Water;
                    }
                }
                if (tmp[4])
                {
                    if (tmp[5])
                    {
                        map[4 * i + 2] = CellStatе.WoundedShip;
                    }
                    else
                    {
                        map[4 * i + 2] = CellStatе.Ship;
                    }
                }
                else
                {
                    if (tmp[5])
                    {
                        map[4 * i + 2] = CellStatе.WoundedWater;
                    }
                    else
                    {
                        map[4 * i + 2] = CellStatе.Water;
                    }
                }
                if (tmp[6])
                {
                    if (tmp[7])
                    {
                        map[4 * i + 3] = CellStatе.WoundedShip;
                    }
                    else
                    {
                        map[4 * i + 3] = CellStatе.Ship;
                    }
                }
                else
                {
                    if (tmp[7])
                    {
                        map[4 * i + 3] = CellStatе.WoundedWater;
                    }
                    else
                    {
                        map[4 * i + 3] = CellStatе.Water;
                    }
                }
            }
            CellStatе[,] a = Bending <CellStatе>(map, 10, 10);
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    result.SetCellState(a[i, j], i, j);
                }
            }
            return(result);
        }