Hanoi játék visual c#

Hanoi játék visual c#
2010-05-02T22:01:25+02:00
2010-05-02T23:37:56+02:00
2022-11-17T15:25:34+01:00
satire
Sziasztok!
visual c# form alkalmazáshoz szeretnék segítséget kérni. hanoi tornyai játék készítése a célom.
A probléma az, hogy megoldás kérése után, ha klikkelek a formon vagy azon kívül, vagy átméretezem, vagy anélkül fingok, hogy a fejemre csapnék zöld trabant felkiáltással, tehát mindenképpen FAGY A FORM, akármit teszek. Leáll a korongok mozgatásával, és csak percek mulva tér vissza a megoldással Eddig új thread indítás volt az ötletem, de nem sikerült.
--------------------------------------------------------------------------------------------------------
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; public partial class Form1 : Form { //----------------------------------- KORONGOK ------------------------------ private PictureBox[] PicBox = new PictureBox[12]; private int[][] KorongokMereteHelye = new int[3][] { new int[13], new int[13], new int[13] }; private int[] KorongokSzama = new int[3]; private int[] MozgathatoKorongMerete = new int[3]; private int k; private Color oldColor; //----------------------------------- RUDAK --------------------------------- private PictureBox RudBox1; private PictureBox RudBox2; private PictureBox RudBox3; //----------------------------------- MOZGATÁS ------------------------------ private int rectX, rectY; private int origX, origY; private int origRud; private bool move = false; private bool stop = false; //----------------------------------- VISSZAVONÁS --------------------------- private int[,] UndoList = new int[1050, 3]; //----------------------------------- STOPPER ------------------------------- private System.Windows.Forms.Timer timer; private DateTime stopper; private Stopwatch sw; //----------------------------------- LÉPÉS SZÁM ---------------------------- private int StepsNumber = 0; public Form1() { InitializeComponent(); RudBox1 = new PictureBox(); RudBox1.BackColor = Color.Sienna; RudBox1.Top = 140; RudBox1.Width = 10; RudBox1.Height = 300; RudBox1.Left = 130; Controls.Add(RudBox1); RudBox2 = new PictureBox(); RudBox2.BackColor = Color.Sienna; RudBox2.Top = 140; RudBox2.Width = 10; RudBox2.Height = 300; RudBox2.Left = 380; Controls.Add(RudBox2); RudBox3 = new PictureBox(); RudBox3.BackColor = Color.Sienna; RudBox3.Top = 140; RudBox3.Width = 10; RudBox3.Height = 300; RudBox3.Left = 630; Controls.Add(RudBox3); timer = new System.Windows.Forms.Timer(); timer.Interval = 1; timer.Tick += new EventHandler(timer_Tick); stopper = new DateTime(); RefreshTimer(); } private void hanoi(int n, int a, int b, int c) { if (n > 0) { hanoi(n - 1, a, c, b); sw.Start(); mozgat(PicBox[n], a, c); Refresh(); System.Threading.Thread.Sleep(250); sw.Stop(); label4.Text = Convert.ToString(TimeSpan.FromSeconds(Math.Round(sw.Elapsed.TotalSeconds * 60))); StepsNumber++; UndoList[StepsNumber, 0] = n; UndoList[StepsNumber, 1] = a; UndoList[StepsNumber, 2] = c; this.label3.Text = Convert.ToString(StepsNumber); hanoi(n - 1, b, a, c); } } private void mozgat(PictureBox curent, int origRud, int newRud) { KorongokSzama[newRud] += 1; KorongokSzama[origRud] -= 1; switch (newRud) { case 0: curent.Left = 135 - curent.Width / 2; break; case 1: curent.Left = 385 - curent.Width / 2; break; case 2: curent.Left = 635 - curent.Width / 2; break; } curent.Top = 440 - 25 * KorongokSzama[newRud]; MozgathatoKorongMerete[newRud] = curent.Width; KorongokMereteHelye[newRud][k - KorongokSzama[newRud]] = curent.Width; MozgathatoKorongMerete[origRud] = KorongokMereteHelye[origRud][k - KorongokSzama[origRud]]; if (KorongokSzama[origRud] == 0) { MozgathatoKorongMerete[origRud] = (k + 1) * 20; } } private void undo() { if (StepsNumber > 0) { mozgat(PicBox[UndoList[StepsNumber, 0]], UndoList[StepsNumber, 2], UndoList[StepsNumber, 1]); StepsNumber--; move = false; this.label3.Text = Convert.ToString(StepsNumber); } } private void timer_Tick(object sender, EventArgs e) { stopper = stopper.AddMilliseconds(10); RefreshTimer(); } private void RefreshTimer() { label4.Text = stopper.Minute + ":" + stopper.Second + ":" + stopper.Millisecond / 10; } //----------------------------------- EVENTS -------------------------------- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { k = comboBox1.SelectedIndex + 4; KorongokSzama[0] = 0; KorongokSzama[1] = 0; KorongokSzama[2] = 0; StepsNumber = 0; timer.Stop(); stopper = new DateTime(); RefreshTimer(); this.label3.Text = Convert.ToString(StepsNumber); for (int i = 0; i < PicBox.Length; i++) { Controls.Remove(PicBox[i]); } for (int i = 0; i < k; i++) { PicBox[i] = new PictureBox(); PicBox[i].BackColor = Color.FromArgb(200 - 20 * i, 230 - 10 * i, 20 + 6 * i); PicBox[i].Top = 440 - k * 25 + 25 * i; PicBox[i].Width = 20 * i; PicBox[i].Height = 20; PicBox[i].Left = 135 - 10 * i; Controls.Add(PicBox[i]); PicBox[i].BringToFront(); KorongokMereteHelye[0][i] = PicBox[i].Width; KorongokSzama[0]++; PicBox[i].MouseDown += new System.Windows.Forms.MouseEventHandler(PicBox_MouseDown); PicBox[i].MouseMove += new System.Windows.Forms.MouseEventHandler(PicBox_MouseMove); PicBox[i].MouseUp += new System.Windows.Forms.MouseEventHandler(PicBox_MouseUp); PicBox[i].MouseEnter += new System.EventHandler(PicBox_MouseEnter); PicBox[i].MouseLeave += new System.EventHandler(PicBox_MouseLeave); } KorongokSzama[0]--; MozgathatoKorongMerete[0] = 20; MozgathatoKorongMerete[1] = (k + 1) * 20; MozgathatoKorongMerete[2] = (k + 1) * 20; } private void button1_Click(object sender, EventArgs e) { stop = false; timer.Stop(); stopper = new DateTime(); RefreshTimer(); if (StepsNumber == 0) { sw = Stopwatch.StartNew(); hanoi(k - 1, 0, 1, 2); } else { for (int i = StepsNumber; i > 0; i--) { Refresh(); System.Threading.Thread.Sleep(250); undo(); } sw = Stopwatch.StartNew(); hanoi(k - 1, 0, 1, 2); } } private void button2_Click(object sender, EventArgs e) { undo(); } private void PicBox_MouseDown(object sender, MouseEventArgs e) { if (StepsNumber == 0) { stopper = new DateTime(); RefreshTimer(); timer.Start(); } origX = (sender as PictureBox).Left; origY = (sender as PictureBox).Top; if (MozgathatoKorongMerete[0] == (sender as PictureBox).Width || MozgathatoKorongMerete[1] == (sender as PictureBox).Width || MozgathatoKorongMerete[2] == (sender as PictureBox).Width) { if ((sender as PictureBox).Left + (sender as PictureBox).Width / 2 == 135) { origRud = 0; } else if ((sender as PictureBox).Left + (sender as PictureBox).Width / 2 == 385) { &n
Mutasd a teljes hozzászólást!

Tetszett amit olvastál? Szeretnél a jövőben is értesülni a hasonló érdekességekről?
abcd