Advertisement
Konark

Laba 6 Sidenko

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