while (true) 循环随机停止

While (true) loop randomly stopps

首先,我对编码很陌生,我不得不承认我承担了相当大的任务。所以请注意,代码根本不会写得最优。 好的,所以我的问题是,如果我 运行 我在 Visual Studio 中的代码,它会在一段时间后停止。可能在 30、100 或更多次迭代之后。它只是在感觉喜欢的时候停止。

我读过一些不同的主题,但其中 none 似乎回答了我的问题。事情是: 我不使用任何线程,我不使用“中断”或任何东西,因为程序不应该停止。

现在程序的实际使用没有问题,它只是让我学习一些新东西。 它应该使用 2 个不同的输入变量来玩我的轮盘赌版本,然后选择结果更好的那个,将这些结果随机化一点,然后一次又一次地进行,直到我退出程序。 这是代码(如果你有问题告诉我,我不得不承认它很混乱而且很长。也非常欢迎提出建议和改进):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Automation_for_Le_Algorythm
{
class Program
{
    static void Main(string[] args)
    {
        int generation = 1;
        
        float total = 15f;
        float black = 7f;
        float red = 7f;
        float green = 1f;

        float cBetsize;
        float cConfidence;
        float cBetsizemp;
        float cWhentomp;

        
        bool sndrun = false;
        bool rounddone = false;


        float chanceRed = red / total;
        float chanceBlack = black / total;
        float chanceGreen = green / total;

        float startingBal = 100;
        float gameBal = 100;
        float startbetSize = 0.5f;
        float beforeincrease = 0.5f;

        float expectedGreen = chanceGreen;
        float expectedRed = chanceRed;
        float expectedBlack = chanceBlack;
        float gamessinceGreenPredict = 0;
        
        float diff;

        

        List<int> set1 = new List<int>();
        List<int> set2 = new List<int>();

        string lastRound;
        string s = "None";


        Console.WriteLine("Enter starting balance");
        startingBal = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter starting bet size ( Will be divided by 10 )");
        startbetSize = Convert.ToInt32(Console.ReadLine()) / 10f;

        Console.WriteLine("Enter rounds per generation");
        int rounds = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Do you wish to proceed? (y/n)");
        string proceed = Console.ReadLine();
        if (proceed != "y")
        {
            Environment.Exit(0);
        }
        Console.WriteLine("\n Running...\n");

        float betsize1 = startbetSize;
        float confidence1 = 0.65f;
        float betsizemp1 = 4f;
        float whentomp1 = 14;

        float betsize2 = startbetSize;
        float confidence2 = 0.65f;
        float betsizemp2 = 4f;
        float whentomp2 = 14;



        while (true)
        {
            Random rand = new Random();

            betsize1 = betsize1 + rand.Next(-1, 1);
          //confidence1 = confidence1 + (rand.Next(-2, 2) / 100f); //Let's not randomise that...
            betsizemp1 = betsizemp1 + (rand.Next(-5, 5) / 10f);
            whentomp1 = whentomp1 + rand.Next(-1, 1);

            betsize2 = betsize2 + rand.Next(-1, 1);
         //confidence2 = confidence2 + (rand.Next(-2, 2) / 100f); //Let's not randomise that...
            betsizemp2 = betsizemp2 + (rand.Next(-5, 5) / 10f);
            whentomp2 = whentomp2 + rand.Next(-1, 1);

            if (betsize1 <= 0)
            {
                betsize1 = betsize1 + 1;
            }
            if (betsizemp1 <= 0)
            {
                betsizemp1 = betsizemp1 + 1;
            }
            if (whentomp1 <= 0)
            {
                whentomp1 = whentomp1 + 1;
            }

            if (betsize2 <= 0)
            {
                betsize2 = betsize2 + 1;
            }
            if (betsizemp2 <= 0)
            {
                betsizemp2 = betsizemp2 + 1;
            }
            if (whentomp2 <= 0)
            {
                whentomp2 = whentomp2 + 1;
            }
                for (int i = 0; i < rounds; i++)
            {
               // Console.WriteLine(1);  //Debug
               // System.Threading.Thread.Sleep(500); //Debug
            secondrun:
               // Console.WriteLine(3); //Debug
               // System.Threading.Thread.Sleep(500); //Debug
                if (sndrun == false)
                {
                    cBetsize = betsize1;
                    cConfidence = confidence1;
                    cBetsizemp = betsizemp1;
                    cWhentomp = whentomp1;

                }



                else
                {
                    cBetsize = betsize2;
                    cConfidence = confidence2;
                    cBetsizemp = betsizemp2;
                    cWhentomp = whentomp2;

                }
               
               
                
                
                
                gameBal = startingBal;
                s = "None";
                expectedBlack = 7f / 15f;
                expectedRed = 7f / 15f;
                expectedGreen = 1f / 15f;


            notfinished:
                //Console.WriteLine(2 + "\n" + gameBal + " \n"); //Debug
                //Console.WriteLine(gamessinceGreenPredict); //Debug
                //System.Threading.Thread.Sleep(1); //Debug

                int pickedSlot = rand.Next(0, 16);

                if (pickedSlot != 15)
                {
                    if (pickedSlot <= 7)
                    {
                        lastRound = "r";
                    }
                    else
                    {
                        lastRound = "b";
                    }
                }
                else
                {
                    lastRound = "g";
                }

                if (gamessinceGreenPredict >=cBetsize * cWhentomp)
                {
                    beforeincrease = cBetsize;
                    cBetsize = cBetsize * cBetsizemp;
                }

                if (expectedGreen > expectedRed)
                {
                    if (expectedGreen > expectedBlack)
                    {
                        if (expectedGreen > cConfidence) // Wie wahrscheinlich muss es sein?
                        {
                            s = "Green";
                            gamessinceGreenPredict++;
                        }

                        else
                        {
                            s = "None";
                        }

                    }
                }

                else if (expectedGreen > expectedBlack)
                {
                    if (expectedGreen > expectedRed)
                    {

                        if (expectedGreen > cConfidence) // Wie wahrscheinlich muss es sein?
                        {
                            s = "Green";
                            gamessinceGreenPredict++;

                        }

                        else
                        {
                            s = "None";
                        }

                    }
                }


                if (expectedGreen < 0.65f)
                {
                    s = "None";
                }


                if (lastRound == "r")
                {
                    diff = expectedRed;
                    expectedRed = expectedRed * chanceRed;

                    diff = diff - expectedRed;
                    expectedBlack = expectedBlack + (6f / 7f) * diff;
                    expectedGreen = expectedGreen + (1f / 7f) * diff;

                    if (s == "Green")
                    {
                        gameBal = gameBal - cBetsize;
                    }
                }

                else if (lastRound == "b")
                {
                    diff = expectedBlack;
                    expectedBlack = expectedBlack * chanceBlack;

                    diff = diff - expectedBlack;
                    expectedRed = expectedRed + (6f / 7f) * diff;
                    expectedGreen = expectedGreen + (1f / 7f) * diff;

                    if (s == "Green")
                    {
                        gameBal = gameBal - cBetsize;
                    }
                }

                else if (lastRound == "g")
                {
                    diff = expectedGreen;
                    expectedGreen = expectedGreen * chanceGreen;

                    diff = diff - expectedGreen;
                    expectedBlack = expectedBlack + (1f / 2f) * diff;
                    expectedRed = expectedRed + (1f / 2f) * diff;

                    gamessinceGreenPredict = 0;

                    if (s == "Green")
                    {
                        gameBal = gameBal + (cBetsize * 14);
                        cBetsize = beforeincrease;
                    }
                }



                /*  foreach (var item in set1.ToArray()) // DEBUG
                  {

                      Console.WriteLine(item + ",");
                  }
                  foreach (var item in set2.ToArray())
                  {

                      Console.WriteLine(item + ",");
                  }
                  Console.WriteLine("\n");
                  Console.WriteLine(lastRound);
                  Console.WriteLine(gameBal);
                  Console.WriteLine(Convert.ToString(expectedGreen));
                  Console.WriteLine(cBetsize + "Betsize");
                  Console.WriteLine(cBetsizemp + "Mp");
                  Console.WriteLine("\n");
                  System.Threading.Thread.Sleep(20); // DEBUG
               */

                if (gameBal >= 300)
                {
                    
                    if (sndrun == false)
                    {
                        set1.Add(1);
                    }
                    else
                    {
                        set2.Add(1);
                        sndrun = false;
                        if (rounddone == true)
                        {
                            goto done;
                        }
                        rounddone = true;
                    }

                    
                    sndrun = true;
                    goto secondrun;
                }

                else if (gameBal <= 0)
                {
                   /* Console.WriteLine(gameBal); //Debug
                    Console.WriteLine(cBetsize); //Debug
                    Console.WriteLine(cBetsizemp); //Debug
                    Console.WriteLine(gamessinceGreenPredict + "\n"); //Debug
                   */
                    if (sndrun == false)
                    {
                        set1.Add(0);
                    }
                    else
                    {
                        set2.Add(0);
                        sndrun = false;
                        if (rounddone == true)
                        {
                            goto done;
                        }
                        rounddone = true;
                    }


                    sndrun = true;
                    
                    goto secondrun;
                }

                else
                {
                    goto notfinished;
                }
            done:;
                rounddone = false;
                //Console.WriteLine("Finished a Round"); //Can be annoying
               



            }
           // Console.WriteLine(5); //Debug
            float length1 = set1.ToArray().Length;
            float length2 = set2.ToArray().Length;
           
            set1.RemoveAll(i => i == 0);
            set2.RemoveAll(i => i == 0);
            
            
            float gottogoal1 = set1.ToArray().Length;
            float gottogoal2 = set2.ToArray().Length;
           
            float score1 = gottogoal1 / length1;
            float score2 = gottogoal2 / length2;
           

            
            if (score1 >= score2)
            {
                score2 = score1;
                betsize2 = betsize1;
                confidence2 = confidence1;
                betsizemp2 = betsizemp1;
                whentomp2 = whentomp1;
            }
            else
            {
                score1 = score2;
                betsize1 = betsize2;
                confidence1 = confidence2;
                betsizemp1 = betsizemp2;
                whentomp1 = whentomp2;
            }
            
            
            Console.WriteLine("\n  Generation " + generation + " achieved " + (score1 * 100) + "% !\n");
            generation++;
            set1.Clear();
            set2.Clear();




        }

       










        }

        
    
}

}

(在问题结束之前)以下是一些适用于许多性质相似的问题的一般提示。排名不分先后:

  • 将代码减少到重现错误行为所需的最低限度。减少可能已经帮助您自己发现问题
  • 使用调试器设置断点,single-step 程序,并检查变量值
  • 避免goto,除非绝对必要
  • 删除所有注释代码,它只会分散实际代码的注意力
  • 使用正确的格式(缩进,遵循公认的命名准则,在适当的位置添加空行)
  • 将问题拆分成更小的 sub-problems 并先解决每个 sub-problem。一旦所有 sub-problems 都解决了,将它们组合起来解决更大的问题
  • 保持方法的低复杂性。如果一个方法变得太长并且有太多分支(条件,循环),将它拆分成更小的方法
  • 先拿笔和纸勾勒出你的算法
  • 在特定位置打印或记录变量值