[Silverlight] Tower games with AI

Source: Internet
Author: User

First view results

 

 

CompleteCodeDownload/aimeast/slanyhanoi.zip

 

Briefly describe the design

Viewmodel and model are designed as follows:

 

The animation effect uses behavior ).

Using system; using system. windows; using system. windows. interactivity; using system. windows. media. animation; namespace anyhanoi {public class discfluidmovebehavior: behavior <frameworkelement> {public point translate {get {return (point) getvalue (translateproperty);} set {setvalue (translateproperty, value) ;}} // using a dependencyproperty as the backing store for translate. this enables animation, styling, binding, Etc... public static readonly dependencyproperty translateproperty = dependencyproperty. register ("translate", typeof (point), typeof (discfluidmovebehavior), new propertymetadata (translate_propertychangedcallback); Private Static void translate_propertychangedcallback (dependencyobject D, subject e) {point P = (point) E. newvalue; discfluidmovebehavior B = (discfluidmovebehavior) D; try {B. update (p);} catch {}} private void Update (point P) {storyboard = new storyboard (); doubleanimation x = new doubleanimation (); doubleanimation y = new doubleanimation (); X. setvalue (storyboard. targetpropertyproperty, new propertypath ("(uielement. rendertransform ). (compositetransform. translatex) "); Y. setvalue (storyboard. targetpropertyproperty, new propertypath ("(uielement. rendertransform ). (compositetransform. translatey) "); X. duration = This. duration; Y. duration = This. duration; X. to = P. x; Y. to = P. y; storyboard. settarget (x, base. associatedobject); storyboard. settarget (Y, base. associatedobject); storyboard. children. add (x); storyboard. children. add (y); storyboard. begin ();} public duration {get {return (duration) getvalue (durationproperty);} set {setvalue (durationproperty, value );}} // using a dependencyproperty as the backing store for duration. this enables animation, styling, binding, Etc... public static readonly dependencyproperty durationproperty = dependencyproperty. register ("duration", typeof (duration), typeof (discfluidmovebehavior), new propertymetadata (new duration (timespan. fromseconds (0.1 ))));}}

Expression must be referenced in advanceSystem. Windows. InteractivityProgramSet.

Apply this behavior to every items.

 

The Design of AI is as follows:

 

The method is recursive.

The basic idea is the same as that of the standard state. To move the largest plate to a target column, you need to find a temporary column. Then follow this idea to recursively solve the problem. Until the last plate is left, you can move it directly.

 

The core code of recursive solution is as follows:

Private void recsolve (puzzle) {int max = 0; peg maxpeg = NULL; // locate the column foreach (PEG peg in puzzle. pegcollection) {If (PEG. count> 0 & max <peg. buttom) {max = peg. buttom; maxpeg = peg; }}// the current status only has one plate and moves if (puzzle. pega. count + puzzle. pegb. count + puzzle. pegc. count = 1) {If (maxpeg. pegid! = Puzzle. destpeg) Move (maxpeg, puzzle. getpeg (puzzle. destpeg); return;} // The Current Status has multiple plates if (maxpeg. pegid = puzzle. destpeg) // The largest plate is on the target column and does not need to be moved {recsolve (puzzle. newlevelpuzzle (maxpeg. pegid, puzzle. destpeg);} else // The largest plate is not on the target column, you need to move {// find the temporary column, that is, it is neither the target pillar nor the pillar where the largest plate is located, Pegs temppagid = pegs. a; If (temppagid = maxpeg. pegid | temppagid = puzzle. destpeg) temppagid = pegs. b; If (temppagid = maxpeg. pegid | temppagid = puzzle. destpeg) temppagid = pegs. c; // remove the current state from the new State after the maximum plate and continue recursive processing // This step moves all the dishes to the temporary column recsolve (puzzle. newlevelpuzzle (maxpeg. pegid, temppagid); // Move the current maximum plate to the target column to move (maxpeg, puzzle. getpeg (puzzle. destpeg); // remove the status after the largest plate from the processed status in the previous step. // move all the plates to the target status recsolve (puzzle. newlevelpuzzle (puzzle. destpeg, puzzle. destpeg ));}}

 

Welcome to comments!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.