<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java never die &#187; RIA Frameworks</title>
	<atom:link href="http://www.javaneverdie.com/category/ria-frameworks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javaneverdie.com</link>
	<description>Java modern frameworks. But not only.</description>
	<lastBuildDate>Fri, 27 Aug 2010 20:25:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>5 ways to 15puzzles</title>
		<link>http://www.javaneverdie.com/ria-frameworks/5-ways-to-15puzzles/</link>
		<comments>http://www.javaneverdie.com/ria-frameworks/5-ways-to-15puzzles/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 14:40:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[RIA Frameworks]]></category>

		<guid isPermaLink="false">http://www.javaneverdie.com/?p=283</guid>
		<description><![CDATA[or comparing ajax(ria, web 2.0) frameworks or jQuery vs ExtJs vs YUI vs Flex vs GWT To leverage my programming skills in client side and to concurrently  compare modern UI frameworks and most importantly &#8220;just for fun&#8221; i start to implement 15 puzzles game in: 1 jQuery; 2 Ext-Js; 3 Yahoo UI; 4 GWT; 5 [...]


Related posts:<ol><li><a href='http://www.javaneverdie.com/java/how-to-start-desktop-application-with-netbeans/' rel='bookmark' title='Permanent Link: How to start desktop application with Netbeans'>How to start desktop application with Netbeans</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-jquery-way/' rel='bookmark' title='Permanent Link: 15puzzles jQuery way'>15puzzles jQuery way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/269/' rel='bookmark' title='Permanent Link: 15puzzles GWT way'>15puzzles GWT way</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>or comparing ajax(ria, web 2.0) frameworks</h2>
<h3>or jQuery vs ExtJs vs YUI vs Flex vs GWT</h3>
<p><span style="font-size: large;"><strong><br />
</strong></span></p>
<p>To leverage my programming skills in client side and to concurrently  compare modern UI frameworks and most importantly &#8220;just for fun&#8221; i start to implement <a id="at8n" title="15 puzzle" href="http://en.wikipedia.org/wiki/Fifteen_puzzle">15 puzzles</a> game</p>
<div>
<div><a href="http://en.wikipedia.org/wiki/File:15-puzzle.svg"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/9/91/15-puzzle.svg/180px-15-puzzle.svg.png" alt="" width="202" height="198" /></a></div>
</div>
<p>in:<br />
<span style="font-size: small;"><strong>1 <a id="eavp" title="jQuery" href="http://jquery.com/">jQuery</a>;<br />
2 <a id="sa_l" title="Ext-Js" href="http://extjs.com/">Ext-Js</a>;<br />
3 <a id="wfd5" title="Yahoo UI" href="http://developer.yahoo.com/yui/">Yahoo UI</a>;<br />
4 <a id="u.9z" title="GWT" href="http://code.google.com/webtoolkit/">GWT</a>;<br />
5 <a id="y5lk" title="Flex" href="http://www.flex.org/">Flex</a>.</strong></span></p>
<p>I have almost no experience working with this  frameworks, so it can be useful to discover, how simple and quick to start with, each of them is.</p>
<p>So, what exactly i want to implement? Really, no much:</p>
<p>puzzle must be dragged, not moved by click.</p>
<p>That&#8217;s all. No server side <img src='http://www.javaneverdie.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> ). I will use the same (only colors is different) css for all of them. Although, application is not complex it will check  selectors, DOM manipulating and drag-n-drop  capabilities of those frameworks.</p>
<p>On finish i will compare:<br />
&#8211; starting time,<br />
&#8211; amount and simplicity of code,<br />
&#8211; availability of examples, documentation, demos etc.,<br />
&#8211; integration with some available IDE.</p>
<p><span style="font-size: medium;"><strong><br />
&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..<br />
Two week-ends later</strong></span></p>
<div>
<table id="ixdx" style="height: 142px;" border="0" cellspacing="0" cellpadding="3" width="823" bordercolor="#000000">
<tbody>
<tr>
<td width="20%"><a href="http://javaghost.com/15puzzles/jquery"><img style="width: 131px; height: 131px;" src="http://docs.google.com/File?id=dgnwcmwc_148dg35n8cn_b" alt="" /></a></td>
<td width="20%">
<div id="lx4f" style="text-align: left;"><a href="http://javaghost.com/15puzzles/extjs"><img style="width: 131px; height: 132px;" src="http://docs.google.com/File?id=dgnwcmwc_149dztnz3d7_b" alt="" /></a></div>
</td>
<td width="20%">
<div id="nkcq" style="text-align: left;"><a href="http://javaghost.com/15puzzles/yui"><img style="width: 133px; height: 133px;" src="http://docs.google.com/File?id=dgnwcmwc_150dgpb2559_b" alt="" /></a></div>
</td>
<td width="20%">
<div id="p6ie" style="text-align: left;"><a href="http://javaghost.com/15puzzles/flex"><img style="width: 131px; height: 131px;" src="http://docs.google.com/File?id=dgnwcmwc_154gzgvstcx_b" alt="" /></a></div>
</td>
<td width="20%">
<div id="lje7" style="text-align: left;"><a href="http://javaghost.com/15puzzles/gwt"><img style="width: 133px; height: 133px;" src="http://docs.google.com/File?id=dgnwcmwc_151gqg8krf5_b" alt="" /></a></div>
</td>
</tr>
</tbody>
</table>
</div>
<div id="w8cn" style="text-align: left;">All implementations definitely not bugs free. It was not a goal to create production level software, but rather to make some starting point and to compare tools.</div>
<p><strong><span style="font-size: medium;">jQuery.</span></strong></p>
<p><strong> </strong><span style="font-size: medium;"><strong><br />
<span style="font-size: small;"><span style="text-decoration: underline;"><a id="cpx5" title="15 puzzles jQuery way demo" href="http://javaghost.com/15puzzles/jquery/">demo</a></span> <span style="text-decoration: underline;"><a id="r_:t" title="download jq15puzzles.zip" href="http://javaghost.com/download/15puzzles/jq15puzzles.zip">download</a></span> <span style="color: #ff0000;">*</span> </span></strong><a id="bo_3" title="view source code" href="http://javaghost.com/jquery/15puzzles-jquery-way">view source code</a></span><br />
<strong><span style="font-size: medium;"> </span></strong><br />
jQuery &#8212; the best. API docs online coupled with examples. API itself is easy, natural, intuitive understandable and self described. Required javascript knowledge level to start with jQuery is very low. Starting time is very small. Highly recommended to all from novices to professionals. Only possible reason don&#8217;t to use it is probable lack of some widget/plugin that you need and can lucky found in ext-js or yui.<span style="font-size: medium;"><strong> </strong></span></p>
<p><strong>Ext-Js and YUI</strong></p>
<p><strong><span style="font-size: small;">YUI</span></strong> <span style="font-size: medium;"><a id="laob" title="15 puzzles YUI way demo" href="http://javaghost.com/15puzzles/yui/">demo</a><strong><span style="font-size: small;"> <span style="text-decoration: underline;"><a id="yu65" title="download yui15puzzles.zip" href="http://javaghost.com/download/15puzzles/yui15puzzles.zip">download</a> </span></span></strong></span><strong style="color: #ff0000;"><span style="font-size: medium;">*</span></strong> <span style="font-size: medium;"><a id="porb" title="view source code" href="http://javaghost.com/yui/15puzzles-yui-way">view source code</a></span></p>
<p><span style="font-size: small;"><strong>Ext &#8211; Js</strong></span><span style="font-size: medium;"><strong><span style="font-size: small;"> <span style="text-decoration: underline;"><a id="k_-5" title="demo" href="http://javaghost.com/15puzzles/extjs">demo</a></span> </span></strong><a id="mrmx" title="download" href="http://javaghost.com/download/15puzzles/xjs15puzzles.zip">download</a></span> <span style="font-size: medium;"> *</span> <span style="font-size: medium;"><a id="sxwl" title="view source code" href="http://javaghost.com/15puzzles-extjs-way">view source code</a></span></p>
<p><span style="color: #ff0000;">*</span> jQuery, Ext-Js and YUI ways designed as Netbeans PHP projects. After downloading use &#8220;Import Project -&gt; Netbeans project from zip&#8221; to import project to IDE.</p>
<p>Configure project properties to automatically copy  files to your apache server installation folder</p>
<div id="osne" style="text-align: left;"><img style="width: 648px; height: 304.327px;" src="http://docs.google.com/File?id=dgnwcmwc_145kxdw6qf8_b" alt="" /></div>
<p>I never used Netbeans for php projects with windows, but Netbeans has great tutorials about all and will be no problem,  i sure, to configure php project to copy files to proper place under apache server in windows too.</p>
<p>Ext-Js and YUI,  in my opinion, are in the same level if not the same. Ext-Js may be slightly better written, but YUI better documented. If i was required to choose between YUI and Ext-Js, YUI is better choice, because of better support, more demos and documentations. In other side, as you will see later YUI and Ext-Js are very similar, so YUI demos and examples sometimes can be useful for Ext-js writing.</p>
<p>Minimum size of required javascript files for such applications  for YUI is about 60 kB, for Ext-Js is 600 kB(!) (i use ext-all.js) and this is not so obvious how to decrease it.<br />
For jQuery is 240 kB.</p>
<p>jQuery and YUI completely free, but Ext-Js has two license  and, if used for commercial non open source applications, must be paid on per developer basis.</p>
<p>Below comparison of some code excerpts in jQuery, Ext-Js and YUI.</p>
<p>Select by id:</p>
<p>jQuery</p>
<pre class="javascript">   var box = $('#box');</pre>
<p>Ext-Js</p>
<pre class="javascript"> var box = Ext.get('box');
//seems to be simple but there is also Ext.select method
//who needs two methods that do almost the same?</pre>
<p>Creating element:<br />
jQuery</p>
<pre class="javascript">  $('#box').append(element);</pre>
<p>Ext-Js</p>
<pre class="javascript">  $('#box').append(element);</pre>
<p>Making draggable:<br />
jQuery</p>
<pre class="javascript">  $('#puzzleId' + puzzle.number).draggable();</pre>
<p>Making droppable:</p>
<pre class="javascript">  $('#freeSpaceId').droppable();</pre>
<p>So, as possible to see, jQuery code is most short and understandable, the Ext-js and YUI code almost the same, except creating element in YUI, that is ugly. The Ext-js and YUI implementations have the same bug, that can be reproduced if user quickly move mouse and mouseup event occurs  outside of container.</p>
<p>I used Netbeans for all above implementations and I repeat again and again  that Netbeans is the best IDE for javascript development.</p>
<p><strong><span style="font-size: medium;">Flex</span></strong></p>
<p><span style="font-size: medium;"><strong><span style="font-size: small;"><span style="text-decoration: underline;"><a id="aty9" title="demo" href="http://javaghost.com/15puzzles/flex">demo</a></span> <span style="text-decoration: underline;"><a id="yjrs" title="download" href="http://javaghost.com/download/15puzzles/fx15puzzles.com">download</a></span> </span></strong></span><strong><span style="font-size: small;"><a id="mieb" title="view source code" href="http://javaghost.com/ria-frameworks/15puzzles-flex-way">view source code</a></span></strong></p>
<p>There is still a question if it worth to compare Flex and GWT with above javascript libraries, but until they can be used for the same purpose i think this question is redundant.<br />
Flex pros: &#8212; look and feel, that quiet hard to implement with CSS (rounded corners, gradient backgrounds)<br />
&#8211; all in one workbench, very comfortable to code, deploy and debug,<br />
&#8211; editor design mode allowing simply drag and drop component to container.</p>
<p>contras: &#8212; some css features hard (not obvious) to implement &#8212; for example: inset/outset borders.<br />
&#8211; start time for average web developer is much bigger (java background can  be very helpful, because actionscript syntax,  i think, is simply dwarfish java),<br />
&#8211; drag and drop API much more complex and confusable,<br />
&#8211; need additional efforts to add Google Adds to application page ,<br />
&#8211; API very big and unfamiliar and intuitively not understandable,<br />
&#8211; compiler error messages is not informative at all,<br />
&#8211; lack of java community (hard to find demos/examples/tutorials) or may be i don&#8217;t understand them? <img src='http://www.javaneverdie.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ,<br />
&#8211; flex eclipse plugin for linux  doesn&#8217;t have design mode or doesn&#8217;t properly work,<br />
&#8211; IDE is not free;</p>
<p>I used Flex Builder 3 as IDE and i  it was very comfortable to code, deploy and debug with some exceptions. For example class methods autocomplete window  contains  no information  what ancestor this method come from and no method description as well. I hope in Flash 4 it was improved.</p>
<div>Unlike jQuery Flex is very hard to start with. And after starting it is very confused. Flex implementation took me the most time from all, but i also like it more than others.</p>
<p><span style="font-size: medium;"><strong><br />
GWT</strong></span></p>
<p><span style="font-size: medium;"><strong><span style="font-size: small;"><span style="text-decoration: underline;"><a id="j_72" title="demo application" href="http://javaghost.com/15puzzles/flex">demo</a></span> <span style="text-decoration: underline;"><a id="w1pg" title="download" href="http://javaghost.com/download/15puzzles/gwt15puzzles.zip">download</a></span> </span></strong><a id="hbh8" title="view source code" href="http://javaghost.com/ria-frameworks/15puzzles-gwt-way">view source code</a><br />
</span></div>
<div>Finally the code for real men. Script kids rest aside.<br />
GWT is the one than does not have native support to drag-and-drop. There is some third party library present and may be it suitable for my needs, but i did not find exact example , so i implemented drag n drop by myself, using this <a id="ki7b" title="this great but a little out of date tutorial." href="http://chaoticjava.com/posts/drag-and-drop-in-gwt-the-how-to/"> great but a little out of date tutorial.</a> It work worse than others, but i sure that it not need much work to improve. Currently i don&#8217;t have time to enter in more details. Although, i had to write additional code, implementation time was very short. Even shortest than jQuery way. Java-java.<br />
I am very comfortable with GWT at all, probably because of big java with some swing experience. In some bigger application lack of markup, probably,  will be more valuable.<br />
I used GWT designer eclipse plugin from Instantiations and must to say that it almost so comfortable as Flex Builder if not more.</p>
<p>pros:  &#8212; code is pure java ,<br />
&#8211; regular css for look and feel,<br />
&#8211; all in one workbench comfortable to code, deploy, debug,<br />
&#8211; comfortable and intuitive API,<br />
&#8211; java community with all those smart guys, bored at home,<br />
&#8211; editor design mode allowing simply drag and drop component to container,<br />
&#8211; choice of components (using Ext-GWT or GWT-Ext) is richer than standard Flex palette, though may be not cover all covered by Flex components and vice versa.</p>
<p>contras:   &#8212; lack of markup,<br />
&#8211; lack of native functionality (drag-n-drop);<br />
&#8211; bigger amount of code compare to others (almost the same as Flex),<br />
&#8211; bigger time to compile compare to Flex and to deploy compare to all others,</p></div>
<div>&#8211; very big time (supposing learning java included) to novice to start work with,<br />
&#8211; GWT designer plugin allowing visual editing is not free, though much cheaper than Flex Builder.</p>
<p><strong><span style="font-size: medium;"> </span></strong></p>
<p><strong>Summary</strong></p>
<p>jQuery is the best choice for such applications. Easy to start, best documented and simply the best.</p>
<p>Ext-Js and YUI may be possible alternative/extension to jQuery if your application need some widgets that present in Ext-Js/YUI and not in jQuery.</p>
<p>Flex now is de facto UI standard for java (and not only java) enterprise applications. Look at image below.</p>
<div id="jheg" style="text-align: left;"><img style="width: 648px; height: 350.009px;" src="http://docs.google.com/File?id=dgnwcmwc_141c4kvq6dh_b" alt="" /></div>
<p>5 500 (1298 job offers with title like Flex developer/programmer and 5446 with Flex in description) job offers compare to 600 for gwt/extjs/yui and to 3000 for jQuery. Your experience in Flex surely will be paid.</p>
<p>Flex application very easy can be made to look great. Very recommended for novices as way to programming world as well as for java professionals which are bored from server side programming <img src='http://www.javaneverdie.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> ).</p>
<p>GWT is intended for java programmers, that want to write web applications in traditional swing non-markup way. BTW, <span>rumors are in the air</span>, that in GWT 2.0 (current version 1.7) mark up will be present. In all cases, in tomorrow Google Wave driven world, anyone (any web developer i mean) will have no choice ;0).</p>
<p>Next chart is very self explained.</p>
<div id="vh:k" style="text-align: left;"><img style="width: 648px; height: 340.085px;" src="http://docs.google.com/File?id=dgnwcmwc_14332h7j5cb_b" alt="" /></div>
<p>The next chart confirm previous. Possible to see that in case if tendency will not changed,  during next (2010) year, jQuery will beat Flex and will become the most popular UI framework over the world.</p>
<div id="b38f" style="text-align: left;"><img style="width: 648px; height: 340.085px;" src="http://docs.google.com/File?id=dgnwcmwc_146dpp5dnch_b" alt="" /></div>
<p><span style="font-size: medium;"><strong> </strong></span></p>
<p><strong>Usage recommendations.</strong></p>
<p><strong> </strong></p>
<div id="x8lq" style="text-align: left;"><img style="width: 210px; height: 293px;" src="http://docs.google.com/File?id=dgnwcmwc_147f2whc5g8_b" alt="" /></div>
<p>If you are web developer with some basic knowledge of html, css, javascript &#8212; go jQuery, don&#8217;t think twice.</p>
<p>If your application is build around some ExtJs/YUI widget(s) go them. Possible with jQuery.</p>
<p>If you completely novice to programming &#8212; go Flex &#8212; you will get the same starting point with more experienced programmers. One year of experience in your resume must be enough.<br />
If  your customer (your own things view) needs Flex &#8212; go it, but remember that it was your own decision.</p>
<p>If you are java programmer &#8212; use GWT.<br />
If you think that Google Wave can change the Web (read the World) &#8212; learn java (its cool!) and then use GWT.</p></div>
<div id="seolinx-tooltip" style="border: 1px solid #000000; margin: 0pt;">
<table style="border: 0pt none; margin: 0pt; width: auto;" border="0">
<tbody>
<tr>
<td id="seolinx-table" style="border: 0pt none; margin: 1px; font-family: Tahoma;">
<div style="margin: 0pt;">
<table id="seolinx-paramtable" style="border: 1px solid gray; margin: 0pt;" border="0">
<tbody>
<tr>
<td style="border: 1px solid gray; background: #f0f0f0 none repeat scroll 0% 0%; color: darkgreen; font-family: Tahoma;"><img style="vertical-align: middle;" src="http://toolbarqueries.google.com/favicon.ico" alt="" width="12" height="12" /><strong><span style="font-size: xx-small;"> PR: </span></strong><a style="color: blue; font-family: Tahoma;" title="Google pagerank"><strong><span style="text-decoration: underline;"><span style="font-size: xx-small;">wait&#8230;</span></span></strong></a></td>
<td style="border: 1px solid gray; background: #f0f0f0 none repeat scroll 0% 0%; color: darkgreen; font-family: Tahoma;"><img style="vertical-align: middle;" src="http://www.google.com/favicon.ico" alt="" width="12" height="12" /><strong><span style="font-size: xx-small;"> I: </span></strong><a style="color: blue; font-family: Tahoma;" title="Google index"><strong><span style="text-decoration: underline;"><span style="font-size: xx-small;">wait&#8230;</span></span></strong></a></td>
<td style="border: 1px solid gray; background: #f0f0f0 none repeat scroll 0% 0%; color: darkgreen; font-family: Tahoma;"><img style="vertical-align: middle;" src="http://www.google.com/favicon.ico" alt="" width="12" height="12" /><strong><span style="font-size: xx-small;"> L: </span></strong><a style="color: blue; font-family: Tahoma;" title="Google links"><strong><span style="text-decoration: underline;"><span style="font-size: xx-small;">wait&#8230;</span></span></strong></a></td>
<td style="border: 1px solid gray; background: #f0f0f0 none repeat scroll 0% 0%; color: darkgreen; font-family: Tahoma;"><img style="vertical-align: middle;" src="http://siteexplorer.search.yahoo.com/favicon.ico" alt="" width="12" height="12" /><strong><span style="font-size: xx-small;"> LD: </span></strong><a style="color: blue; font-family: Tahoma;" title="Yahoo linkdomain"><strong><span style="text-decoration: underline;"><span style="font-size: xx-small;">wait&#8230;</span></span></strong></a></td>
<td style="border: 1px solid gray; background: #f0f0f0 none repeat scroll 0% 0%; color: darkgreen; font-family: Tahoma;"><img style="vertical-align: middle;" src="http://www.bing.com/favicon.ico" alt="" width="12" height="12" /><strong><span style="font-size: xx-small;"> I: </span></strong><a style="color: blue; font-family: Tahoma;" title="Bing index"><strong><span style="text-decoration: underline;"><span style="font-size: xx-small;">wait&#8230;</span></span></strong></a></td>
<td style="border: 1px solid gray; background: #f0f0f0 none repeat scroll 0% 0%; color: darkgreen; font-family: Tahoma;"><a style="color: blue; font-family: Tahoma;" title="Sitemap.xml"><strong><span style="text-decoration: underline;"><span style="font-size: xx-small;">wait&#8230;</span></span></strong></a></td>
<td style="border: 1px solid gray; background: #f0f0f0 none repeat scroll 0% 0%; color: darkgreen; font-family: Tahoma;"><img style="vertical-align: middle;" src="http://www.semrush.com/favicon.ico" alt="" width="12" height="12" /><strong><span style="font-size: xx-small;"> Rank: </span></strong><a style="color: blue; font-family: Tahoma;" title="SEMRush Rank"><strong><span style="text-decoration: underline;"><span style="font-size: xx-small;">wait&#8230;</span></span></strong></a></td>
<td style="border: 1px solid gray; background: #f0f0f0 none repeat scroll 0% 0%; color: darkgreen; font-family: Tahoma;"><img style="vertical-align: middle;" src="http://www.semrush.com/favicon.ico" alt="" width="12" height="12" /><strong><span style="font-size: xx-small;"> Traffic: </span></strong><a style="color: blue; font-family: Tahoma;" title="SEMRush SE Traffic"><strong><span style="text-decoration: underline;"><span style="font-size: xx-small;">wait&#8230;</span></span></strong></a></td>
<td style="border: 1px solid gray; background: #f0f0f0 none repeat scroll 0% 0%; color: darkgreen; font-family: Tahoma;"><img style="vertical-align: middle;" src="http://www.semrush.com/favicon.ico" alt="" width="12" height="12" /><strong><span style="font-size: xx-small;"> Price: </span></strong><a style="color: blue; font-family: Tahoma;" title="SEMRush SE Traffic price"><strong><span style="text-decoration: underline;"><span style="font-size: xx-small;">wait&#8230;</span></span></strong></a></td>
<td style="border: 1px solid gray; background: #f0f0f0 none repeat scroll 0% 0%; color: darkgreen; font-family: Tahoma;"><img style="vertical-align: middle;" src="http://siteanalytics.compete.com/favicon.ico" alt="" width="12" height="12" /><strong><span style="font-size: xx-small;"> C: </span></strong><a style="color: blue; font-family: Tahoma;" title="Compete Rank"><strong><span style="text-decoration: underline;"><span style="font-size: xx-small;">wait&#8230;</span></span></strong></a></td>
</tr>
</tbody>
</table>
</div>
</td>
<td id="seolinx-tooltip-close" style="border: 0pt none; margin: 0pt; vertical-align: middle; width: auto;" title="close"></td>
</tr>
</tbody>
</table>
</div>


<p>Related posts:<ol><li><a href='http://www.javaneverdie.com/java/how-to-start-desktop-application-with-netbeans/' rel='bookmark' title='Permanent Link: How to start desktop application with Netbeans'>How to start desktop application with Netbeans</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-jquery-way/' rel='bookmark' title='Permanent Link: 15puzzles jQuery way'>15puzzles jQuery way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/269/' rel='bookmark' title='Permanent Link: 15puzzles GWT way'>15puzzles GWT way</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.javaneverdie.com/ria-frameworks/5-ways-to-15puzzles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>15puzzles Flex way</title>
		<link>http://www.javaneverdie.com/ria-frameworks/15puzzles-flex-way/</link>
		<comments>http://www.javaneverdie.com/ria-frameworks/15puzzles-flex-way/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 14:25:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[RIA Frameworks]]></category>

		<guid isPermaLink="false">http://www.javaneverdie.com/?p=277</guid>
		<description><![CDATA[15puzzles Flex implementation source code Code does not contains auto generated by plugin and framework parts . Files: index.mxml, 15puzzles.css, FifteenPuzzles.as, Puzzle.as, FreeSpace.as index.mxml file below is not completely displayed and also not formatted well. Please download whole project. index.mxml FifteenPuzzles.as package includes { import flash.events.*; import mx.containers.Canvas; import mx.controls.Button; import mx.controls.Label; public class FifteenPuzzles [...]


Related posts:<ol><li><a href='http://www.javaneverdie.com/ria-frameworks/269/' rel='bookmark' title='Permanent Link: 15puzzles GWT way'>15puzzles GWT way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-jquery-way/' rel='bookmark' title='Permanent Link: 15puzzles jQuery way'>15puzzles jQuery way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-extjs-way/' rel='bookmark' title='Permanent Link: 15puzzles ExtJs way'>15puzzles ExtJs way</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><strong>15puzzles Flex implementation source code</strong></p>
<p>Code does not contains auto generated by plugin and framework parts .<br />
Files: index.mxml,<br />
         15puzzles.css,<br />
         FifteenPuzzles.as,<br />
         Puzzle.as,<br />
         FreeSpace.as<br />
index.mxml file below is not completely displayed and also not formatted well.  Please <a href="http://javaghost.com/15puzzles/download/fx15puzzles.zip">download whole project</a>.</p>
<p>index.mxml</p>
<pre class="html" name="code">
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
				layout="absolute"
				creationComplete="start()">

	<mx:Style source="15puzzles.css"/>
	<mx:Label x="92"
			  y="10"
			  text="15puzzles: Flex way"
			  width="344"
			  id="title"
			  enabled="true"
			  fontSize="27"
			  fontFamily="Verdana"/>
	<mx:Canvas width="208"
			   height="208"
			   id="box"
			   styleName="box"
			   y="92"
			   x="92">

	</mx:Canvas>
	<mx:Button x="390"
			   y="132"
			   label="Scramble"
			   id="scramble"
			   enabled="true"
			   click="FifteenPuzzles.getInstance().scramble15puzzles()"
			   styleName="button"/>
	<mx:Button x="390"
			   y="194"
			   label="Sort"
			   id="sort"
			   enabled="true"
			   click="FifteenPuzzles.getInstance().sort15puzzles()"
			   styleName="button"
			   width="98"/>
	<mx:Script>
		<![CDATA[
			import includes.*;

			function start () : void {
				var fifteenPuzzles : FifteenPuzzles = FifteenPuzzles.createInstance(box);
			}

		]]&gt;
	</mx:Script>

</mx:Application>
</pre>
<p>FifteenPuzzles.as</p>
<pre class="java" name="code">
package includes {

	import flash.events.*;

	import mx.containers.Canvas;
	import mx.controls.Button;
	import mx.controls.Label;

	public class FifteenPuzzles {

		public var freeSpace : FreeSpace;
		public var lastDraggedPuzzle : Puzzle;
		public var puzzles : Array;
		public var dragDirection : String;
		public var box : Canvas;

		private static var fifteenPuzzles : FifteenPuzzles;

		public static function createInstance (box : Canvas) : FifteenPuzzles {
			if (fifteenPuzzles == null) {
				fifteenPuzzles = new FifteenPuzzles(box);
			}
			return fifteenPuzzles;
		}

		public static function getInstance () : FifteenPuzzles {
			return fifteenPuzzles;
		}

		public function FifteenPuzzles (box : Canvas) {
			this.box = box;
			init();
			makeDraggable();
		}

		private function init () : void {
			puzzles = new Array();
			var i : Number;
			for (i = 1 ; i <= 16 ; i++) {
				var x : int;
				if (i < 5) {
					x = (i - 1) * 50;
				} else if (5 <= i &#038;&#038; i < 9) {
					x = (i - 5) * 50;
				} else if (9 <= i &#038;&#038; i < 13) {
					x = (i - 9) * 50;
				} else if (13 <= i &#038;&#038; i <= 16) {
					x = (i - 13) * 50;
				}
				var y : int;
				if (i < 5) {
					y = 0;
				} else if (5 <= i &#038;&#038; i < 9) {
					y = 50;
				} else if (9 <= i &#038;&#038; i < 13) {
					y = 100;
				} else if (13 <= i &#038;&#038; i <= 16) {
					y = 150;
				}
				var puzzle : Puzzle;
				if (i == 16) {
					//freeSpace = new FreeSpace(x , y , i);
				} else {
					puzzle = new Puzzle(i , x , y , i);
				}
				if (i == 16) {
					appendFreeSpace(x , y , i);
				} else {
					appendPuzzle(puzzle);
				}
			}
		}

		public function makeDraggable () : void {
			for (var i : int = 1 ; i < 16 ; i++) {
				if (freeSpace.x == puzzles[i].x || freeSpace.y == puzzles[i].y) {
					if (puzzles[i].position == freeSpace.position - 1) {
						(Puzzle(puzzles[i])).addEventListener(MouseEvent.MOUSE_DOWN , (Puzzle(puzzles[i])).onMouseDown);
						(Puzzle(puzzles[i])).addEventListener(MouseEvent.MOUSE_UP , (Puzzle(puzzles[i])).onMouseUp);
					} else if (puzzles[i].position == freeSpace.position + 1) {
						(Puzzle(puzzles[i])).addEventListener(MouseEvent.MOUSE_DOWN , (Puzzle(puzzles[i])).onMouseDown);
						(Puzzle(puzzles[i])).addEventListener(MouseEvent.MOUSE_UP , (Puzzle(puzzles[i])).onMouseUp);
					} else if (puzzles[i].position == freeSpace.position + 4) {
						(Puzzle(puzzles[i])).addEventListener(MouseEvent.MOUSE_DOWN , (Puzzle(puzzles[i])).onMouseDown);
						(Puzzle(puzzles[i])).addEventListener(MouseEvent.MOUSE_UP , (Puzzle(puzzles[i])).onMouseUp);
					} else if (puzzles[i].position == freeSpace.position - 4) {
						(Puzzle(puzzles[i])).addEventListener(MouseEvent.MOUSE_DOWN , (Puzzle(puzzles[i])).onMouseDown);
						(Puzzle(puzzles[i])).addEventListener(MouseEvent.MOUSE_UP , (Puzzle(puzzles[i])).onMouseUp);
					}
				}
			}
		}

		public function appendPuzzle (puzzle : Puzzle) : void {
			puzzles[puzzle.number] = puzzle;
			puzzle.id = 'puzzle' + puzzle.number;
			//puzzle.setStyle('position' , 'absolute');
			puzzle.styleName = 'puzzle';
			var label : Label = new Label();
			label.text = "" + puzzle.number;
			puzzle.addChild(label);
			box.addChild(puzzle);
		}

		public function appendFreeSpace (x : int , y : int , position : int) : void {
			freeSpace = new FreeSpace(x , y , position);
			freeSpace.id = 'freeSpace';
			freeSpace.setStyle('position' , 'absolute');
			freeSpace.styleName = 'freeSpace';
			var label : Label = new Label();
			//freeSpace.addChild(label);

			freeSpace.addChild(label);
			box.addChild(freeSpace);

			//box.setChildIndex(freeSpace, 100); 

			var z : int = box.getChildIndex(freeSpace);

		}

		public function scramble15puzzles () : void {
			while (box.numChildren > 0) {
				box.removeChildAt(0);
			}
			var xTemp : int = freeSpace.x;
			var yTemp : int = freeSpace.y;
			var positionTemp : int = freeSpace.position;

			var n : int = Math.ceil(Math.random() * 15);
			var randomPuzzle : Puzzle = puzzles[n];
			freeSpace.x = randomPuzzle.x;
			freeSpace.y = randomPuzzle.y;
			freeSpace.position = randomPuzzle.position;

			randomPuzzle.x = xTemp;
			randomPuzzle.y = yTemp;
			randomPuzzle.position = positionTemp;

			appendFreeSpace(freeSpace.x , freeSpace.y , freeSpace.position);

			for (var i : int = 0 ; i < 20 ; i++) {
				var randomPuzzle1 : Puzzle = puzzles[Math.ceil(Math.random() * 15)];
				var randomPuzzle2 : Puzzle = puzzles[Math.ceil(Math.random() * 15)];
				xTemp = randomPuzzle1.x;
				yTemp = randomPuzzle1.y;
				positionTemp = randomPuzzle1.position;

				randomPuzzle1.x = randomPuzzle2.x;
				randomPuzzle1.y = randomPuzzle2.y;
				randomPuzzle1.position = randomPuzzle2.position;

				randomPuzzle2.x = xTemp;
				randomPuzzle2.y = yTemp;
				randomPuzzle2.position = positionTemp;
			}

			for (i = 1 ; i < 16 ; i++) {
				appendPuzzle(puzzles[i]);
			}
			this.makeDraggable();
		}

		public function sort15puzzles () : void {
			while (box.numChildren > 0) {
				box.removeChildAt(0);
			}
			init();
			makeDraggable();
		}

	}
}
</pre>
<p>Puzzle.as</p>
<pre class="java" name="code">
package includes {
	import flash.events.*;
	import flash.geom.Rectangle;

	import mx.containers.Canvas;

	public class Puzzle extends Canvas {

		public function Puzzle (number : int , x : int , y : int , position : int) {
			this.number = number;
			this.x = x;
			this.y = y;
			this.position = position;
			this.explicitWidth = 50;
			this.explicitHeight = 50;
			this.fifteenPuzzles = FifteenPuzzles.getInstance();
			//addEventListener(MouseEvent.MOUSE_DOWN , onMouseDown);
			//addEventListener(MouseEvent.MOUSE_UP , onMouseUp);
		}
		public var number : int;
		public var position : int;
		public var fifteenPuzzles : FifteenPuzzles;

		public var alreadyDragged : Boolean;
		public var lastDraggedPuzzleNormalX : int;
		public var lastDraggedPuzzleNormalY : int;

		//public var x:int;
		//public var y:int;

		public function onMouseDown (event : Event) : void {
			if (fifteenPuzzles == null) {
				fifteenPuzzles = FifteenPuzzles.getInstance();
			}
			this.parent.setChildIndex(fifteenPuzzles.freeSpace , 0);

			if (fifteenPuzzles.freeSpace.x == x || fifteenPuzzles.freeSpace.y == y) {
				if (position == fifteenPuzzles.freeSpace.position - 1) {
					markLastDraggedPuzzle();
					this.startDrag(false , new Rectangle(lastDraggedPuzzleNormalX , lastDraggedPuzzleNormalY , 50 , 0));
					fifteenPuzzles.dragDirection = 'right';
				} else if (position == fifteenPuzzles.freeSpace.position + 1) {
					markLastDraggedPuzzle();
					this.startDrag(false , new Rectangle(lastDraggedPuzzleNormalX - 50 , lastDraggedPuzzleNormalY , 50 , 0));
					fifteenPuzzles.dragDirection = 'left';
				} else if (position == fifteenPuzzles.freeSpace.position + 4) {
					markLastDraggedPuzzle();
					this.startDrag(false , new Rectangle(lastDraggedPuzzleNormalX , lastDraggedPuzzleNormalY - 50 , 0 , 50));
					fifteenPuzzles.dragDirection = 'up';
				} else if (position == fifteenPuzzles.freeSpace.position - 4) {
					markLastDraggedPuzzle();
					this.startDrag(false , new Rectangle(lastDraggedPuzzleNormalX , lastDraggedPuzzleNormalY , 0 , 50));
					fifteenPuzzles.dragDirection = 'down';
				}
			}
			//this.startDrag();
		}

		private function markLastDraggedPuzzle () {
			if (!alreadyDragged) {
				lastDraggedPuzzleNormalX = this.x;
				lastDraggedPuzzleNormalY = this.y;
				alreadyDragged = true;
			}
			fifteenPuzzles.lastDraggedPuzzle = new Puzzle(this.number , lastDraggedPuzzleNormalX , lastDraggedPuzzleNormalY , this.position);
		}

		public function onMouseUp (event : MouseEvent) : void {
			this.stopDrag();
			if (fifteenPuzzles.dragDirection == 'right' &#038;&#038; this.x > fifteenPuzzles.freeSpace.x - 20) {
				drop();
			} else if (fifteenPuzzles.dragDirection == 'left' &#038;&#038; this.x < fifteenPuzzles.freeSpace.x + 20) {
				drop();
			} else if (fifteenPuzzles.dragDirection == 'up' &#038;&#038; this.y < fifteenPuzzles.freeSpace.y + 20) {
				drop();
			} else if (fifteenPuzzles.dragDirection == 'down' &#038;&#038; this.y > fifteenPuzzles.freeSpace.y - 20) {
				drop();
			}

		}

		private function drop () : void {
			alreadyDragged = false;
			this.parent.removeChild(fifteenPuzzles.freeSpace);
			var xTemp : int = fifteenPuzzles.freeSpace.x;
			var yTemp : int = fifteenPuzzles.freeSpace.y;
			var positionTemp : int = fifteenPuzzles.freeSpace.position;

			fifteenPuzzles.freeSpace.x = fifteenPuzzles.lastDraggedPuzzle.x;
			fifteenPuzzles.freeSpace.y = fifteenPuzzles.lastDraggedPuzzle.y;
			fifteenPuzzles.freeSpace.position = fifteenPuzzles.lastDraggedPuzzle.position;

			fifteenPuzzles.lastDraggedPuzzle.x = xTemp;
			fifteenPuzzles.lastDraggedPuzzle.y = yTemp;
			fifteenPuzzles.lastDraggedPuzzle.position = positionTemp;

			fifteenPuzzles.puzzles[fifteenPuzzles.lastDraggedPuzzle.number] = fifteenPuzzles.lastDraggedPuzzle;
			//destroy all draggable and dropable

			fifteenPuzzles.appendFreeSpace(fifteenPuzzles.freeSpace.x , fifteenPuzzles.freeSpace.y , fifteenPuzzles.freeSpace.position);

			this.parent.removeChild(this);

			var newPuzzle = new Puzzle(fifteenPuzzles.lastDraggedPuzzle.number , xTemp , yTemp , positionTemp);
			fifteenPuzzles.appendPuzzle(newPuzzle);
			fifteenPuzzles.makeDraggable();
		}
	}
}
</pre>
<p>FreeSpace.as</p>
<pre class="java" name="code">
package includes {

	import mx.containers.Canvas;
	import mx.controls.Label;

	public class FreeSpace extends Canvas {
		public var position : int;

		public function FreeSpace (x : int , y : int , position : int) {
			this.x = x;
			this.y = y;
			this.position = position;
			this.explicitWidth = 50;
			this.explicitHeight = 50;
		}

	}
}
</pre>
<p>15puzzles.css</p>
<pre class="css" name="code">
/* CSS file */

.box{

    width: 208px;
    height: 208px;
    position: absolute;
    top: 92px;
    left: 92px;
    border-right-style: double;
    border-left-style: double;
    border-bottom-style: double;
    border-top-style: double;
    border-right-width: 8px;
    border-left-width: 8px;
    border-bottom-width: 8px;
    border-top-width: 8px;
    border-right-color: #e08b10;
    border-left-color: #e08b10;
    border-bottom-color: #e08b10;
    border-top-color: #e08b10;
    background-color: #B964D2;
    visibility: visible;
	borderStyle: solid;
	borderColor: #46284F;
	borderThickness: 4;
	cornerRadius: 10;
	backgroundAlpha: 0.74;
}

.puzzle{

    position: absolute;
    width: 42px;
    height: 42px;
    visibility: visible;
    border-top-style: outset;
    border-bottom-style: outset;
    border-right-color: #d41212;
    border-left-color: #d41212;
    border-bottom-color: #d41212;
    border-top-color: #d41212;
    border-right-width: 4px;
    border-left-width: 4px;
    border-bottom-width: 4px;
    border-top-width: 4px;
    border-left-style: outset;
    border-right-style: outset;
    background-color: #191717;
    font-family: Verdana,Arial,Helvetica,sans-serif;
    font-size: 24px;
    font-style: normal;
    font-weight: bold;
    font-variant: normal;
    color: #C7A7D5;
    text-align: center;
    vertical-align: bottom;
    line-height: 43px;
	borderStyle: solid;
	borderColor: #943A8A;
	borderThickness: 4;
	backgroundAlpha: 0.9;
	backgroundSize: auto;
	cornerRadius: 7;
	themeColor: #C000FF;
}

.freeSpace{

    width: 100px;
    height: 100px;
    visibility: visible;

    background-color: #B964D2;
	borderColor: #9AA5D2;
	borderStyle: none;
	borderThickness: 3;

}

.buttons{

    position: absolute;
    top: 100px;
    left: 350px;
    width: 100px;
    height: 300px;
}

.button{

    border-right-style: outset;
    border-left-style: outset;
    border-bottom-style: outset;
    border-top-style: outset;
    border-right-color: #d41212;
    border-bottom-color: #d41212;
    border-top-color: #d41212;
    background-color: #ee1515;
    width: 100px;
    height: 30px;
    vertical-align: bottom;
    text-align: center;
    border-right-width: 5px;
    border-left-width: 5px;
    border-bottom-width: 5px;
    border-top-width: 5px;
    font-size: 14px;
    font-family: Verdana,Arial,Helvetica,sans-serif;
    color: #fce5c7;
    font-weight: bold;
     border-left-color: #d41212;
    margin-right: 30px;
    margin-left: 30px;
    margin-bottom: 30px;
    margin-top: 30px;
    text-indent: 1px;

    line-height: 30px;
	themeColor: #9656B1;
	borderColor: #5F467A;
	fillAlphas: 1.0, 1.0;
	fillColors: #C99BCE, #4D0B55, #9739BE, #5C1E62;
	textRollOverColor: #E0D0E1;
}

.button-clicked{

    border-right-style: inset;
    border-left-style: inset;
    border-bottom-style: inset;
    border-top-style: inset;
    border-right-color: #d41212;
    border-bottom-color: #d41212;
    border-top-color: #d41212;
    background-color: #ee1515;
    width: 100px;
    height: 30px;
    vertical-align: middle;
    text-align: center;
    border-right-width: 5px;
    border-left-width: 5px;
    border-bottom-width: 5px;
    border-top-width: 5px;
    font-size: 14px;
    color: #fce5c7;
    font-weight: bold;
     border-left-color: #d41212;
    margin-right: 30px;
    margin-left: 30px;
    margin-bottom: 30px;
    margin-top: 30px;
    position: absolute;
    line-height: 30px;
}
global
{
	backgroundGradientAlphas: 1.0, 1.0;
	backgroundGradientColors: #8253A6, #BFA2D6;
	borderColor: #AF8BD7;
	color: #C393CD;
	fontSize: 23;
	disabledColor: #D4C1DE;
	paddingLeft: 0;
	paddingTop: 0;
	paddingRight: 0;
	paddingBottom: 0;
}
</pre>


<p>Related posts:<ol><li><a href='http://www.javaneverdie.com/ria-frameworks/269/' rel='bookmark' title='Permanent Link: 15puzzles GWT way'>15puzzles GWT way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-jquery-way/' rel='bookmark' title='Permanent Link: 15puzzles jQuery way'>15puzzles jQuery way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-extjs-way/' rel='bookmark' title='Permanent Link: 15puzzles ExtJs way'>15puzzles ExtJs way</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.javaneverdie.com/ria-frameworks/15puzzles-flex-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>15puzzles GWT way</title>
		<link>http://www.javaneverdie.com/ria-frameworks/269/</link>
		<comments>http://www.javaneverdie.com/ria-frameworks/269/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 13:22:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[RIA Frameworks]]></category>

		<guid isPermaLink="false">http://www.javaneverdie.com/?p=269</guid>
		<description><![CDATA[15puzzles GWT implementation source code Code does not contains auto generated by plugin and framework parts . Files: index.html, gwt15puzzles.css, gwt15puzzles.java, FifteenPuzzles.java, Puzzle.java, FreeSpace.java Html code below is not completely displayed and also not formatted well. Please download whole project. index.html Blog View Source Download Ext-Js way Yahoo UI way Flex way jQuery way gwt15puzzles.css [...]


Related posts:<ol><li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-flex-way/' rel='bookmark' title='Permanent Link: 15puzzles Flex way'>15puzzles Flex way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-extjs-way/' rel='bookmark' title='Permanent Link: 15puzzles ExtJs way'>15puzzles ExtJs way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-jquery-way/' rel='bookmark' title='Permanent Link: 15puzzles jQuery way'>15puzzles jQuery way</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><strong>15puzzles GWT implementation source code</strong></p>
<p>Code does not contains auto generated by plugin and framework parts .<br />
Files: index.html,<br />
         gwt15puzzles.css,<br />
         gwt15puzzles.java,<br />
         FifteenPuzzles.java,<br />
         Puzzle.java,<br />
         FreeSpace.java<br />
Html code below is not completely displayed and also not formatted well.  Please <a href="http://javaghost.com</p>
<p>/15puzzles/download/gwt15puzzles.zip">download whole project</a>.</p>
<p>index.html</p>
<pre class="HTML" name="code">
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta name="keywords" content="ui, ria, javascript, jquery, ext-js, extjs, yahoo ui, yui, flex, gwt, google web toolkit, client side programming" />
        <meta name="description" content="15puzzles game implemented using Google Web Toolkit (GWT) framework. View source/Download project. Page also contains links to 15puzzles game implemented in Ext Js, Yahoo UI, Flex and jQuery" />
<link rel="stylesheet" type="text/css" media="screen" href="gwt15puzzles.css" />

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>

        <iframe src="gwt15puzzles.html" class="gwt-iframe" ></iframe>
<div id="header-menu" class="header-menu">
            <a href="http://javaghost.com/ria-frameworks/5-ways-to-15puzzles" class="header-link">Blog</a>
            <a href="http://javaghost.com/ria-frameworks/15puzzles-gwt-way" class="header-link">View Source</a>
            <a href="http://javaghost.com/download/gwt15puzzles.zip" class="header-link">Download</a>
        </div>
<div class="ads" id="ads"></div>
<div class="others-ways" id="others-ways">
            <a class="other-way-link" href="http://javaghost.com/15puzzles/extjs">Ext-Js way</a>
            
            <a class="other-way-link" href="http://javaghost.com/15puzzles/yui">Yahoo UI way</a>
            
            <a class="other-way-link" href="http://javaghost.com/15puzzles/flex">Flex way</a>
            
            <a class="other-way-link" href="http://javaghost.com/15puzzles/jquery">jQuery way</a>
        </div>

    </body>
</html>
</pre>
<p>gwt15puzzles.css</p>
<pre class="css" name="code">
.page{

	color: #000000;
	margin: 0px;
	padding: 0px;
	font-family: fantasy;
	font-size: 24px;
	font-weight: bold;
	background-color: Silver;
}

.title{
    position: absolute;
	color: #000000;
	font-family: fantasy;
	font-size: 18px;
}

.box{

    width: 200px;
    height: 200px;
    position: absolute;
    top: 92px;
    left: 92px;
    /*z-index:-2;*/

    visibility: visible;
    display: block;

	border-top: 8px double Black;
	border-right: 8px double black;
	border-bottom: 8px double Black;
	border-left: 8px double Black;
	background-color: Gray;
}

.puzzle{

    position: absolute;
    width: 42px;
    height: 42px;
    visibility: visible;
    z-index: 2;
	color: Black;
	border: 4px outset orange;
	font-family: Verdana,Arial,Helvetica,sans-serif;
	font-size: 24px;
	font-style: normal;
	font-variant: normal;
	font-weight: bold;
	line-height: 43px;
	text-align: center;
	vertical-align: middle;
	background-color: Yellow;
}

.freeSpace{

    width: 48px;
    height: 48px;
    visibility: visible;
	border: 1px dotted yellow;
	background-color: Gray;
}

.buttons{

    position: absolute;
    top: 100px;
    left: 350px;
    width: 100px;
    height: 300px;
}

.button{

    width: 100px;
    height: 30px;

	color: Black;
	margin: 30px;
	border: 5px outset #ffcc66;
	font-family: Verdana,Arial,Helvetica,sans-serif;
	font-size: 14px;
	font-weight: bold;
	line-height: 30px;
	text-align: center;
	text-indent: 1px;
	vertical-align: bottom;
	background-color: #ffff66;
}

.button-clicked{

    width: 100px;
    height: 30px;
	color: dimgray;
	margin: 30px;
	border: 5px inset #8c4e9b;
	font-family: Verdana,Arial,Helvetica,sans-serif;
	font-size: 14px;
	font-weight: bold;
	line-height: 30px;
	text-align: center;
	text-indent: 1px;
	vertical-align: bottom;
	background-color: #ccff33;
}

.gwt-iframe{

    top: 0px;
    left: 0px;
    width: 600px;
    height: 500px;
    visibility: visible;
    border-right-style: none;
    border-left-style: none;
    border-bottom-style: none;
    border-top-style: none;
    border-right-color: silver;
    border-left-color: silver;
    border-bottom-color: silver;
    border-top-color: silver;
}

.header-menu{

    position: absolute;
    top: 0px;
    left: 500px;
    width: 500px;
    height: 50px;
    visibility: visible;
}

.header-link{

    font-style: normal;
    font-family: fantasy;
    font-size: 18px;
    color: #000000;
    margin-left: 8px;
    margin-right: 8px;
    padding-left: 8px;
    padding-right: 8px;
}

.ads{

    border-right-style: none;
    border-left-style: none;
    border-bottom-style: none;
    border-top-style: none;
    border-right-color: #e08989;
    border-left-color: #e08989;
    border-bottom-color: #e08989;
    border-top-color: #e08989;
    border-right-width: 1px;
    border-left-width: 1px;
    border-bottom-width: 1px;
    border-top-width: 1px;
    position: absolute;
    top: 92px;
    left: 550px;
    width: 300px;
    height: 350px;
    visibility: visible;
}

.others-ways{

    position: absolute;
    top: 92px;
    left: 860px;
    color: #000000;
}

.other-way-link{

    font-size: 14px;
    font-style: normal;
    font-weight: bold;
    color: #000000;
    line-height: 40px;
}
</pre>
<p>gwt15puzzles.java</p>
<pre class="java" name="code">
package com.javaghost.gwt15puzzles.client;

import com.google.gwt.core.client.EntryPoint;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class gwt15puzzles implements EntryPoint {

	FifteenPuzzles fifteenPuzzles;

	public void onModuleLoad() {
		RootPanel rootPanel = RootPanel.get();
		rootPanel.setStyleName("page");
		{
			Box box = new Box();
			box.setStyleName("box");
			rootPanel.add(box);
			box.setSize("200", "200");
			fifteenPuzzles = FifteenPuzzles.createInstance(box);
			fifteenPuzzles.init();
			fifteenPuzzles.makeDraggable();
			AbsolutePanel buttonsPanel = new AbsolutePanel();
			buttonsPanel.setStyleName("buttons");
			rootPanel.add(buttonsPanel, 357, 97);
			buttonsPanel.setSize("134px", "300px");
			Button scrableButton = new Button("Scramble");
			buttonsPanel.add(scrableButton, -12, 5);
			scrableButton.setSize("100px", "30px");
			scrableButton.setStyleName("button");
			Button sortButton = new Button("Sort");
			buttonsPanel.add(sortButton, -12, 83);
			sortButton.setSize("100px", "30px");
			sortButton.setStyleName("button");

			Label label = new Label("15puzzles GWT way");
			label.setStyleName("title");
			rootPanel.add(label, 92, 26);
			scrableButton.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					fifteenPuzzles.scramble15puzzles();
				}
			});
			sortButton.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					fifteenPuzzles.sort15puzzles();
				}
			});
		}
	}
}
</pre>
<p>FifteenPuzzles.java</p>
<pre class="java" name="code">
package com.javaghost.gwt15puzzles.client;

import com.google.gwt.user.client.ui.Label;
import com.javaghost.gwt15puzzles.client.Puzzle.DragDirection;

public class FifteenPuzzles {

	public FreeSpace freeSpace;
	public Puzzle lastDraggedPuzzle;
	public Puzzle[] puzzles;
	public String dragDirection;
	public Box box;

	private static FifteenPuzzles fifteenPuzzles;

	public static FifteenPuzzles createInstance(Box box) {
		if (fifteenPuzzles == null) {
			fifteenPuzzles = new FifteenPuzzles(box);
		}
		return fifteenPuzzles;
	}

	public static FifteenPuzzles getInstance() {
		return fifteenPuzzles;
	}

	public FifteenPuzzles(Box box) {
		this.box = box;
	}

	public void init() {
		puzzles = new Puzzle[16];

		for (int i = 1; i <= 16; i++) {
			int x = 0;
			if (i < 5) {
				x = (i - 1) * 50;
			} else if (5 <= i &#038;&#038; i < 9) {
				x = (i - 5) * 50;
			} else if (9 <= i &#038;&#038; i < 13) {
				x = (i - 9) * 50;
			} else if (13 <= i &#038;&#038; i <= 16) {
				x = (i - 13) * 50;
			}
			int y = 0;
			if (i < 5) {
				y = 0;
			} else if (5 <= i &#038;&#038; i < 9) {
				y = 50;
			} else if (9 <= i &#038;&#038; i < 13) {
				y = 100;
			} else if (13 <= i &#038;&#038; i <= 16) {
				y = 150;
			}
			Puzzle puzzle = null;
			if (i == 16) {
				// freeSpace = new FreeSpace(x , y , i);
			} else {
				puzzle = new Puzzle(i, x, y, i);
			}
			if (i == 16) {
				appendFreeSpace(x, y, i);
			} else {
				appendPuzzle(puzzle);
			}
		}
	}

	public void makeDraggable() {
		for (int i = 1; i < 16; i++) {
			if (freeSpace.x == puzzles[i].x || freeSpace.y == puzzles[i].y) {
				if (puzzles[i].position == freeSpace.position - 1) {
					puzzles[i].makeDraggable(DragDirection.RIGHT);
				} else if (puzzles[i].position == freeSpace.position + 1) {
					puzzles[i].makeDraggable(DragDirection.LEFT);
				} else if (puzzles[i].position == freeSpace.position + 4) {
					puzzles[i].makeDraggable(DragDirection.UP);
				} else if (puzzles[i].position == freeSpace.position - 4) {
					puzzles[i].makeDraggable(DragDirection.DOWN);
				}
			}
		}
	}

	public void appendPuzzle(Puzzle puzzle) {
		puzzles[puzzle.number] = puzzle;
		puzzle.setStyleName("puzzle");
		Label label = new Label(puzzle.number + "");
		puzzle.add(label);
		box.add(puzzle, puzzle.x, puzzle.y);
	}

	public void appendFreeSpace(int x, int y, int position) {
		freeSpace = new FreeSpace(x, y, position);
		freeSpace.setStyleName("freeSpace");
		box.add(freeSpace, freeSpace.x, freeSpace.y);
	}

	public void scramble15puzzles() {
		while (box.getChildren().size() > 0) {
			box.remove(0);
		}
		int xTemp = freeSpace.x;
		int yTemp = freeSpace.y;
		int positionTemp = freeSpace.position;

		int n = (int) Math.ceil(Math.random() * 15);
		Puzzle randomPuzzle = puzzles[n];
		freeSpace.x = randomPuzzle.x;
		freeSpace.y = randomPuzzle.y;
		freeSpace.position = randomPuzzle.position;

		randomPuzzle.x = xTemp;
		randomPuzzle.y = yTemp;
		randomPuzzle.position = positionTemp;

		appendFreeSpace(freeSpace.x, freeSpace.y, freeSpace.position);

		for (int i = 0; i < 20; i++) {
			Puzzle randomPuzzle1 = puzzles[(int) Math.ceil(Math.random() * 15)];
			Puzzle randomPuzzle2 = puzzles[(int) Math.ceil(Math.random() * 15)];
			xTemp = randomPuzzle1.x;
			yTemp = randomPuzzle1.y;
			positionTemp = randomPuzzle1.position;

			randomPuzzle1.x = randomPuzzle2.x;
			randomPuzzle1.y = randomPuzzle2.y;
			randomPuzzle1.position = randomPuzzle2.position;

			randomPuzzle2.x = xTemp;
			randomPuzzle2.y = yTemp;
			randomPuzzle2.position = positionTemp;
		}

		for (int i = 1; i < 16; i++) {
			appendPuzzle(puzzles[i]);
		}
		this.makeDraggable();
	}

	public void sort15puzzles() {
		while (box.getChildren().size() > 0) {
			box.remove(0);
		}
		init();
		makeDraggable();
	}

}
</pre>
<p>Puzzle.java</p>
<pre class="java" name="code">
package com.javaghost.gwt15puzzles.client;

import com.google.gwt.user.client.DOM;

@SuppressWarnings("deprecation")
public class Puzzle extends AbsolutePanel implements SourcesMouseEvents, EventPreview {

	private FifteenPuzzles fifteenPuzzles;

	public int number;
	public int position;
	public int x; // left
	public int y; // top

	private DragDirection dragDirection;
	private int normalPositionX;
	private int normalPositionY;

	public boolean alreadyDragged;
	public int lastDraggedPuzzleNormalX;
	public int lastDraggedPuzzleNormalY;

	private MouseListenerCollection mouseListeners = new MouseListenerCollection();

	public Puzzle(int number, int x, int y, int position) {
		fifteenPuzzles = FifteenPuzzles.getInstance();
		this.x = x;
		this.y = y;
		this.position = position;
		this.number = number;
		DOM.sinkEvents(getElement(), DOM.getEventsSunk(getElement())
				| Event.MOUSEEVENTS);
		DOM.addEventPreview(this);
		addMouseListener(new DraggableMouseListener());
	}

	public void makeDraggable(DragDirection dragDirection) {
		this.dragDirection = dragDirection;
		normalPositionX = x;
		normalPositionY = y;
	}

	public void addMouseListener(MouseListener l) {
		mouseListeners.add(l);
	}

	public void removeMouseListener(MouseListener l) {
		mouseListeners.remove(l);
	}

	public void onBrowserEvent(Event event) {
		switch (DOM.eventGetType(event)) {
		case Event.ONMOUSEDOWN:
		case Event.ONMOUSEUP:
		case Event.ONMOUSEMOVE:
		//case Event.ONMOUSEOVER:
		case Event.ONMOUSEOUT:
			mouseListeners.fireMouseEvent(this, event);
			break;
		}
	}

	public boolean onEventPreview(Event event) {
	    if (DOM.eventGetType(event) == Event.ONMOUSEDOWN &#038;&#038;
	        DOM.isOrHasChild(getElement(), DOM.eventGetCurrentTarget(event))) {
	      DOM.eventPreventDefault(event);
	    }
	    // Always returning true as we don't want to cancel
	    // the event, just to prevent the default behaviour.
	    return true;
	}

	private class DraggableMouseListener extends MouseListenerAdapter {

		private boolean dragging = false;
		private int dragStartX;
		private int dragStartY;

		public void onMouseDown(Widget sender, int x, int y) {

			if (dragDirection == null){
				return;
			}
			dragging = true;

			// capturing the mouse to the dragged widget.
			DOM.setCapture(getElement());
			dragStartX = x;
			dragStartY = y;
			markLastDraggedPuzzle();
		}

		private void markLastDraggedPuzzle () {
			if (!alreadyDragged) {
				lastDraggedPuzzleNormalX = Puzzle.this.x;
				lastDraggedPuzzleNormalY = Puzzle.this.y;
				alreadyDragged = true;
			}
			fifteenPuzzles.lastDraggedPuzzle = new Puzzle(Puzzle.this.number , lastDraggedPuzzleNormalX , lastDraggedPuzzleNormalY , Puzzle.this.position);
		}

		public void onMouseUp(Widget sender, int x, int y) {
			dragging = false;
			DOM.releaseCapture(getElement());
			if (dragDirection == DragDirection.RIGHT &#038;&#038; x > fifteenPuzzles.freeSpace.x - 20) {
				drop();
			} else if (dragDirection == DragDirection.LEFT &#038;&#038; x < fifteenPuzzles.freeSpace.x + 20) {
				drop();
			} else if (dragDirection == DragDirection.UP &#038;&#038; y < fifteenPuzzles.freeSpace.y + 20) {
				drop();
			} else if (dragDirection == DragDirection.DOWN &#038;&#038; y > fifteenPuzzles.freeSpace.y - 20) {
				drop();
			}
		}

		public void onMouseMove(Widget sender, int x, int y) {
			int newX = normalPositionX;
			int newY = normalPositionY;

			if (dragging) {
				// we don't want the widget to go off-screen, so the top/left
				// values should always remain be positive.
				if (dragDirection == DragDirection.RIGHT) {
					if (x >= normalPositionX &#038;&#038; x <= normalPositionX + 50 ){
						newX = x;
					} else if (x > normalPositionX + 50) {
						newX = normalPositionX + 50;
					}
					//newX = Math.max(0, x + getAbsoluteLeft() - dragStartX);
				} else if (dragDirection == DragDirection.LEFT) {
					if (x >= normalPositionX - 50 &#038;&#038; x <= normalPositionX ){
						newX = x;
					} else if (x < normalPositionX - 50) {
						newX = normalPositionX - 50;
					}
					//newX = Math.max(0, x + getAbsoluteLeft() - dragStartX);
				} else if (dragDirection == DragDirection.UP) {
					if (y >= normalPositionY - 50 &#038;&#038; y <= normalPositionY ){
						newY = y;
					} else if (y < normalPositionY - 50) {
						newY = normalPositionY;
					}
					//newY = Math.max(0, y + getAbsoluteTop() - dragStartY);
				} else if (dragDirection == DragDirection.DOWN) {
					if (y >= normalPositionY &#038;&#038; y <= normalPositionY + 50){
						newY = y;
					} else if (y > normalPositionY + 50) {
						newY = normalPositionY + 50;
					}
					//newY = Math.max(0, y + getAbsoluteTop() - dragStartY);
				}

				DOM.setStyleAttribute(getElement(), "left", newX + "");
				DOM.setStyleAttribute(getElement(), "top", newY + "");
			}
		}
	}

	public void drop(){
		alreadyDragged = false;
		fifteenPuzzles.box.remove(fifteenPuzzles.freeSpace);
		int xTemp = fifteenPuzzles.freeSpace.x;
		int yTemp = fifteenPuzzles.freeSpace.y;
		int positionTemp = fifteenPuzzles.freeSpace.position;

		fifteenPuzzles.freeSpace.x = fifteenPuzzles.lastDraggedPuzzle.x;
		fifteenPuzzles.freeSpace.y = fifteenPuzzles.lastDraggedPuzzle.y;
		fifteenPuzzles.freeSpace.position = fifteenPuzzles.lastDraggedPuzzle.position;

		fifteenPuzzles.lastDraggedPuzzle.x = xTemp;
		fifteenPuzzles.lastDraggedPuzzle.y = yTemp;
		fifteenPuzzles.lastDraggedPuzzle.position = positionTemp;

		fifteenPuzzles.puzzles[fifteenPuzzles.lastDraggedPuzzle.number] = fifteenPuzzles.lastDraggedPuzzle;
		//destroy all draggable and dropable

		fifteenPuzzles.appendFreeSpace(fifteenPuzzles.freeSpace.x , fifteenPuzzles.freeSpace.y , fifteenPuzzles.freeSpace.position);

		fifteenPuzzles.box.remove(this);

		Puzzle newPuzzle = new Puzzle(fifteenPuzzles.lastDraggedPuzzle.number , xTemp , yTemp , positionTemp);
		fifteenPuzzles.appendPuzzle(newPuzzle);
		fifteenPuzzles.makeDraggable();
	}

	public enum DragDirection {
		LEFT, RIGHT, UP, DOWN;
	}

}
</pre>
<p>FreeSpace.java</p>
<pre class="java" name="code">
package com.javaghost.gwt15puzzles.client;

import com.google.gwt.user.client.ui.AbsolutePanel;

public class FreeSpace extends AbsolutePanel {

	public int position;
	public int x; // left
	public int y; // top

	public FreeSpace(int x, int y, int position) {
		this.x = x;
		this.y = y;
		this.position = position;

	}
}
</pre>


<p>Related posts:<ol><li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-flex-way/' rel='bookmark' title='Permanent Link: 15puzzles Flex way'>15puzzles Flex way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-extjs-way/' rel='bookmark' title='Permanent Link: 15puzzles ExtJs way'>15puzzles ExtJs way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-jquery-way/' rel='bookmark' title='Permanent Link: 15puzzles jQuery way'>15puzzles jQuery way</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.javaneverdie.com/ria-frameworks/269/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>15puzzles YUI way</title>
		<link>http://www.javaneverdie.com/ria-frameworks/15puzzles-yui-way/</link>
		<comments>http://www.javaneverdie.com/ria-frameworks/15puzzles-yui-way/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 12:29:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[RIA Frameworks]]></category>

		<guid isPermaLink="false">http://www.javaneverdie.com/?p=265</guid>
		<description><![CDATA[15puzzles YUI implementation source code The purpose of this explanation is only to shift down code, because of beginning of code is not displayed. So it will be clever do not to read it at all. But in all cases implementation consists of index.html, 15puzzles.css and 15puzzles.js. index.html contains all page mark up. 15puzzles.css contains [...]


Related posts:<ol><li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-jquery-way/' rel='bookmark' title='Permanent Link: 15puzzles jQuery way'>15puzzles jQuery way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-extjs-way/' rel='bookmark' title='Permanent Link: 15puzzles ExtJs way'>15puzzles ExtJs way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-flex-way/' rel='bookmark' title='Permanent Link: 15puzzles Flex way'>15puzzles Flex way</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><strong>15puzzles YUI implementation source code</strong></p>
<p>The purpose of this explanation is only to shift down code, because of beginning of code is not displayed.</p>
<p>So it will be clever do not to read it at all. </p>
<p>But in all cases implementation consists of index.html, 15puzzles.css and 15puzzles.js. </p>
<p>index.html contains all page mark up. </p>
<p>15puzzles.css contains css rules.</p>
<p>And 15puzzles.js contains all game logic and implementation.</p>
<p>Its also does not help. I need to do something with code highlighting plugin. Html code below is not completely displayed and also not formatted well.  Please <a href="http://javaghost.com/15puzzles/download/yui15puzzles.zip">download whole project</a>.</p>
<p>index.html</p>
<pre class="HTML" name="code">
<html>
    <head>
        <meta name="keywords" content="ui, ria, javascript, jquery, ext-js, extjs, yahoo ui, yui, flex, gwt, google web toolkit, client side programming" />
        <meta name="description" content="15puzzles game implemented using Yahoo UI (YUI) framework. View source/Download project. Page also contains links to 15puzzles game implemented in jQuery UI, Ext Js, Flex and Google Web Toolkit" />
<link rel="stylesheet" type="text/css" media="screen" href="15puzzle.css" />
        <script type="text/javascript" src="yui/build/yahoo-dom-event/yahoo-dom-event.js"> </script>
        <script type="text/javascript" src="yui/build/dragdrop/dragdrop-min.js"></script>
        <script type="text/javascript" src="yui/build/element/element.js"></script>
        <script type="text/javascript" src="15puzzles.js"></script>
        <script type="text/javascript">
            if ((typeof YAHOO !== "undefined") &#038;&#038; (YAHOO.util) &#038;&#038; (YAHOO.util.Event)) {
                YAHOO.util.Event.throwErrors = true;
            }
            YAHOO.util.Event.onDOMReady(function() {
                fifteenPuzzles = new FifteenPuzzles();
                fifteenPuzzles.init();
                fifteenPuzzles.makeDraggable();
                fifteenPuzzles.makeDroppable();
            });
        </script>

    </head>
    <body>
<div class="title">
<h1>15puzzles YUI way</h1>
</div>
<div class="box" id="box"></div>
<div class="buttons" id="buttons">
<div class="button" id="scramble" onmousedown=" new YAHOO.util.Element('scramble').setStyle('border-style', 'inset');" onmouseup="new YAHOO.util.Element('scramble').setStyle('border-style', 'outset');" onclick="fifteenPuzzles.scramble();">Scramble</div>
<div class="button" id="sort" onmousedown=" new YAHOO.util.Element('sort').setStyle('border-style', 'inset');" onmouseup="new YAHOO.util.Element('sort').setStyle('border-style', 'outset');" onclick="fifteenPuzzles.sort()">Sort</div>
</div>
<div id="header-menu" class="header-menu">
            <a href="http://javaghost.com/ria-frameworks/5-ways-to-15puzzles" class="header-link">Blog</a>
            <a href="http://javaghost.com/ria-frameworks/15puzzles-yui-way" class="header-link">View Source</a>
            <a href="http://javaghost.com/download/xjs15puzzles.zip" class="header-link">Download</a>
        </div>
<div class="ads" id="ads"></div>
<div class="others-ways" id="others-ways">
            <a class="other-way-link" href="http://javaghost.com/15puzzles/jquery">jQuery way</a>
            
            <a class="other-way-link" href="http://javaghost.com/15puzzles/extjs">Ext-Js way</a>
            
            <a class="other-way-link" href="http://javaghost.com/15puzzles/flex">Flex way</a>
            
            <a class="other-way-link" href="http://javaghost.com/15puzzles/gwt">Google Web Toolkit way</a>
        </div>

    </body>
</html>
</pre>
<p>15puzzles.css</p>
<pre class="css" name="code">
/*
    Document   : 15puzzle
    Created on : Oct 14, 2009, 5:52:32 PM
    Author     : yuri
    Description:
        Purpose of the stylesheet follows.
*/

/*
   TODO customize this sample style
   Syntax recommendation http://www.w3.org/TR/REC-CSS2/
*/

root {
    display: block;
}

body{

    margin-right: 0px;
    margin-left: 0px;
    margin-bottom: 0px;
    margin-top: 0px;
    padding-right: 0px;
    padding-left: 0px;
    padding-bottom: 0px;
    padding-top: 0px;
    background-color: #f1e2ef;
    color: #784280;
    font-weight: bold;
    font-size: 18px;
    font-family: fantasy;
}

.title{
    position: absolute;
    left:92px;
    top: 2px;
}

.box{

    width: 200px;
    height: 200px;
    position: absolute;
    top: 92px;
    left: 92px;
    border-right-style: double;
    border-left-style: double;
    border-bottom-style: double;
    border-top-style: double;
    border-right-width: 8px;
    border-left-width: 8px;
    border-bottom-width: 8px;
    border-top-width: 8px;
    border-right-color: #26244e;
    border-left-color: #26244e;
    border-bottom-color: #26244e;
    border-top-color: #26244e;
    z-index:-2;

    background-color: #aa97e3;
    visibility: visible;
}

.puzzle{

    position: absolute;
    width: 42px;
    height: 42px;
    visibility: visible;
    z-index: 2;
    border-top-style: outset;
    border-bottom-style: outset;
    border-right-color: #8d41a2;
    border-left-color: #8d41a2;
    border-bottom-color: #8d41a2;
    border-top-color: #8d41a2;
    border-right-width: 4px;
    border-left-width: 4px;
    border-bottom-width: 4px;
    border-top-width: 4px;
    border-left-style: outset;
    border-right-style: outset;
    background-color: #9229ab;
    font-family: Verdana,Arial,Helvetica,sans-serif;
    font-size: 24px;
    font-style: normal;
    font-weight: bold;
    font-variant: normal;
    color: #e9d5ef;
    text-align: center;
    vertical-align: middle;
    line-height: 43px;
}

.freeSpace{

    width: 48px;
    height: 48px;
    visibility: visible;
    z-index: -1;
    border-right-style: dotted;
    border-left-style: dotted;
    border-bottom-style: dotted;
    border-top-style: dotted;
    border-right-color: #f894de;
    border-left-color: #f894de;
    border-bottom-color: #f894de;
    border-top-color: #f894de;
    background-color: #7827ce;
    border-right-width: 1px;
    border-left-width: 1px;
    border-bottom-width: 1px;
    border-top-width: 1px;
}

.buttons{

    position: absolute;
    top: 100px;
    left: 350px;
    width: 100px;
    height: 300px;
}

.button{

    border-right-style: outset;
    border-left-style: outset;
    border-bottom-style: outset;
    border-top-style: outset;
    border-right-color: #8c4e9b;
    border-bottom-color: #8c4e9b;
    border-top-color: #8c4e9b;
    background-color: #9242a8;
    width: 100px;
    height: 30px;
    vertical-align: bottom;
    text-align: center;
    border-right-width: 5px;
    border-left-width: 5px;
    border-bottom-width: 5px;
    border-top-width: 5px;
    font-size: 14px;
    font-family: Verdana,Arial,Helvetica,sans-serif;
    color: #dfc3e8;
    font-weight: bold;
     border-left-color: #8c4e9b;
    margin-right: 30px;
    margin-left: 30px;
    margin-bottom: 30px;
    margin-top: 30px;
    text-indent: 1px;

    line-height: 30px;
}

.button-clicked{

   border-right-style: inset;
    border-left-style: inset;
    border-bottom-style: inset;
    border-top-style: inset;
    border-right-color: #8c4e9b;
    border-bottom-color: #8c4e9b;
    border-top-color: #8c4e9b;
    background-color: #9242a8;
    width: 100px;
    height: 30px;
    vertical-align: bottom;
    text-align: center;
    border-right-width: 5px;
    border-left-width: 5px;
    border-bottom-width: 5px;
    border-top-width: 5px;
    font-size: 14px;
    font-family: Verdana,Arial,Helvetica,sans-serif;
    color: #dfc3e8;
    font-weight: bold;
     border-left-color: #8c4e9b;
    margin-right: 30px;
    margin-left: 30px;
    margin-bottom: 30px;
    margin-top: 30px;
    text-indent: 1px;

    line-height: 30px;
}

.header-menu{

    position: absolute;
    top: 0px;
    left: 500px;
    width: 500px;
    height: 50px;
    visibility: visible;
}

.header-link{

    font-style: normal;
    font-family: fantasy;
    font-size: 18px;
    color: #784280;
    margin-left: 8px;
    margin-right: 8px;
    padding-left: 8px;
    padding-right: 8px;
}

.ads{

    border-right-style: none;
    border-left-style: none;
    border-bottom-style: none;
    border-top-style: none;
    border-right-color: #e08989;
    border-left-color: #e08989;
    border-bottom-color: #e08989;
    border-top-color: #e08989;
    border-right-width: 1px;
    border-left-width: 1px;
    border-bottom-width: 1px;
    border-top-width: 1px;
    position: absolute;
    top: 92px;
    left: 500px;
    width: 300px;
    height: 350px;
    visibility: visible;
}

.others-ways{

    position: absolute;
    top: 92px;
    left: 810px;
    color: #784280;
}

.other-way-link{

    font-size: 14px;
    font-style: normal;
    font-weight: bold;
    color: #784280;
    line-height: 40px;
}
</pre>
<p>15puzzles.js</p>
<pre class="javascript" name="code">

var puzzles;
var lastDraggedPuzzle;
var freeSpace;

function FifteenPuzzles(){
    puzzles = new Array();
    this.init = init;
    this.makeDraggable = makeDraggable;
    this.makeDroppable = makeDroppable;
    this.scramble = scramble;
    this.sort = sort;
}

function Puzzle(number, x, y, isDraggable, position){
    this.number = number;
    this.x = x;
    this.y = y;
    this.isDraggable = isDraggable;
    this.position = position;

}

function FreeSpace(x, y, position){
    this.x = x;
    this.y = y;
    this.position = position;
}

function init(){
    for (var i = 1; i <= 16; i++){
        var box_x = 0;
        var x;
        if (i < 5){
            x = box_x + (i - 1) * 50;
        } else if (5 <= i &#038;&#038; i < 9){
            x = box_x + (i - 5) * 50;
        } else if (9 <= i  &#038;&#038; i < 13){
            x = box_x + (i - 9) * 50;
        } else if (13 <= i  &#038;&#038; i <= 16){
            x = box_x + (i - 13) * 50;
        }
        var box_y = 0;
        var y;
        if (i < 5){
            y = box_y;
        } else if (5 <= i  &#038;&#038; i < 9){
            y = box_y + 50;
        } else if (9 <= i  &#038;&#038; i < 13){
            y = box_y + 100;
        } else if (13 <= i  &#038;&#038; i <= 16){
            y = box_y + 150;
        }
        var puzzle;
        if (i == 16){
            freeSpace = new FreeSpace(x, y, i);
        }else{
            puzzle = new Puzzle(i, x, y, false, i);
            puzzles[i] = puzzle;
        }
        if (i == 16){
            appendFreeSpace(freeSpace.x, freeSpace.y);
        }else{
            appendPuzzle(puzzle.x, puzzle.y, puzzle.number);
        }
    }

}

//find puzzles near freespace to make draggable
function makeDraggable(){
    for (var i = 1; i < 16; i++){
        if (freeSpace.x == puzzles[i].x || freeSpace.y == puzzles[i].y){
            if (puzzles[i].position == freeSpace.position - 1){
                makeRightDraggable(puzzles[i]);
            } else if (puzzles[i].position == freeSpace.position + 1){
                makeLeftDraggable(puzzles[i]);
            } else if (puzzles[i].position == freeSpace.position + 4){
                makeUpDraggable(puzzles[i]);
            } else if (puzzles[i].position == freeSpace.position - 4){
                makeDownDraggable(puzzles[i]);
            }
        }
    }
}

function makeUpDraggable(puzzle){
    //alert("puzzle " + puzzle.number + " must be draggable");
    var draggablePuzzle = new YAHOO.util.DD('puzzleId' + puzzle.number, 'puzzles', {
        isTarget : false
    });
    draggablePuzzle.startDrag = function (x, y){
        lastDraggedPuzzle = puzzle;
    }
    draggablePuzzle.onDragDrop = function (e, target){
        dragDropCommon();
    }
    draggablePuzzle.setXConstraint(0, 0);
    draggablePuzzle.setYConstraint(50, 0, 5);

}

function makeDownDraggable(puzzle){
    //alert("puzzle " + puzzle.number + " must be draggable");
    var draggablePuzzle = new YAHOO.util.DD('puzzleId' + puzzle.number, 'puzzles', {
        isTarget : false
    });
    draggablePuzzle.startDrag = function (x, y){
        lastDraggedPuzzle = puzzle;
    }
    draggablePuzzle.onDragDrop = function (e, target){
        dragDropCommon();
    }
    draggablePuzzle.setXConstraint(0, 0);
    draggablePuzzle.setYConstraint(0, 50, 5);

}

function makeLeftDraggable(puzzle){
    //alert("puzzle " + puzzle.number + " must be draggable");
    var draggablePuzzle = new YAHOO.util.DD('puzzleId' + puzzle.number, 'puzzles', {
        isTarget : false
    });
    draggablePuzzle.startDrag = function (x, y){
        lastDraggedPuzzle = puzzle;
    }
    draggablePuzzle.onDragDrop = function (e, target){
        dragDropCommon();
    }
    draggablePuzzle.setXConstraint(50, 0, 5);
    draggablePuzzle.setYConstraint(0, 0);

}

function makeRightDraggable(puzzle){
    //alert("puzzle " + puzzle.number + " must be draggable");
    var draggablePuzzle = new YAHOO.util.DD('puzzleId' + puzzle.number, 'puzzles', {
        isTarget : false
    });
    draggablePuzzle.startDrag = function (x, y){
        lastDraggedPuzzle = puzzle;
    }
    draggablePuzzle.onDragDrop = function (e, target){
        dragDropCommon();
    }
    draggablePuzzle.setXConstraint(0, 50, 5);
    draggablePuzzle.setYConstraint(0, 0);

}

function makeDroppable(){
    var droppableFreeSpace = new YAHOO.util.DDTarget('freeSpaceId', 'puzzles');
}

function scramble(){
    var box = new YAHOO.util.Element('box');
    while (box.hasChildNodes()) {
        box.removeChild(box.get('firstChild'));
    }
    var xTemp = freeSpace.x;
    var yTemp = freeSpace.y;
    var positionTemp = freeSpace.position;

    var n = Math.ceil(Math.random() * 15);
    var randomPuzzle = puzzles[n];
    freeSpace.x = randomPuzzle.x;
    freeSpace.y = randomPuzzle.y;
    freeSpace.position = randomPuzzle.position;

    randomPuzzle.x = xTemp;
    randomPuzzle.y = yTemp;
    randomPuzzle.position = positionTemp;

    appendFreeSpace(freeSpace.x, freeSpace.y);

    for (var i = 0; i < 20; i++){
        var randomPuzzle1 = puzzles[Math.ceil(Math.random() * 15)];
        var randomPuzzle2 = puzzles[Math.ceil(Math.random() * 15)];

        xTemp = randomPuzzle1.x;
        yTemp = randomPuzzle1.y;
        positionTemp = randomPuzzle1.position;

        randomPuzzle1.x = randomPuzzle2.x;
        randomPuzzle1.y = randomPuzzle2.y;
        randomPuzzle1.position = randomPuzzle2.position;

        randomPuzzle2.x = xTemp;
        randomPuzzle2.y = yTemp;
        randomPuzzle2.position =positionTemp;
    }

    for (i = 1; i < 16; i ++){
        appendPuzzle(puzzles[i].x, puzzles[i].y, puzzles[i].number);
    }
    this.makeDraggable();
    this.makeDroppable();

}

function sort(){
    var box = new YAHOO.util.Element('box');
    while (box.hasChildNodes()) {
        box.removeChild(box.get('firstChild'));
    }
    puzzles = new Array();
    this.init();
    this.makeDraggable();
    this.makeDroppable();
}

function appendPuzzle(x, y, number){
    var box = new YAHOO.util.Element('box');
    var puzzle = document.createElement('div');
    puzzle.id = 'puzzleId' + number;
    puzzle.innerHTML = number;
    puzzle.className = 'puzzle';
    puzzle.setAttribute('style', 'position:absolute; left:' + x + 'px; top:' + y +'px;')
    box.appendChild(puzzle);
}

function appendFreeSpace(x, y){
    var box = new YAHOO.util.Element('box');
    var freeSpace = document.createElement('div');
    freeSpace.id = 'freeSpaceId';
    freeSpace.className = 'freeSpace';
    freeSpace.setAttribute('style', 'position:absolute; left:' + x + 'px; top:' + y +'px;')
    box.appendChild(freeSpace);
}

function dragDropCommon(){
    //alert ('dragDropCommon');
    var xTemp = freeSpace.x;
    var yTemp = freeSpace.y;
    var positionTemp = freeSpace.position;

    freeSpace.x = lastDraggedPuzzle.x;
    freeSpace.y = lastDraggedPuzzle.y;
    freeSpace.position = lastDraggedPuzzle.position;

    lastDraggedPuzzle.x = xTemp;
    lastDraggedPuzzle.y = yTemp;
    lastDraggedPuzzle.position = positionTemp;

    puzzles[lastDraggedPuzzle.number] = lastDraggedPuzzle;
    //destroy all draggable and dropable
    var box = new YAHOO.util.Element('box');
    box.removeChild(new YAHOO.util.Element('freeSpaceId'));
    appendFreeSpace(freeSpace.x, freeSpace.y);

    box.removeChild(new YAHOO.util.Element('puzzleId' + lastDraggedPuzzle.number));
    appendPuzzle(lastDraggedPuzzle.x, lastDraggedPuzzle.y, lastDraggedPuzzle.number);
    makeDraggable();
    makeDroppable();
}
</pre>


<p>Related posts:<ol><li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-jquery-way/' rel='bookmark' title='Permanent Link: 15puzzles jQuery way'>15puzzles jQuery way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-extjs-way/' rel='bookmark' title='Permanent Link: 15puzzles ExtJs way'>15puzzles ExtJs way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-flex-way/' rel='bookmark' title='Permanent Link: 15puzzles Flex way'>15puzzles Flex way</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.javaneverdie.com/ria-frameworks/15puzzles-yui-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>15puzzles ExtJs way</title>
		<link>http://www.javaneverdie.com/ria-frameworks/15puzzles-extjs-way/</link>
		<comments>http://www.javaneverdie.com/ria-frameworks/15puzzles-extjs-way/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 11:18:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[RIA Frameworks]]></category>

		<guid isPermaLink="false">http://www.javaneverdie.com/?p=257</guid>
		<description><![CDATA[15puzzles implemented using Ext-Js index.html Ext.onReady(function() { fifteenPuzzles = new FifteenPuzzles(); fifteenPuzzles.init(); fifteenPuzzles.makeDraggable(); fifteenPuzzles.makeDroppable(); }); 15puzzles Ext-Js way Scramble Sort Blog View Source Download jQuery way Yahoo UI way Flex way Google Web Toolkit way 15puzzles.css /* Document : 15puzzle Created on : Oct 14, 2009, 5:52:32 PM Author : yuri Description: Purpose of the [...]


Related posts:<ol><li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-flex-way/' rel='bookmark' title='Permanent Link: 15puzzles Flex way'>15puzzles Flex way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-jquery-way/' rel='bookmark' title='Permanent Link: 15puzzles jQuery way'>15puzzles jQuery way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-yui-way/' rel='bookmark' title='Permanent Link: 15puzzles YUI way'>15puzzles YUI way</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><strong>15puzzles implemented using Ext-Js</strong></p>
<p>index.html</p>
<pre class="html" name="code">
<html>
    <head>
        <meta name="keywords" content="ui, ria, javascript,
                    jquery, ext-js, extjs, yahoo ui, yui,
                    flex, gwt, google web toolkit,
                    client side programming" />
        <meta name="description" content="15puzzles game
                    implemented using Ext-Js  framework.
                    View source/Download project.
                    Page also contains links to 15puzzles
                    game implemented in jQuery UI, Yahoo UI,
                    Flex and Google Web Toolkit" />
<link rel="stylesheet" type="text/css"
              media="screen" href="15puzzle.css" />
        <script type="text/javascript"
                src="extjs/adapter/ext/ext-base.js"></script>
        <script type="text/javascript"
                src="extjs/ext-all.js"></script>

        <script type="text/javascript" src="15puzzles.js"></script>
        <script type="text/javascript">
            Ext.onReady(function() {
                fifteenPuzzles = new FifteenPuzzles();
                fifteenPuzzles.init();
                fifteenPuzzles.makeDraggable();
                fifteenPuzzles.makeDroppable();
            });

        </script>

    </head>
    <body>
<div class="title">
<h1>15puzzles Ext-Js way</h1>
</div>
<div class="box" id="box"></div>
<div class="buttons" id="buttons">
<div class="button" id="scramble"
                 onmousedown=" Ext.get('scramble').setStyle('border-style', 'inset');"
                 onmouseup="Ext.get('scramble').setStyle('border-style', 'outset');"
                 onclick="fifteenPuzzles.scramble();">Scramble</div>
<div class="button" id="sort"
                 onmousedown=" Ext.get('sort').setStyle('border-style', 'inset');"
                 onmouseup="Ext.get('sort').setStyle('border-style', 'outset');"
                 onclick="fifteenPuzzles.sort()">Sort</div>
</div>
<div id="header-menu" class="header-menu">
            <a href="http://javaghost.com/ria-frameworks/5-ways-to-15puzzles"
               class="header-link">Blog</a>
            <a href="http://javaghost.com/ria-frameworks/15puzzles-extjs-way"
               class="header-link">View Source</a>
            <a href="http://javaghost.com/download/xjs15puzzles.zip"
               class="header-link">Download</a>
        </div>
<div class="ads" id="ads"></div>
<div class="others-ways" id="others-ways">
            <a class="other-way-link"
               href="http://javaghost.com/15puzzles/jquery">jQuery way</a>
            
            <a class="other-way-link"
               href="http://javaghost.com/15puzzles/yui">Yahoo UI way</a>
            
            <a class="other-way-link"
               href="http://javaghost.com/15puzzles/flex">Flex way</a>
            
            <a class="other-way-link"
               href="http://javaghost.com/15puzzles/gwt">Google Web Toolkit way</a>
        </div>

    </body>
</html>
</pre>
<p>15puzzles.css</p>
<pre class="css" name="code">
/*
    Document   : 15puzzle
    Created on : Oct 14, 2009, 5:52:32 PM
    Author     : yuri
    Description:
        Purpose of the stylesheet follows.
*/

/*
   TODO customize this sample style
   Syntax recommendation http://www.w3.org/TR/REC-CSS2/
*/

root {
    display: block;
}

body{

    margin-right: 0px;
    margin-left: 0px;
    margin-bottom: 0px;
    margin-top: 0px;
    padding-right: 0px;
    padding-left: 0px;
    padding-bottom: 0px;
    padding-top: 0px;
    background-color: #e6f8f6;
    color: #2736a4;
    font-weight: bold;
    font-size: 18px;
    font-family: fantasy;
}

.title{
    position: absolute;
    left:92px;
    top: 2px;
}

.box{

    width: 200px;
    height: 200px;
    position: absolute;
    top: 92px;
    left: 92px;
    border-right-style: double;
    border-left-style: double;
    border-bottom-style: double;
    border-top-style: double;
    border-right-width: 8px;
    border-left-width: 8px;
    border-bottom-width: 8px;
    border-top-width: 8px;
    border-right-color: #416a3d;
    border-left-color: #416a3d;
    border-bottom-color: #416a3d;
    border-top-color: #416a3d;
    z-index:-2;

    background-color: #83eead;
    visibility: visible;
}

.puzzle{

    position: absolute;
    width: 42px;
    height: 42px;
    visibility: visible;
    z-index: 2;
    border-top-style: outset;
    border-bottom-style: outset;
    border-right-color: #1224d4;
    border-left-color: #1224d4;
    border-bottom-color: #1224d4;
    border-top-color: #1224d4;
    border-right-width: 4px;
    border-left-width: 4px;
    border-bottom-width: 4px;
    border-top-width: 4px;
    border-left-style: outset;
    border-right-style: outset;
    background-color: #2715ee;
    font-family: Verdana,Arial,Helvetica,sans-serif;
    font-size: 24px;
    font-style: normal;
    font-weight: bold;
    font-variant: normal;
    color: #86dfd5;
    text-align: center;
    vertical-align: middle;
    line-height: 43px;
}

.freeSpace{

    width: 48px;
    height: 48px;
    visibility: visible;
    z-index: -1;
    border-right-style: dotted;
    border-left-style: dotted;
    border-bottom-style: dotted;
    border-top-style: dotted;
    border-right-color: #f894de;
    border-left-color: #f894de;
    border-bottom-color: #f894de;
    border-top-color: #f894de;
    background-color: #16f56d;
    border-right-width: 1px;
    border-left-width: 1px;
    border-bottom-width: 1px;
    border-top-width: 1px;
}

.buttons{

    position: absolute;
    top: 100px;
    left: 350px;
    width: 100px;
    height: 300px;
}

.button{

    border-right-style: outset;
    border-left-style: outset;
    border-bottom-style: outset;
    border-top-style: outset;
    border-right-color: #2715ee;
    border-bottom-color: #2715ee;
    border-top-color: #2715ee;
    background-color: #2715ee;
    width: 100px;
    height: 30px;
    vertical-align: bottom;
    text-align: center;
    border-right-width: 5px;
    border-left-width: 5px;
    border-bottom-width: 5px;
    border-top-width: 5px;
    font-size: 14px;
    font-family: Verdana,Arial,Helvetica,sans-serif;
    color: #86dfd5;
    font-weight: bold;
     border-left-color: #2715ee;
    margin-right: 30px;
    margin-left: 30px;
    margin-bottom: 30px;
    margin-top: 30px;
    text-indent: 1px;

    line-height: 30px;
}

.button-clicked{

    border-right-style: inset;
    border-left-style: inset;
    border-bottom-style: inset;
    border-top-style: inset;
    border-right-color: #d41212;
    border-bottom-color: #d41212;
    border-top-color: #d41212;
    background-color: #ee1515;
    width: 100px;
    height: 30px;
    vertical-align: middle;
    text-align: center;
    border-right-width: 5px;
    border-left-width: 5px;
    border-bottom-width: 5px;
    border-top-width: 5px;
    font-size: 14px;
    color: #facf32;
    font-weight: bold;
     border-left-color: #d41212;
    margin-right: 30px;
    margin-left: 30px;
    margin-bottom: 30px;
    margin-top: 30px;
    position: absolute;
    line-height: 30px;
}

.header-menu{

    position: absolute;
    top: 0px;
    left: 500px;
    width: 500px;
    height: 50px;
    visibility: visible;
}

.header-link{

    font-style: normal;
    font-family: fantasy;
    font-size: 18px;
    color: #2736a4;
    margin-left: 8px;
    margin-right: 8px;
    padding-left: 8px;
    padding-right: 8px;
}

.ads{

    border-right-style: none;
    border-left-style: none;
    border-bottom-style: none;
    border-top-style: none;
    border-right-color: #e08989;
    border-left-color: #e08989;
    border-bottom-color: #e08989;
    border-top-color: #e08989;
    border-right-width: 1px;
    border-left-width: 1px;
    border-bottom-width: 1px;
    border-top-width: 1px;
    position: absolute;
    top: 92px;
    left: 500px;
    width: 300px;
    height: 350px;
    visibility: visible;
}

.others-ways{

    position: absolute;
    top: 92px;
    left: 810px;
    color: #2736a4;
}

.other-way-link{

    font-size: 14px;
    font-style: normal;
    font-weight: bold;
    color: #2736a4;
    line-height: 40px;
}
</pre>
<p>15puzzles.js</p>
<pre class="javascript" name="code">

var puzzles;
var lastDraggedPuzzle;
var freeSpace;

function FifteenPuzzles(){
    puzzles = new Array();
    this.init = init;
    this.makeDraggable = makeDraggable;
    this.makeDroppable = makeDroppable;
    this.scramble = scramble;
    this.sort = sort;
}

function Puzzle(number, x, y, isDraggable, position){
    this.number = number;
    this.x = x;
    this.y = y;
    this.isDraggable = isDraggable;
    this.position = position;

}

function FreeSpace(x, y, position){
    this.x = x;
    this.y = y;
    this.position = position;
}

function init(){
    for (var i = 1; i <= 16; i++){
        var box_x = 0;
        var x;
        if (i < 5){
            x = box_x + (i - 1) * 50;
        } else if (5 <= i &#038;&#038; i < 9){
            x = box_x + (i - 5) * 50;
        } else if (9 <= i  &#038;&#038; i < 13){
            x = box_x + (i - 9) * 50;
        } else if (13 <= i  &#038;&#038; i <= 16){
            x = box_x + (i - 13) * 50;
        }
        var box_y = 0;
        var y;
        if (i < 5){
            y = box_y;
        } else if (5 <= i  &#038;&#038; i < 9){
            y = box_y + 50;
        } else if (9 <= i  &#038;&#038; i < 13){
            y = box_y + 100;
        } else if (13 <= i  &#038;&#038; i <= 16){
            y = box_y + 150;
        }
        var puzzle;
        if (i == 16){
            freeSpace = new FreeSpace(x, y, i);
        }else{
            puzzle = new Puzzle(i, x, y, false, i);
            puzzles[i] = puzzle;
        }
        if (i == 16){
            appendFreeSpace(freeSpace.x, freeSpace.y);
        }else{
            appendPuzzle(puzzle.x, puzzle.y, puzzle.number);
        }
    }

}

//find puzzles near freespace to make draggable
function makeDraggable(){
    for (var i = 1; i < 16; i++){
        if (freeSpace.x == puzzles[i].x || freeSpace.y == puzzles[i].y){
            if (puzzles[i].position == freeSpace.position - 1){
                makeRightDraggable(puzzles[i]);
            } else if (puzzles[i].position == freeSpace.position + 1){
                makeLeftDraggable(puzzles[i]);
            } else if (puzzles[i].position == freeSpace.position + 4){
                makeUpDraggable(puzzles[i]);
            } else if (puzzles[i].position == freeSpace.position - 4){
                makeDownDraggable(puzzles[i]);
            }
        }
    }
}

function makeUpDraggable(puzzle){
    //alert("puzzle " + puzzle.number + " must be draggable");
    var draggablePuzzle = new Ext.dd.DD('puzzleId' + puzzle.number, 'puzzles', {
        isTarget : false
    });
    draggablePuzzle.startDrag = function (x, y){
        lastDraggedPuzzle = puzzle;
    }
    draggablePuzzle.onDragDrop = function (e, target){
        dragDropCommon();
    }
    draggablePuzzle.setXConstraint(0, 0);
    draggablePuzzle.setYConstraint(50, 0, 5);

}

function makeDownDraggable(puzzle){
    //alert("puzzle " + puzzle.number + " must be draggable");
    var draggablePuzzle = new Ext.dd.DD('puzzleId' + puzzle.number, 'puzzles', {
        isTarget : false
    });
    draggablePuzzle.startDrag = function (x, y){
        lastDraggedPuzzle = puzzle;
    }
    draggablePuzzle.onDragDrop = function (e, target){
        dragDropCommon();
    }
    draggablePuzzle.setXConstraint(0, 0);
    draggablePuzzle.setYConstraint(0, 50, 5);

}

function makeLeftDraggable(puzzle){
    //alert("puzzle " + puzzle.number + " must be draggable");
    var draggablePuzzle = new Ext.dd.DD('puzzleId' + puzzle.number, 'puzzles', {
        isTarget : false
    });
    draggablePuzzle.startDrag = function (x, y){
        lastDraggedPuzzle = puzzle;
    }
    draggablePuzzle.onDragDrop = function (e, target){
        dragDropCommon();
    }
    draggablePuzzle.setXConstraint(50, 0, 5);
    draggablePuzzle.setYConstraint(0, 0);

}

function makeRightDraggable(puzzle){
    //alert("puzzle " + puzzle.number + " must be draggable");
    var draggablePuzzle = new Ext.dd.DD('puzzleId' + puzzle.number, 'puzzles', {
        isTarget : false
    });
    draggablePuzzle.startDrag = function (x, y){
        lastDraggedPuzzle = puzzle;
    }
    draggablePuzzle.onDragDrop = function (e, target){
        dragDropCommon();
    }
    draggablePuzzle.setXConstraint(0, 50, 5);
    draggablePuzzle.setYConstraint(0, 0);

}

function makeDroppable(){
    var droppableFreeSpace = new Ext.dd.DDTarget('freeSpaceId', 'puzzles');
}

function scramble(){
    //$("#box > *").draggable('destroy');
    //$("#box > *").droppable('destroy');
    Ext.get('freeSpaceId').remove();
    Ext.select('.puzzle').remove();
    var xTemp = freeSpace.x;
    var yTemp = freeSpace.y;
    var positionTemp = freeSpace.position;

    var n = Math.ceil(Math.random() * 15);
    var randomPuzzle = puzzles[n];
    freeSpace.x = randomPuzzle.x;
    freeSpace.y = randomPuzzle.y;
    freeSpace.position = randomPuzzle.position;

    randomPuzzle.x = xTemp;
    randomPuzzle.y = yTemp;
    randomPuzzle.position = positionTemp;

    appendFreeSpace(freeSpace.x, freeSpace.y);

    for (var i = 0; i < 20; i++){
        var randomPuzzle1 = puzzles[Math.ceil(Math.random() * 15)];
        var randomPuzzle2 = puzzles[Math.ceil(Math.random() * 15)];

        xTemp = randomPuzzle1.x;
        yTemp = randomPuzzle1.y;
        positionTemp = randomPuzzle1.position;

        randomPuzzle1.x = randomPuzzle2.x;
        randomPuzzle1.y = randomPuzzle2.y;
        randomPuzzle1.position = randomPuzzle2.position;

        randomPuzzle2.x = xTemp;
        randomPuzzle2.y = yTemp;
        randomPuzzle2.position =positionTemp;
    }

    for (i = 1; i < 16; i ++){
        appendPuzzle(puzzles[i].x, puzzles[i].y, puzzles[i].number);
    }
    this.makeDraggable();
    this.makeDroppable();

}

function sort(){
    Ext.get('freeSpaceId').remove();
    Ext.select('.puzzle').remove();
    puzzles = new Array();
    this.init();
    this.makeDraggable();
    this.makeDroppable();
}

function appendPuzzle(x, y, number){
    var box = Ext.get('box');
    Ext.DomHelper.append(box,'
<div class="puzzle" id="puzzleId'
        + number
        + '"'
        +'" style="position:absolute; '
        + 'left:' + x + 'px; top:' + y +'px;'
        +'">'
        + number
        + '</div>

');
}

function appendFreeSpace(x, y){
    var box = Ext.get('box');
    Ext.DomHelper.append(box, '
<div class="freeSpace" id="freeSpaceId'
        + '"'
        +'" style="position:absolute; '
        + 'left:' + x + 'px; top:' + y +'px;'
        +'">'
        + '</div>

')
}

function dragDropCommon(){
    var xTemp = freeSpace.x;
    var yTemp = freeSpace.y;
    var positionTemp = freeSpace.position;

    freeSpace.x = lastDraggedPuzzle.x;
    freeSpace.y = lastDraggedPuzzle.y;
    freeSpace.position = lastDraggedPuzzle.position;

    lastDraggedPuzzle.x = xTemp;
    lastDraggedPuzzle.y = yTemp;
    lastDraggedPuzzle.position = positionTemp;

    puzzles[lastDraggedPuzzle.number] = lastDraggedPuzzle;
    Ext.get('freeSpaceId').remove();
    appendFreeSpace(freeSpace.x, freeSpace.y);

    Ext.get('puzzleId' + lastDraggedPuzzle.number).remove();
    appendPuzzle(lastDraggedPuzzle.x, lastDraggedPuzzle.y, lastDraggedPuzzle.number);
    makeDraggable();
    makeDroppable();
}
</pre>


<p>Related posts:<ol><li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-flex-way/' rel='bookmark' title='Permanent Link: 15puzzles Flex way'>15puzzles Flex way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-jquery-way/' rel='bookmark' title='Permanent Link: 15puzzles jQuery way'>15puzzles jQuery way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-yui-way/' rel='bookmark' title='Permanent Link: 15puzzles YUI way'>15puzzles YUI way</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.javaneverdie.com/ria-frameworks/15puzzles-extjs-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>15puzzles jQuery way</title>
		<link>http://www.javaneverdie.com/ria-frameworks/15puzzles-jquery-way/</link>
		<comments>http://www.javaneverdie.com/ria-frameworks/15puzzles-jquery-way/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 14:30:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[RIA Frameworks]]></category>

		<guid isPermaLink="false">http://www.javaneverdie.com/?p=239</guid>
		<description><![CDATA[15puzzles jQuery implementation source code The purpose of this explanation is only to shift down code, because of beginning of code is not displayed. So it will be clever do not to read it at all. But in all cases implementation consists of index.html, 15puzzles.css and 15puzzles.js. index.html contains all page mark up. 15puzzles.css contains [...]


Related posts:<ol><li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-extjs-way/' rel='bookmark' title='Permanent Link: 15puzzles ExtJs way'>15puzzles ExtJs way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-flex-way/' rel='bookmark' title='Permanent Link: 15puzzles Flex way'>15puzzles Flex way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-yui-way/' rel='bookmark' title='Permanent Link: 15puzzles YUI way'>15puzzles YUI way</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><strong>15puzzles jQuery implementation source code</strong></p>
<p>The purpose of this explanation is only to shift down code, because of beginning of code is not displayed.</p>
<p>So it will be clever do not to read it at all. </p>
<p>But in all cases implementation consists of index.html, 15puzzles.css and 15puzzles.js. </p>
<p>index.html contains all page mark up. </p>
<p>15puzzles.css contains css rules.</p>
<p>And 15puzzles.js contains all game logic and implementation.</p>
<p>Its also does not help. I need to do something with code highlighting plugin. Html code below is not completely displayed and also not formatted well.  Please <a href="http://javaghost.com/15puzzles/download/jq15puzzles.zip">download whole project</a>.</p>
<p>index.html</p>
<p><code></p>
<pre class="html" name="code">

<html>
    <head>
        <meta name="keywords" content="ui, ria, javascript, jquery,
                    ext-js, extjs, yahoo ui, yui, flex, gwt, google web toolkit,
                    client side programming" >
        <meta name="description" content="15puzzles game
                    implemented using jQuery UI framework.
                    View source/Download project. Page also contains links
                     to 15puzzles game implemented in Ext Js, Yahoo UI,
                     Flex and Google Web Toolkit" >
<link rel="stylesheet" type="text/css" media="screen"
                       href="15puzzles.css" >
        <script type="text/javascript" src="jquery/js/jquery-1.3.2.min.js">
        <script type="text/javascript" 
                    src="jquery/jqueryui/jquery-ui-1.7.2.custom.min.js"></script>
        <script type="text/javascript" src="15puzzles.js"></script>
        <script type="text/javascript">
            // we will add our javascript code here
            jQuery(function() {
                fifteenPuzzles = new FifteenPuzzles();
                fifteenPuzzles.init();
                fifteenPuzzles.makeDraggable();
                fifteenPuzzles.makeDroppable();
            });

        </script>

    </head>
    <body>
<div class="title">
<h1>15puzzles jQuery way</h1>
</div>
<div class="box" id="box"></div>
<div class="buttons" id="buttons">
<div class="button" id="scramble"
                    onmousedown=" $('#scramble').css('border-style', 'inset');"
                    onmouseup="$('#scramble').css('border-style', 'outset');"
                    onclick="fifteenPuzzles.scramble();">Scramble</div>
<div class="button" id="sort" onmousedown=" $('#sort').css('border-style', 'inset');"
                                onmouseup="$('#sort').css('border-style', 'outset');"
                                onclick="fifteenPuzzles.sort()">Sort</div>
</div>
<div id="header-menu" class="header-menu">
            <a href="http://javaghost.com/ria-frameworks/5-ways-to-15puzzles"
                  class="header-link">
                  Blog
            </a>
            <a href="http://javaghost.com/ria-frameworks/15puzzles-jquery-way"
                            class="header-link">View Source</a>
            <a href="http://javaghost.com/download/jq15puzzles.zip"
                            class="header-link">Download</a>
           </div>
<div class="ads" id="ads">
           <!-- for google ads-->
        </div>
<div class="others-ways" id="others-ways">
            <a class="other-way-link"
                  href="http://javaghost.com/15puzzles/extjs">Ext-Js way</a>
            
            <a class="other-way-link"
                   href="http://javaghost.com/15puzzles/yui">Yahoo UI way</a>
            
            <a class="other-way-link"
                  href="http://javaghost.com/15puzzles/flex">Flex way</a>
            
            <a class="other-way-link"
                   href="http://javaghost.com/15puzzles/gwt">Google Web Toolkit way</a>
<div>

    <body>
<html>
</pre>
<p></code><br />
15puzzles.css</p>
<pre class="css" name="code">
/*
    Document   : 15puzzle
    Created on : Oct 14, 2009, 5:52:32 PM
    Author     : yuri
    Description:
        Purpose of the stylesheet follows.
*/

/*
   TODO customize this sample style
   Syntax recommendation http://www.w3.org/TR/REC-CSS2/
*/

root {
    display: block;
}

body{

    margin-right: 0px;
    margin-left: 0px;
    margin-bottom: 0px;
    margin-top: 0px;
    padding-right: 0px;
    padding-left: 0px;
    padding-bottom: 0px;
    padding-top: 0px;
    background-color: #f5f3d9;
    color: #e42020;
    font-weight: bold;
    font-size: 18px;
    font-family: fantasy;
}

.title{
    position: absolute;
    left:92px;
    top: 2px;
}

.box{

    width: 200px;
    height: 200px;
    position: absolute;
    top: 92px;
    left: 92px;
    border-right-style: double;
    border-left-style: double;
    border-bottom-style: double;
    border-top-style: double;
    border-right-width: 8px;
    border-left-width: 8px;
    border-bottom-width: 8px;
    border-top-width: 8px;
    border-right-color: #e08b10;
    border-left-color: #e08b10;
    border-bottom-color: #e08b10;
    border-top-color: #e08b10;
    z-index:-2;

    background-color: #eef516;
    visibility: visible;
}

.puzzle{

    position: absolute;
    width: 42px;
    height: 42px;
    visibility: visible;
    z-index: 2;
    border-top-style: outset;
    border-bottom-style: outset;
    border-right-color: #d41212;
    border-left-color: #d41212;
    border-bottom-color: #d41212;
    border-top-color: #d41212;
    border-right-width: 4px;
    border-left-width: 4px;
    border-bottom-width: 4px;
    border-top-width: 4px;
    border-left-style: outset;
    border-right-style: outset;
    background-color: #ee1515;
    font-family: Verdana,Arial,Helvetica,sans-serif;
    font-size: 24px;
    font-style: normal;
    font-weight: bold;
    font-variant: normal;
    color: #fce5c7;
    text-align: center;
    vertical-align: bottom;
    line-height: 43px;
}

.freeSpace{

    width: 48px;
    height: 48px;
    visibility: visible;
    z-index: -1;
    border-right-style: dotted;
    border-left-style: dotted;
    border-bottom-style: dotted;
    border-top-style: dotted;
    border-right-color: #f894de;
    border-left-color: #f894de;
    border-bottom-color: #f894de;
    border-top-color: #f894de;
    background-color: #eef516;
    border-right-width: 1px;
    border-left-width: 1px;
    border-bottom-width: 1px;
    border-top-width: 1px;
}

.buttons{

    position: absolute;
    top: 100px;
    left: 350px;
    width: 100px;
    height: 300px;
}

.button{

    border-right-style: outset;
    border-left-style: outset;
    border-bottom-style: outset;
    border-top-style: outset;
    border-right-color: #d41212;
    border-bottom-color: #d41212;
    border-top-color: #d41212;
    background-color: #ee1515;
    width: 100px;
    height: 30px;
    vertical-align: bottom;
    text-align: center;
    border-right-width: 5px;
    border-left-width: 5px;
    border-bottom-width: 5px;
    border-top-width: 5px;
    font-size: 14px;
    font-family: Verdana,Arial,Helvetica,sans-serif;
    color: #fce5c7;
    font-weight: bold;
     border-left-color: #d41212;
    margin-right: 30px;
    margin-left: 30px;
    margin-bottom: 30px;
    margin-top: 30px;
    text-indent: 1px;

    line-height: 30px;
}

.button-clicked{

    border-right-style: inset;
    border-left-style: inset;
    border-bottom-style: inset;
    border-top-style: inset;
    border-right-color: #d41212;
    border-bottom-color: #d41212;
    border-top-color: #d41212;
    background-color: #ee1515;
    width: 100px;
    height: 30px;
    vertical-align: middle;
    text-align: center;
    border-right-width: 5px;
    border-left-width: 5px;
    border-bottom-width: 5px;
    border-top-width: 5px;
    font-size: 14px;
    color: #fce5c7;
    font-weight: bold;
     border-left-color: #d41212;
    margin-right: 30px;
    margin-left: 30px;
    margin-bottom: 30px;
    margin-top: 30px;
    position: absolute;
    line-height: 30px;
}

.header-menu{

    position: absolute;
    top: 0px;
    left: 500px;
    width: 500px;
    height: 50px;
    visibility: visible;
}

.header-link{

    font-style: normal;
    font-family: fantasy;
    font-size: 18px;
    color: #c91d1d;
    margin-left: 8px;
    margin-right: 8px;
    padding-left: 8px;
    padding-right: 8px;
}

.ads{

    border-right-style: none;
    border-left-style: none;
    border-bottom-style: none;
    border-top-style: none;
    border-right-color: #e08989;
    border-left-color: #e08989;
    border-bottom-color: #e08989;
    border-top-color: #e08989;
    border-right-width: 1px;
    border-left-width: 1px;
    border-bottom-width: 1px;
    border-top-width: 1px;
    position: absolute;
    top: 92px;
    left: 500px;
    width: 300px;
    height: 350px;
    visibility: visible;
}

.others-ways{

    position: absolute;
    top: 92px;
    left: 810px;
    color: #e31818;
}

.other-way-link{

    font-size: 14px;
    font-style: normal;
    font-weight: bold;
    color: #c91d1d;
    line-height: 40px;
}
</pre>
<p>15puzzles.js</p>
<pre class="javascript" name="code">

var puzzles;
var lastDraggedPuzzle;
var freeSpace;

function FifteenPuzzles(){
    puzzles = new Array();
    this.init = init;
    this.makeDraggable = makeDraggable;
    this.makeDroppable = makeDroppable;
    this.scramble = scramble;
    this.sort = sort;
}

function Puzzle(number, x, y, isDraggable, position){
    this.number = number;
    this.x = x;
    this.y = y;
    this.isDraggable = isDraggable;
    this.position = position;

}

function FreeSpace(x, y, position){
    this.x = x;
    this.y = y;
    this.position = position;
}

function init(){
    for (var i = 1; i <= 16; i++){
        var box_x = 0;
        var x;
        if (i < 5){
            x = box_x + (i - 1) * 50;
        } else if (5 <= i &#038;&#038; i < 9){
            x = box_x + (i - 5) * 50;
        } else if (9 <= i  &#038;&#038; i < 13){
            x = box_x + (i - 9) * 50;
        } else if (13 <= i  &#038;&#038; i <= 16){
            x = box_x + (i - 13) * 50;
        }
        var box_y = 0;
        var y;
        if (i < 5){
            y = box_y;
        } else if (5 <= i  &#038;&#038; i < 9){
            y = box_y + 50;
        } else if (9 <= i  &#038;&#038; i < 13){
            y = box_y + 100;
        } else if (13 <= i  &#038;&#038; i <= 16){
            y = box_y + 150;
        }
        var puzzle;
        if (i == 16){
            freeSpace = new FreeSpace(x, y, i);
        }else{
            puzzle = new Puzzle(i, x, y, false, i);
            puzzles[i] = puzzle;
        }
        if (i == 16){
            appendFreeSpace(freeSpace.x, freeSpace.y);
        }else{
            appendPuzzle(puzzle.x, puzzle.y, puzzle.number);
        }
    }
    $('#box').css('height', '200');

}

//find puzzles near freespace to make draggable
function makeDraggable(){
    for (var i = 1; i < 16; i++){
        if (freeSpace.x == puzzles[i].x || freeSpace.y == puzzles[i].y){
            if (puzzles[i].position == freeSpace.position - 1){
                jQMakeRightDraggable(puzzles[i]);
            } else if (puzzles[i].position == freeSpace.position + 1){
                jQMakeLeftDraggable(puzzles[i]);
            } else if (puzzles[i].position == freeSpace.position + 4){
                jQMakeUpDraggable(puzzles[i]);
            } else if (puzzles[i].position == freeSpace.position - 4){
                jQMakeDownDraggable(puzzles[i]);
            }
        }
    }
}

function jQMakeUpDraggable(puzzle){
    $('#puzzleId' + puzzle.number).draggable({
        axis: 'y',
        containment: [puzzle.x - 100, puzzle.y + 50, puzzle.x + 100, puzzle.y + 100],
        stop: function(event, ui) {
            lastDraggedPuzzle = puzzle;
        },
        start: function(event, ui) {
            lastDraggedPuzzle = puzzle;
        }
    });
}

function jQMakeDownDraggable(puzzle){
    // alert("puzzle " + puzzle.number + " must be draggable");
    $('#puzzleId' + puzzle.number).draggable({
        axis: 'y',
        containment: [puzzle.x - 100, puzzle.y + 100, puzzle.x + 100, puzzle.y + 150],
        stop: function(event, ui) {
            lastDraggedPuzzle = puzzle;
        },
        start: function(event, ui) {
            lastDraggedPuzzle = puzzle;
        }
    });
}

function jQMakeLeftDraggable(puzzle){
    //alert("puzzle " + puzzle.number + " must be draggable");
    $('#puzzleId' + puzzle.number).draggable({
        axis: 'x',
        containment: [puzzle.x + 50, puzzle.y - 100, puzzle.x + 50, puzzle.y + 100],
        stop: function(event, ui) {
            lastDraggedPuzzle = puzzle;
        },
        start: function(event, ui) {
            lastDraggedPuzzle = puzzle;
        }
    });
}

function jQMakeRightDraggable(puzzle){
    //alert("puzzle " + puzzle.number + " must be draggable");
    $('#puzzleId' + puzzle.number).draggable({
        axis: 'x',
        containment: [puzzle.x + 100, puzzle.y - 100, puzzle.x + 150, puzzle.y + 100],
        stop: function(event, ui) {
            lastDraggedPuzzle = puzzle;
        },
        start: function(event, ui) {
            lastDraggedPuzzle = puzzle;
        }

    });
}

function makeDroppable(){
    $('#freeSpaceId').droppable({
        drop: function() {
            var xTemp = freeSpace.x;
            var yTemp = freeSpace.y;
            var positionTemp = freeSpace.position;

            freeSpace.x = lastDraggedPuzzle.x;
            freeSpace.y = lastDraggedPuzzle.y;
            freeSpace.position = lastDraggedPuzzle.position;

            lastDraggedPuzzle.x = xTemp;
            lastDraggedPuzzle.y = yTemp;
            lastDraggedPuzzle.position = positionTemp;

            puzzles[lastDraggedPuzzle.number] = lastDraggedPuzzle;
            //destroy all draggable and dropable
            $("#box > *").draggable('destroy');
            $("#box > *").droppable('destroy');
            $('#freeSpaceId').remove();
            appendFreeSpace(freeSpace.x, freeSpace.y);

            $('#puzzleId' + lastDraggedPuzzle.number).remove();
            appendPuzzle(lastDraggedPuzzle.x, lastDraggedPuzzle.y, lastDraggedPuzzle.number);
            makeDraggable();
            makeDroppable();
        }
    });

}

function scramble(){
    $("#box > *").draggable('destroy');
    $("#box > *").droppable('destroy');
    $('#freeSpaceId').remove();
    $('.puzzle').remove();
    var xTemp = freeSpace.x;
    var yTemp = freeSpace.y;
    var positionTemp = freeSpace.position;

    var n = Math.ceil(Math.random() * 15);
    var randomPuzzle = puzzles[n];
    freeSpace.x = randomPuzzle.x;
    freeSpace.y = randomPuzzle.y;
    freeSpace.position = randomPuzzle.position;

    randomPuzzle.x = xTemp;
    randomPuzzle.y = yTemp;
    randomPuzzle.position = positionTemp;

    appendFreeSpace(freeSpace.x, freeSpace.y);

    for (var i = 0; i < 20; i++){
        var randomPuzzle1 = puzzles[Math.ceil(Math.random() * 15)];
        var randomPuzzle2 = puzzles[Math.ceil(Math.random() * 15)];
        xTemp = randomPuzzle1.x;
        yTemp = randomPuzzle1.y;
        positionTemp = randomPuzzle1.position;

        randomPuzzle1.x = randomPuzzle2.x;
        randomPuzzle1.y = randomPuzzle2.y;
        randomPuzzle1.position = randomPuzzle2.position;

        randomPuzzle2.x = xTemp;
        randomPuzzle2.y = yTemp;
        randomPuzzle2.position =positionTemp;
    }

    for (i = 1; i < 16; i ++){
        appendPuzzle(puzzles[i].x, puzzles[i].y, puzzles[i].number);
    }
    this.makeDraggable();
    this.makeDroppable();

}

function sort(){
    $("#box > *").draggable('destroy');
    $("#box > *").droppable('destroy');
    $('#freeSpaceId').remove();
    $('.puzzle').remove();
    puzzles = new Array();
    this.init();
    this.makeDraggable();
    this.makeDroppable();
}

function appendPuzzle(x, y, number){
    $('#box').append('
<div class="puzzle" id="puzzleId'
        + number
        + '"'
        +'" style="position:absolute; '
        + 'left:' + x + 'px; top:' + y +'px;'
        +'">'
        + number
        + '</div>

');
}

function appendFreeSpace(x, y){
    $('#box').append('
<div class="freeSpace" id="freeSpaceId'
        + '"'
        +'" style="position:absolute; '
        + 'left:' + x + 'px; top:' + y +'px;'
        +'">'
        + '</div>

');
}
</pre>


<p>Related posts:<ol><li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-extjs-way/' rel='bookmark' title='Permanent Link: 15puzzles ExtJs way'>15puzzles ExtJs way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-flex-way/' rel='bookmark' title='Permanent Link: 15puzzles Flex way'>15puzzles Flex way</a></li>
<li><a href='http://www.javaneverdie.com/ria-frameworks/15puzzles-yui-way/' rel='bookmark' title='Permanent Link: 15puzzles YUI way'>15puzzles YUI way</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.javaneverdie.com/ria-frameworks/15puzzles-jquery-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
