Giter Site home page Giter Site logo

Comments (5)

DLotts avatar DLotts commented on July 19, 2024 2

The tab(c) moves to the next column c, independent of the length of previous thing that was printed. This is actually a useful thing, and not equivalent to:
"new string(' ', n)".

I've been wanting to dive into this tab(x) thing. Here is what I found:

Other languages can't do this with a simple function because it does not know the current position on the line. Languages that I know do this with a format string to pad items with spaces, so the next thing starts at the same column as the line above it. This is Python, and almost identical to Rust using index numbers to choose the values and alignment > and the padding:

print('{0:<30} {1:>35} {2:>35} {3:>35} {4:>20} {5:>20}'.format(a,b,c,d,e,f) )

Similar in C#
Console.WriteLine("{0,10}{1,10}{2,10}", x, y, z);

Oh cool, C# let's you embed the variables in the {} braces instead of indexes. Start the format string with a $" --You'd be a coding hero if you showed how to do that!

Also, I wonder if the tab character works? print(x+"\t"+y+"\t"+z)
david.

from basic-computer-games.

Timo-Weike avatar Timo-Weike commented on July 19, 2024 1

Other languages can't do this with a simple function because it does not know the current position on the line.

You can actually do this in c#. At the class Console we have properties and methods that allow us (on the supported OSs) to read and set the cursor position.

I'm also now unsure how many spaces the basic code would produce for the PRINT TAB(32);"CALENDAR"

I would also assume that the purpose of the tabing is to center the text in the console window. So one could argue that we should determin the width of the console with Console.BufferWidth and do a proper centering based on this width.

Regarding the tabbing, in gerneral I see 4 ways to do it that would be common and easy to unterstand

// format string with right-aligned min-field-width
// formating is okay; one would need to research string interpolation to understand that
Console.WriteLine("{0,40}", "CALENDAR");
Console.WriteLine("{0,55}", "CREATE COMPUTING  MORRISTOWN, NEW JERSEY");

Console.WriteLine();

// interpolated string with right-aligned min-field-width
// formating (IMO) less good; same as aboth
Console.WriteLine($"{"CALENDAR",40}");
Console.WriteLine($"{"CREATE COMPUTING  MORRISTOWN, NEW JERSEY",55}");

Console.WriteLine();

// naive print with prepended spaces
// formating okay; easy to understand
Console.WriteLine(new string(' ', 32) + "CALENDAR");
Console.WriteLine(new string(' ', 15) + "CREATE COMPUTING  MORRISTOWN, NEW JERSEY");

Console.WriteLine();

// left padded strings
// formating okay; would also need to know that the number must include the length of the string
// would in general in a non print context use this over 2
// but on in the print context I would use 1 over this
Console.WriteLine("CALENDAR".PadLeft(40));
Console.WriteLine("CREATE COMPUTING  MORRISTOWN, NEW JERSEY".PadLeft(55));

from basic-computer-games.

coding-horror avatar coding-horror commented on July 19, 2024

Sure, I agree, feel free to send through a PR on that!

from basic-computer-games.

coding-horror avatar coding-horror commented on July 19, 2024

I believe https://github.com/coding-horror/basic-computer-games/tree/main/00_Common is an attempt at this?

from basic-computer-games.

coding-horror avatar coding-horror commented on July 19, 2024

Gonna close this out because I believe we have a folder / pattern for it now, thank you!

from basic-computer-games.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.