Bradley99
Bradley99

Reputation: 13

C# If statements and Random class are acting weird

I am making a program for practice in C# that involves 3 duelists, each with different accuracies. They take turns shooting in a preset order until the other 2 are dead, then this get repeated 10k times and adds a win to the winner each time. My problem is that for some reason once a true is given on one of the if statements, that dueler will get all 10k wins. I think this is a problem with the random class, but I didn't start learning C# until last week. This is also my first post on this website. I use it a lot but I've never had to ask before, usually someone else already has.

    class MainClass
{
    public static void Main(string[] args)
    {
        //sets up objects and the won boolean
        bool won = false;
        Random shot = new Random();
        Duelist a = new Duelist();
        Duelist b = new Duelist();
        Duelist c = new Duelist();

        //sets names
        c.SetName("Charlie");
        a.SetName("Aaron");
        b.SetName("Bob");

        //loops through 10k times
        for (int i = 10000; i > 0; i--)
        {
            //resets won to false
            won = false;
            while (!won)
            {
                //Aarons turn
                if (a.GetAlive())
                {
                    //If Charlie is alive, shoot at him
                    if (c.GetAlive())
                    {
                        if (shot.Next(1, 4) == 1)
                        {
                            c.SetAlive(false);
                        }
                    }
                    //If bob is alive, shoot at him
                    else if (b.GetAlive())
                    {
                        if (shot.Next(1, 4) == 1)
                        {
                            b.SetAlive(false);
                        }
                    }
                    //if neither of them are alive, aaron wins, and end of while loop
                    else
                    {
                        a.AddWin();
                        won = true;
                    }
                }

                //Bobs turn
                if (b.GetAlive())
                {
                    if (c.GetAlive())
                    {
                        if (shot.Next(1, 3) == 1)
                        {
                            c.SetAlive(false);
                        }
                    }
                    else if (a.GetAlive())
                    {
                        if (shot.Next(1, 3) == 1)
                        {
                            a.SetAlive(false);
                        }
                    }
                    else
                    {
                        b.AddWin();
                        won = true;
                    }
                }

                //Charlies turn
                if (c.GetAlive())
                {
                    if (b.GetAlive())
                    {
                        b.SetAlive(false);
                    }
                    else if (a.GetAlive())
                    {
                        a.SetAlive(false);
                    }
                    else
                    {
                        c.AddWin();
                        won = true;
                    }
                }
            }
        }
        //prints results
        Console.WriteLine(a.GetName() + ": " + a.GetWin());
        Console.WriteLine(b.GetName() + ": " + b.GetWin());
        Console.WriteLine(c.GetName() + ": " + c.GetWin());
        Console.Read();
    }
}

class Duelist
{
    private string name;
    private int wins;
    private bool alive;

    public Duelist()
    {
        name = "Name";
        wins = 0;
        alive = true;
    }

    public void SetName(string n)
    {
        name = n;
    }

    public string GetName()
    {
        return name;
    }

    public void AddWin()
    {
        wins++;
    }

    public int GetWin()
    {
        return wins;
    }

    public void SetAlive(bool al)
    {
        alive = al;
    }

    public bool GetAlive()
    {
        return alive;
    }
}

Upvotes: 1

Views: 89

Answers (1)

Han
Han

Reputation: 3072

You forgot the set each character alive in each iteration.

//resets won to false
a.SetAlive(true);
b.SetAlive(true);
c.SetAlive(true);
won = false;

Upvotes: 3

Related Questions