Skip to main content
2 of 2
deleted 1 character in body
aepot
  • 2k
  • 8
  • 20

Few tips:

  1. Process name is conflicting with System.Diagnostics.Process the conflict may appear in the further development. Avoid using .NET class names. For example Game instead of Process
  2. Give methods names which state what the method do e.g. Game.Run() instead of Game.UI() or DrawGameBoard instead of GameBoard.
  3. Same for variable neames Random rnd instead of Random test.
  4. To introduce OOP make everything non-static, then create objects using new keyword.
  5. A lot of public methods which isn't used outside of the class, make it private.
  6. Infinite, never ending loop while(true). I suggest to find a way to exit the app.

I'll show some simple example.

Here's implementation via static.

public class Parent
{
    public static Run()
    {
        Child.Go();
    }

    public static DoCommonJob()
    {
    }
}

public class Child()
{
    public static Go()
    {
        Parent.DoCommonJob();
    }
}

class Program
{
    static void Main()
    {
        Parent.Run();
    }
}

That's how it looks now.

The following example avoids static starting using objects.

public class Parent
{
    private Child child;

    public Parent()
    {
        child = new Child(this);
    }

    public Run()
    {
        child.Go();
    }

    public DoCommonJob()
    {
    }
}

public class Child()
{
    private Parent parent;

    public Child(Parent parent)
    {
        this.parent = parent;
    }

    public Go()
    {
        parent.DoCommonJob();
    }
}

class Program
{
    static void Main()
    {
        Parent parent = new Parent();
        parent.Run();
    }
}

But here's obvious not good solution, cyclic dependency: Parent depends on Child, while Child depends on Parent. It commonly acceptable but can cause problems in future development.

This can be solved by introducing 3rd class where's common method located.

public class Common
{
    public DoCommonJob()
    {
    }
}

public class Parent
{
    private Child child;
    private Common common;

    public Parent(Common common)
    {
        this.common = new Common();
        this.child = new Child(common);
    }

    public Run()
    {
        child.Go();
    }
}

public class Child()
{
    private Common common;

    public Child(Common common)
    {
        this.common = common;
    }

    public Go()
    {
        common.DoCommonJob();
    }
}

class Program
{
    static void Main()
    {
        Parent parent = new Parent();
        parent.Run();
    }
}

Both Parent and Child depends on Common while there's no cyclic dependency. Now it looks fine.

Note: new keyword creates new instance of the class every time you call it.

For example, this one won't work as expected according to current logic

while(true)
{
    Game game = new Game();
    game.Run();
}

But this one will

Game game = new Game();
while(true)
{
    game.Run();
}

Feel the difference.

aepot
  • 2k
  • 8
  • 20