Exemple #1
0
        private void Generation_Click(object sender, EventArgs e)
        {
            // horisontal: form width = max object width - 18
            // vertical: form height = max object height - 48
            // textBox1.Text = 3.ToString();
            //MountainNumber.Text = 1.ToString();
            //RiverNumber.Text = 1.ToString();
            //ClinicNumber.Text = 10.ToString();
            //PitNumber.Text = 10.ToString();
            //proprietorExists.Checked = true;
            //eggExists.Checked = true;
            //elfExists.Checked = true;
            //hatExists.Checked = true;
            //ozoneExists.Checked = true;
            //combatForceOptions.SelectedIndex = 1;
            //killOptions.SelectedIndex = 2;

            if (killOptions.SelectedIndex < 0)
            {
                MessageBox.Show("Please choose kill options", "Warning: No choice of kill options");
                return;
            }
            if (combatForceOptions.SelectedIndex < 0)
            {
                MessageBox.Show("Please choose combat force options", "Warning: No choice of combat options");
                return;
            }
            Random random = new Random(Guid.NewGuid().GetHashCode());

            BattleField.Top    = groupBox1.Top;
            BattleField.Left   = groupBox1.Left;
            BattleField.Width  = Width - groupBox1.Left - 10 - 10;
            BattleField.Height = Height - groupBox1.Top - 35 - 10;
            groupBox1.Visible  = false;
            //groupBox2.Visible = false;
            Result.Visible      = false;
            BattleField.Visible = true;
            Result.Visible      = false;
            Result.Rows.Clear();

            #region player initialization
            players = new PlayerGroup();
            StreamReader PlayerName;
            try
            {
                PlayerName = new StreamReader(@"./PlayerName.txt");
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show(@"请确认PlayerName.txt和此程序在同一路径。", "未发现PlayerName.txt");
                CleanUp();
                return;
            }
            int    order    = 0;
            string nameTemp = "";
            while (!PlayerName.EndOfStream)
            {
                nameTemp = PlayerName.ReadToEnd();
            }
            string[] names = nameTemp.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            if (names.Length <= 1)
            {
                MessageBox.Show("请添加至少2名玩家。", "玩家太少啦");
                CleanUp();
                return;
            }
            Player.PlayerNumber         = names.Length;
            Player.PlayerRemainedNumber = names.Length;
            foreach (var name in names)
            {
                var p = new Player(order, combatForceOptions.SelectedIndex, BattleField, (int)TeamNumber.Value)
                {
                    PlayerName = name
                };
                p.PlayerLabel.Text = $"{p.Team.ToString()} {p.PlayerName} {p.CombatForceLevel.ToString()}";
                players.AddPlayer(p);
                ++order;
            }
            PlayerName.Close();

            if (proprietorExists.Checked)
            {
                proprieter = new Proprieter(BattleField);
                players.AddPlayer(proprieter);
            }
            if (eggExists.Checked)
            {
                egg = new Egg(BattleField);
                players.AddPlayer(egg);
            }
            if (elfExists.Checked)
            {
                elf = new Elf(BattleField);
                players.AddPlayer(elf);
            }
            if (hatExists.Checked)
            {
                hat = new Hat(combatForceOptions.SelectedIndex, BattleField);
                players.AddPlayer(hat);
            }
            if (ozoneExists.Checked)
            {
                ozone = new Ozone(combatForceOptions.SelectedIndex, BattleField);
                players.AddPlayer(ozone);
            }
            #endregion

            int Temp;
            if (!int.TryParse(MountainNumber.Text, out Temp))
            {
                MessageBox.Show("Invalid mountain number");
                CleanUp();
                MountainNumber.Text = "山的数量";
                return;
            }
            else
            {
                Temp      = Temp <= 0 ? 1 : Temp;
                mountains = new Mountain[Temp];
                double switchL = 5 * (Math.Atan(-Temp / 10) / 2 + Math.PI / 4);
                for (int i = 0; i < Temp; i++)
                {
                    int rand = random.Next(5);
                    mountains[i] = new Mountain(rand, switchL, BattleField);
                }
            }// Mountain
            if (!int.TryParse(RiverNumber.Text, out Temp))
            {
                MessageBox.Show("Invalid river number");
                CleanUp();
                RiverNumber.Text = "河的数量";
                return;
            }
            else
            {
                Temp   = Temp <= 0 ? 1 : Temp;
                rivers = new River[Temp];
                double switchL = 5 * (Math.Atan(-Temp / 10) / 8 + Math.PI / 16);
                for (int i = 0; i < Temp; i++)
                {
                    rivers[i] = new River(switchL, BattleField);
                }
            }// River
            if (!int.TryParse(ClinicNumber.Text, out Temp))
            {
                MessageBox.Show("Invalid clinic number");
                CleanUp();
                ClinicNumber.Text = "医疗站数量";
                return;
            }
            else
            {
                Temp    = Temp <= 0 ? 1 : Temp;
                clinics = new Clinic[Temp];
                Clinic.NumberOfClinic = Temp;
                for (int i = 0; i < Temp; i++)
                {
                    clinics[i] = new Clinic(10, 10, BattleField);
                }
            }// Clinic
            if (!int.TryParse(PitNumber.Text, out Temp))
            {
                MessageBox.Show("Invalid pit number");
                CleanUp();
                PitNumber.Text = "坑的数量";
                return;
            }
            else
            {
                Temp = Temp <= 0 ? 1 : Temp;
                pits = new Pit[Temp];
                for (int i = 0; i < Temp; i++)
                {
                    pits[i] = new Pit(i, 10, 10, BattleField);
                }
            }// Pit

            isGenerated              = true;
            playerRemained.Text      = players.Count.ToString();
            Number.Text              = Player.PlayerNumber.ToString();
            RandomGeneration.Enabled = false;
            generation.Enabled       = false;
            start.Enabled            = true;
            clear.Enabled            = true;
        }
        private void Generation_Click(object sender, EventArgs e)
        {
            // horisontal: form width = max object width - 18
            // vertical: form height = max object height - 48
            textBox1.Text                    = 3.ToString();
            MountainNumber.Text              = 40.ToString();
            RiverNumber.Text                 = 20.ToString();
            ClinicNumber.Text                = 10.ToString();
            PitNumber.Text                   = 10.ToString();
            proprietorExists.Checked         = true;
            eggExists.Checked                = true;
            elfExists.Checked                = true;
            hatExists.Checked                = true;
            ozoneExists.Checked              = true;
            combatForceOptions.SelectedIndex = 2;



            if (combatForceOptions.SelectedIndex < 0)
            {
                MessageBox.Show("Please choose combat force options", "Warning: No choice of combat options");
                return;
            }
            Random random = new Random(Guid.NewGuid().GetHashCode());

            BattleField.Top     = groupBox1.Top;
            BattleField.Left    = groupBox1.Left;
            BattleField.Width   = this.Width - groupBox1.Left - 10 - 10;
            BattleField.Height  = this.Height - groupBox1.Top - 35 - 10;
            groupBox1.Visible   = false;
            groupBox2.Visible   = false;
            BattleField.Visible = true;

            int Temp = 0;

            if (!int.TryParse(textBox1.Text, out Temp))
            {
                textBox1.Text = "Invalid input";
                return;
            }
            else
            {
                Player.PlayerNumber  = Temp;
                playerRemainedNumber = Temp;

                if (proprietorExists.Checked)
                {
                    proprieter = new Proprieter(BattleField);
                }
                if (eggExists.Checked)
                {
                    egg = new Egg(BattleField);
                }
                if (elfExists.Checked)
                {
                    elf = new Elf(BattleField);
                }
                if (hatExists.Checked)
                {
                    hat = new Hat(combatForceOptions.SelectedIndex, BattleField);
                }
                if (ozoneExists.Checked)
                {
                    ozone = new Ozone(combatForceOptions.SelectedIndex, BattleField);
                }

                players = new Player[Temp];
                StreamReader PlayerName = new StreamReader(@"./PlayerName.txt");
                for (int i = 0; i < Temp; i++)
                {
                    players[i] = new Player(i, combatForceOptions.SelectedIndex, BattleField);
                    players[i].PlayerLabel.Text =
                        PlayerName.ReadLine() + " " + players[i].CombatForceLevel.ToString();
                }
                PlayerName.Close();

                // Horizontal Order Sorting
                for (int i = 0; i < Player.PlayerNumber; i++)
                {
                    for (int j = 0; j < Player.PlayerNumber; j++)
                    {
                        if (players[i].PlayerLabel.Left > players[j].PlayerLabel.Left && players[i].HorizontalOrder < players[j].HorizontalOrder)
                        {
                            int temp = players[i].HorizontalOrder;
                            players[i].HorizontalOrder = players[j].HorizontalOrder;
                            players[j].HorizontalOrder = temp;
                        }
                    }
                    if (proprietorExists.Checked)
                    {
                        int temp = random.Next(14);
                        if (temp == 0)
                        {
                            switch (proprieter.FistProprieter)
                            {
                            case 0: players[i].FingerGuessState = 2; break;

                            case 1: players[i].FingerGuessState = 0; break;

                            case 2: players[i].FingerGuessState = 1; break;

                            default: break;
                            }
                        }
                        else if (temp < 4)
                        {
                            players[i].FingerGuessState = proprieter.FistProprieter;
                        }
                        else
                        {
                            switch (proprieter.FistProprieter)
                            {
                            case 0: players[i].FingerGuessState = 1; break;

                            case 1: players[i].FingerGuessState = 2; break;

                            case 2: players[i].FingerGuessState = 0; break;

                            default: break;
                            }
                        }
                    }
                }
            }// player number
            if (!int.TryParse(MountainNumber.Text, out Temp))
            {
                MountainNumber.Text = "Invalid input";
                return;
            }
            else
            {
                mountains = new Mountain[Temp];
                double switchL = 5 * (Math.Atan(-Temp / 10) / 2 + Math.PI / 4);
                for (int i = 0; i < Temp; i++)
                {
                    mountains[i] = new Mountain(Temp, switchL, players[0], BattleField);
                }
            }// Mountain
            if (!int.TryParse(RiverNumber.Text, out Temp))
            {
                RiverNumber.Text = "Invalid input";
                return;
            }
            else
            {
                rivers = new River[Temp];
                double switchL = 5 * (Math.Atan(-Temp / 10) / 8 + Math.PI / 16);
                for (int i = 0; i < Temp; i++)
                {
                    rivers[i] = new River(Temp, switchL, players[0], BattleField);
                }
            }// River
            if (!int.TryParse(ClinicNumber.Text, out Temp))
            {
                ClinicNumber.Text = "Invalid input";
                return;
            }
            else
            {
                clinics = new Clinic[Temp];
                for (int i = 0; i < Temp; i++)
                {
                    clinics[i] = new Clinic(10, 10, BattleField);
                }
            }// Clinic
            if (!int.TryParse(PitNumber.Text, out Temp))
            {
                PitNumber.Text = "Invalid input";
                return;
            }
            else
            {
                pits = new Pit[Temp];
                for (int i = 0; i < Temp; i++)
                {
                    pits[i] = new Pit(10, 10, BattleField);
                }
            }// Pit

            isGenerated         = true;
            playerRemained.Text = playerRemainedNumber.ToString();
            generation.Enabled  = false;
            start.Enabled       = true;
            clear.Enabled       = true;
        }