Why is one method hammered into another? FROM#
If any of you are following my slow learning process, you will know what is happening.
I created a border rectangle that looks like this:
+--------+
| |
| |
| |
+--------+
Are you ready to play hangman? yes/no:
Of course it is larger and more rectangular.
Unfortunately, with the code I have now, what used to have words under the rectangle now has them in the middle, like this:
+--------+
|abcdefghi... |
Are you ready . . .
| |
| |
+--------+
The box is now large enough to insert all the words into it, but the starting question should be below the box. It all started when I named the alphabet into an array and then displayed it at specific coordinates.
Here is my code:
namespace Hangman
{
class Program
{
protected static int firstColumn;
protected static int firstRow;
protected static void headerWindow(string border, int posX, int posY)
{
try
{
Console.SetCursorPosition(firstColumn + posX, firstRow + posY);
Console.Write(border);
}
catch (ArgumentOutOfRangeException error)
{
Console.Clear();
Console.Write(error.Message);
}
}
private static void printWord()
{
String[] myWordArrays = File.ReadAllLines("WordList.txt");
Random randomWord = new Random();
//int lineCount = File.ReadLines("WordList.txt").Count();
int activeWord = randomWord.Next(0, myWordArrays.Length);
string userSelection = "";
Console.WriteLine("Are you Ready to play Hangman? yes/no: ");
userSelection = Console.ReadLine();
if (userSelection == "yes")
{
foreach (char letter in myWordArrays[activeWord])
{
Console.Write("_ ");
}
Console.WriteLine("\n \nCan you guess what this " + myWordArrays[activeWord].Length + " letter word is?");
Console.ReadLine();
}
else if (userSelection == "no")
{
Console.WriteLine("I'm sorry you feel that way. Press Enter to Exit the program!");
Console.ReadLine();
}
}
//THIS IS THE CREATION OF THE RECTANGLE!!!
private static void headerFile()
{
Console.Clear();
firstColumn = Console.CursorLeft;
firstRow = Console.CursorTop;
int HEADER_HEIGHT = 6;
int columnNumber = Console.WindowWidth - 1;
var xcoord = 0;
var ycoord = 0;
for (int i = 0; i < columnNumber; i++)
{
headerWindow("-", i, 0);
headerWindow("-", i, HEADER_HEIGHT);
}
for (int i = 0; i < HEADER_HEIGHT; i++)
{
headerWindow("|", 0, i);
headerWindow("|", columnNumber, i);
}
headerWindow("+", xcoord = 0, ycoord = 0);
headerWindow("+", xcoord = columnNumber, ycoord = 0);
headerWindow("+", xcoord = 0, ycoord = 6);
headerWindow("+", xcoord = columnNumber, ycoord = 6);
}
//THIS IS THE CREATION OF THE LIST OF UNUSED CHARACTERS FOR THE GAME
private static void letterChoices()
{
string[] alphabetSelection = File.ReadAllLines("alphabet.txt");
for (int i = 0; i < alphabetSelection.Length; i++)
{
headerWindow(alphabetSelection[i] + " ", i + 1, 1);
Console.WriteLine("\n ");
}
//return;
}
//SHOULD I HAVE MORE HERE??
static void Main(string[] args)
{
headerFile();
letterChoices();
printWord();
}
}
}
I would appreciate it if they didn't give me an answer because I really need to figure this out, but I moved the method call from main to headerwindow () and I even wrote it all separately and in a different path. Ugh!
Please, help!
source to share
Since you didn't ask a direct answer: the fix belongs to the method headerWindow
. The same fix can be applied to letterChoices
, depending on your preference.
When the method headFile()
ends, the cursor blinks in the lower left corner of the window (in the "correct" position for writing text)
After your loop, letterChoices
your cursor is just below the alphabet, because it requested headerWindow
to be written at a specific position.
Something needs to be changed in headerWindow
so that it doesn't hold the cursor where it just wrote. You have used every property / method required to create this fix, so Console
it is not some kind of secret fix or hidden method.
source to share
Well a rectangle is created with what looks like absolute positioning, but the text is based on relative positioning (i.e. it will print wherever the cursor is)
You can either make sure that creating the header leaves the cursor where you want, or make the other two methods absolute positioning as well.
source to share
So I fixed it and wanted to update this question with a code for anyone else who had this same problem:
private static void letterChoices()
{
string[] alphabetSelection = File.ReadAllLines("alphabet.txt");
for (int i = 0; i < alphabetSelection.Length; i++)
{
//headerWindow("|", 0, 3);
headerWindow(alphabetSelection[i], i*3+1, 1);
//Console.Write("\n ");
Console.SetCursorPosition(0, 7);
}
It now works correctly, without any "extra" spaces between "|" on the sides and it correctly puts the first "question" under the heading. :)
source to share