Пример #1
0
        private bool CreateCharacter()
        {
            PlayerCharacter player = new PlayerCharacter();
            try
            {
                //Name
                if (CharacterNameTextBox.Text == String.Empty)
                {
                    throw new NoNullAllowedException("Name cannot be empty");
                }
                else
                {
                    player.name = CharacterNameTextBox.Text;
                }

                //Race
                if (CharacterRaceTextBox.Text == String.Empty)
                {
                    throw new NoNullAllowedException("Race cannot be empty");
                }
                else
                {
                    player.race = CharacterRaceTextBox.Text;
                }

                //Class and Subclass
                if (CharacterClassTextBox.Text == String.Empty)
                {
                    throw new NoNullAllowedException("Class cannot be empty");
                }
                else
                {
                    player.primaryClass = CharacterClassTextBox.Text;
                    player.subClass = null;
                }

                //Get number values
                player.armorClass = (int)CharacterArmorClassNum.Value;
                player.hitPoints = (int)CharacterHitPointsNum.Value;
                player.currentHitPoints = (int)CharacterHitPointsNum.Value;
                player.primaryClassLevel = Convert.ToInt32(CharacterLevelNum.Value);

                //Create player
                GameManagement.playerCharacters.Add(player);
                return true;
            }
            catch(NoNullAllowedException e){
                MessageBox.Show(e.Message);
                return false;
            }
        }
Пример #2
0
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stream s;
            StreamReader sr;
            string line;
            string[] splitLine;
            string section = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((s = openFileDialog1.OpenFile()) != null)
                    {
                        using (sr = new StreamReader(s))
                        {
                            while ((line = sr.ReadLine()) != null)
                            {
                                if (line.Contains('#'))
                                {
                                    splitLine = line.Split('#');
                                    for (int i = 0; i < splitLine.Length; i++)
                                    {
                                        if (!String.Empty.Equals(splitLine[i]))
                                        {
                                            section = splitLine[i];
                                        }
                                    }
                                }
                                else if (line.Contains(','))
                                {
                                    splitLine = line.Split(',');

                                    if (section.Equals("PLAYERS"))
                                    {
                                        PlayerCharacter p = new PlayerCharacter();
                                        p.name = splitLine[0];
                                        p.race = splitLine[1];
                                        p.primaryClass = splitLine[2].Split('/')[0].Split('-')[0];
                                        p.primaryClassLevel = Convert.ToInt32(splitLine[2].Split('/')[0].Split('-')[1]);
                                        p.currentHitPoints = Convert.ToInt32(splitLine[3].Split('/')[0]);
                                        p.hitPoints = Convert.ToInt32(splitLine[3].Split('/')[1]);
                                        p.armorClass = Convert.ToInt32(splitLine[4]);
                                        GameManagement.playerCharacters.Add(p);
                                        p = null;
                                        AddPlayerCharacterToPanel();
                                    }
                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }