Comments (5)
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.
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.
Sure, I agree, feel free to send through a PR on that!
from basic-computer-games.
I believe https://github.com/coding-horror/basic-computer-games/tree/main/00_Common is an attempt at this?
from basic-computer-games.
Gonna close this out because I believe we have a folder / pattern for it now, thank you!
from basic-computer-games.
Related Issues (20)
- terminal_style.css pegs cpu usage HOT 1
- Mastermind.py has trouble counting black and white pegs. HOT 5
- Typo in the Basic and JavaScript versions of SALVO HOT 2
- hangman.rb doesn't randomize the order of puzzle words HOT 9
- Are less mainstream languages allowed? HOT 4
- DEC Basic Computer Games HOT 2
- A little more about the spirit of the project??? HOT 6
- Super Star Trek README has a typo that's a rude word. HOT 1
- Edited latest commit requesting review for pull request (Life for Two.py)
- Super Star Trek - Ship class does not work correctly HOT 2
- A function never called in tictactoe2.py HOT 1
- Hacktoberfest HOT 1
- Confusion between 91_Train and 92_Trap Rust HOT 3
- 84_Super_Star_Trek java issues HOT 1
- Lunar lander initial weight HOT 1
- Battle doesn't seem to accept the num,num format suggested in the readme HOT 4
- Python Acey-Ducey has a "1" card instead of the "10" HOT 5
- Code decomposition question HOT 4
- Another possible tourist trade bug in King HOT 2
- Hacktoberfest
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from basic-computer-games.