Festelo

C# code

Feb 29th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.36 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.Navigation;
  14. using System.Windows.Shapes;
  15.  
  16. namespace WpfApplication6
  17. {
  18.     /// <summary>
  19.     /// Interaction logic for MainWindow.xaml
  20.     /// </summary>
  21.     public partial class MainWindow : Window
  22.     {
  23.         Brush color;
  24.         bool[,] coinc = new bool[9, 9];
  25.         bool[,] start = new bool[9, 9];
  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.         public MainWindow()
  40.         {
  41.             InitializeComponent();
  42.             color = boxes()[1,1].BorderBrush;
  43.             string[] lines = System.IO.File.ReadAllLines(Environment.CurrentDirectory + @"\words.txt");
  44.             char[,] table = new char[9, 9];
  45.             Random rndA = new Random();
  46.             int rnd = rndA.Next(0, lines.Length/10);
  47.             boxes();
  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.                         boxes()[i, j].IsReadOnly = true;
  58.                         start[i, j] = true;
  59.                         ColorChange(i, j, 3);
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.         public void kykarekalPetyx(TextBox sender, int hor, int ver)
  65.         {
  66.             if (sender.Text != "")
  67.             {
  68.                 int[] COUB = { 0, 1, 2 };
  69.                 for (int i = 0; i < 9; i++)
  70.                 {
  71.                     if (sender.Text == boxes()[i, hor].Text)
  72.                     {
  73.                         if (boxes()[i, hor] != sender)
  74.                         {
  75.                             coinc[ver, hor] = true;
  76.                             coinc[i, hor] = true;
  77.                             ColorChange(ver, hor, 2);
  78.                             ColorChange(i, hor, 2);
  79.                         }
  80.                     }
  81.                     if (sender.Text == boxes()[ver, i].Text)
  82.                     {
  83.                         if (boxes()[ver, i] != sender)
  84.                         {
  85.                             ColorChange(ver, hor, 2);
  86.                             ColorChange(ver, i, 2);
  87.                             coinc[ver, hor] = true;
  88.                             coinc[ver, i] = true;
  89.                         }
  90.                     }
  91.                 }
  92.                 //
  93.                 double temp1 = ver / 3;
  94.                 double temp2 = hor / 3;
  95.                 int temp3 = COUB[(int)Math.Floor(temp1)] * 3;
  96.                 int temp4 = COUB[(int)Math.Floor(temp2)] * 3;
  97.                 for (int i = 0; i < 3; i++)
  98.                 {
  99.                     for (int j = 0; j < 3; j++)
  100.                     {
  101.                         if (sender.Text == boxes()[temp3+i, temp4+j].Text)
  102.                         {
  103.                             if (boxes()[temp3 + i, temp4 + j] != sender)
  104.                             {
  105.                                 ColorChange(temp3+i, temp4+j, 2);
  106.                                 ColorChange(ver, hor, 2);
  107.                                 coinc[temp3 + i, temp4 + j] = true;
  108.                                 coinc[ver, hor] = true;
  109.                             }
  110.                         }
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.         public void ColorChange(int ver, int hor, int G)
  116.         {
  117.             if (G==1)
  118.             {
  119.                 boxes()[ver, hor].BorderBrush = color;
  120.             }
  121.             else if(G==2)
  122.             {
  123.                 boxes()[ver, hor].BorderBrush = Brushes.Red;
  124.             }
  125.             else
  126.             {
  127.                 boxes()[ver, hor].Foreground = Brushes.DimGray;
  128.             }
  129.         }
  130.         private void TextChanged(object sender, TextChangedEventArgs e)
  131.         {
  132.             if ((sender as TextBox).Text != "")
  133.             {
  134.                 if ((Convert.ToChar((sender as TextBox).Text) < '1') || (Convert.ToChar((sender as TextBox).Text) > '9'))
  135.                 {
  136.                     (sender as TextBox).Text = "";
  137.                     return;
  138.                 }
  139.             }
  140.             int hor = 0;
  141.             int ver = 0;
  142.             for (int i = 0; i < 9; i++)
  143.             {
  144.                 for (int j = 0; j < 9; j++)
  145.                 {
  146.                     ColorChange(i, j, 1);
  147.                     if (coinc[i, j])
  148.                     {
  149.                         kykarekalPetyx(boxes()[i, j], j, i);
  150.                     }
  151.                     if (boxes()[i, j] == (sender as TextBox))
  152.                     {
  153.                         ver = i;
  154.                         hor = j;
  155.                     }
  156.                 }
  157.             }
  158.             kykarekalPetyx((sender as TextBox), hor, ver);
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment