You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UiConsole.cs 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using uqac_ia_aspirobot.Agent;
  4. using uqac_ia_aspirobot.Agent.Interfaces.Effectors;
  5. using uqac_ia_aspirobot.Agent.Interfaces.Sensors;
  6. using uqac_ia_aspirobot.Extensions;
  7. using uqac_ia_aspirobot.Interfaces;
  8. namespace uqac_ia_aspirobot.UI
  9. {
  10. public class UiConsole : IUi
  11. {
  12. private readonly IEnvironment _environment;
  13. private readonly IAgEngineEffector _agEngineEffector;
  14. private readonly IAgPickedSensor _agPickedSensor;
  15. private readonly IAgBatterySensor _agBatterySensor;
  16. private readonly IAgVaccumSensor _agVaccumSensor;
  17. private readonly AgState _agState;
  18. public UiConsole(IEnvironment environment, IAgEngineEffector agEngineEffector, IAgPickedSensor agPickedSensor,
  19. IAgBatterySensor agBatterySensor, IAgVaccumSensor agVaccumSensor, AgState agState)
  20. {
  21. _environment = environment;
  22. _agEngineEffector = agEngineEffector;
  23. _agPickedSensor = agPickedSensor;
  24. _agBatterySensor = agBatterySensor;
  25. _agVaccumSensor = agVaccumSensor;
  26. _agState = agState;
  27. }
  28. public void SetBackgroundColor(ConsoleColor? color)
  29. {
  30. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  31. {
  32. Console.BackgroundColor = color.GetValueOrDefault(ConsoleColor.Black);
  33. }
  34. else
  35. {
  36. var str = "";
  37. if (color == null)
  38. {
  39. str = $"{(char)27}[1;49m";
  40. }
  41. if (color == ConsoleColor.Black)
  42. {
  43. str = $"{(char)27}[1;40m";
  44. }
  45. if (color == ConsoleColor.Red)
  46. {
  47. str = $"{(char)27}[1;41m";
  48. }
  49. if (color == ConsoleColor.Green)
  50. {
  51. str = $"{(char)27}[1;42m";
  52. }
  53. if (color == ConsoleColor.Yellow)
  54. {
  55. str = $"{(char)27}[1;43m";
  56. }
  57. if (color == ConsoleColor.Blue)
  58. {
  59. str = $"{(char)27}[1;44m";
  60. }
  61. if (color == ConsoleColor.Magenta)
  62. {
  63. str = $"{(char)27}[1;45m";
  64. }
  65. if (color == ConsoleColor.Cyan)
  66. {
  67. str = $"{(char)27}[1;46m";
  68. }
  69. if (color == ConsoleColor.Gray)
  70. {
  71. str = $"{(char)27}[1;47m";
  72. }
  73. Console.Write(str);
  74. }
  75. }
  76. public void SetForegroundColor(ConsoleColor? color)
  77. {
  78. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  79. {
  80. Console.ForegroundColor = color.GetValueOrDefault(ConsoleColor.Black);
  81. }
  82. else
  83. {
  84. var str = "";
  85. if (color == null)
  86. {
  87. str = $"{(char)27}[1;39m";
  88. }
  89. if (color == ConsoleColor.Black)
  90. {
  91. str = $"{(char)27}[1;30m";
  92. }
  93. if (color == ConsoleColor.Red)
  94. {
  95. str = $"{(char)27}[1;31m";
  96. }
  97. if (color == ConsoleColor.Green)
  98. {
  99. str = $"{(char)27}[1;32m";
  100. }
  101. if (color == ConsoleColor.Yellow)
  102. {
  103. str = $"{(char)27}[1;33m";
  104. }
  105. if (color == ConsoleColor.Blue)
  106. {
  107. str = $"{(char)27}[1;34m";
  108. }
  109. if (color == ConsoleColor.Magenta)
  110. {
  111. str = $"{(char)27}[1;35m";
  112. }
  113. if (color == ConsoleColor.Cyan)
  114. {
  115. str = $"{(char)27}[1;36m";
  116. }
  117. if (color == ConsoleColor.Gray)
  118. {
  119. str = $"{(char)27}[1;37m";
  120. }
  121. Console.Write(str);
  122. }
  123. }
  124. public void Clear()
  125. {
  126. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  127. {
  128. Console.Clear();
  129. }
  130. else
  131. {
  132. Console.WriteLine();
  133. }
  134. }
  135. public void Update()
  136. {
  137. Clear();
  138. var bgColor = Console.BackgroundColor;
  139. for (var x = 0; x < _environment.GetWidth(); ++x)
  140. {
  141. Console.Write("|");
  142. for (var y = 0; y < _environment.GetHeight(); ++y)
  143. {
  144. var state = _environment.GetRoomState(x, y);
  145. ConsoleColor? color = null;
  146. if (state == RoomState.Clean)
  147. {
  148. color = null;
  149. }
  150. else if (state == RoomState.Dust)
  151. {
  152. color = ConsoleColor.Gray;
  153. }
  154. else if (state == RoomState.Jewel)
  155. {
  156. color = ConsoleColor.Cyan;
  157. }
  158. else if (state == RoomState.Unknown)
  159. {
  160. color = ConsoleColor.Yellow;
  161. }
  162. else if (state == (RoomState.Jewel | RoomState.Dust))
  163. {
  164. color = ConsoleColor.Red;
  165. }
  166. SetBackgroundColor(color);
  167. var isDest = _agState.Destination?.IsInPosition(x, y) ?? false;
  168. if (_agEngineEffector.IsInPosition(x, y))
  169. {
  170. Console.Write("X");
  171. }
  172. else if (isDest)
  173. {
  174. SetForegroundColor(ConsoleColor.Red);
  175. Console.Write("+");
  176. }
  177. else
  178. {
  179. Console.Write(" ");
  180. }
  181. SetForegroundColor(null);
  182. SetBackgroundColor(null);
  183. }
  184. Console.WriteLine("|");
  185. }
  186. SetBackgroundColor(bgColor);
  187. Console.WriteLine($"Battery: {_agBatterySensor.Spent}");
  188. Console.WriteLine($"Vaccumed: {_agVaccumSensor.Vaccumed}");
  189. Console.WriteLine($"Picked: {_agPickedSensor.Picked}");
  190. Console.WriteLine($"Think: {_agState.ThinkTimeInterval}ms");
  191. }
  192. }
  193. }