Advertisement
Konark

Untitled

Mar 31st, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Threading;
  11.  
  12. namespace WindowsFormsApplication29
  13. {
  14.    
  15.     public partial class Form1 : Form
  16.     {
  17.         static int boardSize = 5;
  18.         bool[,] board = new bool[boardSize, boardSize];
  19.         bool[] rules = new bool[2];
  20.         int side = 40;
  21.         int count = 1;
  22.         List<Point> available_points = new List<Point>();
  23.  
  24.         public Form1()
  25.         {            
  26.              InitializeComponent();          
  27.         }
  28.  
  29.          void drawBoard()
  30.         {
  31.             panel1.Height = Form1.boardSize * 40;
  32.             panel1.Width  =  Form1.boardSize * 40;
  33.             Graphics g = panel1.CreateGraphics();
  34.             for (int i = 0; i < boardSize*boardSize; i++)
  35.                g.FillRectangle((i / boardSize + i % boardSize) % 2 != 0 ? Brushes.Blue : Brushes.White,(i / boardSize) * side, (i % boardSize) * side, side, side);
  36.       }
  37.  
  38.          void method(Point curPoint)
  39.          {
  40.              board[curPoint.x, curPoint.y] = true;
  41.              available_points.Clear();
  42.              drawFigure(curPoint.x, curPoint.y);
  43.              //find available points
  44.              for (int j = 0; j < boardSize; j++)
  45.              {
  46.                  for (int i = 0; i < boardSize; i++)
  47.                  {
  48.                      if (board[i, j]) continue;
  49.                      rules[0] = ((Math.Abs(i - curPoint.x) == 1) && (Math.Abs(j - curPoint.y) == 2));
  50.                      rules[1] = ((Math.Abs(i - curPoint.x) == 2) && (Math.Abs(j - curPoint.y) == 1));
  51.                      if (rules[0] || rules[1])
  52.                      {
  53.                          available_points.Add(new Point(i,j));
  54.                      }
  55.                  }
  56.              }
  57.              if (available_points.Any())
  58.              {
  59.                  Point nextPoint = getMinStepsPoint(available_points);
  60.                  method(nextPoint);
  61.              }
  62.              return;  
  63.  
  64.          }
  65.  
  66.          Point getMinStepsPoint(List<Point> points)
  67.          {
  68.              int[] steps = new int[points.Count];
  69.              int i = 0;
  70.              foreach (Point point in points)
  71.              {
  72.                  steps[i] = countSteps(point);
  73.                  i++;
  74.              }
  75.            
  76.              return points.ElementAt(Array.IndexOf(steps, steps.Min())); ;
  77.          }
  78.  
  79.          int countSteps(Point point)
  80.          {
  81.              int steps = 0;
  82.              for (int i = 0; i < boardSize; i++)
  83.              {
  84.                  for (int j = 0; j < boardSize; j++)
  85.                  {
  86.                      if (board[i, j]) continue;
  87.                      rules[0] = ((Math.Abs(i - point.x) == 1) && (Math.Abs(j - point.y) == 2));
  88.                      rules[1] = ((Math.Abs(i - point.x) == 2) && (Math.Abs(j - point.y) == 1));
  89.                      if (rules[0] || rules[1])
  90.                      {
  91.                          steps++;
  92.                      }
  93.                  }
  94.              }
  95.              return steps;
  96.          }
  97.          void allHorseWaysForPoint(int x, int y)
  98.          {
  99.            for (int i = 0; i < boardSize; i++)
  100.              {
  101.                  for (int j = 0; j < boardSize; j++)
  102.                  {
  103.                      rules[0] = ((Math.Abs(i - x) == 1) && (Math.Abs(j - y) == 2));
  104.                      rules[1] = ((Math.Abs(i - x) == 2) && (Math.Abs(j - y) == 1));
  105.                      if (rules[0] || rules[1])
  106.                      {
  107.  
  108.                          richTextBox1.AppendText((x+1) + "," + (y+1) + "-->" + (i+1) + "," + (j+1) + '\n');                        
  109.                      }
  110.                  }
  111.              }
  112.              
  113.          }
  114.  
  115.          void allRookWaysForPoint(int x, int y)
  116.         {                
  117.             for (int i = 0; i < boardSize; i++)
  118.             {
  119.                 for (int j = 0; j < boardSize; j++)
  120.                 {
  121.                     rules[0] = ((i - x == 0) && (Math.Abs(j - y) < boardSize) && Math.Abs(j - y) !=0);
  122.                     rules[1] = ((Math.Abs(i - x) < boardSize) && (j - y) == 0 && Math.Abs(i - x) !=0 );
  123.                     if (rules[0] || rules[1])
  124.                     {
  125.                         richTextBox1.AppendText((x + 1) + "," + (y + 1) + "-->" + (i + 1) + "," + (j + 1) + '\n');                                              
  126.                     }
  127.                 }
  128.             }
  129.         }
  130.         void newRookConf(int x, int y)
  131.         {
  132.             board[x, y] = true;
  133.             drawFigure(x, y);
  134.             for (int i = 0; i < boardSize; i++)
  135.             {
  136.                 for (int j = 0; j < boardSize; j++)
  137.                 {
  138.                     if (board[i,j]) continue;
  139.                     rules[0] = ((i - x == 0) && (Math.Abs(j -y) < boardSize));
  140.                     rules[1] = ((Math.Abs(i - x) < boardSize) && (j - y) == 0);
  141.                     if (rules[0] || rules[1])
  142.                     {
  143.                         newRookConf(i, j);
  144.                         return;
  145.                     }
  146.                 }
  147.             }
  148.         }
  149.         void drawFigure(int x, int y)
  150.         {
  151.             richTextBox1.AppendText("Правило №" + count + " x=" + (x + 1) + " y=" + (y + 1) + '\n');
  152.             Graphics g = panel1.CreateGraphics();
  153.             Image newImage = Image.FromFile("C:/Users/Влад/Desktop/WindowsFormsApplication29/WindowsFormsApplication29/horse.png");
  154.             Image newImage2 = Image.FromFile("C:/Users/Влад/Desktop/WindowsFormsApplication29/WindowsFormsApplication29/rook.png");
  155.             Image newImage3 = Image.FromFile("C:/Users/Влад/Desktop/WindowsFormsApplication29/WindowsFormsApplication29/1.jpg");
  156.             Image newImage4 = Image.FromFile("C:/Users/Влад/Desktop/WindowsFormsApplication29/WindowsFormsApplication29/2.png");
  157.             if (comboBox1.Text == "Horse")
  158.             {
  159.                 g.DrawImage(newImage3, new PointF(x * 40, y * 40));
  160.             }
  161.             else if(comboBox1.Text == "Rook")
  162.             {
  163.                 g.DrawImage(newImage3, new PointF(x * 40, y * 40));
  164.             }
  165.             g.DrawString((count).ToString(), new Font("Times New Romance", 12), new SolidBrush(Color.Black), new PointF(x * 40, y * 40));
  166.             count++;
  167.             Thread.Sleep(500);
  168.             g.DrawImage(newImage4, new PointF(x * 40, y * 40));
  169.             g.DrawString((count).ToString(), new Font("Times New Romance", 12), new SolidBrush(Color.Black), new PointF(x * 40, y * 40));
  170.         }
  171.         private void button1_Click(object sender, EventArgs e)
  172.         {
  173.             richTextBox1.Clear();
  174.             count = 1;
  175.             board = new bool[boardSize, boardSize];
  176.             if (string.IsNullOrEmpty(textBox2.Text) || string.IsNullOrEmpty(textBox2.Text))
  177.             {
  178.                 MessageBox.Show("Введите x и y.","Ошибка");
  179.                 return;
  180.             }
  181.             int x = Int32.Parse(textBox2.Text)-1;
  182.             int y = Int32.Parse(textBox3.Text)-1;
  183.             if (x >= boardSize || y >= boardSize || x < 0 || y < 0)
  184.             {
  185.                 MessageBox.Show("Проверьте x или y.", "Ошибка");
  186.                 return;
  187.             }
  188.  
  189.             drawBoard();
  190.             switch (comboBox1.SelectedIndex)
  191.             {
  192.                 case 0:
  193.                     //checkHorsePos(x, y);
  194.                     method(new Point(x,y));
  195.                     break;
  196.                 case 1:
  197.                     newRookConf(x, y);
  198.                     break;
  199.             }
  200.              
  201.                
  202.                
  203.            
  204.            
  205.         }
  206.         public void prepareForm()
  207.         {
  208.             boardSize = Int32.Parse(textBox1.Text);
  209.             board = new bool[boardSize, boardSize];
  210.             this.Width = boardSize * side + 47 + richTextBox1.Width;
  211.             this.Height = (boardSize * side) + textBox1.Height+50;
  212.             //button1.Location = new System.Drawing.Point(12, richTextBox1.Height + 25);
  213.             //label1.Location = new System.Drawing.Point(boardSize * side + 20, 27); // System.Drawing.Point(boardSize * side + 20, 27);
  214.             //richTextBox1.Location = new System.Drawing.Point(boardSize * side + 20, 43);
  215.  
  216.  
  217.         }
  218.         private void button2_Click(object sender, EventArgs e)
  219.         {
  220.             prepareForm();
  221.             drawBoard();
  222.         }
  223.  
  224.         private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  225.         {
  226.             if ((e.KeyChar <= 47 || e.KeyChar >= 58) && e.KeyChar != 8)
  227.                 e.Handled = true;
  228.         }
  229.  
  230.         private void Form1_Load(object sender, EventArgs e)
  231.         {
  232.              comboBox1.SelectedIndex = 0;
  233.              prepareForm();
  234.  
  235.         }
  236.  
  237.         private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
  238.         {
  239.             if ((e.KeyChar <= 47 || e.KeyChar >= 58) && e.KeyChar != 8)            
  240.                  e.Handled = true;
  241.                  
  242.         }
  243.  
  244.         private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
  245.         {
  246.             if ((e.KeyChar <= 47 || e.KeyChar >= 58) && e.KeyChar != 8)
  247.                                   e.Handled = true;
  248.            
  249.              
  250.         }
  251.  
  252.  
  253.         private void panel1_Paint(object sender, PaintEventArgs e)
  254.         {
  255.             panel1.Height = Form1.boardSize * 40;
  256.             panel1.Width = Form1.boardSize * 40;
  257.            
  258.             for (int i = 0; i < boardSize * boardSize; i++)
  259.                 e.Graphics.FillRectangle((i / boardSize + i % boardSize) % 2 != 0 ? Brushes.Blue : Brushes.White, (i / boardSize) * side, (i % boardSize) * side, side, side);
  260.    
  261.         }
  262.  
  263.         private void Form1_Resize(object sender, EventArgs e)
  264.         {
  265.         }
  266.  
  267.         private void richTextBox1_Resize(object sender, EventArgs e)
  268.         {
  269.  
  270.         }
  271.  
  272.         private void label1_Click(object sender, EventArgs e)
  273.         {
  274.  
  275.         }
  276.     }
  277.     class Point
  278.     {
  279.         public int x;
  280.         public int y;
  281.  
  282.         public Point(int x, int y)
  283.         {
  284.             this.x = x;
  285.             this.y = y;
  286.         }
  287.         public Point()
  288.         {
  289.             this.x = 0;
  290.             this.y = 0;
  291.         }
  292.     }
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement