-2

First of all, I'm quite new to coding and I have to admit I've taken on quite a big task. So please be mindful that the code will not be written optimally at all. Ok, so my problem is that if I run my code in Visual Studio it just stops after some time. Could be after 30, 100, or more iterations. It just stops when it feels like it.

I've read some different threads but none of them seem to answer my question. The thing is: I don't use any Threads, I don't use "break" or anything because the program isn't supposed to stop.

Now the actual use of the program isn't in question it is just supposed to let me learn some new stuff. It is supposed to play my version of Roulette with 2 different input variables, then take the one with the better results, randomize those results a bit and go again and again and again until I exit out of the program. Here's the code (If you have questions tell me, it is quite confusing and long I have to admit. Suggestions and improvements are also highly welcome):

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();




        }

       










        }

        
    
}

}

FloatyBoi
  • 11
  • 2
  • 2
    What do you mean with _it just stops after some time_? Do you see an error message? If yes what is the message and on which line it occurs? Please explain – Steve Feb 14 '22 at 16:51
  • No Error message, nothing. It just stopps. Sorry, forgot to add that – FloatyBoi Feb 14 '22 at 16:52
  • 3
    The best thing you can do at this stage is to learn how to use a debugger. Use breakpoints to confirm that it runs what you expect (or doesn't and goes somewhere else). Use watches to see what your variables contain up the point when it doesn't meet your expectations. – madreflection Feb 14 '22 at 16:54
  • Wow, I'm terrible at adding details. Ok, so I already did some debugging, checking variables at different points in the code, letting it print out different things based on where it is, and by that, I determined that it is doing everything in the correct order. Until it, well, stopps... – FloatyBoi Feb 14 '22 at 16:57
  • 3
    If I were to guide you towards becoming a better programmer, the first thing I'd ask you to do is to stop using `goto`. – Bent Tranberg Feb 14 '22 at 16:57
  • 2
    It's possible it has not stopped, but gotten stuck in a loop jumping between `goto`s? – 001 Feb 14 '22 at 16:57
  • *"Until it, well, stopps"* - That statement is kind of like `¯\_(ツ)_/¯` - What can you tell us about the state prior to that happening (variable values)? What was the last line of code that you stepped past? ***Dig deeper.*** – madreflection Feb 14 '22 at 17:01
  • 2
    Side note: never declare `Random rand = new Random();` inside a loop. You only want to declare that once. – LarsTech Feb 14 '22 at 17:02
  • @LarsTech Or use the `Random.Shared` added in .NET 6 – SpaceBeeGaming Feb 14 '22 at 17:05
  • Ok I just quickly did some more testing based on the "maybe it doesn't stop but just keeps jumping between goto's" and it seems like that could be correct. – FloatyBoi Feb 14 '22 at 17:06
  • When I pause the program in VS itself, it always pauses at a different line of code. My question now would be what could cause that? Why does it happen at random? Why does it for example sometimes last 100+ iterations – FloatyBoi Feb 14 '22 at 17:07
  • If you're pressing the pause button on the Debug toolbar (the two vertical lines, standard pause icon), it's because it could be executing *any* instruction when you press it because of how fast code runs. Need more information, though, but this is not a tutorial site so this is going to be off-topic *real* quick. – madreflection Feb 14 '22 at 17:09
  • @madreflection I'd just have one more question. The program skipping over code isn't a possibility, right? So why would it do something different after a while? – FloatyBoi Feb 14 '22 at 17:16
  • @FloatyBoi once it "stops", hit pause in your debugger. Then single-step through your program to see which statements are executed in which order, once your program is "stuck". No, "skipping over code" is not a possibility (Winning the lottery is more likely) – knittl Feb 14 '22 at 17:18
  • @knittl Great advice! I'll try that right now. – FloatyBoi Feb 14 '22 at 17:19
  • 1
    The program does exactly what you tell it to do. A colleague of mine jokes "works as implemented" (a play on "works as expected") to indicate that something you wrote doesn't work the way you expect but happily does what you told it to do. So your program works exactly as implemented, just not the way you intended. That's why you need to step through the code, set breakpoints to have it pause at points of interest, and use the Watch window to inspect the variables, so you can see what causes it to do what it does. – madreflection Feb 14 '22 at 17:19
  • When debugging, use `F8` to go to the next line. You can watch where it goes and inspect the values of variables as they get updated. – LarsTech Feb 14 '22 at 17:20
  • The problem lies within my code, goto's have been replaced, rest is up to me! – FloatyBoi Feb 14 '22 at 21:34

1 Answers1

0

(before the question gets closed) Here are some general tips that apply to many questions that are similar in nature. In no particular order:

  • Reduce code to the required minimum to reproduce the faulty behavior. Reducing might already help you discover the problem yourself
  • Use a debugger to set breakpoints, single-step a program, and inspect variable values
  • Avoid goto, unless absolutely necessary
  • Remove any commented code, it only distracts from the actual code
  • Use proper formatting (indentation, following accepted naming guidelines, add empty lines at the appropriate places)
  • Split the problem into smaller sub-problems and solve each sub-problem first. Once all sub-problems are solved, combine them to solve the bigger problem
  • Keep complexity of methods low. If a method becomes too long and has too many branches (conditions, loops), split it into smaller methods
  • Take a pen and paper to sketch out your algorithm first
  • Print or log values of variables at specific places
knittl
  • 246,190
  • 53
  • 318
  • 364