Few tips:
Process
name is conflicting withSystem.Diagnostics.Process
the conflict may appear in the further development. Avoid using .NET class names. For exampleGame
instead ofProcess
- Give methods names which state what the method do e.g.
Game.Run()
instead ofGame.UI()
orDrawGameBoard
instead ofGameBoard
. - Same for variable neames
Random rnd
instead ofRandom test
. - To introduce OOP make everything non-
static
, then create objects usingnew
keyword. - A lot of
public
methods which isn't used outside of the class, make it private. - 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.