简体   繁体   中英

How to Write a two dimensional array in a fine rectangle in console application

Please i have an array of size int[][] and i want to display it in a finely arange manner on console. Basically its a 2048 game in console. So i want to display it in a 4 row 4column manner. Unlike the usual 2048, this one displays 0 for null. So please how i achieve this ?

Previously i just indent like below

 public static string Indent(int count)
 {
      return "".PadLeft(count);
 }

and the console writ elike

for (int i = 0; i < Tiles.Count(); i++)
    {
      for (int j = 0; j < Tiles[i].Count(); j++)
          {
             Console.Write(Indent(2) + Tiles[i][j]);
          }
          Console.WriteLine();
   }

The problem now is that. The arrangement get destroyed when the tiles reaches 2 to 3 decimal numbers. Please any alternative , guide or help would be appreciated.

Well, if you want to keep the layout the same no matter what numbers are in there, increase the intent count to 5. Then, whenever you get a number with four or more decimal numbers divide it by 1024 and add a "k" after it. So instead of "2048" write "2k".

The other option would be to find the highest number in the column, count it's decimal spaces and add 1 to the intent count of every value in that column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM