0

After learning how to develop in unity, and the basics of C#, I went on to start making my first standalone C# application, remaking Conway's game of life. I'm doing it as a CLI application. Right now I'm just figuring out how to set up and display the game board. I want to display the game board in the terminal/CLI, and then update/refresh the view every x seconds (determined by the user at the startup of the application). I've written some code to try this, but it doesn't seem to work, and I just get a blank terminal after the user inputs the parameters. I've traied brainstorming a way to make it work but I'm stumped.

Here's an image of what it looks like after the user inputs parameters: blank windows CLI

Here is my code: `using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace CSharps_Game_Of_Life
{
    class Program
    {
    static int rows;
    static int cols;
    static int rowLength;
    static int colLength;
    static int[,] boardSize = new int[/*rows*/10, /*cols*/10];

    static void Main(string[] args)
    {
        //get user input for rows and cols and timer and stores in variables
        Console.Write("Enter amount of rows: ");
        string userInput = Console.ReadLine();
        rows = int.Parse(userInput);
        Console.WriteLine();
        Console.Write("Enter amount of columns: ");
        userInput = Console.ReadLine();
        cols = int.Parse(userInput);
        Console.WriteLine();
        Console.Write("Enter interval between generations in milliseconds: ");
        userInput = Console.ReadLine();
        int interval = int.Parse(userInput);
        Console.Clear();

        //get board length and height
        rowLength = boardSize.GetLength(0);
        colLength = boardSize.GetLength(1);

        //initialize timer
        System.Windows.Forms.Timer generationInterval = new System.Windows.Forms.Timer();
        generationInterval.Interval = interval;
        generationInterval.Tick += new EventHandler(generation);

        //initialize board
        //int[,] boardSize = new int[rows, cols];

        //start timer
        generationInterval.Enabled = true;

        while(true)
        {

        }

        //print ig?
        //for (int i = 0; i < rowLength; i++)
        //{
        //    for (int j = 0; j < colLength; j++)
        //    {
        //        Console.Write(string.Format("{0} ", boardSize[1, j]));
        //    }
        //    Console.Write(Environment.NewLine);
        //}
        //Console.WriteLine("Finished");
        //Console.ReadLine();
    }

    public static void generation(Object myObject, EventArgs myEventArgs)
    {
        if (Console.ReadKey().Key == ConsoleKey.A)
        {
            boardSize[1, 0] = 1;
        }
        if (Console.ReadKey().Key == ConsoleKey.S)
        {
            boardSize[1, 0] = 0;
        }
        if (Console.ReadKey().Key == ConsoleKey.Escape)
        {
            
        }
        Console.Clear();
        for (int i = 0; i < rowLength; i++)
        {
            for (int j = 0; j < colLength; j++)
            {
                Console.Write(string.Format("{0} ", boardSize[1, j]));
            }
            Console.Write(Environment.NewLine);
        }
        //System.Threading.Thread.Sleep(500);
    }
}
}`
MachCoyote
  • 11
  • 3
  • `System.Windows.Forms.Timer` is designed for Windows Forms applications, not console applications. `System.Threading.Timer` might be a better choice. See https://stackoverflow.com/questions/186084/how-do-you-add-a-timer-to-a-c-sharp-console-application – BenM Jul 16 '21 at 04:19

0 Answers0