public partial class Form1 : Form { Button[] buttons = new Button[16]; public static void shuffle(String[] ar) { Random r = new Random(); int len = ar.Length; for (int i = len - 1; i > 0; i--) { int num = r.Next(i + 1); String temp = ar[i]; ar[i] = ar[num]; ar[num] = temp; } } public void button_Click(object sender, EventArgs e) { if (sender is Button) { Button b = (Button)sender; for (int i = 0; i < buttons.Length; i++) { if (b == buttons[i]) { //יעני אם לא הגענו לשורה אפס כי בשורה אפס בסוף שלה יש את מס 3 if (!buttons[i + 1].Visible && i % 4 != 3)//אם ימין פנוי אז להעתיק ולמחוק { buttons[i + 1].Visible = true; buttons[i + 1].BackColor = buttons[i].BackColor; buttons[i + 1].Text = buttons[i].Text; buttons[i].Visible = false; buttons[i].Text = ""; buttons[i].BackColor = Control.DefaultBackColor; } if (!buttons[i - 1].Visible && i % 4 != 0)//אם שמאל פנוי אז להעתיק ולמחוק { buttons[i - 1].Visible = true; buttons[i - 1].BackColor = buttons[i].BackColor; buttons[i - 1].Text = buttons[i].Text; buttons[i].Visible = false; buttons[i].Text = ""; buttons[i].BackColor = Control.DefaultBackColor; } if (!buttons[i + 4].Visible && i <= 12)//אם למטה פנוי אז להעתיק ולמחוק { buttons[i + 4].Visible = true; buttons[i + 4].BackColor = buttons[i].BackColor; buttons[i + 4].Text = buttons[i].Text; buttons[i].Visible = false; buttons[i].Text = ""; buttons[i].BackColor = Control.DefaultBackColor; } //כל עוד לא בשורה הראשונה if (!buttons[i - 4].Visible && i >= 4)//אם למעלה פנוי אז להעתיק ולמחוק { buttons[i - 4].Visible = true; buttons[i - 4].BackColor = buttons[i].BackColor; buttons[i - 4].Text = buttons[i].Text; buttons[i].Visible = false; buttons[i].Text = ""; buttons[i].BackColor = Control.DefaultBackColor; } } } } } public Form1() { Random rand = new Random(); String[] labels = new string[16]; for (int q = 0; q < labels.Length; q++) { if (q < 15) { int n = q + 1; labels[q] = n.ToString(); } else { labels[q] = q.ToString(); } } shuffle(labels);//SHUFFLED InitializeComponent(); for (int i = 0; i < 16; i++) { //int nums = dnar.Next(1, 16);//1-15 buttons[i] = new Button(); buttons[i].Size = new Size(80, 80); buttons[i].BackColor = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256)); int row = i / 4; int col = i % 4; int inbetween = 5; int size = 80; int x = row * (size + inbetween); int y = col * (size + inbetween); buttons[i].Location = new Point(x, y); buttons[i].Text = labels[i].ToString(); this.Controls.Add(buttons[i]); if (i == 15) { buttons[i].Visible = false; } buttons[i].Click += new EventHandler(button_Click); //this.Controls.Add(buttons[i]); } if(Sorted(buttons) == false || Sorted(buttons) == true)//גם ככה בין אם נצח או לא צריך לעשות מחדש { askingUser();//RESTART } } public void askingUser() { string message = "Do u want to start a new game?"; string title = "Game Over!"; MessageBoxButtons buttons = MessageBoxButtons.YesNo; DialogResult result = MessageBox.Show(message, title, buttons); if (result == DialogResult.No) { this.Close();// סגור אותו } else { restartGame();//עושה FORM חדש } } public void restartGame() { Random rand = new Random(); String[] labels = new string[16]; for (int q = 0; q < labels.Length; q++) { if (q < 15) { int n = q + 1; labels[q] = n.ToString(); } else { labels[q] = q.ToString(); } } shuffle(labels);//SHUFFLED for (int i = 0; i < 16; i++) { //int nums = dnar.Next(1, 16);//1-15 buttons[i].Size = new Size(80, 80); buttons[i].BackColor = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256)); int row = i / 4; int col = i % 4; int inbetween = 5; int size = 80; int x = row * (size + inbetween); int y = col * (size + inbetween); buttons[i].Location = new Point(x, y); buttons[i].Text = labels[i].ToString(); if (i == 15) { buttons[i].Visible = false; } } } public bool Sorted(Button[] buttons) { string curr; string next; for (int i = 0; i < buttons.Length - 1; i++) { curr = buttons[i].ToString(); next = buttons[i+1].ToString(); if (string.Compare(curr, next) < 0) //אם הנוכחי גדול מהבא בתור זה לא ממויין { return false; } } return true; } }