开发者

How to only run methods when called?

I have a bunch of code below. However, I am hitting some bugs because the methods Move() and Genius() are running logic too much. I only want to two methods to run if they are being called by the submit click method. How can I do this?

namespace ShotgunApp
{
    public partial class SingleGame : PhoneApplicationPage
    {
        public static class AmmoCount
        {
            public static int userAmmo = startVars.startAmmo;
            public static int geniusAmmo = startVars.startAmmo;
        }

        public static class Global
        {
            public static int lives = 1;
            public static string GeniusMove;
            public static string UserMove;
        }

        public SingleGame()
        {
            InitializeComponent();
            GeniusAmmo.Text = "ammo: " + AmmoCount.geniusAmmo;
            UserAmmo.Text = "ammo: " + AmmoCount.userAmmo;   
        }

        private void submit_Click(object sender, RoutedEventArgs e)
        {
            if (((String)submit.Content) == "Submit")
            {
                Move();
                submit.Content = "Wait for Genius...";
                uReload.IsEnabled = false;
                uFire.IsEnabled = false;
                uShield.IsEnabled = false;
                Genius();
            }
            else if (((String)submit.Content) == "Go!")
            {
                GeniusSpeak.Text = "";
                OutcomeDesc.Text = "You have " + Move() + " and Genius has " + Genius();
                Outcome.Text = "ANOTHER ROUND...";
                submit.Content = "Continue";
            }
            else if (((String)submit.Content) == "Continue")
            {

                uReload.IsEnabled = true;
                uFire.IsEnabled = true;
                uShield.IsEnabled = true;
                OutcomeDesc.Text = "";
                Outcome.Text = "";
                submit.Content = "Submit";
            }
        }


        public string Move()
        { 
            if (uReload.IsChecked.HasValue && uReload.IsChecked.Value == true)
            {
                UserAmmo.Text = "ammo: " + ++AmmoCount.userAmmo;
                Global.UserMove = "reloaded";

            }
            else if (uShield.IsChecked.HasValue && uShield.IsChecked.Value == true)
            {
                Global.UserMove = "shielded";
            }
            else if (uFire.IsChecked.HasValue && uFire.IsChecked.Value == true)
            {
                UserAmmo.Text = "ammo: " + --AmmoCount.userAmmo;
                Global.UserMove = "fired";

            }
            else
            {
                submit.Content = "Enter a move!";
            }

            return Global.UserMove;
        }

        public string Genius()
        {
            GeniusSpeak.Text = "Genius has moved";
            submit.Content = "Go!";

            Random RandomNumber = new Random();
            int x = RandomNumber.Next(0, 3);

            if (x == 0)
            {
                Global.GeniusMove = "reloaded";
                GeniusAmmo.Text = "ammo: " + ++AmmoCount.geniusAmmo;
            }
            else if (x == 1)
            {
                Global.GeniusMove = "shielded";
            }
            else if (x == 2)
            {
                Global.G开发者_Go百科eniusMove = "fired";
                GeniusAmmo.Text = "ammo: " + ++AmmoCount.geniusAmmo;
            }

            return Global.GeniusMove;

        }
    }
}


Store the last results in data members:

        private string lastMoveResult = string.Empty;
        private string lastGeniusResult = string.Empty;

        private void submit_Click(object sender, RoutedEventArgs e)
        {
            if (((String)submit.Content) == "Submit")
            {
                lastMoveResult = Move();
                submit.Content = "Wait for Genius...";
                uReload.IsEnabled = false;
                uFire.IsEnabled = false;
                uShield.IsEnabled = false;
                lastGeniusResult = Genius();
            }
            else if (((String)submit.Content) == "Go!")
            {
                GeniusSpeak.Text = "";
                OutcomeDesc.Text = "You have " + lastMoveResult + " and Genius has " + lastGeniusResult ;
                Outcome.Text = "ANOTHER ROUND...";
                submit.Content = "Continue";
            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜