コード例 #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 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);
        }