如何将我的标题基于窗口的宽度W / O必须重复写入相同的代码

时间:2015-07-23 00:41:48

标签: c# user-interface position cursor

我准备拔头发了。我一直试图让这个工作,但我能想到的是一个控制台屏幕,在顶部看起来像这样:

+-------------------------------------------------------------------------------------------+
                                                                                            +
                                                                                            +
                                                                                            +
                                                                                            +
                                                                                            +
Are you ready to play Hangman? yes/no:

我要做的是在顶部的矩形(长度为6行)的短边上放置管道,在四个角的每个角上都有“+”,以及“----”对面底部(就像它目前在顶部)。我用40种不同的方式写了这个东西,但我似乎总是得到相同的结果。看起来它应该比编写每条臭味线更容易。

所以这是我的代码(我隐藏了不必要的代码部分):

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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);

        }

    }

    //I created a method to perform this, so that I can have the program stay open for either 1) a new game or 2) just to see if I could do it. It works!
    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")
            {
                //This runs through the randomly chosen word and prints an underscore in place of each letter - it does work
                foreach(char letter in myWordArrays[activeWord])
                {
                    Console.Write("_ ");

                }

                //This prints to the console "Can you guess what this 'varyingLength' letter word is?" - it does work.
                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();
            }

    }

    private static void headerFile()
    {
        Console.Clear();
        firstColumn = Console.CursorLeft;
        firstRow = Console.CursorTop;

        int columnNumber = Console.WindowWidth - 1;
        var xcoord = 0;
        var ycoord = 0;

        if(xcoord==0 && ycoord==0)
        {
            headerWindow("+",xcoord,ycoord);
            Console.Write("");
            xcoord++;
        }
     }



    static void Main(string[] args)
    {
        headerFile();
        printWord();
    }
}

}

我需要指出的是,如果你看到同一个东西的倍数,那是因为我已经评论并隐藏了长行代码(以防我以后想要使用它)。它应该保持隐藏,但显然Ctrl + A,C甚至复制IDE中的折叠文本。

1 个答案:

答案 0 :(得分:1)

尝试使用此代码,我认为它可以满足您的需求:

        for(int i = 0; i < columnNumber; i++) // Loop across long length
        {
            headerWindow("-", i, 0); // Top line
            headerWindow("-", i, 6); // Bottom line
        }
        for(int i = 0; i < 6; i++) // Loop across short length
        {
            headerWindow("|", 0, i); // Left side
            headerWindow("|", columnNumber, i); // Right side
        }
        // Draw over corners with "+"
        headerWindow("+", 0, 0);
        headerWindow("+", 0, 6);
        headerWindow("+", columnNumber, 0);
        headerWindow("+", columnNumber, 6);

输出:

+------------------------------------------------------------------------------+
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
+------------------------------------------------------------------------------+

打算进入headerFile()。此外,你应该用一个名为HEADER_HEIGHT的常量或类似的东西替换6,以便于修改和阅读。