帮助简化长期的代码 [英] Help with simplifying code that is way to long

查看:53
本文介绍了帮助简化长期的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是本学期的最后一个项目,只是看看是否有办法简化一些代码以使其更小但仍然可以在完成后做同样的事情?这件作品来自于我正在制作的游戏类。如果有一种方法来简化这件作品会对我有很大帮助



  internal   class  CWheel 
{
public CWheel()
{
Program.form.txtAvg.Text = ;
Program.form.txtResults.Text = ;
Program.form.txtPrevBet.Text = ;
Program.form.labelBet.Visible = false ;

Program.player = new CPlayer( 200 );
Program.form.ignoreChange = true ;
Program.form.singleBet.Maximum = 100 ;
Program.form.ignoreChange = true ;
Program.form.singleBet.Value = 0 ;
Program.form.prevUpDown = 0 ;

foreach var pb in Program.form.Controls.OfType< PictureBox>())
{
pb.Paint - = Program.form.highlighter;
pb.Invalidate();
pb.Update();
}
}

public void spin( )
{
int value =( INT )Program.form.singleBet.Value;
if value > 0 && Program.player.curBet == null
{
MessageBox.Show( 请先下注);
return ;
}

随机q = new Random();
int num = q.Next( 38 );
String result = num.ToString();
if (num == 37
{
result = 00;
}

Program.form.txtResults.Text = ; + Program.form.txtResults.Text;

if (Program.player.curBet!= null
{
if (Program.player.curBet.getBet()。isWinner(result))
{
int amountWon = Program.player.curBet.getBet()。onWin()+ value ;
Program.player.adjustStack(amountWon);
Program.form.txtResults.Text = + $ + Program.player.curBet .getBet()。onWin()+ + Program.form.txtResults.Text;
}
else
{
Program.form.txtResults.Text = - $ + value + + Program.form.txtResults.Text;
}
Program.player.curBet.addNode(Program.player.allBets);
Program.player.allBets = Program.player.curBet;
Program.player.curBet = null ;

int bets = Program.player.allBets.count();
int totals = Program.player.allBets.total();

Program.form.txtAvg.Text =(totals / bets).ToString();

Program.form.txtPrevBet.Text = Program.player.allBets.ToString();

Program.form.labelBet.Visible = false ;
}
Program.form.txtResults.Text = Roll: +结果+ Program.form.txtResults.Text;

if (Program.player.stack < 100
{
Program.form.ignoreChange = true ;
Program.form.singleBet.Maximum = Program.player.stack;
}
else
{
Program.form.ignoreChange = true ;
Program.form.singleBet.Maximum = 100 ;
}
Program.form.prevUpDown = 0 ;
Program.form.ignoreChange = true ;
Program.form.singleBet.Value = 0 ;

foreach var pb in Program.form.Controls.OfType< PictureBox>())
{
pb.Paint - = Program.form.highlighter;
pb.Invalidate();
pb.Update();
}

int varR = Int16 .Parse(result) ;
字符串元素= pb + result;
Control [] ctrl = Program.form.Controls.Find(element, true );
ctrl [ 0 ]。Paint + = Program.form.highlighter;
ctrl [ 0 ]。Invalidate();
ctrl [ 0 ]。Update();

if (varR > 0 && varR < 13
{
ctrl = Program.form.Controls.Find( pbDozenA);
ctrl [ 0 ]。Paint + = Program.form.highlighter;
ctrl [ 0 ]。Invalidate();
ctrl [ 0 ]。Update();
}
else if (varR > 12 && varR < 25
{
ctrl = Program.form.Controls.Find( pbDozenB true );
ctrl [ 0 ]。Paint + = Program.form.highlighter;
ctrl [ 0 ]。Invalidate();
ctrl [ 0 ]。Update();
}
else if (varR > 24 && varR < 37
{
ctrl = Program.form.Controls.Find( pbDozenC true );
ctrl [ 0 ]。Paint + = Program.form.highlighter;
ctrl [ 0 ]。Invalidate();
ctrl [ 0 ]。Update();
}

如果(varR!= 0 && ; varR% 2 == 0
{
ctrl = Program.form .Controls.Find( pbEven true );
ctrl [ 0 ]。Paint + = Program.form.highlighter;
ctrl [ 0 ]。Invalidate();
ctrl [ 0 ]。Update();
}
else if (varR!= 0 && varR% 2 == 1
{
ctrl = Program.form.Controls.Find( pbOdd);
ctrl [ 0 ]。Paint + = Program.form.highlighter;
ctrl [ 0 ]。Invalidate();
ctrl [ 0 ]。Update();
}

if (varR < 4
{
ctrl = Program.form.Controls.Find( pbTop true );
ctrl [ 0 ]。Paint + = Program.form.highlighter;
ctrl [ 0 ]。Invalidate();
ctrl [ 0 ]。Update();
}

if (Program.player.stack < 10
{
MessageBox.Show( 对不起,没有筹码。你输了。再也没有下注了。);
}
}
}
}

解决方案

+ Program.player.curBet.getBet()。onWin()+ + Program.form.txtResults .Text;
}
else
{
Program.form.txtResults.Text = -


+ value + + Program.form.txtResults.Text;
}
Program.player.curBet.addNode(Program.player.allBets);
Program.player.allBets = Program.player.curBet;
Program.player.curBet = null ;

int bets = Program.player.allBets.count();
int totals = Program.player.allBets.total();

Program.form.txtAvg.Text =(totals / bets).ToString();

Program.form.txtPrevBet.Text = Program.player.allBets.ToString();

Program.form.labelBet.Visible = false ;
}
Program.form.txtResults.Text = Roll: +结果+ Program.form.txtResults.Text;

if (Program.player.stack < 100
{
Program.form.ignoreChange = true ;
Program.form.singleBet.Maximum = Program.player.stack;
}
else
{
Program.form.ignoreChange = true ;
Program.form.singleBet.Maximum = 100 ;
}
Program.form.prevUpDown = 0 ;
Program.form.ignoreChange = true ;
Program.form.singleBet.Value = 0 ;

foreach var pb in Program.form.Controls.OfType< PictureBox>())
{
pb.Paint - = Program.form.highlighter;
pb.Invalidate();
pb.Update();
}

int varR = Int16 .Parse(result) ;
字符串元素= pb + result;
Control [] ctrl = Program.form.Controls.Find(element, true );
ctrl [ 0 ]。Paint + = Program.form.highlighter;
ctrl [ 0 ]。Invalidate();
ctrl [ 0 ]。Update();

if (varR > 0 && varR < 13
{
ctrl = Program.form.Controls.Find( pbDozenA);
ctrl [ 0 ]。Paint + = Program.form.highlighter;
ctrl [ 0 ]。Invalidate();
ctrl [ 0 ]。Update();
}
else if (varR > 12 && varR < 25
{
ctrl = Program.form.Controls.Find( pbDozenB true );
ctrl [ 0 ]。Paint + = Program.form.highlighter;
ctrl [ 0 ]。Invalidate();
ctrl [ 0 ]。Update();
}
else if (varR > 24 && varR < 37
{
ctrl = Program.form.Controls.Find( pbDozenC true );
ctrl [ 0 ]。Paint + = Program.form.highlighter;
ctrl [ 0 ]。Invalidate();
ctrl [ 0 ]。Update();
}

如果(varR!= 0 && ; varR% 2 == 0
{
ctrl = Program.form .Controls.Find( pbEven true );
ctrl [ 0 ]。Paint + = Program.form.highlighter;
ctrl [ 0 ]。Invalidate();
ctrl [ 0 ]。Update();
}
else if (varR!= 0 && varR% 2 == 1
{
ctrl = Program.form.Controls.Find( pbOdd);
ctrl [ 0 ]。Paint + = Program.form.highlighter;
ctrl [ 0 ]。Invalidate();
ctrl [ 0 ]。Update();
}

if (varR < 4
{
ctrl = Program.form.Controls.Find( pbTop true );
ctrl [ 0 ]。Paint + = Program.form.highlighter;
ctrl [ 0 ]。Invalidate();
ctrl [ 0 ]。Update();
}

if (Program.player.stack < 10
{
MessageBox.Show( 对不起,没有筹码。你输了。再也没有下注了。);
}
}
}
}


会员11520855,您认识到需要进行简化是件好事。 。从我看到的,大部分代码都在一个地方。重构为较小的方法并引入类来分离关注点将有助于组织它。在一个真正的应用程序中,这将使维护更容易。



正如您所写的那样,轮子直接绑定到UI并操纵表单。考虑让车轮只实现车轮的spin()行为,这是获取并返回随机值。



只在方法中做一件简单的事情。 CWheel的构造函数应该只启动轮子的状态而不是UI。 CWheel.spin()也做了很多,它获得随机旋转值并解释玩家投注行为并更新UI。通常,如果在描述方法时必须使用和,则应将其重构为较小的方法。 CWheel.Spin()内部的行为看起来更像是游戏中的转弯。



或许创建一个实现游戏动态的类,比如说CRoulette可以通过转弯方式与滚轮,播放器和表格进行交互,可以进一步帮助清理。



祝你好运完成项目。

Its the last project of the semester and just seeing if there is a way to simplify some code to make it smaller but still be able to do the same things when it is done? This piece is from a class for a game that I'm making. If there is a way to simplify this piece would help me out greatly

 internal class CWheel
    {
        public CWheel()
        {
            Program.form.txtAvg.Text = "";
            Program.form.txtResults.Text = "";
            Program.form.txtPrevBet.Text = "";
            Program.form.labelBet.Visible = false;

            Program.player = new CPlayer(200);
            Program.form.ignoreChange = true;
            Program.form.singleBet.Maximum = 100;
            Program.form.ignoreChange = true;
            Program.form.singleBet.Value = 0;
            Program.form.prevUpDown = 0;

            foreach (var pb in Program.form.Controls.OfType<PictureBox>())
            {
                pb.Paint -= Program.form.highlighter;
                pb.Invalidate();
                pb.Update();
            }
        }

        public void spin()
        {
            int value = (int)Program.form.singleBet.Value;
            if (value > 0 && Program.player.curBet == null)
            {
                MessageBox.Show("Please place a bet first");
                return;
            }

            Random q = new Random();
            int num = q.Next(38);
            String result = num.ToString();
            if (num == 37)
            {
                result = "00";
            }

            Program.form.txtResults.Text = ";" + Program.form.txtResults.Text;

            if (Program.player.curBet != null)
            {
                if (Program.player.curBet.getBet().isWinner(result))
                {
                    int amountWon = Program.player.curBet.getBet().onWin() + value;
                    Program.player.adjustStack(amountWon);
                    Program.form.txtResults.Text = " +$" + Program.player.curBet.getBet().onWin() + " " + Program.form.txtResults.Text;
                }
                else
                {
                    Program.form.txtResults.Text = " -$" + value + " " + Program.form.txtResults.Text;
                }
                Program.player.curBet.addNode(Program.player.allBets);
                Program.player.allBets = Program.player.curBet;
                Program.player.curBet = null;

                int bets = Program.player.allBets.count();
                int totals = Program.player.allBets.total();

                Program.form.txtAvg.Text = (totals / bets).ToString();

                Program.form.txtPrevBet.Text = Program.player.allBets.ToString();

                Program.form.labelBet.Visible = false;
            }
            Program.form.txtResults.Text = " Roll: " + result + Program.form.txtResults.Text;

            if (Program.player.stack < 100)
            {
                Program.form.ignoreChange = true;
                Program.form.singleBet.Maximum = Program.player.stack;
            }
            else
            {
                Program.form.ignoreChange = true;
                Program.form.singleBet.Maximum = 100;
            }
            Program.form.prevUpDown = 0;
            Program.form.ignoreChange = true;
            Program.form.singleBet.Value = 0;

            foreach (var pb in Program.form.Controls.OfType<PictureBox>())
            {
                pb.Paint -= Program.form.highlighter;
                pb.Invalidate();
                pb.Update();
            }

            int varR = Int16.Parse(result);
            String element = "pb" + result;
            Control[] ctrl = Program.form.Controls.Find(element, true);
            ctrl[0].Paint += Program.form.highlighter;
            ctrl[0].Invalidate();
            ctrl[0].Update();

            if (varR > 0 && varR < 13)
            {
                ctrl = Program.form.Controls.Find("pbDozenA", true);
                ctrl[0].Paint += Program.form.highlighter;
                ctrl[0].Invalidate();
                ctrl[0].Update();
            }
            else if (varR > 12 && varR < 25)
            {
                ctrl = Program.form.Controls.Find("pbDozenB", true);
                ctrl[0].Paint += Program.form.highlighter;
                ctrl[0].Invalidate();
                ctrl[0].Update();
            }
            else if (varR > 24 && varR < 37)
            {
                ctrl = Program.form.Controls.Find("pbDozenC", true);
                ctrl[0].Paint += Program.form.highlighter;
                ctrl[0].Invalidate();
                ctrl[0].Update();
            }

            if (varR != 0 && varR % 2 == 0)
            {
                ctrl = Program.form.Controls.Find("pbEven", true);
                ctrl[0].Paint += Program.form.highlighter;
                ctrl[0].Invalidate();
                ctrl[0].Update();
            }
            else if (varR != 0 && varR % 2 == 1)
            {
                ctrl = Program.form.Controls.Find("pbOdd", true);
                ctrl[0].Paint += Program.form.highlighter;
                ctrl[0].Invalidate();
                ctrl[0].Update();
            }

            if (varR < 4)
            {
                ctrl = Program.form.Controls.Find("pbTop", true);
                ctrl[0].Paint += Program.form.highlighter;
                ctrl[0].Invalidate();
                ctrl[0].Update();
            }

            if (Program.player.stack < 10)
            {
                MessageBox.Show("Sorry, out of chips. You lose. No further bets possible.");
            }
        }
    }
}

解决方案

" + Program.player.curBet.getBet().onWin() + " " + Program.form.txtResults.Text; } else { Program.form.txtResults.Text = " -


" + value + " " + Program.form.txtResults.Text; } Program.player.curBet.addNode(Program.player.allBets); Program.player.allBets = Program.player.curBet; Program.player.curBet = null; int bets = Program.player.allBets.count(); int totals = Program.player.allBets.total(); Program.form.txtAvg.Text = (totals / bets).ToString(); Program.form.txtPrevBet.Text = Program.player.allBets.ToString(); Program.form.labelBet.Visible = false; } Program.form.txtResults.Text = " Roll: " + result + Program.form.txtResults.Text; if (Program.player.stack < 100) { Program.form.ignoreChange = true; Program.form.singleBet.Maximum = Program.player.stack; } else { Program.form.ignoreChange = true; Program.form.singleBet.Maximum = 100; } Program.form.prevUpDown = 0; Program.form.ignoreChange = true; Program.form.singleBet.Value = 0; foreach (var pb in Program.form.Controls.OfType<PictureBox>()) { pb.Paint -= Program.form.highlighter; pb.Invalidate(); pb.Update(); } int varR = Int16.Parse(result); String element = "pb" + result; Control[] ctrl = Program.form.Controls.Find(element, true); ctrl[0].Paint += Program.form.highlighter; ctrl[0].Invalidate(); ctrl[0].Update(); if (varR > 0 && varR < 13) { ctrl = Program.form.Controls.Find("pbDozenA", true); ctrl[0].Paint += Program.form.highlighter; ctrl[0].Invalidate(); ctrl[0].Update(); } else if (varR > 12 && varR < 25) { ctrl = Program.form.Controls.Find("pbDozenB", true); ctrl[0].Paint += Program.form.highlighter; ctrl[0].Invalidate(); ctrl[0].Update(); } else if (varR > 24 && varR < 37) { ctrl = Program.form.Controls.Find("pbDozenC", true); ctrl[0].Paint += Program.form.highlighter; ctrl[0].Invalidate(); ctrl[0].Update(); } if (varR != 0 && varR % 2 == 0) { ctrl = Program.form.Controls.Find("pbEven", true); ctrl[0].Paint += Program.form.highlighter; ctrl[0].Invalidate(); ctrl[0].Update(); } else if (varR != 0 && varR % 2 == 1) { ctrl = Program.form.Controls.Find("pbOdd", true); ctrl[0].Paint += Program.form.highlighter; ctrl[0].Invalidate(); ctrl[0].Update(); } if (varR < 4) { ctrl = Program.form.Controls.Find("pbTop", true); ctrl[0].Paint += Program.form.highlighter; ctrl[0].Invalidate(); ctrl[0].Update(); } if (Program.player.stack < 10) { MessageBox.Show("Sorry, out of chips. You lose. No further bets possible."); } } } }


Member 11520855, it is good that you recognize simplification is needed. From what I see, much of the code is in one place. Refactoring into smaller methods and introducing classes to separate the concerns will help to organize it. In a real app this will lend to easier maintenance.

The wheel, as you have written it, is tied directly to the UI and manipulates the form. Consider having the wheel only implement the spin() behavior of the wheel, which is obtaining and returning random values.

Only do one simple thing in a method. CWheel's constructor should only init the state of the wheel not the UI. CWheel.spin() does a bunch as well, it obtains the random spin value and interprets the player bet behavior and updates the UI. Generally, if you have to use an "and" in describing a method it should be refactored into smaller methods. The behavior inside of CWheel.Spin() the way it is written looks more like a turn in the game.

Perhaps create a class that implements the game dynamics, say CRoulette, which can interact with the wheel, player, and the form in a turn method may further help to clean things up.

Good luck on finishing up your project.


这篇关于帮助简化长期的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆