Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.53 KB | None | 0 0
  1. /*
  2. * Написать вместе (вместо?) списка точек матрицу, потому что x_ch и y_ch работают не точно.
  3. * ставим точку. Вокруг нее ставим 8 точек и пытаемся от каждой дойти до края карты, если это не получилось, то точка лежит внутри
  4. * цикла. Запускаем от нее рекурсию или бфс и ищем границы.
  5. */
  6.  
  7. using Android.App;
  8. using Android.Widget;
  9. using Android.OS;
  10. using Android.Views;
  11. using Android.Content;
  12. using Android.Graphics.Drawables;
  13. using Android.Graphics;
  14. using Android.Graphics.Drawables.Shapes;
  15. using System;
  16. using System.Collections.Generic;
  17.  
  18. namespace test_app
  19. {
  20. [Activity(Label = "test_app", MainLauncher = true, Icon = "@drawable/icon")]
  21. public class MainActivity : Activity//, View.IOnTouchListener
  22. {
  23. public Field field;
  24. protected override void OnCreate(Bundle bundle)
  25. {
  26. base.OnCreate(bundle);
  27. field = new Field(this);
  28. // Set our view from the "main" layout resource
  29.  
  30. SetContentView(field);
  31.  
  32.  
  33. }
  34.  
  35.  
  36.  
  37.  
  38. }
  39.  
  40.  
  41.  
  42. public class Field : View
  43. {
  44.  
  45. public Field_Point[,] CreateGrid(int width, int height)
  46. {
  47. Field_Point[,] grid = new Field_Point[(N + 1), (M + 1)];
  48. for (int i = 0; i < N; i++)
  49. {
  50. for (int j = 0; j < M; j++)
  51. {
  52. grid[i, j] = new Field_Point(i * width / N, j * height / M, 0, i, j);
  53. }
  54. }
  55. return grid;
  56. }
  57.  
  58. public struct Point
  59. {
  60. public int x, y, color;
  61. public Point(int _x, int _y, int _color)
  62. {
  63. x = _x;
  64. y = _y;
  65. color = _color;
  66. }
  67. }
  68.  
  69. public struct Field_Point
  70. {
  71. public int xp, yp, color, xm, ym;
  72. public Field_Point(int _xp, int _yp, int _color, int _xm, int _ym)
  73. {
  74. xp = _xp;
  75. yp = _yp;
  76. color = _color;
  77. xm = _xm;
  78. ym = _ym;
  79. }
  80. }
  81.  
  82. private Point[] points_red = new Point[0];
  83. private Point[] points_blue = new Point[0];
  84. private List<List<Field_Point>> Cycles = new List<List<Field_Point>>();
  85. private Paint paint_line, paint_point_red, paint_point_blue, paint_fill_blue, paint_fill_red;
  86. public bool flag;
  87. public const int N = 28;
  88. public const int M = 40;
  89. int player = 1;
  90. Field_Point[,] points_all = new Field_Point[(N + 1), (M + 1)];
  91. public Field(Context context) : base(context)
  92. {
  93. paint_line = new Paint();
  94. paint_line.SetARGB(255, 200, 255, 0);
  95. paint_line.SetStyle(Paint.Style.Stroke);
  96. paint_line.StrokeWidth = 4;
  97.  
  98. paint_point_red = new Paint();
  99. paint_point_red.SetARGB(255, 255, 0, 0);
  100. paint_point_red.SetStyle(Paint.Style.Stroke);
  101. paint_point_red.StrokeWidth = 10;
  102.  
  103. paint_point_blue = new Paint();
  104. paint_point_blue.SetARGB(255, 0, 0, 255);
  105. paint_point_blue.SetStyle(Paint.Style.Stroke);
  106. paint_point_blue.StrokeWidth = 10;
  107.  
  108. paint_fill_blue = new Paint();
  109. paint_fill_blue.SetARGB(255, 0, 0, 255);
  110. paint_fill_blue.SetStyle(Paint.Style.Stroke);
  111. paint_fill_blue.StrokeWidth = 3;
  112.  
  113. paint_fill_red = new Paint();
  114. paint_fill_red.SetARGB(255, 255, 0, 0);
  115. paint_fill_red.SetStyle(Paint.Style.Stroke);
  116. paint_fill_red.StrokeWidth = 3;
  117.  
  118. flag = true;
  119. points_all = CreateGrid(720, 1120);
  120.  
  121. }
  122.  
  123. protected override void OnDraw(Canvas canvas)
  124. {
  125.  
  126. for (int i = 0; i < N; i++)
  127. canvas.DrawLine(i * this.Width / N, 0, i * this.Width / N, this.Height, paint_line);
  128. for (int i = 0; i < M; i++)
  129. canvas.DrawLine(0, i * this.Height / M, this.Width, i * this.Height / M, paint_line);
  130. for (int i = 0; i < points_red.Length; i++)
  131. canvas.DrawPoint(points_red[i].x, points_red[i].y, paint_point_red);
  132. for (int i = 0; i < points_blue.Length; i++)
  133. canvas.DrawPoint(points_blue[i].x, points_blue[i].y, paint_point_blue);
  134. for (int i = 0; i < Cycles.Count; i++)
  135. for (int j = 0; j < Cycles[i].Count; j++)
  136. for (int k = 0; k < Cycles[i].Count; k++)
  137. if (Cycles[i][j].color == 1)
  138. canvas.DrawLine(Cycles[i][j].xp, Cycles[i][j].yp, Cycles[i][k].xp, Cycles[i][k].yp, paint_fill_red);
  139. else
  140. canvas.DrawLine(Cycles[i][j].xp, Cycles[i][j].yp, Cycles[i][k].xp, Cycles[i][k].yp, paint_fill_blue);
  141.  
  142. }
  143. public bool check(int x, int y, Field_Point p)
  144. {
  145. return x >= 0 && x < N && y >= 0 && y < M && (points_all[x, y].color == p.color || points_all[x, y].color == 0);
  146. }
  147.  
  148. public List<Field_Point> bfs(Field_Point p)
  149. {
  150. List<Field_Point> queue = new List<Field_Point>();
  151. List<Field_Point> answer = new List<Field_Point>();
  152. queue.Add(p);
  153. int x_ch = this.Width / N;
  154. int y_ch = this.Height / M;
  155. int[] X = { -1, -1, -1, 0, 0, 1, 1, 1 };
  156. int[] Y = { -1, 0, 1, -1, 1, -1, 0, 1 };
  157. while (queue.Count != 0)
  158. {
  159. for (int i = 0; i < X.Length; i++)
  160. {
  161. if (check(p.xm + X[i], p.ym + Y[i], p))
  162. {
  163. Field_Point newp = new Field_Point(p.xp + x_ch * X[i], p.yp + y_ch * Y[i], p.color, p.xm + X[i], p.ym + Y[i]);
  164. if (points_all[p.xm + X[i], p.ym + Y[i]].color == p.color)
  165. {
  166. if (answer.IndexOf(points_all[p.xm + X[i], p.ym + Y[i]]) == -1)
  167. answer.Add(points_all[p.xm + X[i], p.ym + Y[i]]);
  168. }
  169.  
  170. }
  171. }
  172. }
  173. }
  174. public List<Field_Point> FindCycle(Field_Point p)
  175. {
  176. int x_ch = this.Width / N;
  177. int y_ch = this.Height / M;
  178. int[] X = { -1, -1, -1, 0, 0, 1, 1, 1 };
  179. int[] Y = { -1, 0, 1, -1, 1, -1, 0, 1 };
  180. List<Field_Point> Cycle = new List<Field_Point>();
  181. for (int i = 0; i < X.Length; i++)
  182. {
  183. if (check(p.xm + X[i], p.ym + Y[i], p))
  184. {
  185. Field_Point newp = new Field_Point(p.xp + x_ch * X[i], p.yp + y_ch * Y[i], p.color, p.xm + X[i], p.ym + Y[i]);
  186. Cycle = bfs(newp);
  187. if (Cycle != null)
  188. if (Cycle.Count > 0)
  189. return Cycle;
  190. }
  191. }
  192. return null;
  193. /*int x_ch = this.Width / N;
  194. int y_ch = this.Height / M;
  195. if (p.xm == begin.xm && p.ym == begin.ym && depth >= 2)
  196. return cycle;
  197. cycle.Add(p);
  198. int[] X = { -1, -1, -1, 0, 0, 1, 1, 1 };
  199. int[] Y = { -1, 0, 1, -1, 1, -1, 0, 1 };
  200. List<Field_Point> local_list = new List<Field_Point>();
  201. for (int i = 0; i < X.Length; i++)
  202. {
  203. if (check(p.xm + X[i], p.ym + Y[i], p) && (((p.xm + X[i]) != prev.xm) || ((p.ym + Y[i]) != prev.ym)))
  204. {
  205. Field_Point newp = new Field_Point(p.xp + x_ch * X[i], p.yp + y_ch * Y[i], p.color, p.xm + X[i], p.ym + Y[i]);
  206. List<Field_Point> l = FindCycle(newp, depth + 1, p, begin, cycle);
  207. if (l != null)
  208. if (l.Count > local_list.Count)
  209. local_list = l;
  210. }
  211. }
  212. return local_list;*/
  213. }
  214.  
  215.  
  216. public override bool OnTouchEvent(MotionEvent e)
  217. {
  218. switch (e.Action)
  219. {
  220. case MotionEventActions.Down:
  221. player *= -1;
  222. break;
  223. case MotionEventActions.Move:
  224. return true;
  225. }
  226. Point[] points_local = new Point[0];
  227. if (player == 1)
  228. points_local = (Point[])points_red.Clone();
  229. else
  230. points_local = (Point[])points_blue.Clone();
  231. Array.Resize(ref points_local, points_local.Length + 1);
  232. int x = (int)e.GetX();
  233. int y = (int)e.GetY();
  234. int new_xp = 0, new_yp = 0, new_xm = 0, new_ym = 0;
  235. double dist = 10000000;
  236. for (int i = 0; i < N; i++)
  237. for (int j = 0; j < M; j++)
  238. if (Math.Sqrt((points_all[i, j].xp - x) * (points_all[i, j].xp - x) + (points_all[i, j].yp - y) * (points_all[i, j].yp - y)) < dist)
  239. {
  240. dist = Math.Sqrt((points_all[i, j].xp - x) * (points_all[i, j].xp - x) + (points_all[i, j].yp - y) * (points_all[i, j].yp - y));
  241. new_xp = points_all[i, j].xp;
  242. new_yp = points_all[i, j].yp;
  243. new_xm = points_all[i, j].xm;
  244. new_ym = points_all[i, j].ym;
  245. }
  246. if (points_all[new_xm, new_ym].color != 0)
  247. return true;
  248. points_local[points_local.Length - 1] = new Point(new_xp, new_yp, player);
  249. if (player == 1)
  250. points_red = (Point[])points_local.Clone();
  251. else
  252. points_blue = (Point[])points_local.Clone();
  253. List<Field_Point> local_cycle = new List<Field_Point>();
  254. Field_Point new_fp = new Field_Point(new_xp, new_yp, player, new_xm, new_ym);
  255. points_all[new_xm, new_ym] = new_fp;
  256. List<Field_Point> Cycle = FindCycle(new_fp, 0, new_fp, new_fp, local_cycle);
  257. if (Cycle != null)
  258. Cycles.Add(Cycle);
  259. Invalidate();
  260.  
  261. return true;
  262. }
  263.  
  264. }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement