Advertisement
Festelo

Untitled

Apr 13th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14.  
  15. namespace WpfApplication6
  16. {
  17.     /// <summary>
  18.     /// Логика взаимодействия для Window1.xaml
  19.     /// </summary>
  20.     public partial class Window1 : Window
  21.     {
  22.         Brush color;
  23.         bool[,] coinc = new bool[9, 9];
  24.         bool[,] otdix = new bool[9, 9];
  25.         int lvl = -1;
  26.         public TextBox[,] boxes()
  27.         {
  28.             TextBox[,] boxes = { { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8, textBox9 } ,
  29.                                 { textBox10, textBox11, textBox12, textBox13, textBox14, textBox15, textBox16, textBox17, textBox18 },
  30.                                 { textBox19, textBox20, textBox21, textBox22, textBox23, textBox24, textBox25, textBox26, textBox27 },
  31.                                 { textBox28, textBox29, textBox30, textBox31, textBox32, textBox33, textBox34, textBox35, textBox36 },
  32.                                 { textBox37, textBox38, textBox39, textBox40, textBox41, textBox42, textBox43, textBox44, textBox45 },
  33.                                 { textBox46, textBox47, textBox48, textBox49, textBox50, textBox51, textBox52, textBox53, textBox54 },
  34.                                 { textBox55, textBox56, textBox57, textBox58, textBox59, textBox60, textBox61, textBox62, textBox63 },
  35.                                 { textBox64, textBox65, textBox66, textBox67, textBox68, textBox69, textBox70, textBox71, textBox72 },
  36.                                 { textBox73, textBox74, textBox75, textBox76, textBox77, textBox78, textBox79, textBox80, textBox81 }};
  37.             return boxes;
  38.         }
  39.  
  40.         public Window1(int rnd = -1)
  41.         {
  42.             InitializeComponent();
  43.             if (rnd != -1)
  44.             {
  45.                 lvl = rnd;
  46.                 char[,] table = new char[9, 9];
  47.                 string[] lines = System.IO.File.ReadAllLines(Environment.CurrentDirectory + @"\table.txt");
  48.                 for (int i = 0; i < 9; i++)
  49.                 {
  50.                     char[] home = lines[rnd * 10 + i].ToCharArray();
  51.                     for (int j = 0; j < 9; j++)
  52.                     {
  53.                         table[i, j] = home[j];
  54.                         if (Convert.ToString(home[j]) != " ")
  55.                         {
  56.                             boxes()[i, j].Text = Convert.ToString(home[j]);
  57.                         }
  58.                         else boxes()[i, j].Text = "";
  59.                     }
  60.                 }
  61.             }
  62.             color = boxes()[1, 1].BorderBrush;
  63.         }
  64.  
  65.         public void kykarekalPetyx(TextBox sender, int hor, int ver, bool zeroing = false)
  66.         {
  67.             if (zeroing) ColorChange(ver, hor, 1); coinc[ver, hor] = false;
  68.             if (sender.Text != "")
  69.             {
  70.                 int[] COUB = { 0, 1, 2 };
  71.                 for (int i = 0; i < 9; i++)
  72.                 {
  73.                     if (sender.Text == boxes()[i, hor].Text)
  74.                     {
  75.                         if (boxes()[i, hor] != sender)
  76.                         {
  77.                             coinc[ver, hor] = true;
  78.                             coinc[i, hor] = true;
  79.                             ColorChange(ver, hor, 2);
  80.                             ColorChange(i, hor, 2);
  81.                         }
  82.                     }
  83.                     //else coinc[ver, hor] = false; coinc[i, hor] = false;
  84.                     if (sender.Text == boxes()[ver, i].Text)
  85.                     {
  86.                         if (boxes()[ver, i] != sender)
  87.                         {
  88.                             ColorChange(ver, hor, 2);
  89.                             ColorChange(ver, i, 2);
  90.                             coinc[ver, hor] = true;
  91.                             coinc[ver, i] = true;
  92.                         }
  93.                     }
  94.                     //else coinc[ver, hor] = false; coinc[ver, i] = false;
  95.                 }
  96.                 //
  97.                 double temp1 = ver / 3;
  98.                 double temp2 = hor / 3;
  99.                 int temp3 = COUB[(int)Math.Floor(temp1)] * 3;
  100.                 int temp4 = COUB[(int)Math.Floor(temp2)] * 3;
  101.                 for (int i = 0; i < 3; i++)
  102.                 {
  103.                     for (int j = 0; j < 3; j++)
  104.                     {
  105.                         if (sender.Text == boxes()[temp3 + i, temp4 + j].Text)
  106.                         {
  107.                             if (boxes()[temp3 + i, temp4 + j] != sender)
  108.                             {
  109.                                 ColorChange(temp3 + i, temp4 + j, 2);
  110.                                 ColorChange(ver, hor, 2);
  111.                                 coinc[temp3 + i, temp4 + j] = true;
  112.                                 coinc[ver, hor] = true;
  113.                             }
  114.                         }
  115.                     }
  116.                 }
  117.             }
  118.         }
  119.         public void ColorChange(int ver, int hor, int G)
  120.         {
  121.             if (G == 1)
  122.             {
  123.                 boxes()[ver, hor].BorderBrush = color;
  124.             }
  125.             else if (G == 2)
  126.             {
  127.                 boxes()[ver, hor].BorderBrush = Brushes.Red;
  128.             }
  129.             else
  130.             {
  131.                 boxes()[ver, hor].Foreground = Brushes.DimGray;
  132.             }
  133.         }
  134.         private void TextChanged(object sender, TextChangedEventArgs e)
  135.         {
  136.             int hor = 0;
  137.             int ver = 0;
  138.             for (int i = 0; i < 9; i++)
  139.             {
  140.                 for (int j = 0; j < 9; j++)
  141.                 {
  142.                     if (coinc[i, j])
  143.                     {
  144.                         kykarekalPetyx(boxes()[i, j], j, i, true);
  145.                     }
  146.                     if (boxes()[i, j] == (sender as TextBox))
  147.                     {
  148.                         ver = i;
  149.                         hor = j;
  150.                     }
  151.                 }
  152.             }
  153.             if (!otdix[ver, hor])
  154.             {
  155.                 if ((sender as TextBox).Text != "")
  156.                 {
  157.                     if ((Convert.ToChar((sender as TextBox).Text) < '1') || (Convert.ToChar((sender as TextBox).Text) > '9'))
  158.                     {
  159.                         (sender as TextBox).Text = "";
  160.                         return;
  161.                     }
  162.                 }
  163.                 kykarekalPetyx((sender as TextBox), hor, ver);
  164.             }
  165.         }
  166.  
  167.         /*private void button_Click(object sender, RoutedEventArgs e)
  168.         {
  169.             bool ERROR = false;
  170.             string Text = "";
  171.             for (int i = 0; i < 9; i++)
  172.             {
  173.                 Text += "\n";
  174.                 for (int j = 0; j < 9; j++)
  175.                 {
  176.                     if (coinc[i, j]) ERROR = true;
  177.                     else
  178.                     {
  179.                         string NewText;
  180.                         if (boxes()[i, j].Text == "") NewText = " ";
  181.                         else NewText = boxes()[i, j].Text;
  182.                         Text += NewText;
  183.                     }
  184.                 }
  185.             }
  186.             if (!ERROR)
  187.             {
  188.                 if (lvl == -1)
  189.                 {
  190.                     string OLDText = System.IO.File.ReadAllText(Environment.CurrentDirectory + @"\table.txt");
  191.                     OLDText += Text + "\n// NOT REMOVE";
  192.                     System.IO.File.WriteAllText(Environment.CurrentDirectory + @"\table.txt", OLDText);
  193.                     MessageBox.Show("Судоку сохранен!", "Save");
  194.                 }
  195.             }
  196.             else MessageBox.Show("Найдена ошибка! Сохранение не выполнено!", "Save");
  197.         }*/
  198.  
  199.         private void button_Click(object sender, RoutedEventArgs e)
  200.         {
  201.             bool ERROR = false;
  202.             string Text = "";
  203.             for (int i = 0; i < 9; i++)
  204.             {
  205.                 Text += "\n";
  206.                 for (int j = 0; j < 9; j++)
  207.                 {
  208.                     if (coinc[i, j]) ERROR = true;
  209.                     else
  210.                     {
  211.                         string NewText;
  212.                         if (boxes()[i, j].Text == "") NewText = " ";
  213.                         else NewText = boxes()[i, j].Text;
  214.                         Text += NewText;
  215.                     }
  216.                 }
  217.             }
  218.                 string[] OLDLines = System.IO.File.ReadAllLines(Environment.CurrentDirectory + @"\table.txt");
  219.                 string[] NewLines = new string[9];
  220.                 for (int i = 0; i < 9; i++)
  221.                 {
  222.                     for (int j = 0; j < 9; j++)
  223.                     {
  224.                         if (coinc[i, j]) ERROR = true;
  225.                         else
  226.                         {
  227.                             if (boxes()[i, j].Text == "") NewLines[i] += " ";
  228.                             else NewLines[i] += boxes()[i, j].Text;
  229.                         }
  230.                     }
  231.                 }
  232.                 string[] FNLines;
  233.             if (!ERROR)
  234.             {
  235.                 if (lvl != -1)
  236.                 {
  237.                     for (int i = 0; i < 9; i++)
  238.                     {
  239.                         OLDLines[lvl * 10 + i] = NewLines[i];
  240.                     }
  241.                     FNLines = OLDLines;
  242.                 }
  243.                 else
  244.                 {
  245.                     FNLines = new string[OLDLines.Length+10];
  246.                     for (int i = 0; i < OLDLines.Length; i++)
  247.                     {
  248.                         FNLines[i] = OLDLines[i];
  249.                     }
  250.                     for (int i = 0; i < 9; i++)
  251.                     {
  252.                         FNLines[i + OLDLines.Length] = NewLines[i];
  253.                     }
  254.                 }
  255.                 FNLines[FNLines.Length-1] = "// NOT REMOVE";
  256.                 System.IO.File.WriteAllLines(Environment.CurrentDirectory + @"\table.txt", FNLines);
  257.             }
  258.             else MessageBox.Show("Найдена ошибка! Сохранение не выполнено!", "Save");
  259.         }
  260.     }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement