If any of you have been following my slow learning process, you'll know what's going on.
I created a border rectangle that looks like this:
+--------+
| |
| |
| |
+--------+
Are you ready to play hangman? yes/no:
Granted, it's bigger and more rectangular.
Unfortunately, with the code I have now, what used to have the words below the rectangle, now have them in the middle, like this:
+--------+
|abcdefghi... |
Are you ready . . .
| |
| |
+--------+
Now, the box is big enough to fit all the words into it, but the initiating question should be below the field. It all started when I called the alphabet into an array, then had it display 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 not being given the answer as I really need to figure it out for myself, but I've moved the method call from main to headerwindow(), and I've even written it all out separately and in different ways. Ugh!
Please help!
Since you asked not for a direct answer: The fix belongs in the method headerWindow. The same fix could also be applied in letterChoices, depending on your preference.
When the method headFile() finishes, the cursor is blinking at the bottom left of the box (in the 'correct' position to write text)
After your loop in letterChoices, your cursor is just below the alphabet, because it asked headerWindow to write in a certain position.
Something should be changed in headerWindow so that it doesn't keep the cursor where it just wrote to. You have used every property/method needed to create this fix, so it's not some arcane fix or hidden method on Console.
Well the rectangle is created with what looks to be absolute positioning, but the text is based on relative positioning (i.e. it will print wherever the cursor is)
You can either look at making sure the header creation leaves the cursor where you want it OR make the other two methods also absolute positioning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With