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.

Extensions.cs 4.5KB

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