Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Extensions.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using uqac_ia_aspirobot.Agent;
  5. using uqac_ia_aspirobot.Agent.Effectors;
  6. using uqac_ia_aspirobot.Common;
  7. using uqac_ia_aspirobot.Interfaces;
  8. namespace uqac_ia_aspirobot.Extensions
  9. {
  10. public static class Extensions
  11. {
  12. public static void AddDust(this IEnvironment env, int x, int y)
  13. {
  14. env.GetRoom(x, y).AddDust();
  15. }
  16. public static void RemoveDust(this IEnvironment env, int x, int y)
  17. {
  18. env.GetRoom(x, y).RemoveDust();
  19. }
  20. public static void AddJewel(this IEnvironment env, int x, int y)
  21. {
  22. env.GetRoom(x, y).AddJewel();
  23. }
  24. public static void RemoveJewel(this IEnvironment env, int x, int y)
  25. {
  26. env.GetRoom(x, y).RemoveJewel();
  27. }
  28. public static RoomState GetRoomState(this IEnvironment env, int x, int y)
  29. {
  30. return env.GetRoom(x, y).State;
  31. }
  32. public static bool ForeachRoom<T>(this IEnvironment env, Func<T, bool> action)
  33. where T : IRoom
  34. {
  35. for (var x = 0; x < env.GetWidth(); ++x)
  36. {
  37. for (var y = 0; y < env.GetHeight(); ++y)
  38. {
  39. if (!action((T) env.GetRoom(x, y)))
  40. {
  41. return false;
  42. }
  43. }
  44. }
  45. return true;
  46. }
  47. public static bool ForeachRoom(this IEnvironment env, Func<IRoom, bool> action)
  48. {
  49. return env.ForeachRoom<IRoom>(action);
  50. }
  51. public static IList<T> FindDustyRooms<T>(this IEnvironment env)
  52. where T : IRoom
  53. {
  54. var rooms = new List<T>();
  55. ForeachRoom<T>(env, room =>
  56. {
  57. if (room.State.HasFlag(RoomState.Dust))
  58. {
  59. rooms.Add(room);
  60. }
  61. return true;
  62. });
  63. return rooms;
  64. }
  65. public static IList<IRoom> FindDustyRooms(this IEnvironment env)
  66. {
  67. return env.FindDustyRooms<IRoom>();
  68. }
  69. public static IList<T> FindDustyRoomsWithoutJewel<T>(this IEnvironment env)
  70. where T : IRoom
  71. {
  72. var rooms = new List<T>();
  73. ForeachRoom<T>(env, room =>
  74. {
  75. if (room.State.CanBeVaccumed())
  76. {
  77. rooms.Add(room);
  78. }
  79. return true;
  80. });
  81. return rooms;
  82. }
  83. public static IList<IRoom> FindDustyRoomsWithoutJewel(this IEnvironment env)
  84. {
  85. return env.FindDustyRoomsWithoutJewel<IRoom>();
  86. }
  87. public static RoomState GetRoomState(this ArClient client, IRoom room)
  88. {
  89. return client.GetRoomState(room.X, room.Y);
  90. }
  91. public static void RemoveDust(this ArClient client, IRoom room)
  92. {
  93. client.RemoveDust(room.X, room.Y);
  94. }
  95. public static void RemoveJewel(this ArClient client, IRoom room)
  96. {
  97. client.RemoveJewel(room.X, room.Y);
  98. }
  99. public static int Distance(this IRoom room, int x, int y)
  100. {
  101. return Math.Abs(room.X - x) + Math.Abs(room.Y - y);
  102. }
  103. public static int Distance(this IRoom room, AgEngineEffector effector)
  104. {
  105. return room.Distance(effector.X, effector.Y);
  106. }
  107. public static int Distance(this AgEngineEffector effector, int x, int y)
  108. {
  109. return Math.Abs(effector.X - x) + Math.Abs(effector.Y - y);
  110. }
  111. public static int Distance(this AgEngineEffector effector, IRoom room)
  112. {
  113. return effector.Distance(room.X, room.Y);
  114. }
  115. public static bool CanBeVaccumed(this RoomState state)
  116. {
  117. return state.HasFlag(RoomState.Dust) && !state.HasFlag(RoomState.Jewel);
  118. }
  119. public static bool IsInPosition(this AgEngineEffector engine, int x, int y)
  120. {
  121. return engine.X == x && engine.Y == y;
  122. }
  123. public static bool IsInPosition(this IRoom room, int x, int y)
  124. {
  125. return room.X == x && room.Y == y;
  126. }
  127. public static bool IsInRoom(this AgEngineEffector engine, IRoom room)
  128. {
  129. return engine.IsInPosition(room.X, room.Y);
  130. }
  131. public static void Move(this AgEngineEffector engine, int dx, int dy)
  132. {
  133. engine.MoveTo(engine.X + dx, engine.Y + dy);
  134. }
  135. }
  136. }