/// <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); } }
/// <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; } }
/// <summary> /// Сохранение текущей позиции /// </summary> public override void SaveGame() { ZLibClass zlibClass = new ZLibClass(); string allInfo = ""; allInfo += "level=" + level.ToString() + ";"; allInfo += "score=" + score.ToString() + ";"; allInfo += "localScore=" + localScore.ToString() + ";"; allInfo += "speed=" + speed.ToString() + ";"; allInfo += "clearRows=" + clearRows[0].ToString(); for (int i = 1; i < 4; i++) { allInfo += "," + clearRows[i].ToString(); } allInfo += ";"; allInfo += "fieldSize=" + maxTop.ToString() + "," + maxWidth.ToString() + ";"; allInfo += "field="; for (int i = 0; i < maxTop; i++) { for (int j = 0; j < maxWidth; j++) { if (field[i, j] != 10) { allInfo += field[i, j] + ","; } else { allInfo += "0,"; } } } allInfo = allInfo.Substring(0, allInfo.Length - 1); byte[] arrInfo = Encoding.GetEncoding("windows-1251").GetBytes(allInfo); byte[] packInfo = zlibClass.Pack(arrInfo); using (FileStream fs = new FileStream(pathSave, FileMode.Create)) { fs.Write(packInfo, 0, packInfo.Length); } }
/// <summary> /// Сохранить /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { try { int n = Convert.ToInt32(textBox1.Text); if (n < 0 && n > 100) { MessageBox.Show("Неправильная запись параметра. Допустимое значение от 0 до 100", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); textBox1.Focus(); return; } } catch { MessageBox.Show("Неправильная запись параметра", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); textBox1.Focus(); return; } try { int n = Convert.ToInt32(textBox2.Text); if (n < 0 && n > 100) { MessageBox.Show("Неправильная запись параметра. Допустимое значение от 0 до 100", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); textBox2.Focus(); return; } } catch { MessageBox.Show("Неправильная запись параметра", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); textBox2.Focus(); return; } try { int n = Convert.ToInt32(textBox3.Text); if (n < 0 && n > 100) { MessageBox.Show("Неправильная запись параметра. Допустимое значение от 0 до 100", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); textBox3.Focus(); return; } } catch { MessageBox.Show("Неправильная запись параметра", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); textBox3.Focus(); return; } try { int n = Convert.ToInt32(textBox4.Text); if (n < 0 && n > 100) { MessageBox.Show("Неправильная запись параметра. Допустимое значение от 0 до 100", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); textBox4.Focus(); return; } } catch { MessageBox.Show("Неправильная запись параметра", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); textBox4.Focus(); return; } if (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text) + Convert.ToInt32(textBox3.Text) + Convert.ToInt32(textBox4.Text) == 0) { MessageBox.Show("Неправильная запись параметров. Хотя бы одно значение должно быть больше 0", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); textBox1.Focus(); return; } if (saveFileDialog.ShowDialog() == DialogResult.OK) { ZLibClass zlibClass = new ZLibClass(); string allInfo = ""; allInfo += textBox1.Text + "," + textBox2.Text + "," + textBox3.Text + "," + textBox4.Text + ","; for (int i = 0; i < board.MaxTop; i++) { for (int j = 0; j < board.MaxWidth; j++) { allInfo += board.field[i, j] + ","; } } allInfo = allInfo.Substring(0, allInfo.Length - 1); byte[] arrInfo = Encoding.GetEncoding("windows-1251").GetBytes(allInfo); byte[] packInfo = zlibClass.Pack(arrInfo); FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.Create); fs.Write(packInfo, 0, packInfo.Length); fs.Close(); } }
/// <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); } }