選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Extensions.cs 4.3KB

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