I programmed a simple Convay’s Game of Life as a C# console application for my own enjoyment. One of the things I want to achieve, for aesthetic reasons, is that the array which I am using to actually host the game is centered in the console. That is, when the windowed console opens, the array is square in the middle of it. However when I run the program what I get is this:
While I am not the sharpest tool in the shed, I would say that “centered” is not the way to describe the aforementioned.
Here are the values that I have calculated, as well as my Display function which coordinates this whole effort, to try to achieve my goal:
int gameWindowWidth = 16;
int gameWindowHeight = 16;
int displayPosWidth = (Console.WindowWidth - gameWindowWidth) / 2;
int displayPosHeight = (Console.WindowHeight - gameWindowHeight) / 2;
string displayPosWidthFill = new StringBuilder().Insert(0, " ", displayPosWidth).ToString();
string displayPosHeightFill = new StringBuilder().Insert(0, " ", displayPosHeight).ToString();
static void DisplayGame(char[,] gameWindow, int refreshRate, string displayPosWidthFill, string displayPosHeightFill) {
foreach (char a in displayPosHeightFill) {
Console.WriteLine("");
}
for (int x = 0; x < gameWindow.GetLength(0); x++) {
Console.Write(displayPosWidthFill);
for (int y = 0; y < gameWindow.GetLength(1); y++) {
Console.Write(gameWindow[x, y].ToString() + " ");
}
Console.WriteLine("");
}
Thread.Sleep(refreshRate);
Console.Clear();
}
From what I read in the documentation, Console.WindowWidth calculates by columns (I assume this translates well to characters i.e. like a grid of characters) and Console.WindowHeight does the same but using rows. So, while I may be an idiot, I cannot see why this would not work. Am I missing the obvious or is there a more fundamental reason that stems from Console behaviour? I require your assistance.