<?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>Ray Jezek's Blog &#187; DeScramble</title>
	<atom:link href="http://ray.jez.net/category/descramble/feed/" rel="self" type="application/rss+xml" />
	<link>http://ray.jez.net</link>
	<description>AllThingsR</description>
	<lastBuildDate>Sun, 10 Jan 2010 01:51:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>DeScramble &#8211; Game Logic</title>
		<link>http://ray.jez.net/descramble-game-logic/</link>
		<comments>http://ray.jez.net/descramble-game-logic/#comments</comments>
		<pubDate>Fri, 08 May 2009 13:23:57 +0000</pubDate>
		<dc:creator>ray</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[DeScramble]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[cheat]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scramble]]></category>

		<guid isPermaLink="false">http://ray.jez.net/?p=727</guid>
		<description><![CDATA[This is the 4th installment about my Scramble solver, DeScramble.  You can check out the first 3 parts here, here, and here.  In this post I am going to write a little about the game logic and how the program is setup to play the game.
The rules of the game are pretty simple; find words [...]]]></description>
			<content:encoded><![CDATA[<p>This is the 4th installment about my <a href="http://apps.facebook.com/scramblegame/" target="_blank">Scramble</a> solver, <a href="http://ray.jez.net/tag/descramble/">DeScramble</a>.  You can check out the first 3 parts <a href="http://ray.jez.net/descramble-an-introduction/">here</a>, <a href="http://ray.jez.net/descramble-ui-overview/">here</a>, and <a href="http://ray.jez.net/descramble-video-demo/">here</a>.  In this post I am going to write a little about the game logic and how the program is setup to play the game.</p>
<p>The <a href="http://apps.facebook.com/scramblegame/how_to_play.php" target="_blank">rules of the game</a> are pretty simple; find words on the game board using a chain of adjacent letters and use each letter only once per word.  The brunt of the logic, which isn’t much, is knowing which letters are adjacent to which.  Since the game board is a square and the adjacent letters are above and below one another, we have to create a map in the code to tell us which letters are next to which.</p>
<table style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" cellspacing="0" cellpadding="2" width="600">
<tbody>
<tr>
<td style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" rowspan="7" width="300" valign="top"><a href="http://ray.jez.net/wp-content/uploads/2009/05/board.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="board" src="http://ray.jez.net/wp-content/uploads/2009/05/board-thumb.png" border="0" alt="board" width="240" height="240" /></a></td>
<td style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" width="300" valign="top">
<p align="left"><span style="text-decoration: underline;"><span style="font-size: medium;"><strong>Valid Moves</strong></span></span></p>
</td>
</tr>
<tr>
<td style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" width="300" valign="top"><strong>0 =&gt; 1, 5, 6</strong></td>
</tr>
<tr>
<td style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" width="300" valign="top"><strong>1 =&gt; 0, 2, 5, 6, 7</strong></td>
</tr>
<tr>
<td style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" width="300" valign="top"><strong>….</strong></td>
</tr>
<tr>
<td style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" width="300" valign="top"><strong>12 =&gt; 6, 7, 8, 11, 13, 16, 17, 18</strong></td>
</tr>
<tr>
<td style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" width="300" valign="top"><strong>….</strong></td>
</tr>
<tr>
<td style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" width="300" height="100" valign="top"><strong>24 =&gt; 18, 19, 23</strong></td>
</tr>
</tbody>
</table>
<p>We could of course create a static map of which squares are adjacent to which, but this doesn’t allow us the flexibility to solve for different sized boards (Scramble has both 4&#215;4 and 5&#215;5 games).  So how do we go about creating this map in a flexible way?  Before I show the code to setup the valid character chains I think it will be helpful to go over a few of the basic constructs of the solver.</p>
<p>A game board is made up a many board letters:</p>
<div class="csharpcode">
<pre><span class="utype">Collection</span>&lt;<span class="utype">BoardLetter</span>&gt; gameBoard = <span class="utype">new</span> <span class="utype">Collection</span>&lt;<span class="utype">BoardLetter</span>&gt;();</pre>
<pre class="alt"><span class="kwrd">internal</span> <span class="kwrd">class</span> <span class="utype">BoardLetter</span></pre>
<pre>{</pre>
<pre class="alt">  <span class="kwrd">public</span> <span class="kwrd">char</span> Letter;</pre>
<pre>  <span class="kwrd">public</span> <span class="kwrd">sbyte</span> Position;</pre>
<pre class="alt"></pre>
<pre>  <span class="kwrd">public</span> BoardLetter(<span class="kwrd">char</span> letter, <span class="kwrd">sbyte</span> position)</pre>
<pre class="alt">  {</pre>
<pre>    Letter = letter;</pre>
<pre class="alt">    Position = position;</pre>
<pre>  }</pre>
<pre class="alt">}</pre>
<pre></pre>
</div>
<p>The dictionary of words is partitioned by first letter so searching goes a little faster:</p>
<div class="csharpcode">
<pre class="alt"><span class="utype">Dictionary</span>&lt;<span class="kwrd">char</span>, <span class="utype">List</span>&lt;<span class="kwrd">string</span>&gt;&gt; dictionary = <span class="kwrd">new</span> <span class="utype">Dictionary</span>&lt;<span class="kwrd">char</span>, <span class="utype">List</span>&lt;<span class="kwrd">string</span>&gt;&gt;();</pre>
</div>
<p>&nbsp;<br />
The valid character chains are keyed off position and contain a list of BoardLetters adjacent:</p>
<div class="csharpcode">
<pre class="alt"><span class="utype">Dictionary</span>&lt;<span class="kwrd">sbyte</span>, <span class="utype">Collection</span>&lt;<span class="utype">BoardLetter</span>&gt;&gt; validCharacterChains =
  <span class="kwrd">new</span> <span class="utype">Dictionary</span>&lt;<span class="kwrd">sbyte</span>, <span class="utype">Collection</span>&lt;<span class="utype">BoardLetter</span>&gt;&gt;();</pre>
</div>
<p>&nbsp;<br />
Creating the character chains is pretty straight forward.  The general formula for finding adjacent positions would be position ± 1 and position ± row ± 1.  This formula works for all positions except those on the edge of the game board (top/bottom row and right/left column).  Since we know the game boards are always square finding the edge positions to handle the exceptions is simplified.  The finished method:</p>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">private</span> <span class="kwrd">void</span> LoadCharChains()</pre>
<pre>{</pre>
<pre class="alt">  validCharacterChains.Clear();</pre>
<pre>  <span class="kwrd">for</span> (<span class="kwrd">sbyte</span> i = 0; i &lt; puzzleLength; i++)</pre>
<pre class="alt">  {</pre>
<pre>    validCharacterChains.Add(i, <span class="kwrd">new</span> <span class="utype">Collection</span>&lt;<span class="utype">BoardLetter</span>&gt;());</pre>
<pre class="alt">    <span class="kwrd">double</span> col = (i + 1) % square;</pre>
<pre>    <span class="kwrd">if</span> (col != 1) <span class="rem">// not left col so there is a char to the left</span></pre>
<pre class="alt">    {</pre>
<pre>      validCharacterChains[i].Add(gameBoard[i - 1]);</pre>
<pre class="alt">    }</pre>
<pre>    <span class="kwrd">if</span> (col != 0) <span class="rem">// not right col so there is a char to the right</span></pre>
<pre class="alt">    {</pre>
<pre>      validCharacterChains[i].Add(gameBoard[i + 1]);</pre>
<pre class="alt">    }</pre>
<pre>    <span class="kwrd">if</span> (i &gt;= square) <span class="rem">// not first row so there is a char above</span></pre>
<pre class="alt">    {</pre>
<pre>      validCharacterChains[i].Add(gameBoard[i - square]);</pre>
<pre class="alt">      <span class="kwrd">if</span> (col != 1) <span class="rem">// not first row not left col so there is a char above left</span></pre>
<pre>      {</pre>
<pre class="alt">        validCharacterChains[i].Add(gameBoard[i - square - 1]);</pre>
<pre>      }</pre>
<pre class="alt">      <span class="kwrd">if</span> (col != 0) <span class="rem">// not first row not right col so there is a char above right</span></pre>
<pre>      {</pre>
<pre class="alt">        validCharacterChains[i].Add(gameBoard[i - square + 1]);</pre>
<pre>      }</pre>
<pre class="alt">    }</pre>
<pre>    <span class="kwrd">if</span> (i &lt; (puzzleLength - square)) <span class="rem">// not last row so there is a char below</span></pre>
<pre class="alt">    {</pre>
<pre>      validCharacterChains[i].Add(gameBoard[i + square]);</pre>
<pre class="alt">      <span class="kwrd">if</span> (col != 1) <span class="rem">// not last row not left col so there is a char below left</span></pre>
<pre>      {</pre>
<pre class="alt">        validCharacterChains[i].Add(gameBoard[i + square - 1]);</pre>
<pre>      }</pre>
<pre class="alt">      <span class="kwrd">if</span> (col != 0) <span class="rem">// not last row not right col so there is a char below right</span></pre>
<pre>      {</pre>
<pre class="alt">        validCharacterChains[i].Add(gameBoard[i + square + 1]);</pre>
<pre>      }</pre>
<pre class="alt">    }</pre>
<pre>  }</pre>
<pre class="alt">}</pre>
</div>
<p>&nbsp;<br />
Now that we have the valid moves mapped out we can just loop though the dictionary of words to see if they can be found on the game board.  We use recursion for this keeping track of where we have traversed on the board so we don’t use the same position twice.  Not the cleanest code but it works:</p>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">private</span> <span class="kwrd">void</span> SolvePuzzle()</pre>
<pre>{</pre>
<pre class="alt">  <span class="kwrd">foreach</span> <span class="utype">BoardLetter</span> boardLetter <span class="kwrd">in</span> gameBoard)</pre>
<pre>  {</pre>
<pre class="alt">    <span class="kwrd">foreach</span> (<span class="kwrd">string</span> possibleAnswer <span class="kwrd">in</span> dictionary[boardLetter.Letter])</pre>
<pre>    {</pre>
<pre class="alt">      <span class="kwrd">if</span> (!answers.Contains(possibleAnswer))</pre>
<pre>      {</pre>
<pre class="alt">        foundAnswer = <span class="kwrd">false</span>;</pre>
<pre>        <span class="utype">List</span>&lt;<span class="kwrd">sbyte</span>&gt; testedPositions = <span class="kwrd">new</span> <span class="utype">List</span>&lt;<span class="kwrd">sbyte</span>&gt;();</pre>
<pre class="alt">        testedPositions.Add(boardLetter.Position);</pre>
<pre>        <span class="kwrd">string</span> startWord = boardLetter.Letter.ToString();</pre>
<pre class="alt">        <span class="kwrd">if</span> (startWord.Equals(<span class="str">"q"</span>))</pre>
<pre>        {</pre>
<pre class="alt">          startWord = <span class="str">"qu"</span>;</pre>
<pre>        }</pre>
<pre class="alt">        CheckBoardForAnswer(boardLetter, startWord, possibleAnswer, testedPositions);</pre>
<pre>      }</pre>
<pre class="alt">    }</pre>
<pre>  }</pre>
<pre class="alt">}</pre>
<pre></pre>
<pre class="alt"><span class="kwrd">private</span> <span class="kwrd">void</span> CheckBoardForAnswer(<span class="utype">BoardLetter</span> boardLetter, <span class="kwrd">string</span> workingWord, <span class="kwrd">string</span> possibleAnswer,</pre>
<pre>        <span class="utype">List</span>&lt;<span class="kwrd">sbyte</span>&gt; testedPositions)</pre>
<pre class="alt">{</pre>
<pre>  <span class="kwrd">for</span> (<span class="kwrd">sbyte</span> i = 0; i &lt; validCharacterChains[boardLetter.Position].Count; i++)</pre>
<pre class="alt">  {</pre>
<pre>    <span class="kwrd">if</span> (!foundAnswer)</pre>
<pre class="alt">    {</pre>
<pre>      <span class="utype">BoardLetter</span> workingLetter = validCharacterChains[boardLetter.Position][i];</pre>
<pre class="alt">      <span class="kwrd">if</span> (!testedPositions.Contains(workingLetter.Position))</pre>
<pre>      {</pre>
<pre class="alt">        <span class="kwrd">string</span> testWord = <span class="utype">String</span>.Concat(workingWord, workingLetter.Letter);</pre>
<pre>        <span class="rem">// if we've added a q then go ahead and add a u</span></pre>
<pre class="alt">        <span class="kwrd">if</span> (testWord.EndsWith(<span class="str">"q"</span>))</pre>
<pre>        {</pre>
<pre class="alt">          testWord += <span class="str">"u"</span>;</pre>
<pre>        }</pre>
<pre class="alt">        <span class="kwrd">if</span> (testWord.Equals(possibleAnswer))</pre>
<pre>        {</pre>
<pre class="alt">          answers.Add(possibleAnswer);</pre>
<pre>          foundAnswer = <span class="kwrd">true</span>;</pre>
<pre class="alt">        }</pre>
<pre>        <span class="kwrd">else</span> <span class="kwrd">if</span> (possibleAnswer.StartsWith(testWord))</pre>
<pre class="alt">        {</pre>
<pre>          testedPositions.Add(workingLetter.Position);</pre>
<pre class="alt">          CheckBoardForAnswer(workingLetter, testWord, possibleAnswer, testedPositions);</pre>
<pre>          testedPositions.Remove(workingLetter.Position);</pre>
<pre class="alt">        }</pre>
<pre>      }</pre>
<pre class="alt">    }</pre>
<pre>  }</pre>
<pre class="alt">}</pre>
</div>
<p>&nbsp;<br />
In my next (and most likely last) post I will go over how the screen processing works.</p>
]]></content:encoded>
			<wfw:commentRss>http://ray.jez.net/descramble-game-logic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DeScramble &#8211; Video Demo</title>
		<link>http://ray.jez.net/descramble-video-demo/</link>
		<comments>http://ray.jez.net/descramble-video-demo/#comments</comments>
		<pubDate>Thu, 13 Nov 2008 07:06:58 +0000</pubDate>
		<dc:creator>ray</dc:creator>
				<category><![CDATA[DeScramble]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[cheat]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Scramble]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://ray.jez.net/?p=632</guid>
		<description><![CDATA[In my previous DeScramble post I did an overview of the program’s UI.  Now it’s time to see a video of the program in action!
However, I want to make a few things clear before you watch the video below.

I have never used this program in any Scramble Live game with the exception of Cheater’s Ball. [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous <a href="http://ray.jez.net/category/descramble/" target="_self">DeScramble</a> post I did an <a href="http://ray.jez.net/descramble-ui-overview/" target="_self">overview of the program’s UI</a>.  Now it’s time to see a video of the program in action!</p>
<p>However, I want to make a few things clear before you watch the video below.</p>
<ul>
<li>I have never used this program in any Scramble Live game with the exception of Cheater’s Ball. All the regulars in Cheater’s Ball know I have this program and have seen it in action.</li>
<li>I have never used this program in any Scramble match.  Just look at my record and that becomes obvious.  Plus, all the people I play matches with know I have the program so it wouldn’t really fool anyone.  Besides, actually playing the game with people is more fun.</li>
<li>I wrote the program because it was an interesting challenge to me – My goal was <b>not</b> to make people think I am a better Scramble player than I am.</li>
<li>After writing this program and watching it run against some of the better players in the game I have a whole new respect for how good those people really are.  The speed at which they are able to find words on the board and enter them in is amazing!</li>
</ul>
<p>So with that said, here is a short demo of DeScramble in action: </p>
<p><iframe src="http://silverlight.services.live.com/invoke/79072/DeScrambleDemo/iframe.html" scrolling="no" frameborder="0" style="width:620px; height:500px"></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://ray.jez.net/descramble-video-demo/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>DeScramble &#8211; UI Overview</title>
		<link>http://ray.jez.net/descramble-ui-overview/</link>
		<comments>http://ray.jez.net/descramble-ui-overview/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 01:49:16 +0000</pubDate>
		<dc:creator>ray</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[DeScramble]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[cheat]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Scramble]]></category>
		<category><![CDATA[UI]]></category>

		<guid isPermaLink="false">http://ray.jez.net/?p=616</guid>
		<description><![CDATA[About a month ago I wrote an introduction to DeScramble an application I wrote this summer to cheat at the Facebook game Scramble.  In this installment I will introduce the UI and the basic functionality of the application.  Below is a screenshot of the main UI and a description of what each element does:


Game window [...]]]></description>
			<content:encoded><![CDATA[<p>About a month ago I wrote an <a href="http://ray.jez.net/descramble-an-introduction/">introduction to DeScramble</a> an application I wrote this summer to cheat at the <a href="http://www.facebook.com/" target="_blank">Facebook</a> game <a href="http://apps.new.facebook.com/scramblegame/" target="_blank">Scramble</a>.  In this installment I will introduce the UI and the basic functionality of the application.  Below is a screenshot of the main UI and a description of what each element does:</p>
<p><a href="http://ray.jez.net/wp-content/uploads/2008/11/descrambleui.jpg"><img style="border: 0px none; display: inline;" title="descrambleUI" src="http://ray.jez.net/wp-content/uploads/2008/11/descrambleui-thumb.jpg" border="0" alt="descrambleUI" width="390" height="157" /></a></p>
<ol>
<li>Game window finder &#8211; drag this target to the browser window that is running the Scramble game.  This allows the program to grab the window handle that is later used to interpret the game board.  In a future version I may attempt to try and find the window handle automagically but this works fine for now.</li>
<li>Send Answers toggle &#8211; checking this option tells the program to send the answers to the game.</li>
<li>Answer Speed slider &#8211; use this slider to change how fast the program sends the answers to the game.  From my testing anything lower than about 40ms tends to choke the game UI.</li>
<li>Auto Stop/Start toggle (bot mode) &#8211; checking this option tells the program to automatically start and stop the solver during live games.</li>
<li>Burst Mode toggle &#8211; checking this option tells the program to send the first 3 answers to the game immediately and then send answers based on the indicated answer speed.  This option is to ensure that when the scores are first posted that you have some points.  This eliminates the &#8220;cheaters lag&#8221; effect that is noticed when people have to type the board into a solver.</li>
<li>Solve/Stop Solve button &#8211; this button starts and stops the solver.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://ray.jez.net/descramble-ui-overview/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>DeScramble &#8211; An Introduction</title>
		<link>http://ray.jez.net/descramble-an-introduction/</link>
		<comments>http://ray.jez.net/descramble-an-introduction/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 04:05:32 +0000</pubDate>
		<dc:creator>ray</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[DeScramble]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[cheat]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Scramble]]></category>

		<guid isPermaLink="false">http://ray.jez.net/?p=545</guid>
		<description><![CDATA[This summer I was introduced to a game on Facebook called Scramble. Essentially it’s Boggle but played in a web browser. I was immediately hooked on this game and started playing it regularly. One day as I was playing the game and wondered if I could write a program that could automate my playing this [...]]]></description>
			<content:encoded><![CDATA[<p>This summer I was introduced to a game on <a href="http://www.facebook.com" target="_blank">Facebook</a> called <a href="http://apps.new.facebook.com/scramblegame/" target="_blank">Scramble</a>. Essentially it’s <a href="http://en.wikipedia.org/wiki/Boggle" target="_blank">Boggle</a> but played in a web browser. I was immediately hooked on this game and started playing it regularly. One day as I was playing the game and wondered if I could write a program that could automate my playing this game. Surely software could play this game WAY more efficiently and accurately than I could right?</p>
<p>So I opened up my trusty IDE and started plugging away at trying to make a program smart enough to play Scramble better than I could. Seems like it should be easy enough with the bar set so low.</p>
<p>About a day later I had a very basic Scramble solver which could take a string of letters as input and then spit out a list of possible answers. While there some interesting things about writing a basic Scramble solver (these are very simple to make), I wanted something a little more automated. I also wanted something that didn’t just flat out cheat and win but would act more human to be less noticeable. About a week later I had it finished, my automated Scramble application affectionately called DeScramble.</p>
<p>It was a pretty fun summer project and I plan on spending the next several blog posts talking about the way it works and some of the things I learned as well as some of the challenges. I am also going to try and put together a video so everyone can see it in action.</p>
<p>So stay tuned.</p>
]]></content:encoded>
			<wfw:commentRss>http://ray.jez.net/descramble-an-introduction/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

