如何基于我的头在窗口的宽度W/O不得不重复编写相同的代码

本文关键字:不得不 代码 我的 何基于 窗口 | 更新日期: 2023-09-27 18:04:03

我都快把头发拔出来了。我一直在尝试让它工作,但我所能想到的是一个控制台屏幕,看起来像这样在顶部:

+-------------------------------------------------------------------------------------------+
                                                                                            +
                                                                                            +
                                                                                            +
                                                                                            +
                                                                                            +
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中折叠的文本。

如何基于我的头在窗口的宽度W/O不得不重复编写相同的代码

试试这段代码,我认为它能满足你的需要:

        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()。此外,您应该将6替换为一个名为HEADER_HEIGHT或类似的常量,以便于修改和可读性。