Exemplo n.º 1
0
        private void RefreshCharList()
        {
            Save save = new Save();

            string     filePath = @"D:\\HeroWarsSaves.txt";
            FileStream file     = new FileStream(filePath, FileMode.Append);

            file.Dispose();

            SavedCharacterList.Items.Clear();
            heroes.Clear();

            try
            {
                if (new FileInfo(filePath).Length != 0)
                {
                    using (StreamReader heroFile = File.OpenText(@"D:\\HeroWarsSaves.txt"))
                    {
                        while (!heroFile.EndOfStream)
                        {
                            string   line = heroFile.ReadLine();
                            string[] info = line.Split(',');
                            SavedCharacterList.Items.Add(info[0] + " lvl: " + info[4]);

                            if (info[3] == "Elf")
                            {
                                Elf elf = new Elf(info[0], info[1], info[2]);

                                elf.Lvl  = int.Parse(info[4]);
                                elf.Gold = int.Parse(info[5]);
                                elf.Dmg  = int.Parse(info[6]);
                                elf.Wins = int.Parse(info[8]);

                                heroes.Add(elf);
                            }
                            else if (info[3] == "Dwarf")
                            {
                                Dwarf dwarf = new Dwarf(info[0], info[1], info[2]);

                                dwarf.Lvl  = int.Parse(info[4]);
                                dwarf.Gold = int.Parse(info[5]);
                                dwarf.Dmg  = int.Parse(info[6]);
                                dwarf.Wins = int.Parse(info[8]);

                                heroes.Add(dwarf);
                            }
                            else if (info[3] == "Human")
                            {
                                Human human = new Human(info[0], info[1], info[2]);

                                human.Lvl  = int.Parse(info[4]);
                                human.Gold = int.Parse(info[5]);
                                human.Dmg  = int.Parse(info[6]);
                                human.Wins = int.Parse(info[8]);

                                heroes.Add(human);
                            }
                        }
                    }
                }
                save.RefreshCharNames();
            }
            catch { }
        }
Exemplo n.º 2
0
        private void CC_Create_Click(object sender, EventArgs e)
        {
            Save   save   = new Save();
            string Name   = "";
            string Gender = "";

            try
            {
                save.RefreshCharNames();

                for (int i = 0; i < CC_NameBox.Text.Length; i++)
                {
                    if (!((char)CC_NameBox.Text[i] >= 65 && (char)CC_NameBox.Text[i] <= 90) &&
                        !((char)CC_NameBox.Text[i] >= 97 && (char)CC_NameBox.Text[i] <= 122))
                    {
                        MessageBox.Show("Your hero name must not contain numbers or special characters!");
                        throw new Exception();
                    }
                }
                if (!CC_NameBox.Text.Any(char.IsDigit) && CC_NameBox.Text != "" && !(CC_NameBox.Text.Length > 18))
                {
                    bool contains = false;
                    if (!File.Exists(@"D:\\Names.txt"))
                    {
                        FileStream file = new FileStream(@"D:\\Names.txt", FileMode.Create, FileAccess.ReadWrite);
                        file.Close();
                    }
                    else
                    {
                        using (StreamReader readNames = File.OpenText(@"D:\\Names.txt"))
                        {
                            while (!readNames.EndOfStream)
                            {
                                string line = readNames.ReadLine();
                                if (CC_NameBox.Text == line)
                                {
                                    contains = true;
                                }
                            }
                            readNames.Close();
                        }
                    }
                    if (!contains)
                    {
                        Name = CC_NameBox.Text;
                    }
                    else
                    {
                        MessageBox.Show("Name already taken!");
                        throw new Exception("Name already taken!");
                    }
                }
                else
                {
                    if (CC_NameBox.Text.Length > 18)
                    {
                        MessageBox.Show("Name is too long!");
                        throw new Exception("Name is too long!");
                    }
                    else
                    {
                        MessageBox.Show("Invalid name. Please try again!");
                        throw new Exception("Invalid name!");
                    }
                }

                if (CC_Male.Checked)
                {
                    Gender = "Male";
                }
                else if (CC_Female.Checked)
                {
                    Gender = "Female";
                }
                else if (!CC_Male.Checked && !CC_Female.Checked && Gender == "")
                {
                    MessageBox.Show("Gender not selected!");
                    throw new Exception("Gender not selected!");
                }



                string race;
                if (CC_RaceList.SelectedItem == null)
                {
                    MessageBox.Show("Race not selected!");
                    throw new Exception("No race selected!");
                }
                else
                {
                    race = CC_RaceList.SelectedItem.ToString();
                }



                string _class;
                if (CC_ClassList.SelectedItem == null)
                {
                    MessageBox.Show("Class not selected!");
                    throw new Exception("No class selected!");
                }
                else
                {
                    _class = CC_ClassList.SelectedItem.ToString();
                }


                Hero player;

                if (race.ToString() == "Human")
                {
                    player = new Human(Name, Gender, _class);
                }
                else if (race.ToString() == "Elf")
                {
                    player = new Elf(Name, Gender, _class);
                }
                else
                {
                    player = new Dwarf(Name, Gender, _class);
                }


                MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                DialogResult      result  = MessageBox.Show("Are you sure?", "", buttons);
                if (result == DialogResult.Yes)
                {
                    string filePath     = @"D:\\HeroWarsSaves.txt";
                    string SaveContents = player.Name + "," + player.Gender + "," + player._Class + "," + player.Race +
                                          "," + player.Lvl + "," + player.Gold + "," + player.Dmg + "," + player.Health + "," + player.Wins;



                    using (FileStream file = new FileStream(filePath, FileMode.Append, FileAccess.Write))
                    {
                        StreamWriter sw = new StreamWriter(file);
                        sw.WriteLine(SaveContents);
                        sw.Close();
                    }

                    this.Hide();
                    Saves saves = new Saves();
                    saves.ShowDialog();
                    this.Close();
                }
                else
                {
                    return;
                }
            }
            catch
            { }
        }