Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- namespace WpfApplication6
- {
- /// <summary>
- /// Логика взаимодействия для Window1.xaml
- /// </summary>
- public partial class Window1 : Window
- {
- Brush color;
- bool[,] coinc = new bool[9, 9];
- bool[,] otdix = new bool[9, 9];
- int lvl = -1;
- public TextBox[,] boxes()
- {
- TextBox[,] boxes = { { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8, textBox9 } ,
- { textBox10, textBox11, textBox12, textBox13, textBox14, textBox15, textBox16, textBox17, textBox18 },
- { textBox19, textBox20, textBox21, textBox22, textBox23, textBox24, textBox25, textBox26, textBox27 },
- { textBox28, textBox29, textBox30, textBox31, textBox32, textBox33, textBox34, textBox35, textBox36 },
- { textBox37, textBox38, textBox39, textBox40, textBox41, textBox42, textBox43, textBox44, textBox45 },
- { textBox46, textBox47, textBox48, textBox49, textBox50, textBox51, textBox52, textBox53, textBox54 },
- { textBox55, textBox56, textBox57, textBox58, textBox59, textBox60, textBox61, textBox62, textBox63 },
- { textBox64, textBox65, textBox66, textBox67, textBox68, textBox69, textBox70, textBox71, textBox72 },
- { textBox73, textBox74, textBox75, textBox76, textBox77, textBox78, textBox79, textBox80, textBox81 }};
- return boxes;
- }
- public Window1(int rnd = -1)
- {
- InitializeComponent();
- if (rnd != -1)
- {
- lvl = rnd;
- char[,] table = new char[9, 9];
- string[] lines = System.IO.File.ReadAllLines(Environment.CurrentDirectory + @"\table.txt");
- for (int i = 0; i < 9; i++)
- {
- char[] home = lines[rnd * 10 + i].ToCharArray();
- for (int j = 0; j < 9; j++)
- {
- table[i, j] = home[j];
- if (Convert.ToString(home[j]) != " ")
- {
- boxes()[i, j].Text = Convert.ToString(home[j]);
- }
- else boxes()[i, j].Text = "";
- }
- }
- }
- color = boxes()[1, 1].BorderBrush;
- }
- public void kykarekalPetyx(TextBox sender, int hor, int ver, bool zeroing = false)
- {
- if (zeroing) ColorChange(ver, hor, 1); coinc[ver, hor] = false;
- if (sender.Text != "")
- {
- int[] COUB = { 0, 1, 2 };
- for (int i = 0; i < 9; i++)
- {
- if (sender.Text == boxes()[i, hor].Text)
- {
- if (boxes()[i, hor] != sender)
- {
- coinc[ver, hor] = true;
- coinc[i, hor] = true;
- ColorChange(ver, hor, 2);
- ColorChange(i, hor, 2);
- }
- }
- //else coinc[ver, hor] = false; coinc[i, hor] = false;
- if (sender.Text == boxes()[ver, i].Text)
- {
- if (boxes()[ver, i] != sender)
- {
- ColorChange(ver, hor, 2);
- ColorChange(ver, i, 2);
- coinc[ver, hor] = true;
- coinc[ver, i] = true;
- }
- }
- //else coinc[ver, hor] = false; coinc[ver, i] = false;
- }
- //
- double temp1 = ver / 3;
- double temp2 = hor / 3;
- int temp3 = COUB[(int)Math.Floor(temp1)] * 3;
- int temp4 = COUB[(int)Math.Floor(temp2)] * 3;
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- if (sender.Text == boxes()[temp3 + i, temp4 + j].Text)
- {
- if (boxes()[temp3 + i, temp4 + j] != sender)
- {
- ColorChange(temp3 + i, temp4 + j, 2);
- ColorChange(ver, hor, 2);
- coinc[temp3 + i, temp4 + j] = true;
- coinc[ver, hor] = true;
- }
- }
- }
- }
- }
- }
- public void ColorChange(int ver, int hor, int G)
- {
- if (G == 1)
- {
- boxes()[ver, hor].BorderBrush = color;
- }
- else if (G == 2)
- {
- boxes()[ver, hor].BorderBrush = Brushes.Red;
- }
- else
- {
- boxes()[ver, hor].Foreground = Brushes.DimGray;
- }
- }
- private void TextChanged(object sender, TextChangedEventArgs e)
- {
- int hor = 0;
- int ver = 0;
- for (int i = 0; i < 9; i++)
- {
- for (int j = 0; j < 9; j++)
- {
- if (coinc[i, j])
- {
- kykarekalPetyx(boxes()[i, j], j, i, true);
- }
- if (boxes()[i, j] == (sender as TextBox))
- {
- ver = i;
- hor = j;
- }
- }
- }
- if (!otdix[ver, hor])
- {
- if ((sender as TextBox).Text != "")
- {
- if ((Convert.ToChar((sender as TextBox).Text) < '1') || (Convert.ToChar((sender as TextBox).Text) > '9'))
- {
- (sender as TextBox).Text = "";
- return;
- }
- }
- kykarekalPetyx((sender as TextBox), hor, ver);
- }
- }
- /*private void button_Click(object sender, RoutedEventArgs e)
- {
- bool ERROR = false;
- string Text = "";
- for (int i = 0; i < 9; i++)
- {
- Text += "\n";
- for (int j = 0; j < 9; j++)
- {
- if (coinc[i, j]) ERROR = true;
- else
- {
- string NewText;
- if (boxes()[i, j].Text == "") NewText = " ";
- else NewText = boxes()[i, j].Text;
- Text += NewText;
- }
- }
- }
- if (!ERROR)
- {
- if (lvl == -1)
- {
- string OLDText = System.IO.File.ReadAllText(Environment.CurrentDirectory + @"\table.txt");
- OLDText += Text + "\n// NOT REMOVE";
- System.IO.File.WriteAllText(Environment.CurrentDirectory + @"\table.txt", OLDText);
- MessageBox.Show("Судоку сохранен!", "Save");
- }
- }
- else MessageBox.Show("Найдена ошибка! Сохранение не выполнено!", "Save");
- }*/
- private void button_Click(object sender, RoutedEventArgs e)
- {
- bool ERROR = false;
- string Text = "";
- for (int i = 0; i < 9; i++)
- {
- Text += "\n";
- for (int j = 0; j < 9; j++)
- {
- if (coinc[i, j]) ERROR = true;
- else
- {
- string NewText;
- if (boxes()[i, j].Text == "") NewText = " ";
- else NewText = boxes()[i, j].Text;
- Text += NewText;
- }
- }
- }
- string[] OLDLines = System.IO.File.ReadAllLines(Environment.CurrentDirectory + @"\table.txt");
- string[] NewLines = new string[9];
- for (int i = 0; i < 9; i++)
- {
- for (int j = 0; j < 9; j++)
- {
- if (coinc[i, j]) ERROR = true;
- else
- {
- if (boxes()[i, j].Text == "") NewLines[i] += " ";
- else NewLines[i] += boxes()[i, j].Text;
- }
- }
- }
- string[] FNLines;
- if (!ERROR)
- {
- if (lvl != -1)
- {
- for (int i = 0; i < 9; i++)
- {
- OLDLines[lvl * 10 + i] = NewLines[i];
- }
- FNLines = OLDLines;
- }
- else
- {
- FNLines = new string[OLDLines.Length+10];
- for (int i = 0; i < OLDLines.Length; i++)
- {
- FNLines[i] = OLDLines[i];
- }
- for (int i = 0; i < 9; i++)
- {
- FNLines[i + OLDLines.Length] = NewLines[i];
- }
- }
- FNLines[FNLines.Length-1] = "// NOT REMOVE";
- System.IO.File.WriteAllLines(Environment.CurrentDirectory + @"\table.txt", FNLines);
- }
- else MessageBox.Show("Найдена ошибка! Сохранение не выполнено!", "Save");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement