Paint a frame in c #

0

I'm in class and our teacher is making us create a basic framework for what would be a snake game, I'm just going for the function that the frame should paint, but I have a certain problem, and that is that the right side, it is painted right next to the left, instead of being painted like a frame ... Let's see if anyone can help me, this is what I have:

    static void pintaMarc()
            {
                //caracteres marc 186 ║, 187 ╗, 188 ╝, 200 ╚, 201 ╔, 205 ═

                int alçada = Console.WindowHeight - 10, amplada = Console.WindowWidth - 30;

                Console.Write('╔');
                for (int i = 0; i < amplada; i++)
                {
                    Console.Write('═');
                }
                Console.Write('╗');

                for (int x = 0; x < alçada; x++)
                {
                    Console.Write('\n');

                    for (int y = 0; y < amplada; y++)
                    {

                        if (y == 0)
                        {
                            Console.Write('║');
                        }

                        if (y == amplada - 1)
                        {
                            Console.Write('║');
                        }
                    }

                }
                Console.Write('\n');
                Console.Write('╚');
                for (int x = 0; x < amplada; x++)
                {
                    Console.Write('═');
                }
                Console.WriteLine('╝');
}

Thank you very much in advance!

Attachment captures how it is executed:

Well, at the end I did it with the console.setcursorposition function:

Console.SetCursorPosition(1, 1);
            Console.WriteLine("╔");
            Console.SetCursorPosition(1, 30);
            Console.WriteLine("╚");
            Console.SetCursorPosition(100, 1);
            Console.WriteLine("╗");
            Console.SetCursorPosition(100, 30);
            Console.WriteLine("╝");


            for (int y = 2; y < 100; y++)
            {
                int x = 1;
                Console.SetCursorPosition(y, x);
                Console.WriteLine("═");
            }

            for (int x = 2; x < 30; x++)
            {
                int y = 1;
                Console.SetCursorPosition(y, x);
                Console.WriteLine("║");

                y = 100;
                Console.SetCursorPosition(y, x);
                Console.WriteLine("║");
            }

            for (int y = 2; y < 100; y++)
            {
                int x = 30;
                Console.SetCursorPosition(y, x);
                Console.WriteLine("═");
            }

So that in the second for, the vertical bars, are placed parallel, at the distance you assign.

    
asked by THR4SH3RP0L0 30.03.2017 в 19:25
source

2 answers

1

Here is the answer, check it: -)

 static void pintaMarc()
    {
        //caracteres marc 186 ║, 187 ╗, 188 ╝, 200 ╚, 201 ╔, 205 ═

        int alçada = Console.WindowHeight - 10, amplada = Console.WindowWidth - 30;

        Console.Write('╔');
        for (int i = 0; i < amplada; i++)
        {
            Console.Write('═');
        }
        Console.Write('╗');

        for (int x = 0; x < alçada; x++)
        {
            Console.Write('\n');

            for (int y = 0; y < amplada; y++)
            {

                if (y == 0)
                {
                    Console.Write('║');
                }

                if (y == amplada - 1)
                {
                    Console.Write(" ║");//Puse un espacio antes aqui
                }
                else
                {
                    Console.Write(' ');//Esta linea agrega los espacios, en el else
                }
            }

        }
        Console.Write('\n');
        Console.Write('╚');
        for (int x = 0; x < amplada; x++)
        {
            Console.Write('═');
        }
        Console.WriteLine('╝');
    }

Try as I mentioned @Arie:

static void Main(string[] args)
    {
        pintaMarc2();
        Console.ReadLine();
    }

    static void pintaMarc2()
    {
        //caracteres marc 186 ║, 187 ╗, 188 ╝, 200 ╚, 201 ╔, 205 ═
        int height = Console.WindowHeight - 10, width = Console.WindowWidth - 30;
        WriteAt("╔", 0, 0);
        WriteAt("╚", 0, height);
        WriteAt("╗", width, 0);
        WriteAt("╝", width, height);
        for (int i = 1; i < width; i++)
        {
            WriteAt("═", i, 0);
            WriteAt("═", i, height);
        }
        for (int i = 1; i < height; i++)
        {
            WriteAt("║", 0, i);
            WriteAt("║", width, i);
        }

    }

    protected static void WriteAt(string s, int x, int y)
    {
        try
        {
            Console.SetCursorPosition(x, y);
            Console.Write(s);
        }
        catch (ArgumentOutOfRangeException e)
        {
            Console.Clear();
            Console.WriteLine(e.Message);
        }
    }

If it works very well.

NOTE: The correct answer must be from: -)

    
answered by 30.03.2017 / 19:37
source
1

Good morning. Maybe you should use a function like the following:

protected static void WriteAt(string s, int x, int y){
    try {
        Console.SetCursorPosition(x, y);
        Console.Write(s);
    } catch (ArgumentOutOfRangeException e){
        Console.Clear();
        Console.WriteLine(e.Message);
    }
}

To print the box the implementation is something like this:

using System;

class Sample {
    protected static void WriteAt(string s, int x, int y){
        /* metodo de arriba ^_^ */
    }

    protected static void printBox(int sizeX, int sizeY){
        int limiteX = sizeX - 1, limiteY = sizeY - 1;

        for(int i = 1; i < limiteY; i++){
            WriteAt("║", 0, i);
            WriteAt("║", limiteX, i);
        }

        for(int i = 1; i < limiteX; i++){
            WriteAt("═", i, 0);
            WriteAt("═", i, limiteY);
        }

        WriteAt ("╔", 0, 0);
        WriteAt ("╗", limiteX, 0);
        WriteAt ("╚", 0, limiteY);
        WriteAt ("╝", limiteX, limiteY);
    }

    public static void Main(){
        Console.Clear();
        printBox(7, 8);
        Console.WriteLine();
    }
}

You can see the example in the Microsoft site

    
answered by 30.03.2017 в 19:35