コード例 #1
0
ファイル: FileSystem.cs プロジェクト: zDainy/Sea-Battleship
        private static bool[,] loadArrangement(string input)
        {
            if (input.Length != 68)
            {
                throw new LoadingArrangementException();
            }

            byte[] byteinput = new byte[input.Length / 2];
            for (int i = 0; i < byteinput.Length; i++)
            {
                byteinput[i] = CryptSystem.HexToByte(input[2 * i].ToString() + input[2 * i + 1]);
            }
            byte[] hashres = new byte[4];
            byte[] hash    = new byte[4];
            for (int i = 0; i < hash.Length; i++)
            {
                hash[i]    = byteinput[26 + i];
                hashres[i] = byteinput[30 + i];
            }
            byte[] bytes = new byte[13];
            for (int i = 0; i < 13; i++)
            {
                bytes[i] = (byte)(byteinput[2 * i] ^ byteinput[2 * i + 1]);
            }
            if (!CryptSystem.CheckHash(bytes, hashres))
            {
                throw new LoadingArrangementException();
            }
            for (int i = 0; i < 13; i++)
            {
                bytes[i] = CryptSystem.UnVigenere(byteinput[2 * i], byteinput[2 * i + 1]);
            }
            if (!CryptSystem.CheckHash(bytes, hash))
            {
                throw new LoadingArrangementException();
            }
            bool[] scrambled = new bool[104];
            for (int i = 0; i < 13; i++)
            {
                bool[] tmp = CryptSystem.ByteToBool(bytes[i]);
                for (int j = 0; j < tmp.Length; j++)
                {
                    scrambled[i * 8 + j] = tmp[j];
                }
            }
            bool[] res = CryptSystem.Unscramble(scrambled, 4);
            return(CryptSystem.Bending <bool>(res, 10, 10));
        }
コード例 #2
0
ファイル: FileSystem.cs プロジェクト: zDainy/Sea-Battleship
        private static string saveArrangement(bool[,] input)
        {
            bool[] field     = CryptSystem.Lining <bool>(input);
            bool[] scrambled = CryptSystem.Scramble(field, 4);
            byte[] bytes     = new byte[13];
            for (int i = 0; i < 13; i++)
            {
                bool[] tmp = new bool[8];
                for (int j = 0; j < 8; j++)
                {
                    tmp[j] = scrambled[8 * i + j];
                }
                bytes[i] = CryptSystem.BoolToByte(tmp);
            }
            byte[] result = new byte[34];
            byte[] hash   = CryptSystem.GetHash(bytes);
            byte[] keys   = new byte[13];
            random.NextBytes(keys);
            for (int i = 0; i < 13; i++)
            {
                result[2 * i]     = CryptSystem.Vigenere(bytes[i], keys[i]);
                result[2 * i + 1] = keys[i];
                bytes[i]          = (byte)(result[2 * i] ^ result[2 * i + 1]);
            }
            for (int i = 0; i < hash.Length; i++)
            {
                result[26 + i] = hash[i];
            }
            hash = CryptSystem.GetHash(bytes);
            for (int i = 0; i < hash.Length; i++)
            {
                result[26 + hash.Length + i] = hash[i];
            }
            string res = "";

            for (int i = 0; i < result.Length; i++)
            {
                res += CryptSystem.ByteToHex(result[i]);
            }
            return(res);
        }
コード例 #3
0
ファイル: FileSystem.cs プロジェクト: zDainy/Sea-Battleship
        private static Game loadGame(string input)
        {
            if (input.Length != 200)
            {
                throw new GameLoadingException();
            }
            byte[] bytes = new byte[input.Length / 2];
            for (int i = 0; i < input.Length / 2; i++)
            {
                bytes[i] = CryptSystem.HexToByte(input[2 * i].ToString() + input[2 * i + 1]);
            }
            byte[,] result = new byte[10, 10];
            result         = CryptSystem.Bending <byte>(bytes, 10, 10);
            for (int i = 0; i < 10; i++)
            {
                byte[] tmp = new byte[6];
                for (int j = 0; j < 6; j++)
                {
                    tmp[j] = result[j, i];
                }
                byte[] hash = new byte[2] {
                    result[8, i], result[9, i]
                };
                if (!CryptSystem.CheckHash(tmp, hash))
                {
                    throw new GameLoadingException();
                }
                hash = new byte[2] {
                    result[6, i], result[7, i]
                };
                tmp = new byte[5];
                for (int j = 0; j < 5; j++)
                {
                    tmp[j] = result[j, i];
                }
                tmp = CryptSystem.UnVigenere(tmp, hash);
                if (!CryptSystem.CheckHash(tmp, hash))
                {
                    throw new GameLoadingException();
                }
                for (int j = 0; j < 5; j++)
                {
                    result[j, i] = tmp[j];
                }
            }
            GameConfig gc;
            PlayerRole to;

            bool[] config = CryptSystem.ByteToBool(result[5, 6]);
            if (!config[0])
            {
                BotLevels bl = BotLevels.Easy;
                GameSpeed gs;
                switch (result[5, 3])
                {
                case 1:
                    bl = BotLevels.Easy;
                    break;

                case 2:
                    bl = BotLevels.Medium;
                    break;

                case 3:
                    bl = BotLevels.Hard;
                    break;
                }
                gs = config[3] ? config[4] ? GameSpeed.Turtle : GameSpeed.Slow : config[4] ? GameSpeed.Medium : GameSpeed.Fast;
                to = config[1] ? PlayerRole.Client : PlayerRole.Server;
                gc = new GameConfig(bl, gs, GameStatus.Pause);
            }
            else
            {
                GameSpeed gs;
                byte[]    ip = new byte[4];
                for (int i = 0; i < 4; i++)
                {
                    ip[i] = result[5, i];
                }
                int port = BitConverter.ToInt32(new byte[4] {
                    result[5, 4], result[5, 5], 0, 0
                }, 0);
                string connection = ip[0].ToString() + '.' + ip[1].ToString() + '.' + ip[2].ToString() + '.' + ip[3].ToString() + ":" + port.ToString();
                gs = config[3] ? config[4] ? GameSpeed.Turtle : GameSpeed.Slow : config[4] ? GameSpeed.Medium : GameSpeed.Fast;
                gc = new GameConfig(PlayerRole.Server, connection, gs, GameStatus.Pause);
                to = config[1] ? PlayerRole.Client : PlayerRole.Server;
            }
            gc.IsOnline = config[0];
            byte[,] map = new byte[5, 10];
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    map[j, i] = result[j, i];
                }
            }
            ShipArrangement[] arrangements = ByteArrayToArrangements(map);
            Game res = new Game(arrangements[0], arrangements[1], gc, to);

            return(res);
        }
コード例 #4
0
ファイル: FileSystem.cs プロジェクト: zDainy/Sea-Battleship
        private static string saveGame(Game g)
        {
            byte[,] result = new byte[10, 10];
            byte[,] map    = ArrangementsToByteArray(g.ServerShipArrangement, g.ClientShipArrangement);
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    result[j, i] = map[j, i];
                }
            }
            byte[] bytes = new byte[1];
            random.NextBytes(bytes);
            byte config = bytes[0];

            bool[] conf = CryptSystem.ByteToBool(config);
            conf[0] = g.GameConfig.IsOnline;
            if (g.GameConfig.IsOnline)  // Сохранение онлайн игры
            {
                byte[] rand = new byte[3];
                random.NextBytes(rand);
                string[] ip = g.GameConfig.Connection.Split(new char[2] {
                    '.', ':'
                });
                result[5, 0] = byte.Parse(ip[0]);
                result[5, 1] = byte.Parse(ip[1]);
                result[5, 2] = byte.Parse(ip[2]);
                result[5, 3] = byte.Parse(ip[3]);
                byte[] port = BitConverter.GetBytes(int.Parse(ip[4]));
                result[5, 4] = port[0];
                result[5, 5] = port[1];
                conf[0]      = g.TurnOwner == PlayerRole.Server;
                conf[3]      = (g.GameConfig.GameSpeed == GameSpeed.Slow) || (g.GameConfig.GameSpeed == GameSpeed.Turtle);
                conf[4]      = (g.GameConfig.GameSpeed == GameSpeed.Medium) || (g.GameConfig.GameSpeed == GameSpeed.Turtle);

                /*switch (g.GameConfig.GameSpeed)
                 * {
                 *  case GameSpeed.Fast:
                 *      conf[3] = false;
                 *      conf[4] = false;
                 *      break;
                 *  case GameSpeed.Medium:
                 *      conf[3] = false;
                 *      conf[4] = true;
                 *      break;
                 *  case GameSpeed.Slow:
                 *      conf[3] = true;
                 *      conf[4] = false;
                 *      break;
                 *  case GameSpeed.Turtle:
                 *      conf[3] = true;
                 *      conf[4] = true;
                 *      break;
                 * }*/
                config       = CryptSystem.BoolToByte(conf);
                result[5, 6] = config;
                result[5, 7] = rand[0];
                result[5, 8] = rand[1];
                result[5, 9] = rand[2];
            }
            else                        // Сохранениe оффлайн игры
            {
                byte[] rand = new byte[8];
                random.NextBytes(rand);
                result[5, 0] = rand[7];
                result[5, 1] = rand[0];
                result[5, 2] = rand[1];
                switch (g.GameConfig.BotLvl)
                {
                case BotLevels.Easy:
                    result[5, 3] = 1;
                    break;

                case BotLevels.Medium:
                    result[5, 3] = 2;
                    break;

                case BotLevels.Hard:
                    result[5, 3] = 3;
                    break;
                }
                result[5, 4] = rand[2];
                result[5, 5] = rand[3];
                conf[3]      = (g.GameConfig.GameSpeed == GameSpeed.Slow) || (g.GameConfig.GameSpeed == GameSpeed.Turtle);
                conf[4]      = (g.GameConfig.GameSpeed == GameSpeed.Medium) || (g.GameConfig.GameSpeed == GameSpeed.Turtle);

                /*switch (g.GameConfig.GameSpeed)
                 * {
                 *  case GameSpeed.Fast:
                 *      conf[3] = false;
                 *      conf[4] = false;
                 *      break;
                 *  case GameSpeed.Medium:
                 *      conf[3] = false;
                 *      conf[4] = true;
                 *      break;
                 *  case GameSpeed.Slow:
                 *      conf[3] = true;
                 *      conf[4] = false;
                 *      break;
                 *  case GameSpeed.Turtle:
                 *      conf[3] = true;
                 *      conf[4] = true;
                 *      break;
                 * }*/
                config       = CryptSystem.BoolToByte(conf);
                result[5, 6] = config;
                result[5, 7] = rand[4];
                result[5, 8] = rand[5];
                result[5, 9] = rand[6];
            }
            for (int i = 0; i < 10; i++)
            {
                byte[] tmp = new byte[5];
                for (int j = 0; j < 5; j++)
                {
                    tmp[j] = result[j, i];
                }
                byte[] hash = CryptSystem.GetHash(tmp);
                result[6, i] = hash[0];
                result[7, i] = hash[1];
                tmp          = CryptSystem.Vigenere(tmp, hash);
                for (int j = 0; j < 5; j++)
                {
                    result[j, i] = tmp[j];
                }
                tmp = new byte[6];
                for (int j = 0; j < 6; j++)
                {
                    tmp[j] = result[j, i];
                }
                hash         = CryptSystem.GetHash(tmp);
                result[8, i] = hash[0];
                result[9, i] = hash[1];
            }
            bytes = CryptSystem.Lining <byte>(result);
            string res = "";

            for (int i = 0; i < bytes.Length; i++)
            {
                res += CryptSystem.ByteToHex(bytes[i]);
            }
            return(res);
        }
コード例 #5
0
ファイル: FileSystem.cs プロジェクト: zDainy/Sea-Battleship
 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);
 }
コード例 #6
0
ファイル: CryptSystem.cs プロジェクト: zDainy/Sea-Battleship
        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);
        }