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

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