Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

UiConsole.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. public UiConsole(IEnvironment environment, AgEngineEffector agEngineEffector, AgPickedSensor agPickedSensor,
  17. AgBatterySensor agBatterySensor, AgVaccumSensor agVaccumSensor)
  18. {
  19. _environment = environment;
  20. _agEngineEffector = agEngineEffector;
  21. _agPickedSensor = agPickedSensor;
  22. _agBatterySensor = agBatterySensor;
  23. _agVaccumSensor = agVaccumSensor;
  24. }
  25. public void SetBackgroundColor(ConsoleColor? color)
  26. {
  27. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  28. {
  29. Console.BackgroundColor = color.GetValueOrDefault(ConsoleColor.Black);
  30. }
  31. else
  32. {
  33. var str = "";
  34. if (color == null)
  35. {
  36. str = $"{(char)27}[1;49m";
  37. }
  38. if (color == ConsoleColor.Black)
  39. {
  40. str = $"{(char)27}[1;40m";
  41. }
  42. if (color == ConsoleColor.Red)
  43. {
  44. str = $"{(char)27}[1;41m";
  45. }
  46. if (color == ConsoleColor.Green)
  47. {
  48. str = $"{(char)27}[1;42m";
  49. }
  50. if (color == ConsoleColor.Yellow)
  51. {
  52. str = $"{(char)27}[1;43m";
  53. }
  54. if (color == ConsoleColor.Blue)
  55. {
  56. str = $"{(char)27}[1;44m";
  57. }
  58. if (color == ConsoleColor.Magenta)
  59. {
  60. str = $"{(char)27}[1;45m";
  61. }
  62. if (color == ConsoleColor.Cyan)
  63. {
  64. str = $"{(char)27}[1;46m";
  65. }
  66. if (color == ConsoleColor.Gray)
  67. {
  68. str = $"{(char)27}[1;47m";
  69. }
  70. Console.Write(str);
  71. }
  72. }
  73. public void Clear()
  74. {
  75. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  76. {
  77. Console.Clear();
  78. }
  79. else
  80. {
  81. Console.WriteLine();
  82. }
  83. }
  84. public void Update()
  85. {
  86. Clear();
  87. var bgColor = Console.BackgroundColor;
  88. for (var x = 0; x < _environment.GetWidth(); ++x)
  89. {
  90. Console.Write("|");
  91. for (var y = 0; y < _environment.GetHeight(); ++y)
  92. {
  93. var state = _environment.GetRoomState(x, y);
  94. ConsoleColor? color = null;
  95. if (state == RoomState.Clean)
  96. {
  97. color = null;
  98. }
  99. else if (state == RoomState.Dust)
  100. {
  101. color = ConsoleColor.Gray;
  102. }
  103. else if (state == RoomState.Jewel)
  104. {
  105. color = ConsoleColor.Cyan;
  106. }
  107. else if (state == RoomState.Unknown)
  108. {
  109. color = ConsoleColor.Yellow;
  110. }
  111. else if (state == (RoomState.Jewel | RoomState.Dust))
  112. {
  113. color = ConsoleColor.Red;
  114. }
  115. SetBackgroundColor(color);
  116. Console.Write(_agEngineEffector.IsInPosition(x, y) ? "X" : " ");
  117. SetBackgroundColor(null);
  118. }
  119. Console.WriteLine("|");
  120. }
  121. SetBackgroundColor(bgColor);
  122. Console.WriteLine($"Battery: {_agBatterySensor.Spent}");
  123. Console.WriteLine($"Vaccumed: {_agVaccumSensor.Vaccumed}");
  124. Console.WriteLine($"Picked: {_agPickedSensor.Picked}");
  125. }
  126. }
  127. }