Пример #1
0
        /// <summary>
        /// Переход на следующий уровень
        /// </summary>
        private bool NextLevel()
        {
            ZLibClass zlibClass = new ZLibClass();

OneMore:
            level++;
            // загрузка нового уровня
            FileInfo fi = new FileInfo(pathLevel + level.ToString("D3") + ".lvl");

            if (!fi.Exists)
            {
                if (level == 1)
                {
                    level = 0;
                    MessageBox.Show("Не найден первый уровень", "Критическая ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }
                MessageBox.Show("Все уровни пройдены. Вы - победитель!", "Победа", MessageBoxButtons.OK, MessageBoxIcon.Information);

                level = 0;
                goto OneMore;
            }

            try
            {
                FileStream fs      = fi.OpenRead();
                byte[]     lvlInfo = new byte[fs.Length];
                fs.Read(lvlInfo, 0, (int)fs.Length);
                byte[] unpackLvlInfo = zlibClass.Unpack(lvlInfo);

                string   lvlStr = Encoding.GetEncoding("windows-1251").GetString(unpackLvlInfo);
                string[] data   = lvlStr.Split(new string[1] {
                    ","
                }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < 4; i++)
                {
                    clearRows[i] = Convert.ToInt32(data[i]);
                }
                for (int i = 0; i < maxTop; i++)
                {
                    for (int j = 0; j < maxWidth; j++)
                    {
                        field[i, j] = Convert.ToInt32(data[i * maxWidth + j + 4]);
                    }
                }

                speed      = 1;
                localScore = 0;
                return(true);
            }
            catch
            {
                level = 0;
                MessageBox.Show("Файл с уровнем " + level.ToString() + " повреждён", "Критическая ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
        }
Пример #2
0
        /// <summary>
        /// Загрузить
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            board = new Board(20, 10);
            ZLibClass zlibClass = new ZLibClass();

            try
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    FileStream fs      = new FileStream(openFileDialog.FileName, FileMode.Open);
                    byte[]     lvlInfo = new byte[fs.Length];
                    fs.Read(lvlInfo, 0, (int)fs.Length);
                    fs.Close();
                    byte[]   unpackLvlInfo = zlibClass.Unpack(lvlInfo);
                    string   lvlStr        = Encoding.GetEncoding("windows-1251").GetString(unpackLvlInfo);
                    string[] data          = lvlStr.Split(new string[1] {
                        ","
                    }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < 4; i++)
                    {
                        clearRows[i] = Convert.ToInt32(data[i]);
                    }
                    for (int i = 0; i < board.MaxTop; i++)
                    {
                        for (int j = 0; j < board.MaxWidth; j++)
                        {
                            board.field[i, j] = Convert.ToInt32(data[i * board.MaxWidth + j + 4]);
                        }
                    }
                    pictureBoard.Image = board.GetImageField();
                    textBox1.Text      = clearRows[0].ToString();
                    textBox2.Text      = clearRows[1].ToString();
                    textBox3.Text      = clearRows[2].ToString();
                    textBox4.Text      = clearRows[3].ToString();
                }
            }
            catch
            {
                MessageBox.Show("Файл " + openFileDialog.FileName + " повреждён", "Критическая ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
Пример #3
0
        /// <summary>
        /// Загрузка сохранённых данных
        /// </summary>
        private bool LoadGame()
        {
            ZLibClass zlibClass = new ZLibClass();

            byte[] unpackInfo;
            using (FileStream fs = new FileStream(pathSave, FileMode.Open))
            {
                byte[] packInfo = new byte[fs.Length];
                fs.Read(packInfo, 0, (int)fs.Length);
                unpackInfo = zlibClass.Unpack(packInfo);
            }

            string fileData = Encoding.GetEncoding("windows-1251").GetString(unpackInfo);

            string[] arrData = fileData.Split(new string[1] {
                ";"
            }, StringSplitOptions.RemoveEmptyEntries);
            int n = 0;

            level = score = maxTop = maxWidth = localScore = -1;
            speed = -1.0;
            while (n < arrData.Length)
            {
                string name  = "";
                string value = "";
                try
                {
                    name  = arrData[n].Substring(0, arrData[n].IndexOf("="));
                    value = arrData[n].Substring(arrData[n].IndexOf("=") + 1);
                    string[] s;

                    switch (name)
                    {
                    case "level":
                        level = Convert.ToInt32(value);
                        break;

                    case "score":
                        score = Convert.ToInt32(value);
                        break;

                    case "localScore":
                        localScore = Convert.ToInt32(value);
                        break;

                    case "speed":
                        speed = Convert.ToDouble(value);
                        break;

                    case "clearRows":
                        s = value.Split(new string[1] {
                            ","
                        }, StringSplitOptions.RemoveEmptyEntries);
                        if (s.Length != 4)
                        {
                            return(false);
                        }
                        for (int i = 0; i < s.Length; i++)
                        {
                            clearRows[i] = Convert.ToInt32(s[i]);
                        }
                        break;

                    case "fieldSize":
                        s = value.Split(new string[1] {
                            ","
                        }, StringSplitOptions.RemoveEmptyEntries);
                        if (s.Length != 2)
                        {
                            return(false);
                        }
                        maxTop   = Convert.ToInt32(s[0]);
                        maxWidth = Convert.ToInt32(s[1]);
                        break;

                    case "field":
                        s = value.Split(new string[1] {
                            ","
                        }, StringSplitOptions.RemoveEmptyEntries);
                        for (int i = 0; i < maxTop; i++)
                        {
                            for (int j = 0; j < maxWidth; j++)
                            {
                                field[i, j] = Convert.ToInt32(s[i * maxWidth + j]);
                            }
                        }
                        break;
                    }
                    n++;
                }
                catch
                {
                    MessageBox.Show("Файл повреждён или записан с ошибкой", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }
            }

            if (level != -1 && score != -1 && speed != -1.0 && maxTop != -1 && maxWidth != -1 && localScore != -1)
            {
                File.Delete(pathSave);
                return(true);
            }
            else
            {
                MessageBox.Show("Не все данные найдены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
        }