Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Написать вместе (вместо?) списка точек матрицу, потому что x_ch и y_ch работают не точно.
- * ставим точку. Вокруг нее ставим 8 точек и пытаемся от каждой дойти до края карты, если это не получилось, то точка лежит внутри
- * цикла. Запускаем от нее рекурсию или бфс и ищем границы.
- */
- using Android.App;
- using Android.Widget;
- using Android.OS;
- using Android.Views;
- using Android.Content;
- using Android.Graphics.Drawables;
- using Android.Graphics;
- using Android.Graphics.Drawables.Shapes;
- using System;
- using System.Collections.Generic;
- namespace test_app
- {
- [Activity(Label = "test_app", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity : Activity//, View.IOnTouchListener
- {
- public Field field;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- field = new Field(this);
- // Set our view from the "main" layout resource
- SetContentView(field);
- }
- }
- public class Field : View
- {
- public Field_Point[,] CreateGrid(int width, int height)
- {
- Field_Point[,] grid = new Field_Point[(N + 1), (M + 1)];
- for (int i = 0; i < N; i++)
- {
- for (int j = 0; j < M; j++)
- {
- grid[i, j] = new Field_Point(i * width / N, j * height / M, 0, i, j);
- }
- }
- return grid;
- }
- public struct Point
- {
- public int x, y, color;
- public Point(int _x, int _y, int _color)
- {
- x = _x;
- y = _y;
- color = _color;
- }
- }
- public struct Field_Point
- {
- public int xp, yp, color, xm, ym;
- public Field_Point(int _xp, int _yp, int _color, int _xm, int _ym)
- {
- xp = _xp;
- yp = _yp;
- color = _color;
- xm = _xm;
- ym = _ym;
- }
- }
- private Point[] points_red = new Point[0];
- private Point[] points_blue = new Point[0];
- private List<List<Field_Point>> Cycles = new List<List<Field_Point>>();
- private Paint paint_line, paint_point_red, paint_point_blue, paint_fill_blue, paint_fill_red;
- public bool flag;
- public const int N = 28;
- public const int M = 40;
- int player = 1;
- Field_Point[,] points_all = new Field_Point[(N + 1), (M + 1)];
- public Field(Context context) : base(context)
- {
- paint_line = new Paint();
- paint_line.SetARGB(255, 200, 255, 0);
- paint_line.SetStyle(Paint.Style.Stroke);
- paint_line.StrokeWidth = 4;
- paint_point_red = new Paint();
- paint_point_red.SetARGB(255, 255, 0, 0);
- paint_point_red.SetStyle(Paint.Style.Stroke);
- paint_point_red.StrokeWidth = 10;
- paint_point_blue = new Paint();
- paint_point_blue.SetARGB(255, 0, 0, 255);
- paint_point_blue.SetStyle(Paint.Style.Stroke);
- paint_point_blue.StrokeWidth = 10;
- paint_fill_blue = new Paint();
- paint_fill_blue.SetARGB(255, 0, 0, 255);
- paint_fill_blue.SetStyle(Paint.Style.Stroke);
- paint_fill_blue.StrokeWidth = 3;
- paint_fill_red = new Paint();
- paint_fill_red.SetARGB(255, 255, 0, 0);
- paint_fill_red.SetStyle(Paint.Style.Stroke);
- paint_fill_red.StrokeWidth = 3;
- flag = true;
- points_all = CreateGrid(720, 1120);
- }
- protected override void OnDraw(Canvas canvas)
- {
- for (int i = 0; i < N; i++)
- canvas.DrawLine(i * this.Width / N, 0, i * this.Width / N, this.Height, paint_line);
- for (int i = 0; i < M; i++)
- canvas.DrawLine(0, i * this.Height / M, this.Width, i * this.Height / M, paint_line);
- for (int i = 0; i < points_red.Length; i++)
- canvas.DrawPoint(points_red[i].x, points_red[i].y, paint_point_red);
- for (int i = 0; i < points_blue.Length; i++)
- canvas.DrawPoint(points_blue[i].x, points_blue[i].y, paint_point_blue);
- for (int i = 0; i < Cycles.Count; i++)
- for (int j = 0; j < Cycles[i].Count; j++)
- for (int k = 0; k < Cycles[i].Count; k++)
- if (Cycles[i][j].color == 1)
- canvas.DrawLine(Cycles[i][j].xp, Cycles[i][j].yp, Cycles[i][k].xp, Cycles[i][k].yp, paint_fill_red);
- else
- canvas.DrawLine(Cycles[i][j].xp, Cycles[i][j].yp, Cycles[i][k].xp, Cycles[i][k].yp, paint_fill_blue);
- }
- public bool check(int x, int y, Field_Point p)
- {
- return x >= 0 && x < N && y >= 0 && y < M && (points_all[x, y].color == p.color || points_all[x, y].color == 0);
- }
- public List<Field_Point> bfs(Field_Point p)
- {
- List<Field_Point> queue = new List<Field_Point>();
- List<Field_Point> answer = new List<Field_Point>();
- queue.Add(p);
- int x_ch = this.Width / N;
- int y_ch = this.Height / M;
- int[] X = { -1, -1, -1, 0, 0, 1, 1, 1 };
- int[] Y = { -1, 0, 1, -1, 1, -1, 0, 1 };
- while (queue.Count != 0)
- {
- for (int i = 0; i < X.Length; i++)
- {
- if (check(p.xm + X[i], p.ym + Y[i], p))
- {
- 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]);
- if (points_all[p.xm + X[i], p.ym + Y[i]].color == p.color)
- {
- if (answer.IndexOf(points_all[p.xm + X[i], p.ym + Y[i]]) == -1)
- answer.Add(points_all[p.xm + X[i], p.ym + Y[i]]);
- }
- }
- }
- }
- }
- public List<Field_Point> FindCycle(Field_Point p)
- {
- int x_ch = this.Width / N;
- int y_ch = this.Height / M;
- int[] X = { -1, -1, -1, 0, 0, 1, 1, 1 };
- int[] Y = { -1, 0, 1, -1, 1, -1, 0, 1 };
- List<Field_Point> Cycle = new List<Field_Point>();
- for (int i = 0; i < X.Length; i++)
- {
- if (check(p.xm + X[i], p.ym + Y[i], p))
- {
- 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]);
- Cycle = bfs(newp);
- if (Cycle != null)
- if (Cycle.Count > 0)
- return Cycle;
- }
- }
- return null;
- /*int x_ch = this.Width / N;
- int y_ch = this.Height / M;
- if (p.xm == begin.xm && p.ym == begin.ym && depth >= 2)
- return cycle;
- cycle.Add(p);
- int[] X = { -1, -1, -1, 0, 0, 1, 1, 1 };
- int[] Y = { -1, 0, 1, -1, 1, -1, 0, 1 };
- List<Field_Point> local_list = new List<Field_Point>();
- for (int i = 0; i < X.Length; i++)
- {
- if (check(p.xm + X[i], p.ym + Y[i], p) && (((p.xm + X[i]) != prev.xm) || ((p.ym + Y[i]) != prev.ym)))
- {
- 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]);
- List<Field_Point> l = FindCycle(newp, depth + 1, p, begin, cycle);
- if (l != null)
- if (l.Count > local_list.Count)
- local_list = l;
- }
- }
- return local_list;*/
- }
- public override bool OnTouchEvent(MotionEvent e)
- {
- switch (e.Action)
- {
- case MotionEventActions.Down:
- player *= -1;
- break;
- case MotionEventActions.Move:
- return true;
- }
- Point[] points_local = new Point[0];
- if (player == 1)
- points_local = (Point[])points_red.Clone();
- else
- points_local = (Point[])points_blue.Clone();
- Array.Resize(ref points_local, points_local.Length + 1);
- int x = (int)e.GetX();
- int y = (int)e.GetY();
- int new_xp = 0, new_yp = 0, new_xm = 0, new_ym = 0;
- double dist = 10000000;
- for (int i = 0; i < N; i++)
- for (int j = 0; j < M; j++)
- 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)
- {
- 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));
- new_xp = points_all[i, j].xp;
- new_yp = points_all[i, j].yp;
- new_xm = points_all[i, j].xm;
- new_ym = points_all[i, j].ym;
- }
- if (points_all[new_xm, new_ym].color != 0)
- return true;
- points_local[points_local.Length - 1] = new Point(new_xp, new_yp, player);
- if (player == 1)
- points_red = (Point[])points_local.Clone();
- else
- points_blue = (Point[])points_local.Clone();
- List<Field_Point> local_cycle = new List<Field_Point>();
- Field_Point new_fp = new Field_Point(new_xp, new_yp, player, new_xm, new_ym);
- points_all[new_xm, new_ym] = new_fp;
- List<Field_Point> Cycle = FindCycle(new_fp, 0, new_fp, new_fp, local_cycle);
- if (Cycle != null)
- Cycles.Add(Cycle);
- Invalidate();
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement