Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Threading;
- namespace WindowsFormsApplication29
- {
- public partial class Form1 : Form
- {
- static int boardSize = 5;
- bool[,] board = new bool[boardSize, boardSize];
- bool[] rules = new bool[2];
- int side = 40;
- int count = 1;
- static Random rnd = new Random();
- List<Point> available_points = new List<Point>();
- public Form1()
- {
- InitializeComponent();
- }
- void drawBoard()
- {
- panel1.Height = Form1.boardSize * 40;
- panel1.Width = Form1.boardSize * 40;
- Graphics g = panel1.CreateGraphics();
- for (int i = 0; i < boardSize*boardSize; i++)
- g.FillRectangle((i / boardSize + i % boardSize) % 2 != 0 ? Brushes.Blue : Brushes.White,(i / boardSize) * side, (i % boardSize) * side, side, side);
- }
- void method(Point curPoint)
- {
- board[curPoint.x, curPoint.y] = true;
- available_points.Clear();
- drawFigure(curPoint.x, curPoint.y);
- //find available points
- int x2 = Int32.Parse(textBox4.Text) - 1;
- int y2 = Int32.Parse(textBox5.Text) - 1;
- for (int j = 0; j < boardSize; j++)
- {
- for (int i = 0; i < boardSize; i++)
- {
- if (board[i, j]) continue;
- rules[0] = ((Math.Abs(i - curPoint.x) == 1) && (Math.Abs(j - curPoint.y) == 2));
- rules[1] = ((Math.Abs(i - curPoint.x) == 2) && (Math.Abs(j - curPoint.y) == 1));
- if (rules[0] || rules[1])
- {
- available_points.Add(new Point(i,j));
- if (x2 != -1 && y2 != -1 && curPoint.x == x2 && curPoint.y == y2)
- {
- goto stop;
- }
- }
- }
- }
- if (available_points.Any())
- {
- Point nextPoint;
- if (comboBox2.SelectedIndex == 0)
- {
- nextPoint = available_points.First();
- method(nextPoint);
- }
- else if(comboBox2.SelectedIndex == 1)
- {
- nextPoint = available_points.Last();
- method(nextPoint);
- }else if(comboBox2.SelectedIndex == 2)
- {
- int index = rnd.Next(available_points.Count);
- nextPoint = available_points.ElementAt(index);
- method(nextPoint);
- }
- }
- stop: return;
- }
- Point getMinStepsPoint(List<Point> points)
- {
- int[] steps = new int[points.Count];
- int i = 0;
- foreach (Point point in points)
- {
- steps[i] = countSteps(point);
- i++;
- }
- return points.ElementAt(Array.IndexOf(steps, steps.Min())); ;
- }
- int countSteps(Point point)
- {
- int steps = 0;
- for (int i = 0; i < boardSize; i++)
- {
- for (int j = 0; j < boardSize; j++)
- {
- if (board[i, j]) continue;
- rules[0] = ((Math.Abs(i - point.x) == 1) && (Math.Abs(j - point.y) == 2));
- rules[1] = ((Math.Abs(i - point.x) == 2) && (Math.Abs(j - point.y) == 1));
- if (rules[0] || rules[1])
- {
- steps++;
- }
- }
- }
- return steps;
- }
- void allHorseWaysForPoint(int x, int y)
- {
- for (int i = 0; i < boardSize; i++)
- {
- for (int j = 0; j < boardSize; j++)
- {
- rules[0] = ((Math.Abs(i - x) == 1) && (Math.Abs(j - y) == 2));
- rules[1] = ((Math.Abs(i - x) == 2) && (Math.Abs(j - y) == 1));
- if (rules[0] || rules[1])
- {
- richTextBox1.AppendText((x+1) + "," + (y+1) + "-->" + (i+1) + "," + (j+1) + '\n');
- }
- }
- }
- }
- void allRookWaysForPoint(int x, int y)
- {
- for (int i = 0; i < boardSize; i++)
- {
- for (int j = 0; j < boardSize; j++)
- {
- rules[0] = ((i - x == 0) && (Math.Abs(j - y) < boardSize) && Math.Abs(j - y) !=0);
- rules[1] = ((Math.Abs(i - x) < boardSize) && (j - y) == 0 && Math.Abs(i - x) !=0 );
- if (rules[0] || rules[1])
- {
- richTextBox1.AppendText((x + 1) + "," + (y + 1) + "-->" + (i + 1) + "," + (j + 1) + '\n');
- }
- }
- }
- }
- void newRookConf(int x, int y)
- {
- board[x, y] = true;
- drawFigure(x, y);
- for (int i = 0; i < boardSize; i++)
- {
- for (int j = 0; j < boardSize; j++)
- {
- if (board[i,j]) continue;
- rules[0] = ((i - x == 0) && (Math.Abs(j -y) < boardSize));
- rules[1] = ((Math.Abs(i - x) < boardSize) && (j - y) == 0);
- if (rules[0] || rules[1])
- {
- newRookConf(i, j);
- return;
- }
- }
- }
- }
- void drawFigure(int x, int y)
- {
- richTextBox1.AppendText("Правило №" + count + " x=" + (x + 1) + " y=" + (y + 1) + '\n');
- Graphics g = panel1.CreateGraphics();
- Image newImage = Image.FromFile("C:/Users/Влад/Desktop/WindowsFormsApplication29/WindowsFormsApplication29/horse.png");
- Image newImage2 = Image.FromFile("C:/Users/Влад/Desktop/WindowsFormsApplication29/WindowsFormsApplication29/rook.png");
- Image newImage3 = Image.FromFile("C:/Users/Влад/Desktop/WindowsFormsApplication29/WindowsFormsApplication29/1.jpg");
- Image newImage4 = Image.FromFile("C:/Users/Влад/Desktop/WindowsFormsApplication29/WindowsFormsApplication29/2.png");
- if (comboBox1.Text == "Horse")
- {
- g.DrawImage(newImage3, new PointF(x * 40, y * 40));
- }
- else if(comboBox1.Text == "Rook")
- {
- g.DrawImage(newImage3, new PointF(x * 40, y * 40));
- }
- g.DrawString((count).ToString(), new Font("Times New Romance", 12), new SolidBrush(Color.Black), new PointF(x * 40, y * 40));
- Thread.Sleep(500);
- g.DrawImage(newImage4, new PointF(x * 40, y * 40));
- g.DrawString((count).ToString(), new Font("Times New Romance", 12), new SolidBrush(Color.Black), new PointF(x * 40, y * 40));
- count++;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- richTextBox1.Clear();
- count = 1;
- board = new bool[boardSize, boardSize];
- if (string.IsNullOrEmpty(textBox2.Text) || string.IsNullOrEmpty(textBox2.Text))
- {
- MessageBox.Show("Введите x и y.","Ошибка");
- return;
- }
- int x = Int32.Parse(textBox2.Text)-1;
- int y = Int32.Parse(textBox3.Text)-1;
- if (x >= boardSize || y >= boardSize || x < 0 || y < 0)
- {
- MessageBox.Show("Проверьте x или y.", "Ошибка");
- return;
- }
- drawBoard();
- switch (comboBox1.SelectedIndex)
- {
- case 0:
- //checkHorsePos(x, y);
- method(new Point(x,y));
- break;
- case 1:
- newRookConf(x, y);
- break;
- }
- }
- public void prepareForm()
- {
- boardSize = Int32.Parse(textBox1.Text);
- board = new bool[boardSize, boardSize];
- this.Width = boardSize * side + 47 + richTextBox1.Width;
- this.Height = (boardSize * side) + textBox1.Height+50;
- //button1.Location = new System.Drawing.Point(12, richTextBox1.Height + 25);
- //label1.Location = new System.Drawing.Point(boardSize * side + 20, 27); // System.Drawing.Point(boardSize * side + 20, 27);
- //richTextBox1.Location = new System.Drawing.Point(boardSize * side + 20, 43);
- }
- private void button2_Click(object sender, EventArgs e)
- {
- prepareForm();
- drawBoard();
- }
- private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
- {
- if ((e.KeyChar <= 47 || e.KeyChar >= 58) && e.KeyChar != 8)
- e.Handled = true;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- comboBox1.SelectedIndex = 0;
- comboBox2.SelectedIndex = 0;
- prepareForm();
- }
- private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
- {
- if ((e.KeyChar <= 47 || e.KeyChar >= 58) && e.KeyChar != 8)
- e.Handled = true;
- }
- private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
- {
- if ((e.KeyChar <= 47 || e.KeyChar >= 58) && e.KeyChar != 8)
- e.Handled = true;
- }
- private void panel1_Paint(object sender, PaintEventArgs e)
- {
- panel1.Height = Form1.boardSize * 40;
- panel1.Width = Form1.boardSize * 40;
- for (int i = 0; i < boardSize * boardSize; i++)
- e.Graphics.FillRectangle((i / boardSize + i % boardSize) % 2 != 0 ? Brushes.Blue : Brushes.White, (i / boardSize) * side, (i % boardSize) * side, side, side);
- }
- private void Form1_Resize(object sender, EventArgs e)
- {
- }
- private void richTextBox1_Resize(object sender, EventArgs e)
- {
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (comboBox1.SelectedIndex == 1)
- {
- comboBox2.Enabled = false;
- label8.Enabled = false;
- }
- }
- }
- class Point
- {
- public int x;
- public int y;
- public Point(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- public Point()
- {
- this.x = 0;
- this.y = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement