0

I have made a hangman game, and I want to be able to restart it after you've won/lost, rather than having to restart the program manually every time.

I saw on the internet 'Application' which can be used to restart the console. However, it says that Application doesnt exist.

 Close();
 System.Diagnostics.Process.Start(Application.ExecutablePath);

Please help me!

Andre
  • 4,560
  • 2
  • 21
  • 27
FatPoo2
  • 41
  • 8
  • 3
    Can't you just add some code to reset application instead of actually restarting it? – JLe Feb 25 '14 at 12:33
  • 1
    I guess you heard about loops and stuff? Is there a valid reason why you need to *restart* the program? – Thorsten Dittmar Feb 25 '14 at 12:33
  • See Patrick's answer on how to get the executing / entry assembly but as JLe says this is not a great solution to your actual problem. I.e. you should not have to restart the game each time. Consider having a method that resets your state to a know state or encapsulating your state in a separate object etc – Andre Feb 25 '14 at 12:43

2 Answers2

1

You can find it over here:

You need to include the System.Windows.Forms assembly and add a using

using System.Windows.Forms

But for Console apps I would prefer to use this (when in your Console assembly):

System.Reflection.Assembly.GetExecutingAssembly().Location

Like Andre said as comment, you'd even better use Assembly.GetEntryAssembly() since it gets the first assembly that is called (which is of course the executable in console applications)

And of couse, like the previous answer which is removed already, first reopen, and then close the application.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

I don't think you really need to restart your console application to restart your game. Here is a possible game restarting scenario which can be used:

class Program
{
    static void Main(string[] args)
    {
        Game game = new Game();

        Menu menu = new Menu(game);

        game.Finished += menu.AskAndRun;

        menu.Welcome();
    }
}

class Menu
{
    public readonly IRunnable runnable;

    public Menu(IRunnable runnable)
    {
        this.runnable = runnable;
    }

    public void Welcome()
    {
        Console.WriteLine("Welcome, Player!");

        AskAndRun();
    }

    public void AskAndRun()
    {
        Console.WriteLine("\nTo play a new game, press [ENTER]");

        Console.ReadLine();

        Console.Clear();

        runnable.Run();
    }
}

interface IRunnable
{
    void Run();
}

class Game : IRunnable
{
    public event Action Finished;

    public void Run()
    {
        Console.WriteLine("Game started here...");

        Console.WriteLine("Oops! Game finished already");

        if (Finished != null)
        {
            Finished();
        }
    }
}

As you can see, the Game class was separated from the restarting logic which now handled by Menu class. Now it is trivial to introduce new features like exiting and counting number of started games.

Pay attention to the fact that Menu doesn't depend on Game. It depends on IRunnable and this will help you in changing the Game, testing Menu and introducing new features painlessly.

astef
  • 8,575
  • 4
  • 56
  • 95