473,219 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,219 software developers and data experts.

The Tower OF Hanoi

I'm currently trying to write The Tower Of Hanoi (http://
illuminations.nctm.org/ActivityDetail.aspx?ID=40) in C#.
I have a panel on the form and I draw pegs on the panel inside the
form class. I passed GraphicObject to drawDisk method in disk class to
draw disks' pictures.
I used MouseDown/Up/Move to move these disks on the panel.
I'm attaching some part of my code:

private void panel1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{

mouseLocation = new Point(e.X, e.Y);
drag = true;
}
private void panel1_MouseMove(object
sender,System.Windows.Forms.MouseEventArgs e)
{//p1 is the first peg
((Disc)p1.discsPile[0]).xLocation = e.X;
((Disc)p1.discsPile[0]).yLocation = e.Y;
this.Invalidate();
panel1.Invalidate();
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
drag = false;
}
When I try to move the disks on the panel, nothing happens
Could you please someone tell me what the mistake is?
I appreciate for your help in advance.

Jul 4 '07 #1
3 5783
sh****************@yahoo.com wrote:
I'm currently trying to write The Tower Of Hanoi (http://
illuminations.nctm.org/ActivityDetail.aspx?ID=40) in C#.
I have a panel on the form and I draw pegs on the panel inside the
form class. I passed GraphicObject to drawDisk method in disk class to
draw disks' pictures.
I used MouseDown/Up/Move to move these disks on the panel.
I'm attaching some part of my code:

private void panel1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{

mouseLocation = new Point(e.X, e.Y);
drag = true;
}
private void panel1_MouseMove(object
sender,System.Windows.Forms.MouseEventArgs e)
{//p1 is the first peg
((Disc)p1.discsPile[0]).xLocation = e.X;
((Disc)p1.discsPile[0]).yLocation = e.Y;
this.Invalidate();
panel1.Invalidate();
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
drag = false;
}
When I try to move the disks on the panel, nothing happens
Standard question #4:
No, "nothing" never happens. Exactly what is happening?
Could you please someone tell me what the mistake is?
I appreciate for your help in advance.
Have you verified that the xLocation and yLocation properties actually
change? Do you actually use the xLocation and yLocation properties when
drawing the discs? What does the Disc class and the xLocation and
yLocation properties look like?

--
Göran Andersson
_____
http://www.guffa.com
Jul 4 '07 #2
On Jul 4, 2:37 pm, Göran Andersson <g...@guffa.comwrote:
shaghayeghcute2...@yahoo.com wrote:
I'm currently trying to write The Tower Of Hanoi (http://
illuminations.nctm.org/ActivityDetail.aspx?ID=40) in C#.
I have a panel on the form and I draw pegs on the panel inside the
form class. I passed GraphicObject to drawDisk method in disk class to
draw disks' pictures.
I used MouseDown/Up/Move to move these disks on the panel.
I'm attaching some part of my code:
private void panel1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
mouseLocation = new Point(e.X, e.Y);
drag = true;
}
private void panel1_MouseMove(object
sender,System.Windows.Forms.MouseEventArgs e)
{//p1 is the first peg
((Disc)p1.discsPile[0]).xLocation = e.X;
((Disc)p1.discsPile[0]).yLocation = e.Y;
this.Invalidate();
panel1.Invalidate();
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
drag = false;
}
When I try to move the disks on the panel, nothing happens

Standard question #4:
No, "nothing" never happens. Exactly what is happening?
Could you please someone tell me what the mistake is?
I appreciate for your help in advance.

Have you verified that the xLocation and yLocation properties actually
change? Do you actually use the xLocation and yLocation properties when
drawing the discs? What does the Disc class and the xLocation and
yLocation properties look like?

--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -
Hi dear Andersson;
Thanks for your quick respond.
I put messgasebox.show inside the Panel1_MouseMove to show me the
xlocation and ylocation but it didn't print anything.
It seems MouseMove/Up/Down arenot executed at all.
I changed System.windows.Form.MouseEventArgs to MouseEventArgs, but it
didn't make difference.
What is your next suggestion?
Thanks;
Shaghayegh

Jul 4 '07 #3
On Wed, 04 Jul 2007 11:17:02 -0700, <sh****************@yahoo.comwrote:
[...]
When I try to move the disks on the panel, nothing happens
Could you please someone tell me what the mistake is?
Well, it's hard to say, since you didn't post minimal-but-complete code.
Given the code that you did post, my first suspicion is that you didn't
actually subscribe those event handlers to correct events. I admit, that
doesn't seem plausible at the face of it, because your event handlers are
named exactly as they would be if you'd double-clicked on the panel
instance's events for them in the designer. But the MouseMove handler in
particular should definitely be changing the coordinates of the discs.
The code's wrong, but _something_ should be happening.

Basically, the only way I see for _nothing_ to happen is if that event
handler isn't called at all.

That said, you do have some other problems. The two that specifically
stand out are:

* You don't use "mouseLocation" when changing the coordinates of the
disc in question. Consequence: the disc will "snap" to the mouse
location, rather than just "sticking" to the mouse cursor wherever it
happens to be
* You don't check "drag" before changing the coordinates.
Consequence: any movement of the mouse over the control will result in
movement of a disc, whether the mouse button was pressed or not.

A third issue that will be a problem when you get around to actually
trying to make this work correctly is that you aren't doing any
hit-testing in MouseDown to figure out _which_ disc you should actually be
moving. I will assume for the moment that you're aware of that, and that
the code is currently the way it is just so you can figure the other stuff
out.

Now, all that said, here's the basic idea of how I think this should be
done:

private List<Peg_pegs = new List<Peg>();
Disc _discDragging;
Peg _pegFrom;
Point _pointDragStart;

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
foreach (Peg peg in _pegs)
{
// assumption: the Disc at index 0 is the top disc. Check
// only that disc, because you can't drag any other disc other
// than the top one anyway
Disc disc = peg.discsPile[0];

// assumption: the Disc class has a property Bounds that gets
// a Rectangle that is the extent of the Disc graphically.
if (disc.Bounds.Contains(e.Location))
{
_discDragging = disc;
_pegFrom = peg;
_pointDragStart = e.Location;
break;
}
}
}

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (_discDragging != null)
{
// assumption: the Disc class has a property Location that
gets/sets
// the position of the Disc graphically (most likely the
upper-left
// corner of the Bounds rectangle, but that's not actually
important
// here)
Point ptNew = _discDragging.Location;

// offset the position by the same distance that the mouse has
moved
// since our last mouse event
ptNew.Offset(e.Location - _pointDragStart);
_discDragging.Location = ptNew;

// update the "last mouse event" position
_pointDragStart = e.Location;

// Signal the panel to redraw
panel1.Invalidate();
}
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (_discDragging != null)
{
// I'm going to leave a lot of stuff out here. Hopefully, you
can see
// the basic idea from the code I did include and take it from
here.
// When the MouseUp event happens, you have to do some things:
// -- compare the disc's position to the peg locations,to
figure
// out which peg the disc is being dragged to
// -- check the existing discs on that peg to make sure
that the
// peg is a valid peg on which the disc can be dropped
// -- depending on the previous check, either remove the
disc from
// its current peg (my field named "_pegFrom"),
calculate the
// correct graphical position for the new peg, set it
to that
// position and add it to the new peg, or simply
calculate the
// correct graphical position for the original peg and
put it back
// there.
// -- if you want to provide the user feedback regarding
whether they
// have succesfully moved the discs from one peg to the
other, this
// would be a good place to check that too

_discDragging = null;
}
}

This is by no means the canonical way to do this. There are variations on
this theme that are all valid, depending on how you like the code to look
and what specifically you are doing with the code. But I think the above
solution is pretty appropriate given your goals.

One thing I will mention: you appear to be storing your list of discs on
each peg with the disc at index 0 being the top disc. IMHO, it makes more
sense to store the discs bottom-up, with the disc at index 0 being the
bottom disc. The reason being that it is more efficient when dealing with
an array to add and remove things at the end, rather than the beginning,
and all of the adding and removing happens at the disc that's at the top
of the peg. So making the disc at the top of the peg being the last in
the array rather than the first is more efficient.

Pete
Jul 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sonia | last post by:
Hi, I have written this code for the Tower of Hanoi problem. I was just wondering if there is any way of optimizing this code ? It's a failry simple code, but I thought that maybe somehow it...
8
by: Constant | last post by:
I need a programe that will deal with the solving of the problem ..i have 3 pegs A, B, C...and I want to move the disk A to disk C using B as auxiliary.At the end all disk should be at peg C in the...
7
by: ashishnh33 | last post by:
hi.......is there nyone to tell me how to solve the "tower of hanoi" problems
3
by: kamvisiouma | last post by:
Hi there, I'm fresh in learning c++ and I have to solve an exercise with hanoi and my problem is that I'm running out of time! The exercise is this, and I have to complete the ??? ...
2
by: sekitoleko | last post by:
This operation involves push/pop operations on a stack,arranging the smaller discs to sit on larger ones.How can I write a program to implement this?
0
by: vaj | last post by:
Desktop Tower defense the most addictive flash game ever http://www.yelp.com/redir?url=http://www.teenwag.com/show/games
6
by: poopsy | last post by:
hi guys cud some1 explain to me the towers of hanoi code, the source code is available evrywhr. i've been trying to understand it bt cant. can some1 plz help me i hav even tried to "trace" it but i...
5
by: seemanikam | last post by:
Can anyone suggest me how to devise an efficient divide-and-conquer algorithm for the Tower-of-Hanoi problem when the disks are colored alternately red and blue, and with the extra rule that no disk...
2
by: sunyboy | last post by:
hi I need a programe "Towers of Hanoi without use recessive function" in c++ Please help. Thanks for your time.
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.