Mega Code Archive

 
Categories / C# / Data Types
 

Display value in a grid

using System; using System.Collections.Generic; using System.Text; namespace SmallBasicFun.TicTacToeGame {   public static class StringUtils   {     private static Random random = new Random();     public static string PrintMany<T>(this IEnumerable<T> many)     {       StringBuilder b = new StringBuilder();       foreach (var m in many)       {         b.AppendFormat("{0}\r\n", m);       }       return b.ToString();     }     public static string DisplayGrid(int width, int height, Func<int, int, string> func)     {       StringBuilder b = new StringBuilder("  ");       for (int x = 0; x < width; x++)       {         b.Append(String.Format("{0:0} ", x));       }       b.AppendLine();       for (int y = 0; y < height; y++)       {         b.Append(String.Format("{0:0} ",y));         for (int x = 0; x < width; x++)         {           b.Append(func(x, y)+ " ");         }         b.AppendLine();       }       return b.ToString();     }     public static T GetRandom<T>(this IList<T> items)     {       return items[random.Next(items.Count)];     }   } }