예제 #1
0
파일: AI.cs 프로젝트: zDainy/Sea-Battleship
 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));
             }
         }
     }
 }
예제 #2
0
 public void GoToGameWindow(PlacementState placement, ShipArrangement shipArrangement, Window owner)
 {
     //if (placement != PlacementState.Manualy)
     //{
     //    CreateGame(shipArrangement);
     //    PlayWindow window = new PlayWindow(this) { Owner = owner };
     //    window.Show();
     //}
     //else
     //{
     //    PlacingWindow window = new PlacingWindow(this) { Owner = owner };
     //    window.Show();
     //}
 }
예제 #3
0
파일: AI.cs 프로젝트: zDainy/Sea-Battleship
        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);
        }
예제 #4
0
파일: AI.cs 프로젝트: zDainy/Sea-Battleship
        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);
        }
예제 #5
0
 public void CreateGame(ShipArrangement arragment)
 {
     EnemyArrangement = new ShipArrangement();
     MyArrangement    = arragment;
     if (PlayerRole == PlayerRole.Server)
     {
         Connect.Server.SendResponse(OpearationTypes.StartConfig, new StartConfig(GameConfig.GameSpeed, Core.GameStatus.Game));
         var res = Connect.Server.GetRequest();
         Network.ShipArrangement clArrangement = (Network.ShipArrangement)res.Item2;
         Connect.Server.SendResponse(OpearationTypes.ShipArrangement, new Network.ShipArrangement(arragment));
         Game     = new Game(arragment, clArrangement.Arragment, GameConfig);
         IsMyTurn = true;
     }
     else
     {
         var         res         = Connect.Client.GetResponse();
         StartConfig startConfig = (StartConfig)res.Item2;
         if (startConfig == null)
         {
             throw new CookieException();
         }
         GameConfig = new GameConfig(BotLevels.Easy, startConfig.GameSpeed);
         if (startConfig.GameStatus == Core.GameStatus.Loaded)
         {
             Connect.Client.SendRequest(OpearationTypes.GameStatus, new Network.GameStatus(Core.GameStatus.Game));
             GameConfig.GameStatus = Core.GameStatus.Loaded;
             var rArr = Connect.Client.GetResponse();
             Network.ShipArrangement myArr = (Network.ShipArrangement)rArr.Item2;
             MyArrangement = myArr.Arragment;
             Connect.Client.SendRequest(OpearationTypes.GameStatus, new Network.GameStatus(Core.GameStatus.Game));
         }
         else
         {
             Connect.Client.SendRequest(OpearationTypes.ShipArrangement, new Network.ShipArrangement(arragment));
         }
         var resArr = Connect.Client.GetResponse();
         Network.ShipArrangement enemyArrangementArrangement = (Network.ShipArrangement)resArr.Item2;
         EnemyArrangement = enemyArrangementArrangement.Arragment;
         IsMyTurn         = false;
     }
 }
        private void Placement_Click(object sender, RoutedEventArgs e)
        {
            RadioButton button = (RadioButton)sender;

            switch (button.Content)
            {
            case "Ручной":
                _placement = PlacementState.Manualy;
                break;

            case "Случайный":
                _placement       = PlacementState.Randomly;
                _shipArrangement = ShipArrangement.Random();
                break;

            case "По стратегии":
                _shipArrangement = ShipArrangement.Strategy();
                _placement       = PlacementState.Strategily;
                break;
            }
        }
예제 #7
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);
        }
예제 #8
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();
        }
예제 #9
0
        /// <summary>
        /// Генерация расстановки по стратегии
        /// </summary>
        /// <returns></returns>
        public static ShipArrangement Strategy()
        {
            ShipArrangement arrangement = new ShipArrangement();

            System.Random random = new System.Random();
            int           dir    = random.Next(4); // определяет сторону, с которой начинается построение
            bool          first  = random.NextDouble() < 0.5;

            System.Collections.Generic.List <int> list = new System.Collections.Generic.List <int>(new int[3] {
                4, 2, 2
            });
            int x = 0;

            for (int i = 0; i < 3; i++)
            {
                int n = random.Next(list.Count);
                int l = list[n];
                list.Remove(l);
                switch (dir)
                {
                case (0):
                    arrangement.SetShip(x, first ? 0 : 2, Direction.Down, l);
                    break;

                case (1):
                    arrangement.SetShip(x, first ? 7 : 9, Direction.Down, l);
                    break;

                case (2):
                    arrangement.SetShip(first ? 0 : 2, x, Direction.Right, l);
                    break;

                case (3):
                    arrangement.SetShip(first ? 7 : 9, x, Direction.Right, l);
                    break;
                }
                x += l + 1;
            }
            list = new System.Collections.Generic.List <int>(new int[3] {
                3, 2, 3
            });
            x = 0;
            for (int i = 0; i < 3; i++)
            {
                int n = random.Next(list.Count);
                int l = list[n];
                list.Remove(l);
                switch (dir)
                {
                case (0):
                    arrangement.SetShip(x, first ? 2 : 0, Direction.Down, l);
                    break;

                case (1):
                    arrangement.SetShip(x, first ? 9 : 7, Direction.Down, l);
                    break;

                case (2):
                    arrangement.SetShip(first ? 2 : 0, x, Direction.Right, l);
                    break;

                case (3):
                    arrangement.SetShip(first ? 9 : 7, x, Direction.Right, l);
                    break;
                }
                x += l + 1;
            }
            for (int i = 0; i < 4; i++)
            {
                int y = 0;
                do
                {
                    x = random.Next(10);
                    y = random.Next(10);
                }while (!arrangement.SetShip(x, y, Direction.Down, 1));
            }
            return(arrangement);
        }
예제 #10
0
        /// <summary>
        /// Генерация случайной расстановки
        /// </summary>
        public static ShipArrangement Random()
        {
            System.Random   random      = new System.Random();
            int             n           = 15; // количество неудачных попыток до перегенерации поля
            int             k           = 0;
            ShipArrangement arrangement = new ShipArrangement();
            bool            r           = false; // флаг необходимости перегенерации поля

            do
            {
                r = false;
                k = 0;
                arrangement.ClearFieldbyWater();
                bool dir;
                int  x = 0;
                int  y = 0;
                do
                {
                    dir = random.NextDouble() < 0.5;
                    x   = random.Next(dir ? 10 : 7);
                    y   = random.Next(dir ? 7 : 10);
                }while (!arrangement.SetShip(x, y, dir ? Direction.Right : Direction.Down, 4));
                for (int i = 0; i < 2; i++) // установка трехпалубных кораблей
                {
                    if (!r)
                    {
                        do
                        {
                            dir = random.NextDouble() < 0.5;
                            x   = random.Next(dir ? 10 : 8);
                            y   = random.Next(dir ? 8 : 10);
                            r   = k++ == n;
                        }while (!r && !arrangement.SetShip(x, y, dir ? Direction.Right : Direction.Down, 3));
                    }
                }
                k = 0;
                for (int i = 0; i < 3; i++) // установка двухпалубных кораблей
                {
                    if (!r)
                    {
                        do
                        {
                            dir = random.NextDouble() < 0.5;
                            x   = random.Next(dir ? 10 : 9);
                            y   = random.Next(dir ? 9 : 10);
                            r   = k++ == n;
                        }while (!r && !arrangement.SetShip(x, y, dir ? Direction.Right : Direction.Down, 2));
                    }
                }
                for (int i = 0; i < 4; i++)                    // установка однопалубных кораблей
                {
                    if (!r)
                    {
                        k = 0;
                        do
                        {
                            x = random.Next(10);
                            y = random.Next(10);
                            r = k++ == n;
                        }while (!r && !arrangement.SetShip(x, y, Direction.Down, 1));
                    }
                }
            }while (r);
            return(arrangement);
        }
예제 #11
0
 /// <summary>
 /// Инициализирует объект расстановки
 /// </summary>
 /// <param name="arr">Расстановка</param>
 public ShipArrangement(Core.ShipArrangement arr)
 {
     Arragment = arr;
 }
예제 #12
0
 private static ShipArrangement[] ByteArrayToArrangements(byte[,] map) // см. комментарий к предыдущему методу
 {
     ShipArrangement[] result = new ShipArrangement[2];
     result[0] = new ShipArrangement();
     result[1] = new ShipArrangement();
     for (int i = 0; i < 5; i++)
     {
         for (int j = 0; j < 10; j++)
         {
             bool[] tmp = CryptSystem.ByteToBool(map[i, j]);
             if (tmp[0])
             {
                 if (tmp[1])
                 {
                     result[0].SetCellState(CellStatе.WoundedShip, 2 * i, j);
                 }
                 else
                 {
                     result[0].SetCellState(CellStatе.Ship, 2 * i, j);
                 }
             }
             else
             {
                 if (tmp[1])
                 {
                     result[0].SetCellState(CellStatе.WoundedWater, 2 * i, j);
                 }
                 else
                 {
                     result[0].SetCellState(CellStatе.Water, 2 * i, j);
                 }
             }
             if (tmp[2])
             {
                 if (tmp[3])
                 {
                     result[0].SetCellState(CellStatе.WoundedShip, 2 * i + 1, j);
                 }
                 else
                 {
                     result[0].SetCellState(CellStatе.Ship, 2 * i + 1, j);
                 }
             }
             else
             {
                 if (tmp[3])
                 {
                     result[0].SetCellState(CellStatе.WoundedWater, 2 * i + 1, j);
                 }
                 else
                 {
                     result[0].SetCellState(CellStatе.Water, 2 * i + 1, j);
                 }
             }
             if (tmp[4])
             {
                 if (tmp[5])
                 {
                     result[1].SetCellState(CellStatе.WoundedShip, 2 * i, j);
                 }
                 else
                 {
                     result[1].SetCellState(CellStatе.Ship, 2 * i, j);
                 }
             }
             else
             {
                 if (tmp[5])
                 {
                     result[1].SetCellState(CellStatе.WoundedWater, 2 * i, j);
                 }
                 else
                 {
                     result[1].SetCellState(CellStatе.Water, 2 * i, j);
                 }
             }
             if (tmp[6])
             {
                 if (tmp[7])
                 {
                     result[1].SetCellState(CellStatе.WoundedShip, 2 * i + 1, j);
                 }
                 else
                 {
                     result[1].SetCellState(CellStatе.Ship, 2 * i + 1, j);
                 }
             }
             else
             {
                 if (tmp[7])
                 {
                     result[1].SetCellState(CellStatе.WoundedWater, 2 * i + 1, j);
                 }
                 else
                 {
                     result[1].SetCellState(CellStatе.Water, 2 * i + 1, j);
                 }
             }
         }
     }
     return(result);
 }
예제 #13
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);
        }
예제 #14
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);
        }
예제 #15
0
        /// <summary>
        /// Представляет расстановку кораблей в байтовый массив.
        /// </summary>
        /// <param name="arrangement"></param>
        /// <returns></returns>
        public static string ArrangementToByte(ShipArrangement arrangement)
        {
            CellStatе[] tmp    = Lining <CellStatе>(arrangement.GetArrangement());
            byte[]      result = new byte[25];
            for (int i = 0; i < 25; i++)
            {
                switch (tmp[4 * i])
                {
                case CellStatе.Water:
                    result[i] = 0;
                    break;

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

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

                case CellStatе.WoundedShip:
                case CellStatе.DestroyedShip:
                    result[i] = 0xC0;
                    break;
                }
                switch (tmp[4 * i + 1])
                {
                case CellStatе.Water:
                    break;

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

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

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

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

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

                case CellStatе.WoundedShip:
                case CellStatе.DestroyedShip:
                    result[i] += 0x0C;
                    break;
                }
                switch (tmp[4 * i + 3])
                {
                case CellStatе.Water:
                    break;

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

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

                case CellStatе.WoundedShip:
                case CellStatе.DestroyedShip:
                    result[i] += 0x03;
                    break;
                }
            }
            string res = "";

            for (int i = 0; i < 25; i++)
            {
                res += ByteToHex(result[i]);
            }
            return(res);
        }